Skip to content

Commit

Permalink
fixup! fixup! feat(github-actions): create a unified status action
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott committed Nov 29, 2022
1 parent 98ab722 commit 5856156
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
7 changes: 5 additions & 2 deletions github-actions/unified-status-check/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ inputs:
angular-robot-key:
description: 'The private key for the Angular Robot Github app.'
required: true
ignored-statuses:
description: 'Statuses which should be ignored by the united status check action.'
ignored:
description: 'Status matchers to be ignored.'
required: false
required:
description: 'Status matchers required to match at least one status.'
required: false

runs:
Expand Down
47 changes: 20 additions & 27 deletions github-actions/unified-status-check/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23644,23 +23644,28 @@ var unifiedStatusCheckName = "Unified Status";
async function main() {
const github = new import_rest2.Octokit({ auth: await getAuthTokenFor(ANGULAR_ROBOT) });
try {
const ignoredStatuses = [
const ignored = [
unifiedStatusCheckName,
...core.getMultilineInput("ignored-statuses", { trimWhitespace: true })
...core.getMultilineInput("ignored", { trimWhitespace: true })
];
const required = core.getMultilineInput("required", { trimWhitespace: true });
const pullRequest = (await github.graphql((0, import_typed_graphqlify.query)(PR_SCHEMA).toString(), {
...import_github2.context.issue
})).repository.pullRequest;
const statusChecks = pullRequest.commits.nodes[0].commit.statusCheckRollup;
if (!statusChecks || pullRequest.isDraft) {
const setStatus = async (state, description) => {
await github.repos.createCommitStatus({
...import_github2.context.repo,
state: "pending",
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
description: "Unified Status checks have not yet started"
state,
description
});
return;
};
if (!statusChecks || pullRequest.isDraft) {
await setStatus("pending", "Unified Status checks have not yet started");
return;
}
const statuses = statusChecks.contexts.nodes.map((checkOrStatus) => {
if (checkOrStatus.__typename === "StatusContext") {
Expand All @@ -23675,8 +23680,13 @@ async function main() {
name: checkOrStatus.name || "Unknown Check Run"
};
}
throw Error();
}).filter(({ name }) => !ignoredStatuses.includes(name));
throw Error("CheckOrStatus was not found to be a check or a status");
}).filter(({ name }) => !ignored.includes(name));
const missedStatuses = required.filter((matcher) => !statuses.some(({ name }) => name.match(matcher)));
if (missedStatuses.length > 0) {
await setStatus("pending", `Waiting for missing required status: ${missedStatuses.join(", ")}`);
return;
}
const counts = statuses.reduce((count, { state }) => {
if (isPassingState(state)) {
count.passing += 1;
Expand All @@ -23690,31 +23700,14 @@ async function main() {
return count;
}, { passing: 0, failing: 0, pending: 0 });
if (counts.failing > 0) {
await github.repos.createCommitStatus({
...import_github2.context.repo,
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
state: "failure",
description: `${counts.failing} expected status(es) failing`
});
await setStatus("failure", `${counts.failing} expected status(es) failing`);
return;
}
if (counts.pending > 0) {
await github.repos.createCommitStatus({
...import_github2.context.repo,
state: "pending",
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
description: "Expected statuses are still pending"
});
await setStatus("pending", "Expected statuses are still pending");
return;
}
await github.repos.createCommitStatus({
...import_github2.context.repo,
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
state: "success"
});
await setStatus("success");
} finally {
await revokeActiveInstallationToken(github);
}
Expand Down
62 changes: 33 additions & 29 deletions github-actions/unified-status-check/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ async function main() {
const github = new Octokit({auth: await getAuthTokenFor(ANGULAR_ROBOT)});

try {
/** Statuses to ignore within the context of the action, always ignoring the actions own status. */
const ignoredStatuses = [
/** Status matchers to ignore within the context of the action, always ignoring the actions own status. */
const ignored = [
unifiedStatusCheckName,
...core.getMultilineInput('ignored-statuses', {trimWhitespace: true}),
...core.getMultilineInput('ignored', {trimWhitespace: true}),
];
/** Status matchers which must match at least one of the current statuses . */
const required = core.getMultilineInput('required', {trimWhitespace: true});
/** The pull request triggering the event */
const pullRequest = (
await github.graphql<typeof PR_SCHEMA>(query(PR_SCHEMA).toString(), {
Expand All @@ -26,16 +28,24 @@ async function main() {
/** The status check rollup results. */
const statusChecks = pullRequest.commits.nodes[0].commit.statusCheckRollup;

/** If no status checks are present, or if the pull request is in a draft state the unified status is in a pending state. */
if (!statusChecks || pullRequest.isDraft) {
const setStatus = async (
state: 'pending' | 'error' | 'failure' | 'success',
description?: string,
) => {
await github.repos.createCommitStatus({
...context.repo,
state: 'pending',
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
description: 'Unified Status checks have not yet started',
state,
description,
});
return;
};

/** If no status checks are present, or if the pull request is in a draft state the unified status is in a pending state. */
if (!statusChecks || pullRequest.isDraft) {
await setStatus('pending', 'Unified Status checks have not yet started');
return;
}

const statuses: {state: StatusState; name: string}[] = statusChecks.contexts.nodes
Expand All @@ -54,9 +64,20 @@ async function main() {
name: checkOrStatus.name || 'Unknown Check Run',
};
}
throw Error();
throw Error('CheckOrStatus was not found to be a check or a status');
})
.filter(({name}) => !ignoredStatuses.includes(name));
.filter(({name}) => !ignored.includes(name));

const missedStatuses = required.filter(
(matcher) => !statuses.some(({name}) => name.match(matcher)),
);
if (missedStatuses.length > 0) {
await setStatus(
'pending',
`Waiting for missing required status: ${missedStatuses.join(', ')}`,
);
return;
}

const counts = statuses.reduce(
(count, {state}) => {
Expand All @@ -75,33 +96,16 @@ async function main() {
);

if (counts.failing > 0) {
await github.repos.createCommitStatus({
...context.repo,
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
state: 'failure',
description: `${counts.failing} expected status(es) failing`,
});
await setStatus('failure', `${counts.failing} expected status(es) failing`);
return;
}

if (counts.pending > 0) {
await github.repos.createCommitStatus({
...context.repo,
state: 'pending',
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
description: 'Expected statuses are still pending',
});
await setStatus('pending', 'Expected statuses are still pending');
return;
}

await github.repos.createCommitStatus({
...context.repo,
sha: pullRequest.commits.nodes[0].commit.oid,
context: unifiedStatusCheckName,
state: 'success',
});
await setStatus('success');
} finally {
await revokeActiveInstallationToken(github);
}
Expand Down

0 comments on commit 5856156

Please sign in to comment.