Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only modify backportable checks inside queue #293

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 1 addition & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import {
} from './utils/label-utils';
import { CHECK_PREFIX, NO_BACKPORT_LABEL, SKIP_CHECK_LABEL } from './constants';
import { getEnvVar } from './utils/env-util';
import {
PRChange,
PRStatus,
BackportPurpose,
CheckRunStatus,
LogLevel,
} from './enums';
import { PRChange, PRStatus, BackportPurpose, CheckRunStatus } from './enums';
import { Label } from '@octokit/webhooks-types';
import {
backportToLabel,
Expand All @@ -33,7 +27,6 @@ import {
updateBackportInformationCheck,
updateBackportValidityCheck,
} from './utils/checks-util';
import { log } from './utils/log-util';
import { register } from './utils/prom';
import {
SimpleWebHookRepoContext,
Expand Down Expand Up @@ -116,44 +109,13 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
for (const label of pr.labels) {
if (!label.name.startsWith(PRStatus.TARGET)) continue;
const targetBranch = labelToTargetBranch(label, PRStatus.TARGET);
const runName = `${CHECK_PREFIX}${targetBranch}`;
let checkRun = checkRuns.find((run) => run.name === runName);
if (checkRun) {
if (checkRun.conclusion !== 'neutral') continue;

log(
'runCheck',
LogLevel.INFO,
`Updating check run ID ${checkRun.id} with status 'queued'`,
);

await context.octokit.checks.update(
context.repo({
name: checkRun.name,
check_run_id: checkRun.id,
status: 'queued' as 'queued',
}),
);
} else {
const response = await context.octokit.checks.create(
context.repo({
name: runName,
head_sha: pr.head.sha,
status: 'queued' as 'queued',
details_url: 'https://github.com/electron/trop',
}),
);

checkRun = response.data;
}

await backportImpl(
robot,
context,
pr,
targetBranch,
BackportPurpose.Check,
checkRun,
);
}

Expand Down
47 changes: 0 additions & 47 deletions src/operations/backport-to-location.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,10 @@
import { CHECK_PREFIX } from '../constants';
import { PRStatus, BackportPurpose, LogLevel } from '../enums';
import { getCheckRun } from '../utils/checks-util';
import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util';
import { backportImpl } from '../utils';
import { Probot } from 'probot';
import { SimpleWebHookRepoContext, WebHookPR } from '../types';

const createOrUpdateCheckRun = async (
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) => {
let check = await getCheckRun(context, pr, targetBranch);

if (check) {
if (check.conclusion === 'neutral') {
log(
'createOrUpdateCheckRun',
LogLevel.INFO,
`Updating check run ID ${check.id} with status 'queued'`,
);

await context.octokit.checks.update(
context.repo({
name: check.name,
check_run_id: check.id,
status: 'queued' as 'queued',
}),
);
}
} else {
const response = 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',
}),
);

check = response.data;
}

return check;
};

