-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathget-missing-cherry-picks-utils.ts
74 lines (61 loc) · 1.95 KB
/
get-missing-cherry-picks-utils.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
import {Octokit} from '@octokit/rest';
import {GetResponseTypeFromEndpointMethod} from '@octokit/types';
type Commits = GetResponseTypeFromEndpointMethod<
Octokit['repos']['compareCommitsWithBasehead']
>['data']['commits'];
const params = {owner: 'ampproject', repo: 'amphtml'};
export async function getMissingCommits(
octokit: Octokit,
ampVersion: string,
releases: Set<string>
): Promise<string[]> {
const missingShas: string[] = [];
// get distinct shas that were cherry-picked
const mainShas = new Set<string>();
for (const release of releases) {
const commits = await getCherryPickCommits(octokit, release);
if (!commits) continue;
for (const commit of commits) {
const sha = getMainBranchShaFromCommitMessage(commit.commit.message);
if (!sha) continue;
mainShas.add(sha);
}
}
// get cherry-picked shas that aren't in commit tree
for (const mainSha of mainShas) {
const commitIsMissing = await isMissing(octokit, ampVersion, mainSha);
if (!commitIsMissing) continue;
missingShas.push(mainSha);
}
return missingShas;
}
async function getCherryPickCommits(
octokit: Octokit,
release: string
): Promise<Commits | undefined> {
if (release.endsWith('000')) return;
const base = release.slice(0, -3) + '000';
const response = await octokit.rest.repos.compareCommitsWithBasehead({
...params,
basehead: `${base}...${release}`,
});
return response.data.commits;
}
function getMainBranchShaFromCommitMessage(
message: string
): string | undefined {
const match = message.match(/cherry picked from commit ([0-9a-f]{40})/);
return match?.[1];
}
async function isMissing(
octokit: Octokit,
ampVersion: string,
sha: string
): Promise<boolean> {
const response = await octokit.rest.repos.compareCommitsWithBasehead({
...params,
basehead: `${ampVersion}...${sha}`,
});
if (response.status !== 200) return false;
return ['diverged', 'ahead'].includes(response.data.status);
}