Skip to content

Commit 437f374

Browse files
authoredMay 7, 2024··
fix: only modify backportable checks inside queue (#293)
1 parent 8f57bb7 commit 437f374

File tree

4 files changed

+35
-93
lines changed

4 files changed

+35
-93
lines changed
 

‎src/index.ts

+1-39
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ import {
1313
} from './utils/label-utils';
1414
import { CHECK_PREFIX, NO_BACKPORT_LABEL, SKIP_CHECK_LABEL } from './constants';
1515
import { getEnvVar } from './utils/env-util';
16-
import {
17-
PRChange,
18-
PRStatus,
19-
BackportPurpose,
20-
CheckRunStatus,
21-
LogLevel,
22-
} from './enums';
16+
import { PRChange, PRStatus, BackportPurpose, CheckRunStatus } from './enums';
2317
import { Label } from '@octokit/webhooks-types';
2418
import {
2519
backportToLabel,
@@ -33,7 +27,6 @@ import {
3327
updateBackportInformationCheck,
3428
updateBackportValidityCheck,
3529
} from './utils/checks-util';
36-
import { log } from './utils/log-util';
3730
import { register } from './utils/prom';
3831
import {
3932
SimpleWebHookRepoContext,
@@ -116,44 +109,13 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
116109
for (const label of pr.labels) {
117110
if (!label.name.startsWith(PRStatus.TARGET)) continue;
118111
const targetBranch = labelToTargetBranch(label, PRStatus.TARGET);
119-
const runName = `${CHECK_PREFIX}${targetBranch}`;
120-
let checkRun = checkRuns.find((run) => run.name === runName);
121-
if (checkRun) {
122-
if (checkRun.conclusion !== 'neutral') continue;
123-
124-
log(
125-
'runCheck',
126-
LogLevel.INFO,
127-
`Updating check run ID ${checkRun.id} with status 'queued'`,
128-
);
129-
130-
await context.octokit.checks.update(
131-
context.repo({
132-
name: checkRun.name,
133-
check_run_id: checkRun.id,
134-
status: 'queued' as 'queued',
135-
}),
136-
);
137-
} else {
138-
const response = await context.octokit.checks.create(
139-
context.repo({
140-
name: runName,
141-
head_sha: pr.head.sha,
142-
status: 'queued' as 'queued',
143-
details_url: 'https://github.com/electron/trop',
144-
}),
145-
);
146-
147-
checkRun = response.data;
148-
}
149112

150113
await backportImpl(
151114
robot,
152115
context,
153116
pr,
154117
targetBranch,
155118
BackportPurpose.Check,
156-
checkRun,
157119
);
158120
}
159121

‎src/operations/backport-to-location.ts

-47
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,10 @@
1-
import { CHECK_PREFIX } from '../constants';
21
import { PRStatus, BackportPurpose, LogLevel } from '../enums';
3-
import { getCheckRun } from '../utils/checks-util';
42
import * as labelUtils from '../utils/label-utils';
53
import { log } from '../utils/log-util';
64
import { backportImpl } from '../utils';
75
import { Probot } from 'probot';
86
import { SimpleWebHookRepoContext, WebHookPR } from '../types';
97

10-
const createOrUpdateCheckRun = async (
11-
context: SimpleWebHookRepoContext,
12-
pr: WebHookPR,
13-
targetBranch: string,
14-
) => {
15-
let check = await getCheckRun(context, pr, targetBranch);
16-
17-
if (check) {
18-
if (check.conclusion === 'neutral') {
19-
log(
20-
'createOrUpdateCheckRun',
21-
LogLevel.INFO,
22-
`Updating check run ID ${check.id} with status 'queued'`,
23-
);
24-
25-
await context.octokit.checks.update(
26-
context.repo({
27-
name: check.name,
28-
check_run_id: check.id,
29-
status: 'queued' as 'queued',
30-
}),
31-
);
32-
}
33-
} else {
34-
const response = await context.octokit.checks.create(
35-
context.repo({
36-
name: `${CHECK_PREFIX}${targetBranch}`,
37-
head_sha: pr.head.sha,
38-
status: 'queued' as 'queued',
39-
details_url: 'https://github.com/electron/trop',
40-
}),
41-
);
42-
43-
check = response.data;
44-
}
45-
46-
return check;
47-
};
48-
498
/**
509
* Performs a backport to a specified label representing a branch.
5110
*
@@ -84,8 +43,6 @@ export const backportToLabel = async (
8443
return;
8544
}
8645

87-
const checkRun = await createOrUpdateCheckRun(context, pr, targetBranch);
88-
8946
const labelToRemove = label.name;
9047
const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT);
9148
await backportImpl(
@@ -94,7 +51,6 @@ export const backportToLabel = async (
9451
pr,
9552
targetBranch,
9653
BackportPurpose.ExecuteBackport,
97-
checkRun,
9854
labelToRemove,
9955
labelToAdd,
10056
);
@@ -119,8 +75,6 @@ export const backportToBranch = async (
11975
`Executing backport to branch '${targetBranch}'`,
12076
);
12177

122-
const checkRun = await createOrUpdateCheckRun(context, pr, targetBranch);
123-
12478
const labelToRemove = undefined;
12579
const labelToAdd = PRStatus.IN_FLIGHT + targetBranch;
12680
await backportImpl(
@@ -129,7 +83,6 @@ export const backportToBranch = async (
12983
pr,
13084
targetBranch,
13185
BackportPurpose.ExecuteBackport,
132-
checkRun,
13386
labelToRemove,
13487
labelToAdd,
13588
);

‎src/utils.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
BACKPORT_REQUESTED_LABEL,
99
DEFAULT_BACKPORT_REVIEW_TEAM,
1010
BACKPORT_LABEL,
11+
CHECK_PREFIX,
1112
} from './constants';
1213
import { PRStatus, BackportPurpose, LogLevel, PRChange } from './enums';
1314

@@ -17,7 +18,7 @@ import { setupRemotes } from './operations/setup-remotes';
1718
import { backportCommitsToBranch } from './operations/backport-commits';
1819
import { getRepoToken } from './utils/token-util';
1920
import { getSupportedBranches, getBackportPattern } from './utils/branch-util';
20-
import { getCheckRun } from './utils/checks-util';
21+
import { getOrCreateCheckRun } from './utils/checks-util';
2122
import { getEnvVar } from './utils/env-util';
2223
import { log } from './utils/log-util';
2324
import { TryBackportOptions } from './interfaces';
@@ -450,7 +451,6 @@ export const backportImpl = async (
450451
pr: WebHookPR,
451452
targetBranch: string,
452453
purpose: BackportPurpose,
453-
checkRun: NonNullable<Awaited<ReturnType<typeof getCheckRun>>>,
454454
labelToRemove?: string,
455455
labelToAdd?: string,
456456
) => {
@@ -486,10 +486,11 @@ export const backportImpl = async (
486486
`backport-${pr.head.sha}-${targetBranch}-${purpose}`,
487487
async () => {
488488
log('backportImpl', LogLevel.INFO, `Executing ${bp} for "${slug}"`);
489+
const checkRun = await getOrCreateCheckRun(context, pr, targetBranch);
489490
log(
490491
'backportImpl',
491492
LogLevel.INFO,
492-
`Updating check run ID ${checkRun.id} with status 'in_progress'`,
493+
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with status 'in_progress'`,
493494
);
494495
await context.octokit.checks.update(
495496
context.repo({
@@ -685,7 +686,7 @@ export const backportImpl = async (
685686
log(
686687
'backportImpl',
687688
LogLevel.INFO,
688-
`Updating check run ID ${checkRun.id} with conclusion 'success'`,
689+
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with conclusion 'success'`,
689690
);
690691

691692
await context.octokit.checks.update(
@@ -766,6 +767,7 @@ export const backportImpl = async (
766767
]);
767768
}
768769

770+
const checkRun = await getOrCreateCheckRun(context, pr, targetBranch);
769771
const mdSep = '``````````````````````````````';
770772
const updateOpts = context.repo({
771773
check_run_id: checkRun.id,
@@ -781,6 +783,11 @@ export const backportImpl = async (
781783
annotations: annotations ? annotations : undefined,
782784
},
783785
});
786+
log(
787+
'backportImpl',
788+
LogLevel.INFO,
789+
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with conclusion 'neutral'`,
790+
);
784791
try {
785792
await context.octokit.checks.update(updateOpts);
786793
} catch (err) {

‎src/utils/checks-util.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { CheckRunStatus } from '../enums';
1+
import { CheckRunStatus, LogLevel } from '../enums';
22
import { BACKPORT_INFORMATION_CHECK, CHECK_PREFIX } from '../constants';
33
import {
44
SimpleWebHookRepoContext,
55
WebHookPR,
66
WebHookPRContext,
77
} from '../types';
8+
import { log } from '../utils/log-util';
89

910
export async function updateBackportValidityCheck(
1011
context: WebHookPRContext,
@@ -93,7 +94,7 @@ export async function queueBackportInformationCheck(context: WebHookPRContext) {
9394
);
9495
}
9596

96-
export async function getCheckRun(
97+
export async function getOrCreateCheckRun(
9798
context: SimpleWebHookRepoContext,
9899
pr: WebHookPR,
99100
targetBranch: string,
@@ -105,7 +106,26 @@ export async function getCheckRun(
105106
}),
106107
);
107108

108-
return allChecks.data.check_runs.find((run) => {
109+
let checkRun = allChecks.data.check_runs.find((run) => {
109110
return run.name === `${CHECK_PREFIX}${targetBranch}`;
110111
});
112+
113+
if (!checkRun) {
114+
const response = await context.octokit.checks.create(
115+
context.repo({
116+
name: `${CHECK_PREFIX}${targetBranch}`,
117+
head_sha: pr.head.sha,
118+
status: 'queued' as 'queued',
119+
details_url: 'https://github.com/electron/trop',
120+
}),
121+
);
122+
checkRun = response.data;
123+
log(
124+
'backportImpl',
125+
LogLevel.INFO,
126+
`Created check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with status 'queued'`,
127+
);
128+
}
129+
130+
return checkRun;
111131
}

0 commit comments

Comments
 (0)
Please sign in to comment.