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

feat(github-actions): merge all validation results instead of exiting on first non-passing validation #938

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion github-actions/unified-status-check/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package(default_visibility = ["//github-actions/unified-status-check:__subpackag
ts_library(
name = "unified-status-check",
srcs = glob(
["src/*.ts"],
["src/**/*.ts"],
),
deps = [
"//github-actions:utils",
Expand Down
113 changes: 82 additions & 31 deletions github-actions/unified-status-check/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2146,10 +2146,10 @@ Support boolean input list: \`true | True | TRUE | false | False | FALSE\``);
command_1.issueCommand("save-state", { name }, value);
}
exports.saveState = saveState;
function getState(name) {
function getState2(name) {
return process.env[`STATE_${name}`] || "";
}
exports.getState = getState;
exports.getState = getState2;
function getIDToken(aud) {
return __awaiter(this, void 0, void 0, function* () {
return yield oidc_utils_1.OidcClient.getIDToken(aud);
Expand Down Expand Up @@ -23706,15 +23706,16 @@ var PullRequest = class {
this.statuses.all.push(status);
});
}
async setCheckResult(state, description) {
async setCheckResult({ state, title, summary }) {
const parameters = {
...import_github2.context.repo,
name: PullRequest.checkName,
head_sha: this.sha,
status: state === "pending" ? "in_progress" : "completed",
conclusion: state === "pending" ? void 0 : state,
output: {
summary: description
title,
summary
}
};
if (this.previousRunId) {
Expand All @@ -23741,7 +23742,7 @@ var checkConclusionStateToStatusStateMap = /* @__PURE__ */ new Map([
["TIMED_OUT", "failure"]
]);
var PR_SCHEMA = {
repository: (0, import_typed_graphqlify.params)({ owner: import_github2.context.repo.owner, name: import_github2.context.repo.repo }, {
repository: (0, import_typed_graphqlify.params)({ owner: `"${import_github2.context.repo.owner}"`, name: `"${import_github2.context.repo.repo}"` }, {
pullRequest: (0, import_typed_graphqlify.params)({ number: import_github2.context.issue.number }, {
isDraft: import_typed_graphqlify.types.boolean,
state: import_typed_graphqlify.types.custom(),
Expand Down Expand Up @@ -23821,7 +23822,7 @@ var checkOnlyPassingStatuses = ({ statuses }) => {
if (statuses.failure.length > 0) {
return {
state: "failure",
description: `${statuses.failure} expected status(es) failing`
description: `${statuses.failure.length} expected status(es) failing`
};
}
if (statuses.pending.length > 0) {
Expand Down Expand Up @@ -24034,35 +24035,85 @@ var isMergeReady = (pullRequest) => {
};

//
function getState(results) {
if (results.failure.length > 0) {
return "failure";
}
if (results.pending.length > 0) {
return "pending";
}
if (results.success.length > 0) {
return "success";
}
return "pending";
}
function getTitle(state, results) {
const openMoreInfoText = " (open details for more info)";
if (state === "success") {
return "Pull Request is ready for merge!";
}
let title = results[state].map(({ description }) => description).join(" ");
if (title.length >= 160) {
return `${title.slice(0, 160 - openMoreInfoText.length)}${openMoreInfoText}`;
}
return title;
}
function getSummary(results, pullRequest) {
return `
### Validations

#### Failing
${results.failure.length ? results.failure.map(({ description }) => ` - ${description}`).join("\n") : "No failing validations."}

#### Pending
${results.pending.length ? results.pending.map(({ description }) => ` - ${description}`).join("\n") : "No pending validations."}

#### Success
${results.success.length ? results.success.map(({ description }) => ` - ${description}`).join("\n") : "No successful validations."}


### Status and Check Results
${pullRequest.statuses.all.map(({ name, state, description }) => {
return ` - ${stateToIconMap.get(state)} **${name}**: ${description}`;
}).join("\n")}
`;
}
var stateToIconMap = /* @__PURE__ */ new Map([
["failure", "\u274C"],
["pending", "\u{1F7E1}"],
["success", "\u2705"]
]);
function buildCheckResultOutput(results, pullRequest) {
const state = getState(results);
return {
state,
title: getTitle(state, results),
summary: getSummary(results, pullRequest)
};
}

//
var validators = [
isDraft,
isMergeReady,
checkRequiredStatuses,
checkOnlyPassingStatuses,
checkForTargelLabel
];
async function main() {
const github = new import_rest2.Octokit({ auth: await getAuthTokenFor(ANGULAR_ROBOT) });
try {
const pullRequest = await PullRequest.get(github);
const isDraftValidationResult = isDraft(pullRequest);
if (isDraftValidationResult.state === "pending") {
await pullRequest.setCheckResult(isDraftValidationResult.state, isDraftValidationResult.description);
return;
}
const isMergeReadyResult = isMergeReady(pullRequest);
if (isMergeReadyResult.state === "pending") {
await pullRequest.setCheckResult(isMergeReadyResult.state, isMergeReadyResult.description);
return;
}
const requiredStatusesResult = checkRequiredStatuses(pullRequest);
if (requiredStatusesResult.state === "pending") {
await pullRequest.setCheckResult(requiredStatusesResult.state, requiredStatusesResult.description);
return;
}
const onlyPassingStatusesResult = checkOnlyPassingStatuses(pullRequest);
if (onlyPassingStatusesResult.state === "pending") {
await pullRequest.setCheckResult(onlyPassingStatusesResult.state, onlyPassingStatusesResult.description);
return;
}
const targetLabelResult = checkForTargelLabel(pullRequest);
if (targetLabelResult.state === "pending") {
await pullRequest.setCheckResult(targetLabelResult.state, targetLabelResult.description);
return;
}
const validationResultByState = {
pending: [],
success: [],
failure: []
};
validators.forEach((validator) => {
const result = validator(pullRequest);
validationResultByState[result.state].push(result);
});
await pullRequest.setCheckResult(buildCheckResultOutput(validationResultByState, pullRequest));
} finally {
await revokeActiveInstallationToken(github);
}
Expand Down
70 changes: 27 additions & 43 deletions github-actions/unified-status-check/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import * as core from '@actions/core';
import {Octokit} from '@octokit/rest';
import {getAuthTokenFor, ANGULAR_ROBOT, revokeActiveInstallationToken} from '../../utils.js';
import {PullRequest} from './pull-request.js';
import {isDraft} from './draft-mode.js';
import {checkOnlyPassingStatuses, checkRequiredStatuses} from './statuses.js';
import {checkForTargelLabel, isMergeReady} from './labels.js';
import {isDraft} from './validators/draft-mode.js';
import {checkOnlyPassingStatuses, checkRequiredStatuses} from './validators/statuses.js';
import {checkForTargelLabel, isMergeReady} from './validators/labels.js';
import {buildCheckResultOutput, ValidationResults} from './validator.js';

/** The validation functions for check against the pull request. */
const validators = [
isDraft,
isMergeReady,
checkRequiredStatuses,
checkOnlyPassingStatuses,
checkForTargelLabel,
];

async function main() {
/** A Github API instance. */
Expand All @@ -13,46 +23,20 @@ async function main() {
try {
/** The pull request triggering the event */
const pullRequest = await PullRequest.get(github);

/** If no status checks are present, or if the pull request is in a draft state the unified status is in a pending state. */
const isDraftValidationResult = isDraft(pullRequest);
if (isDraftValidationResult.state === 'pending') {
await pullRequest.setCheckResult(
isDraftValidationResult.state,
isDraftValidationResult.description,
);
return;
}

const isMergeReadyResult = isMergeReady(pullRequest);
if (isMergeReadyResult.state === 'pending') {
await pullRequest.setCheckResult(isMergeReadyResult.state, isMergeReadyResult.description);
return;
}

const requiredStatusesResult = checkRequiredStatuses(pullRequest);
if (requiredStatusesResult.state === 'pending') {
await pullRequest.setCheckResult(
requiredStatusesResult.state,
requiredStatusesResult.description,
);
return;
}

const onlyPassingStatusesResult = checkOnlyPassingStatuses(pullRequest);
if (onlyPassingStatusesResult.state === 'pending') {
await pullRequest.setCheckResult(
onlyPassingStatusesResult.state,
onlyPassingStatusesResult.description,
);
return;
}

const targetLabelResult = checkForTargelLabel(pullRequest);
if (targetLabelResult.state === 'pending') {
await pullRequest.setCheckResult(targetLabelResult.state, targetLabelResult.description);
return;
}
/** The results of the validation functions, organized by validation result state. */
const validationResultByState: ValidationResults = {
pending: [],
success: [],
failure: [],
};

// Run all of the validators and sort the results.
validators.forEach((validator) => {
const result = validator(pullRequest);
validationResultByState[result.state].push(result);
});

await pullRequest.setCheckResult(buildCheckResultOutput(validationResultByState, pullRequest));
} finally {
await revokeActiveInstallationToken(github);
}
Expand Down
15 changes: 12 additions & 3 deletions github-actions/unified-status-check/src/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ export class PullRequest {
}

/** Set the check run result for the pull request. */
async setCheckResult(state: NormalizedState, description?: string) {
async setCheckResult({
state,
title,
summary,
}: {
state: NormalizedState;
title: string;
summary: string;
}) {
/** The parameters for the check run update or creation. */
const parameters = {
...context.repo,
Expand All @@ -133,7 +141,8 @@ export class PullRequest {
status: state === 'pending' ? 'in_progress' : 'completed',
conclusion: state === 'pending' ? undefined : state,
output: {
summary: description,
title,
summary,
},
};

Expand Down Expand Up @@ -167,7 +176,7 @@ const checkConclusionStateToStatusStateMap = new Map<
/** GraphQL schema for requesting the status information for a given pull request. */
const PR_SCHEMA = {
repository: params(
{owner: context.repo.owner, name: context.repo.repo},
{owner: `"${context.repo.owner}"`, name: `"${context.repo.repo}"`},
{
pullRequest: params(
{number: context.issue.number},
Expand Down
6 changes: 0 additions & 6 deletions github-actions/unified-status-check/src/validation.ts

This file was deleted.

91 changes: 91 additions & 0 deletions github-actions/unified-status-check/src/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {NormalizedState, PullRequest} from './pull-request.js';

type ValidationResult = {
state: NormalizedState;
description: string;
};

export type ValidationFunction = (pullRequest: PullRequest) => ValidationResult;

export type ValidationResults = {
pending: ValidationResult[];
success: ValidationResult[];
failure: ValidationResult[];
};

function getState(results: ValidationResults): NormalizedState {
if (results.failure.length > 0) {
return 'failure';
}
if (results.pending.length > 0) {
return 'pending';
}
if (results.success.length > 0) {
return 'success';
}
return 'pending';
}

function getTitle(state: NormalizedState, results: ValidationResults) {
const openMoreInfoText = ' (open details for more info)';

if (state === 'success') {
return 'Pull Request is ready for merge!';
}

let title = results[state].map(({description}) => description).join(' ');
if (title.length >= 160) {
return `${title.slice(0, 160 - openMoreInfoText.length)}${openMoreInfoText}`;
}
return title;
}

function getSummary(results: ValidationResults, pullRequest: PullRequest) {
return `
### Validations

#### Failing
${
results.failure.length
? results.failure.map(({description}) => ` - ${description}`).join('\n')
: 'No failing validations.'
}

#### Pending
${
results.pending.length
? results.pending.map(({description}) => ` - ${description}`).join('\n')
: 'No pending validations.'
}

#### Success
${
results.success.length
? results.success.map(({description}) => ` - ${description}`).join('\n')
: 'No successful validations.'
}


### Status and Check Results
${pullRequest.statuses.all
.map(({name, state, description}) => {
return ` - ${stateToIconMap.get(state)} **${name}**: ${description}`;
})
.join('\n')}
`;
}

const stateToIconMap = new Map<NormalizedState, string>([
['failure', '❌'],
['pending', '🟡'],
['success', '✅'],
]);

export function buildCheckResultOutput(results: ValidationResults, pullRequest: PullRequest) {
const state = getState(results);
return {
state,
title: getTitle(state, results),
summary: getSummary(results, pullRequest),
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PullRequest} from './pull-request.js';
import {ValidationFunction} from './validation.js';
import {PullRequest} from '../pull-request.js';
import {ValidationFunction} from '../validator.js';

export const isDraft: ValidationFunction = (pullRequest: PullRequest) => {
if (pullRequest.isDraft) {
Expand Down
Loading