/**
* Performs a backport to a specified label representing a branch.
*
Expand Down Expand Up @@ -84,8 +43,6 @@ export const backportToLabel = async (
return;
}

const checkRun = await createOrUpdateCheckRun(context, pr, targetBranch);

const labelToRemove = label.name;
const labelToAdd = label.name.replace(PRStatus.TARGET, PRStatus.IN_FLIGHT);
await backportImpl(
Expand All @@ -94,7 +51,6 @@ export const backportToLabel = async (
pr,
targetBranch,
BackportPurpose.ExecuteBackport,
checkRun,
labelToRemove,
labelToAdd,
);
Expand All @@ -119,8 +75,6 @@ export const backportToBranch = async (
`Executing backport to branch '${targetBranch}'`,
);

const checkRun = await createOrUpdateCheckRun(context, pr, targetBranch);

const labelToRemove = undefined;
const labelToAdd = PRStatus.IN_FLIGHT + targetBranch;
await backportImpl(
Expand All @@ -129,7 +83,6 @@ export const backportToBranch = async (
pr,
targetBranch,
BackportPurpose.ExecuteBackport,
checkRun,
labelToRemove,
labelToAdd,
);
Expand Down
15 changes: 11 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BACKPORT_REQUESTED_LABEL,
DEFAULT_BACKPORT_REVIEW_TEAM,
BACKPORT_LABEL,
CHECK_PREFIX,
} from './constants';
import { PRStatus, BackportPurpose, LogLevel, PRChange } from './enums';

Expand All @@ -17,7 +18,7 @@ import { setupRemotes } from './operations/setup-remotes';
import { backportCommitsToBranch } from './operations/backport-commits';
import { getRepoToken } from './utils/token-util';
import { getSupportedBranches, getBackportPattern } from './utils/branch-util';
import { getCheckRun } from './utils/checks-util';
import { getOrCreateCheckRun } from './utils/checks-util';
import { getEnvVar } from './utils/env-util';
import { log } from './utils/log-util';
import { TryBackportOptions } from './interfaces';
Expand Down Expand Up @@ -450,7 +451,6 @@ export const backportImpl = async (
pr: WebHookPR,
targetBranch: string,
purpose: BackportPurpose,
checkRun: NonNullable<Awaited<ReturnType<typeof getCheckRun>>>,
labelToRemove?: string,
labelToAdd?: string,
) => {
Expand Down Expand Up @@ -486,10 +486,11 @@ export const backportImpl = async (
`backport-${pr.head.sha}-${targetBranch}-${purpose}`,
async () => {
log('backportImpl', LogLevel.INFO, `Executing ${bp} for "${slug}"`);
const checkRun = await getOrCreateCheckRun(context, pr, targetBranch);
log(
'backportImpl',
LogLevel.INFO,
`Updating check run ID ${checkRun.id} with status 'in_progress'`,
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with status 'in_progress'`,
);
await context.octokit.checks.update(
context.repo({
Expand Down Expand Up @@ -685,7 +686,7 @@ export const backportImpl = async (
log(
'backportImpl',
LogLevel.INFO,
`Updating check run ID ${checkRun.id} with conclusion 'success'`,
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with conclusion 'success'`,
);

await context.octokit.checks.update(
Expand Down Expand Up @@ -766,6 +767,7 @@ export const backportImpl = async (
]);
}

const checkRun = await getOrCreateCheckRun(context, pr, targetBranch);
const mdSep = '``````````````````````````````';
const updateOpts = context.repo({
check_run_id: checkRun.id,
Expand All @@ -781,6 +783,11 @@ export const backportImpl = async (
annotations: annotations ? annotations : undefined,
},
});
log(
'backportImpl',
LogLevel.INFO,
`Updating check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with conclusion 'neutral'`,
);
try {
await context.octokit.checks.update(updateOpts);
} catch (err) {
Expand Down
26 changes: 23 additions & 3 deletions src/utils/checks-util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CheckRunStatus } from '../enums';
import { CheckRunStatus, LogLevel } from '../enums';
import { BACKPORT_INFORMATION_CHECK, CHECK_PREFIX } from '../constants';
import {
SimpleWebHookRepoContext,
WebHookPR,
WebHookPRContext,
} from '../types';
import { log } from '../utils/log-util';

export async function updateBackportValidityCheck(
context: WebHookPRContext,
Expand Down Expand Up @@ -93,7 +94,7 @@ export async function queueBackportInformationCheck(context: WebHookPRContext) {
);
}

export async function getCheckRun(
export async function getOrCreateCheckRun(
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
Expand All @@ -105,7 +106,26 @@ export async function getCheckRun(
}),
);

return allChecks.data.check_runs.find((run) => {
let checkRun = allChecks.data.check_runs.find((run) => {
return run.name === `${CHECK_PREFIX}${targetBranch}`;
});

if (!checkRun) {
const response = 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',
}),
);
checkRun = response.data;
log(
'backportImpl',
LogLevel.INFO,
`Created check run '${CHECK_PREFIX}${targetBranch}' (${checkRun.id}) with status 'queued'`,
);
}

return checkRun;
}