export async function updateAllChecks()

in github_bot/src/github/utils.ts [188:256]


export async function updateAllChecks(
  ctx: ProbotEventContext<'issue_comment' | 'push'>,
  options: Partial<Pick<components['schemas']['check-run'], 'status' | 'conclusion'>>,
  buildUrl?: string,
  createNew?: boolean,
  sha?: string,
): Promise<void>;
export async function updateAllChecks(
  ctx: ProbotEventContext<'pull_request'>,
  options: Partial<Pick<components['schemas']['check-run'], 'status' | 'conclusion'>>,
  buildUrl?: string,
  createNew?: boolean,
): Promise<void>;
export async function updateAllChecks(
  ctx: ProbotEventContext<'pull_request' | 'issue_comment' | 'push'>,
  options: Partial<Pick<components['schemas']['check-run'], 'status' | 'conclusion'>>,
  buildUrl?: string,
  createNew: boolean = false,
  sha?: string,
): Promise<void> {
  const headSha = ctx.name === 'pull_request' ? ctx.payload.pull_request.head.sha : sha;
  if (!headSha) throw new Error('No sha provided to set check run');
  const { main, jobs } = getBuildConfig(ctx.name === 'push' && ctx.payload.ref === 'refs/heads/main');
  const updatedCheckIds = new Set<string>();

  if (!createNew) {
    const {
      status: resStatus,
      data: { total_count: totalCount, check_runs: checkRuns },
    } = await ctx.octokit.checks.listForRef({
      ...ctx.repo(),
      ref: headSha,
      app_id: Number(getConfig().github.auth.appId),
      per_page: 100, // max
    });
    if (resStatus !== 200) throw new Error('Failed to get checks for ref');
    if (checkRuns.length < totalCount) {
      // TODO handle this with pagination if check runs exceed 100
      throw new Error('Missing check runs, pagination required');
    }

    for (const { id, external_id, details_url, status } of checkRuns) {
      if (status !== 'completed' || external_id === main.id) {
        await ctx.octokit.checks.update({
          ...ctx.repo(),
          check_run_id: id,
          details_url: buildUrl || details_url,
          ...options,
        });
      }

      if (external_id) {
        // skip all already completed steps unless createNew explicitly set to true
        updatedCheckIds.add(external_id);
      }
    }
  }

  for (const { id, name } of [main, ...jobs].filter((j) => !updatedCheckIds.has(j.id))) {
    await ctx.octokit.checks.create({
      name,
      ...ctx.repo(),
      head_sha: headSha,
      external_id: id,
      details_url: buildUrl,
      ...options,
    });
  }
}