Skip to content

Commit 3a7af43

Browse files
dsanders11jkleinsc
andauthored
fix: always create backport check run (#253)
Co-authored-by: John Kleinschmidt <[email protected]>
1 parent 4744d0b commit 3a7af43

File tree

3 files changed

+104
-68
lines changed

3 files changed

+104
-68
lines changed

src/operations/backport-to-location.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
import { CHECK_PREFIX } from '../constants';
12
import { PRStatus, BackportPurpose, LogLevel } from '../enums';
3+
import { getCheckRun } from '../utils/checks-util';
24
import * as labelUtils from '../utils/label-utils';
35
import { log } from '../utils/log-util';
46
import { backportImpl } from '../utils';
57
import { Probot } from 'probot';
68
import { SimpleWebHookRepoContext, WebHookPR } from '../types';
79

10+
const createOrUpdateCheckRun = async (
11+
context: SimpleWebHookRepoContext,
12+
pr: WebHookPR,
13+
targetBranch: string,
14+
) => {
15+
const check = await getCheckRun(context, pr, targetBranch);
16+
17+
if (check) {
18+
if (check.conclusion === 'neutral') {
19+
await context.octokit.checks.update(
20+
context.repo({
21+
name: check.name,
22+
check_run_id: check.id,
23+
status: 'queued' as 'queued',
24+
}),
25+
);
26+
}
27+
} else {
28+
await context.octokit.checks.create(
29+
context.repo({
30+
name: `${CHECK_PREFIX}${targetBranch}`,
31+
head_sha: pr.head.sha,
32+
status: 'queued' as 'queued',
33+
details_url: 'https://github.com/electron/trop',
34+
}),
35+
);
36+
}
37+
};
38+
839
/**
940
* Performs a backport to a specified label representing a branch.
1041
*
@@ -43,6 +74,8 @@ export const backportToLabel = async (
4374
return;
4475
}
4576

77+
await createOrUpdateCheckRun(context, pr, targetBranch);
78+
4679
const labelToRemove = label.name;
4780
const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT);
4881
await backportImpl(
@@ -75,6 +108,8 @@ export const backportToBranch = async (
75108
`Executing backport to branch '${targetBranch}'`,
76109
);
77110

111+
await createOrUpdateCheckRun(context, pr, targetBranch);
112+
78113
const labelToRemove = undefined;
79114
const labelToAdd = PRStatus.IN_FLIGHT + targetBranch;
80115
await backportImpl(

src/utils.ts

+46-66
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import simpleGit from 'simple-git';
55

66
import queue from './Queue';
77
import {
8-
CHECK_PREFIX,
98
BACKPORT_REQUESTED_LABEL,
109
DEFAULT_BACKPORT_REVIEW_TEAM,
1110
BACKPORT_LABEL,
@@ -18,6 +17,7 @@ import { setupRemotes } from './operations/setup-remotes';
1817
import { backportCommitsToBranch } from './operations/backport-commits';
1918
import { getRepoToken } from './utils/token-util';
2019
import { getSupportedBranches, getBackportPattern } from './utils/branch-util';
20+
import { getCheckRun } from './utils/checks-util';
2121
import { getEnvVar } from './utils/env-util';
2222
import { log } from './utils/log-util';
2323
import { TryBackportOptions } from './interfaces';
@@ -464,36 +464,21 @@ export const backportImpl = async (
464464
const bp = `backport from PR #${pr.number} to "${targetBranch}"`;
465465
log('backportImpl', LogLevel.INFO, `Queuing ${bp} for "${slug}"`);
466466

467-
const getCheckRun = async () => {
468-
const allChecks = await context.octokit.checks.listForRef(
469-
context.repo({
470-
ref: pr.head.sha,
471-
per_page: 100,
472-
}),
473-
);
474-
475-
return allChecks.data.check_runs.find((run) => {
476-
return run.name === `${CHECK_PREFIX}${targetBranch}`;
477-
});
478-
};
479-
480467
let createdDir: string | null = null;
481468

482469
queue.enterQueue(
483470
`backport-${pr.head.sha}-${targetBranch}-${purpose}`,
484471
async () => {
485472
log('backportImpl', LogLevel.INFO, `Executing ${bp} for "${slug}"`);
486-
if (purpose === BackportPurpose.Check) {
487-
const checkRun = await getCheckRun();
488-
if (checkRun) {
489-
await context.octokit.checks.update(
490-
context.repo({
491-
check_run_id: checkRun.id,
492-
name: checkRun.name,
493-
status: 'in_progress' as 'in_progress',
494-
}),
495-
);
496-
}
473+
const checkRun = await getCheckRun(context, pr, targetBranch);
474+
if (checkRun) {
475+
await context.octokit.checks.update(
476+
context.repo({
477+
check_run_id: checkRun.id,
478+
name: checkRun.name,
479+
status: 'in_progress' as 'in_progress',
480+
}),
481+
);
497482
}
498483

499484
const repoAccessToken = await getRepoToken(robot, context);
@@ -679,22 +664,19 @@ export const backportImpl = async (
679664
log('backportImpl', LogLevel.INFO, 'Backport process complete');
680665
}
681666

682-
if (purpose === BackportPurpose.Check) {
683-
const checkRun = await getCheckRun();
684-
if (checkRun) {
685-
context.octokit.checks.update(
686-
context.repo({
687-
check_run_id: checkRun.id,
688-
name: checkRun.name,
689-
conclusion: 'success' as 'success',
690-
completed_at: new Date().toISOString(),
691-
output: {
692-
title: 'Clean Backport',
693-
summary: `This PR was checked and can be backported to "${targetBranch}" cleanly.`,
694-
},
695-
}),
696-
);
697-
}
667+
if (checkRun) {
668+
context.octokit.checks.update(
669+
context.repo({
670+
check_run_id: checkRun.id,
671+
name: checkRun.name,
672+
conclusion: 'success' as 'success',
673+
completed_at: new Date().toISOString(),
674+
output: {
675+
title: 'Clean Backport',
676+
summary: `This PR was checked and can be backported to "${targetBranch}" cleanly.`,
677+
},
678+
}),
679+
);
698680
}
699681

700682
await fs.remove(createdDir);
@@ -762,31 +744,29 @@ export const backportImpl = async (
762744
]);
763745
}
764746

765-
if (purpose === BackportPurpose.Check) {
766-
const checkRun = await getCheckRun();
767-
if (checkRun) {
768-
const mdSep = '``````````````````````````````';
769-
const updateOpts = context.repo({
770-
check_run_id: checkRun.id,
771-
name: checkRun.name,
772-
conclusion: 'neutral' as 'neutral',
773-
completed_at: new Date().toISOString(),
774-
output: {
775-
title: 'Backport Failed',
776-
summary: `This PR was checked and could not be automatically backported to "${targetBranch}" cleanly`,
777-
text: diff
778-
? `Failed Diff:\n\n${mdSep}diff\n${rawDiff}\n${mdSep}`
779-
: undefined,
780-
annotations: annotations ? annotations : undefined,
781-
},
782-
});
783-
try {
784-
await context.octokit.checks.update(updateOpts);
785-
} catch (err) {
786-
// A GitHub error occurred - try to mark it as a failure without annotations.
787-
updateOpts.output!.annotations = undefined;
788-
await context.octokit.checks.update(updateOpts);
789-
}
747+
const checkRun = await getCheckRun(context, pr, targetBranch);
748+
if (checkRun) {
749+
const mdSep = '``````````````````````````````';
750+
const updateOpts = context.repo({
751+
check_run_id: checkRun.id,
752+
name: checkRun.name,
753+
conclusion: 'neutral' as 'neutral',
754+
completed_at: new Date().toISOString(),
755+
output: {
756+
title: 'Backport Failed',
757+
summary: `This PR was checked and could not be automatically backported to "${targetBranch}" cleanly`,
758+
text: diff
759+
? `Failed Diff:\n\n${mdSep}diff\n${rawDiff}\n${mdSep}`
760+
: undefined,
761+
annotations: annotations ? annotations : undefined,
762+
},
763+
});
764+
try {
765+
await context.octokit.checks.update(updateOpts);
766+
} catch (err) {
767+
// A GitHub error occurred - try to mark it as a failure without annotations.
768+
updateOpts.output!.annotations = undefined;
769+
await context.octokit.checks.update(updateOpts);
790770
}
791771
}
792772
},

src/utils/checks-util.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { CheckRunStatus } from '../enums';
2-
import { BACKPORT_INFORMATION_CHECK } from '../constants';
3-
import { WebHookPRContext } from '../types';
2+
import { BACKPORT_INFORMATION_CHECK, CHECK_PREFIX } from '../constants';
3+
import {
4+
SimpleWebHookRepoContext,
5+
WebHookPR,
6+
WebHookPRContext,
7+
} from '../types';
48

59
export async function updateBackportValidityCheck(
610
context: WebHookPRContext,
@@ -88,3 +92,20 @@ export async function queueBackportInformationCheck(context: WebHookPRContext) {
8892
}),
8993
);
9094
}
95+
96+
export async function getCheckRun(
97+
context: SimpleWebHookRepoContext,
98+
pr: WebHookPR,
99+
targetBranch: string,
100+
) {
101+
const allChecks = await context.octokit.checks.listForRef(
102+
context.repo({
103+
ref: pr.head.sha,
104+
per_page: 100,
105+
}),
106+
);
107+
108+
return allChecks.data.check_runs.find((run) => {
109+
return run.name === `${CHECK_PREFIX}${targetBranch}`;
110+
});
111+
}

0 commit comments

Comments
 (0)