-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.ts
More file actions
98 lines (88 loc) · 3.41 KB
/
index.ts
File metadata and controls
98 lines (88 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs = require('fs');
import core = require('@actions/core');
import github = require('@actions/github');
import axios, { AxiosPromise, AxiosRequestConfig } from 'axios';
interface Repo {
repo: string;
owner: string;
}
async function deliver(url: string, secret: string, payload: string): Promise<AxiosPromise<{}>> {
const workflow = github.context.workflow;
const repo = github.context.repo;
const ref = github.context.ref;
const sha = github.context.sha;
const workFlowPaylod = github.context.payload;
const { GITHUB_RUN_ID } = process.env;
let contextUrl: string | null = null;
// Log the actual github context for debugging
core.info(`GitHub Context ${JSON.stringify(github.context)}`);
if (GITHUB_RUN_ID) {
contextUrl = `https://github.com/${repo.owner}/${repo.repo}/actions/runs/${GITHUB_RUN_ID}`;
core.info(`GitHub Context ${contextUrl}`);
}
// If this workflow is triggered by another workflow, use that run's parameters
const targetWorkflowRun = workFlowPaylod?.workflow_run
core.info(`Target workflow run: ${JSON.stringify(targetWorkflowRun)}`)
const headSha = workFlowPaylod?.pull_request?.head?.sha ?? targetWorkflowRun?.head_sha ?? sha;
const sender = workFlowPaylod?.sender?.login;
let additionalContext: string | null = null;
let refFromTargetWorkflow: string | null = null;
if (targetWorkflowRun?.head_branch) {
refFromTargetWorkflow = `refs/heads/${targetWorkflowRun.head_branch}`
}
if (targetWorkflowRun?.name && targetWorkflowRun?.html_url) {
additionalContext = `This workflow was triggered by "${targetWorkflowRun.name}" (${targetWorkflowRun.html_url})`
}
core.info(`ref from workflow target: ${refFromTargetWorkflow}`)
let repoFromTargetWorkflow: Repo | null = null;
if (targetWorkflowRun?.head_repository?.owner?.login &&
targetWorkflowRun?.head_repository?.name) {
repoFromTargetWorkflow = {
"owner": targetWorkflowRun.head_repository.owner.login,
"repo": targetWorkflowRun.head_repository.name
}
}
core.info(`repo from workflow target: ${JSON.stringify(repoFromTargetWorkflow)}`)
// Notify build failures if its copybara-bot merging the changes.
const notifyOnFailure = sender === 'copybara-service[bot]';
const additionalPayload = JSON.parse(payload);
const requestBody = {
'workflow': workflow,
'repo': repoFromTargetWorkflow ?? repo,
'ref': refFromTargetWorkflow ?? ref,
'sha': headSha,
'notifyOnFailure': notifyOnFailure,
'additionalContext': additionalContext,
...additionalPayload
};
if (contextUrl) {
requestBody['pullRequestUrl'] = contextUrl;
}
core.info(`Delivering ${JSON.stringify(requestBody)} to ${url}`);
const requestConfig: AxiosRequestConfig = {
url: url,
method: 'POST',
data: requestBody
};
if (secret) {
requestConfig['headers'] = {
'X-GitHub-Secret': `${secret}`
}
}
const response = axios(requestConfig);
return response;
}
(async function () {
try {
const url = core.getInput('url');
const secret = core.getInput('secret');
const payload = core.getInput('payload');
core.info(`Making a HTTP POST request to ${url}`);
const result = await deliver(url, secret, payload);
core.info(`Result ${result.status}: ${result.statusText}`);
core.setOutput('status', result.status);
core.setOutput('statusText', result.statusText);
} catch (error) {
core.setFailed(`Unable to deliver Web Hook ${error}`);
}
})();