UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

64 lines (63 loc) • 2.13 kB
import * as core from '@actions/core'; import * as github from '@actions/github'; import { CheckConclusion } from '../helpers/types.js'; import { batch } from './utils.js'; const NOT_FOUND_RETRY_DELAYS_MS = [1000, 2000, 4000]; function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function updateWithNotFoundRetry(octokit, params) { for (let attempt = 0;; attempt++) { try { return await octokit.rest.checks.update(params); } catch (error) { const delay = NOT_FOUND_RETRY_DELAYS_MS[attempt]; if (error?.status !== 404 || delay === undefined) { throw error; } core.warning(`Check run ${params.check_run_id} not found, retrying in ${delay}ms (attempt ${attempt + 1} of ${NOT_FOUND_RETRY_DELAYS_MS.length})`); await sleep(delay); } } } export async function updateCheckRun(octokit, checkId, { conclusion, output }) { core.info(`Updating check: ${checkId}`); const { title, summary, annotations = [] } = output; const batches = batch(annotations, 50); core.info(`annotations to be sent: ${annotations.length}`); await updateWithNotFoundRetry(octokit, { check_run_id: checkId, completed_at: new Date().toISOString(), status: 'completed', ...github.context.repo, conclusion, output: { title, summary, }, }); try { for (const chunk of batches) { await updateWithNotFoundRetry(octokit, { check_run_id: checkId, ...github.context.repo, output: { title, summary, annotations: chunk, }, }); core.info(`annotations sent (${chunk.length})`); } } catch (error) { core.error(`failed to send annotations: ${error}`); throw error; } // Fail if (conclusion === CheckConclusion.Failure) { return core.setFailed(output.title); } // Success or Neutral }