diff --git a/src/index.ts b/src/index.ts index 15a5dca..fb3f796 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -33,7 +27,6 @@ import { updateBackportInformationCheck, updateBackportValidityCheck, } from './utils/checks-util'; -import { log } from './utils/log-util'; import { register } from './utils/prom'; import { SimpleWebHookRepoContext, @@ -116,36 +109,6 @@ 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, @@ -153,7 +116,6 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { pr, targetBranch, BackportPurpose.Check, - checkRun, ); } diff --git a/src/operations/backport-to-location.ts b/src/operations/backport-to-location.ts index e593d9f..0a562b2 100644 --- a/src/operations/backport-to-location.ts +++ b/src/operations/backport-to-location.ts @@ -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. * @@ -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( @@ -94,7 +51,6 @@ export const backportToLabel = async ( pr, targetBranch, BackportPurpose.ExecuteBackport, - checkRun, labelToRemove, labelToAdd, ); @@ -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( @@ -129,7 +83,6 @@ export const backportToBranch = async ( pr, targetBranch, BackportPurpose.ExecuteBackport, - checkRun, labelToRemove, labelToAdd, ); diff --git a/src/utils.ts b/src/utils.ts index a22fcde..cf205e2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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'; @@ -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'; @@ -450,7 +451,6 @@ export const backportImpl = async ( pr: WebHookPR, targetBranch: string, purpose: BackportPurpose, - checkRun: NonNullable>>, labelToRemove?: string, labelToAdd?: string, ) => { @@ -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({ @@ -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( @@ -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, @@ -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) { diff --git a/src/utils/checks-util.ts b/src/utils/checks-util.ts index 710cda1..426f820 100644 --- a/src/utils/checks-util.ts +++ b/src/utils/checks-util.ts @@ -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, @@ -93,7 +94,7 @@ export async function queueBackportInformationCheck(context: WebHookPRContext) { ); } -export async function getCheckRun( +export async function getOrCreateCheckRun( context: SimpleWebHookRepoContext, pr: WebHookPR, targetBranch: string, @@ -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; }