fix: always create backport check run (#253)

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>
This commit is contained in:
David Sanders 2024-04-04 17:23:22 -07:00 коммит произвёл GitHub
Родитель 4744d0bb00
Коммит 3a7af438ca
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 104 добавлений и 68 удалений

Просмотреть файл

@ -1,10 +1,41 @@
import { CHECK_PREFIX } from '../constants';
import { PRStatus, BackportPurpose, LogLevel } from '../enums'; import { PRStatus, BackportPurpose, LogLevel } from '../enums';
import { getCheckRun } from '../utils/checks-util';
import * as labelUtils from '../utils/label-utils'; import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util'; import { log } from '../utils/log-util';
import { backportImpl } from '../utils'; import { backportImpl } from '../utils';
import { Probot } from 'probot'; import { Probot } from 'probot';
import { SimpleWebHookRepoContext, WebHookPR } from '../types'; import { SimpleWebHookRepoContext, WebHookPR } from '../types';
const createOrUpdateCheckRun = async (
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) => {
const check = await getCheckRun(context, pr, targetBranch);
if (check) {
if (check.conclusion === 'neutral') {
await context.octokit.checks.update(
context.repo({
name: check.name,
check_run_id: check.id,
status: 'queued' as 'queued',
}),
);
}
} else {
await context.octokit.checks.create(
context.repo({
name: `${CHECK_PREFIX}${targetBranch}`,
head_sha: pr.head.sha,
status: 'queued' as 'queued',
details_url: 'https://github.com/electron/trop',
}),
);
}
};
/** /**
* Performs a backport to a specified label representing a branch. * Performs a backport to a specified label representing a branch.
* *
@ -43,6 +74,8 @@ export const backportToLabel = async (
return; return;
} }
await createOrUpdateCheckRun(context, pr, targetBranch);
const labelToRemove = label.name; const labelToRemove = label.name;
const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT); const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT);
await backportImpl( await backportImpl(
@ -75,6 +108,8 @@ export const backportToBranch = async (
`Executing backport to branch '${targetBranch}'`, `Executing backport to branch '${targetBranch}'`,
); );
await createOrUpdateCheckRun(context, pr, targetBranch);
const labelToRemove = undefined; const labelToRemove = undefined;
const labelToAdd = PRStatus.IN_FLIGHT + targetBranch; const labelToAdd = PRStatus.IN_FLIGHT + targetBranch;
await backportImpl( await backportImpl(

Просмотреть файл

@ -5,7 +5,6 @@ import simpleGit from 'simple-git';
import queue from './Queue'; import queue from './Queue';
import { import {
CHECK_PREFIX,
BACKPORT_REQUESTED_LABEL, BACKPORT_REQUESTED_LABEL,
DEFAULT_BACKPORT_REVIEW_TEAM, DEFAULT_BACKPORT_REVIEW_TEAM,
BACKPORT_LABEL, BACKPORT_LABEL,
@ -18,6 +17,7 @@ import { setupRemotes } from './operations/setup-remotes';
import { backportCommitsToBranch } from './operations/backport-commits'; import { backportCommitsToBranch } from './operations/backport-commits';
import { getRepoToken } from './utils/token-util'; import { getRepoToken } from './utils/token-util';
import { getSupportedBranches, getBackportPattern } from './utils/branch-util'; import { getSupportedBranches, getBackportPattern } from './utils/branch-util';
import { getCheckRun } from './utils/checks-util';
import { getEnvVar } from './utils/env-util'; import { getEnvVar } from './utils/env-util';
import { log } from './utils/log-util'; import { log } from './utils/log-util';
import { TryBackportOptions } from './interfaces'; import { TryBackportOptions } from './interfaces';
@ -464,36 +464,21 @@ export const backportImpl = async (
const bp = `backport from PR #${pr.number} to "${targetBranch}"`; const bp = `backport from PR #${pr.number} to "${targetBranch}"`;
log('backportImpl', LogLevel.INFO, `Queuing ${bp} for "${slug}"`); log('backportImpl', LogLevel.INFO, `Queuing ${bp} for "${slug}"`);
const getCheckRun = async () => {
const allChecks = await context.octokit.checks.listForRef(
context.repo({
ref: pr.head.sha,
per_page: 100,
}),
);
return allChecks.data.check_runs.find((run) => {
return run.name === `${CHECK_PREFIX}${targetBranch}`;
});
};
let createdDir: string | null = null; let createdDir: string | null = null;
queue.enterQueue( queue.enterQueue(
`backport-${pr.head.sha}-${targetBranch}-${purpose}`, `backport-${pr.head.sha}-${targetBranch}-${purpose}`,
async () => { async () => {
log('backportImpl', LogLevel.INFO, `Executing ${bp} for "${slug}"`); log('backportImpl', LogLevel.INFO, `Executing ${bp} for "${slug}"`);
if (purpose === BackportPurpose.Check) { const checkRun = await getCheckRun(context, pr, targetBranch);
const checkRun = await getCheckRun(); if (checkRun) {
if (checkRun) { await context.octokit.checks.update(
await context.octokit.checks.update( context.repo({
context.repo({ check_run_id: checkRun.id,
check_run_id: checkRun.id, name: checkRun.name,
name: checkRun.name, status: 'in_progress' as 'in_progress',
status: 'in_progress' as 'in_progress', }),
}), );
);
}
} }
const repoAccessToken = await getRepoToken(robot, context); const repoAccessToken = await getRepoToken(robot, context);
@ -679,22 +664,19 @@ export const backportImpl = async (
log('backportImpl', LogLevel.INFO, 'Backport process complete'); log('backportImpl', LogLevel.INFO, 'Backport process complete');
} }
if (purpose === BackportPurpose.Check) { if (checkRun) {
const checkRun = await getCheckRun(); context.octokit.checks.update(
if (checkRun) { context.repo({
context.octokit.checks.update( check_run_id: checkRun.id,
context.repo({ name: checkRun.name,
check_run_id: checkRun.id, conclusion: 'success' as 'success',
name: checkRun.name, completed_at: new Date().toISOString(),
conclusion: 'success' as 'success', output: {
completed_at: new Date().toISOString(), title: 'Clean Backport',
output: { summary: `This PR was checked and can be backported to "${targetBranch}" cleanly.`,
title: 'Clean Backport', },
summary: `This PR was checked and can be backported to "${targetBranch}" cleanly.`, }),
}, );
}),
);
}
} }
await fs.remove(createdDir); await fs.remove(createdDir);
@ -762,31 +744,29 @@ export const backportImpl = async (
]); ]);
} }
if (purpose === BackportPurpose.Check) { const checkRun = await getCheckRun(context, pr, targetBranch);
const checkRun = await getCheckRun(); if (checkRun) {
if (checkRun) { const mdSep = '``````````````````````````````';
const mdSep = '``````````````````````````````'; const updateOpts = context.repo({
const updateOpts = context.repo({ check_run_id: checkRun.id,
check_run_id: checkRun.id, name: checkRun.name,
name: checkRun.name, conclusion: 'neutral' as 'neutral',
conclusion: 'neutral' as 'neutral', completed_at: new Date().toISOString(),
completed_at: new Date().toISOString(), output: {
output: { title: 'Backport Failed',
title: 'Backport Failed', summary: `This PR was checked and could not be automatically backported to "${targetBranch}" cleanly`,
summary: `This PR was checked and could not be automatically backported to "${targetBranch}" cleanly`, text: diff
text: diff ? `Failed Diff:\n\n${mdSep}diff\n${rawDiff}\n${mdSep}`
? `Failed Diff:\n\n${mdSep}diff\n${rawDiff}\n${mdSep}` : undefined,
: undefined, annotations: annotations ? annotations : undefined,
annotations: annotations ? annotations : undefined, },
}, });
}); try {
try { await context.octokit.checks.update(updateOpts);
await context.octokit.checks.update(updateOpts); } catch (err) {
} catch (err) { // A GitHub error occurred - try to mark it as a failure without annotations.
// A GitHub error occurred - try to mark it as a failure without annotations. updateOpts.output!.annotations = undefined;
updateOpts.output!.annotations = undefined; await context.octokit.checks.update(updateOpts);
await context.octokit.checks.update(updateOpts);
}
} }
} }
}, },

Просмотреть файл

@ -1,6 +1,10 @@
import { CheckRunStatus } from '../enums'; import { CheckRunStatus } from '../enums';
import { BACKPORT_INFORMATION_CHECK } from '../constants'; import { BACKPORT_INFORMATION_CHECK, CHECK_PREFIX } from '../constants';
import { WebHookPRContext } from '../types'; import {
SimpleWebHookRepoContext,
WebHookPR,
WebHookPRContext,
} from '../types';
export async function updateBackportValidityCheck( export async function updateBackportValidityCheck(
context: WebHookPRContext, context: WebHookPRContext,
@ -88,3 +92,20 @@ export async function queueBackportInformationCheck(context: WebHookPRContext) {
}), }),
); );
} }
export async function getCheckRun(
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) {
const allChecks = await context.octokit.checks.listForRef(
context.repo({
ref: pr.head.sha,
per_page: 100,
}),
);
return allChecks.data.check_runs.find((run) => {
return run.name === `${CHECK_PREFIX}${targetBranch}`;
});
}