1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26 | return new (P || (P = Promise))(function (resolve, reject) {
|
27 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
28 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
29 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
30 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
31 | });
|
32 | };
|
33 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
34 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
35 | };
|
36 | Object.defineProperty(exports, "__esModule", { value: true });
|
37 | const axios_1 = __importDefault(require("axios"));
|
38 | const keymanager_1 = require("./keymanager");
|
39 | const constants_1 = require("./constants");
|
40 | const GITHUB_API_BASE = 'https://api.github.com';
|
41 | const GITHUB_URL_REPO_REGEX = /repos\/([^/]+\/[^/]+)\//;
|
42 | function statusToState(status) {
|
43 | switch (status) {
|
44 | case 'QUEUED':
|
45 | case 'WORKING':
|
46 | return 'pending';
|
47 | case 'SUCCESS':
|
48 | return 'success';
|
49 | case 'FAILURE':
|
50 | return 'failure';
|
51 | default:
|
52 | return 'error';
|
53 | }
|
54 | }
|
55 | function statusToDescription(status) {
|
56 | switch (status) {
|
57 | case 'QUEUED':
|
58 | return 'Build queued.';
|
59 | case 'WORKING':
|
60 | return 'Build started.';
|
61 | case 'SUCCESS':
|
62 | return 'Build succeeded.';
|
63 | case 'FAILURE':
|
64 | return 'Build failed.';
|
65 | case 'INTERNAL_ERROR':
|
66 | return 'Build internal error.';
|
67 | case 'TIMEOUT':
|
68 | return 'Build timed out.';
|
69 | case 'CANCELLED':
|
70 | return 'Build cancelled.';
|
71 | default:
|
72 | return 'Build status unknown.';
|
73 | }
|
74 | }
|
75 | function makeRequest(uri, body) {
|
76 | return __awaiter(this, void 0, void 0, function* () {
|
77 | const repoMatch = GITHUB_URL_REPO_REGEX.exec(uri);
|
78 | if (!repoMatch) {
|
79 | throw new Error('Could not parse github url.');
|
80 | }
|
81 | const githubToken = yield keymanager_1.keyManager.getGithubToken(repoMatch[1]);
|
82 | return axios_1.default.post(uri, body, {
|
83 | headers: {
|
84 | 'User-Agent': 'cloudbuild-github',
|
85 | },
|
86 | auth: {
|
87 | username: 'token',
|
88 | password: githubToken,
|
89 | },
|
90 | });
|
91 | });
|
92 | }
|
93 | function handleBuildEvent(data) {
|
94 | return __awaiter(this, void 0, void 0, function* () {
|
95 | const build = JSON.parse(Buffer.from(data, 'base64').toString('utf8'));
|
96 | let repoName = null;
|
97 | let revisionId = null;
|
98 | if (!build.substitutions) {
|
99 |
|
100 | const repoSource = build.sourceProvenance.resolvedRepoSource;
|
101 |
|
102 | const cloudbuildRepoName = repoSource.repoName;
|
103 | if (!cloudbuildRepoName.startsWith('github-')) {
|
104 |
|
105 | return;
|
106 | }
|
107 | repoName = cloudbuildRepoName
|
108 | .substring('github-'.length)
|
109 | .replace('-', '/');
|
110 | revisionId = repoSource.commitSha;
|
111 | }
|
112 | const statusesUrl = repoName && revisionId
|
113 | ? `${GITHUB_API_BASE}/repos/${repoName}/statuses/${revisionId}`
|
114 | : build.substitutions[constants_1.STATUSES_URL_KEY];
|
115 | if (!statusesUrl) {
|
116 |
|
117 | return;
|
118 | }
|
119 | const status = {
|
120 | state: statusToState(build.status),
|
121 |
|
122 | target_url: build.logUrl,
|
123 | description: statusToDescription(build.status),
|
124 | context: 'ci/cloudbuild',
|
125 | };
|
126 | const statusResponse = yield makeRequest(statusesUrl, status);
|
127 | if (!statusResponse.data.state) {
|
128 | throw new Error(`Failed to set status: ${JSON.stringify(statusResponse.data)}`);
|
129 | }
|
130 | const commentsUrl = repoName && revisionId
|
131 | ? `${GITHUB_API_BASE}/repos/${repoName}/commits/${revisionId}/comments`
|
132 | : build.substitutions[constants_1.COMMENTS_URL_KEY];
|
133 | if (!commentsUrl) {
|
134 |
|
135 |
|
136 | return;
|
137 | }
|
138 | let comment;
|
139 | switch (build.status) {
|
140 | case 'QUEUED':
|
141 | case 'WORKING':
|
142 | case 'CANCELLED':
|
143 | case 'STATUS_UNKNOWN':
|
144 | return;
|
145 | case 'SUCCESS': {
|
146 | if (repoName) {
|
147 |
|
148 | return;
|
149 | }
|
150 | comment = `Build succeded. If you have approval, you're ready to merge!\n\nLogs:\n${build.logUrl}`;
|
151 | break;
|
152 | }
|
153 | case 'FAILURE':
|
154 | comment = `Build failed. Check the logs and try again.\n\nLogs:\n${build.logUrl}`;
|
155 | break;
|
156 | default:
|
157 | comment = `Build terminated with unknown error. You may want to retry.\n\nLogs:\n${build.logUrl}`;
|
158 | break;
|
159 | }
|
160 | const commentResponse = yield makeRequest(commentsUrl, {
|
161 | body: comment,
|
162 | });
|
163 | if (!commentResponse.data.id) {
|
164 | throw new Error(`Failed to set comment: ${JSON.stringify(commentResponse.data)}`);
|
165 | }
|
166 | });
|
167 | }
|
168 | exports.default = handleBuildEvent;
|
169 |
|
\ | No newline at end of file |