-
Notifications
You must be signed in to change notification settings - Fork 76
/
find-successful-workflow.ts
296 lines (272 loc) · 8.68 KB
/
find-successful-workflow.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import * as core from '@actions/core';
import * as github from '@actions/github';
import { Octokit } from '@octokit/action';
import { spawnSync } from 'child_process';
import { existsSync } from 'fs';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';
const {
runId,
repo: { repo, owner },
eventName,
} = github.context;
process.env.GITHUB_TOKEN = process.argv[2];
const mainBranchName = process.argv[3];
const errorOnNoSuccessfulWorkflow = process.argv[4];
const lastSuccessfulEvent = process.argv[5];
const workingDirectory = process.argv[6];
const workflowId = process.argv[7];
const fallbackSHA = process.argv[8];
const defaultWorkingDirectory = '.';
const ProxifiedClient = Octokit.plugin(proxyPlugin);
let BASE_SHA: string;
(async () => {
if (workingDirectory !== defaultWorkingDirectory) {
if (existsSync(workingDirectory)) {
process.chdir(workingDirectory);
} else {
process.stdout.write('\n');
process.stdout.write(
`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`,
);
}
}
const headResult = spawnSync('git', ['rev-parse', 'HEAD'], {
encoding: 'utf-8',
});
const HEAD_SHA = headResult.stdout;
if (
(['pull_request', 'pull_request_target'].includes(eventName) &&
!github.context.payload.pull_request.merged) ||
eventName == 'merge_group'
) {
try {
const mergeBaseRef = await findMergeBaseRef();
const baseResult = spawnSync(
'git',
['merge-base', `origin/${mainBranchName}`, mergeBaseRef],
{ encoding: 'utf-8' },
);
BASE_SHA = baseResult.stdout;
} catch (e) {
core.setFailed(e.message);
return;
}
} else {
try {
BASE_SHA = await findSuccessfulCommit(
workflowId,
runId,
owner,
repo,
mainBranchName,
lastSuccessfulEvent,
);
} catch (e) {
core.setFailed(e.message);
return;
}
if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === 'true') {
reportFailure(mainBranchName);
return;
} else {
process.stdout.write('\n');
process.stdout.write(
`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`,
);
if (fallbackSHA) {
BASE_SHA = fallbackSHA;
process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`);
} else {
// Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash
const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`;
const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], {
encoding: 'utf-8',
});
if (baseRes.status !== 0 || !baseRes.stdout) {
const emptyTreeRes = spawnSync(
'git',
['hash-object', '-t', 'tree', '/dev/null'],
{
encoding: 'utf-8',
},
);
// 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the expected result of hashing the empty tree
BASE_SHA =
emptyTreeRes.stdout ?? `4b825dc642cb6eb9a060e54bf8d69288fbee4904`;
process.stdout.write(
`HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`,
);
} else {
process.stdout.write(
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`,
);
BASE_SHA = baseRes.stdout;
}
process.stdout.write('\n');
process.stdout.write(
`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`,
);
process.stdout.write('\n');
}
core.setOutput('noPreviousBuild', 'true');
}
} else {
process.stdout.write('\n');
process.stdout.write(
`Found the last successful workflow run on 'origin/${mainBranchName}'\n`,
);
process.stdout.write(`Commit: ${BASE_SHA}\n`);
}
}
core.setOutput('base', stripNewLineEndings(BASE_SHA));
core.setOutput('head', stripNewLineEndings(HEAD_SHA));
})();
function reportFailure(branchName: string): void {
core.setFailed(`
Unable to find a successful workflow run on 'origin/${branchName}'
NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error.
Is it possible that you have no runs currently on 'origin/${branchName}'?
- If yes, then you should run the workflow without this flag first.
- If no, then you might have changed your git history and those commits no longer exist.`);
}
function proxyPlugin(octokit: Octokit): void {
octokit.hook.before('request', (options) => {
const proxy: URL = getProxyForUrl(options.baseUrl);
if (proxy) {
options.request.agent = new HttpsProxyAgent(proxy);
}
});
}
/**
* Find last successful workflow run on the repo
*/
async function findSuccessfulCommit(
workflow_id: string | undefined,
run_id: number,
owner: string,
repo: string,
branch: string,
lastSuccessfulEvent: string,
): Promise<string | undefined> {
const octokit = new ProxifiedClient();
if (!workflow_id) {
workflow_id = await octokit
.request(`GET /repos/${owner}/${repo}/actions/runs/${run_id}`, {
owner,
repo,
branch,
run_id,
})
.then(({ data: { workflow_id } }) => workflow_id);
process.stdout.write('\n');
process.stdout.write(
`Workflow Id not provided. Using workflow '${workflow_id}'\n`,
);
}
// fetch all workflow runs on a given repo/branch/workflow with push and success
const shas = await octokit
.request(
`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`,
{
owner,
repo,
// on some workflow runs we do not have branch property
branch:
lastSuccessfulEvent === 'push' ||
lastSuccessfulEvent === 'workflow_dispatch'
? branch
: undefined,
workflow_id,
event: lastSuccessfulEvent,
status: 'success',
},
)
.then(({ data: { workflow_runs } }) =>
workflow_runs.map((run: { head_sha: any }) => run.head_sha),
);
return await findExistingCommit(octokit, branch, shas);
}
async function findMergeBaseRef(): Promise<string> {
if (eventName == 'merge_group') {
const mergeQueueBranch = await findMergeQueueBranch();
return `origin/${mergeQueueBranch}`;
} else {
return 'HEAD';
}
}
function findMergeQueuePr(): string {
const { head_ref, base_sha } = github.context.payload.merge_group;
const result = new RegExp(
`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$`,
).exec(head_ref);
return result ? result.at(1) : undefined;
}
async function findMergeQueueBranch(): Promise<string> {
const pull_number = findMergeQueuePr();
if (!pull_number) {
throw new Error('Failed to determine PR number');
}
process.stdout.write('\n');
process.stdout.write(`Found PR #${pull_number} from merge queue branch\n`);
const octokit = new ProxifiedClient();
const result = await octokit.request(
`GET /repos/${owner}/${repo}/pulls/${pull_number}`,
{ owner, repo, pull_number: +pull_number },
);
return result.data.head.ref;
}
/**
* Get first existing commit
*/
async function findExistingCommit(
octokit: Octokit,
branchName: string,
shas: string[],
): Promise<string | undefined> {
for (const commitSha of shas) {
if (await commitExists(octokit, branchName, commitSha)) {
return commitSha;
}
}
return undefined;
}
/**
* Check if given commit is valid
*/
async function commitExists(
octokit: Octokit,
branchName: string,
commitSha: string,
): Promise<boolean> {
try {
spawnSync('git', ['cat-file', '-e', commitSha], {
stdio: ['pipe', 'pipe', null],
});
// Check the commit exists in general
await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
owner,
repo,
commit_sha: commitSha,
});
// Check the commit exists on the expected main branch (it will not in the case of a rebased main branch)
const commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
owner,
repo,
sha: branchName,
per_page: 100,
});
return commits.data.some(
(commit: { sha: string }) => commit.sha === commitSha,
);
} catch {
return false;
}
}
/**
* Strips LF line endings from given string
*/
function stripNewLineEndings(string: string): string {
return string.replace('\n', '');
}