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

chore(prlint): fail prlinter on codecov failures, with exemption label #32868

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
55 changes: 55 additions & 0 deletions tools/@aws-cdk/prlint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import * as path from 'path';
import { Octokit } from '@octokit/rest';
import { Endpoints } from '@octokit/types';
import { StatusEvent } from '@octokit/webhooks-definitions/schema';
import type { components } from '@octokit/openapi-types';
import { findModulePath, moduleStability } from './module';
import { breakingModules } from './parser';

export type GitHubPr =
Endpoints['GET /repos/{owner}/{repo}/pulls/{pull_number}']['response']['data'];

export const CODE_BUILD_CONTEXT = 'AWS CodeBuild us-east-1 (AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv)';
export const CODECOV_PREFIX = 'codecov/';

export const CODECOV_CHECKS = [
'patch',
'patch/packages/aws-cdk',
'patch/packages/aws-cdk-lib/core',
'project',
'project/packages/aws-cdk',
'project/packages/aws-cdk-lib/core'
];

type CheckRunConclusion = components['schemas']['check-run']['conclusion']

const PR_FROM_MAIN_ERROR = 'Pull requests from `main` branch of a fork cannot be accepted. Please reopen this contribution from another branch on your fork. For more information, see https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md#step-4-pull-request.';

Expand All @@ -24,6 +37,7 @@ enum Exemption {
CLI_INTEG_TESTED = 'pr-linter/cli-integ-tested',
REQUEST_CLARIFICATION = 'pr/reviewer-clarification-requested',
REQUEST_EXEMPTION = 'pr-linter/exemption-requested',
CODECOV = "pr-linter/exempt-codecov",
}

export interface GithubStatusEvent {
Expand Down Expand Up @@ -352,6 +366,24 @@ export class PullRequestLinter {
}
}

private async checkRunConclusion(sha: string, checkName: string): Promise<CheckRunConclusion> {
const response = await this.client.paginate(this.client.checks.listForRef, {
owner: this.prParams.owner,
repo: this.prParams.repo,
ref: sha,
});

// grab the last check run that was started
const conclusion = response.check_runs
.filter(c => c.name === checkName)
.filter(c => c.started_at != null)
.sort((c1, c2) => c2.started_at!.localeCompare(c1.started_at!))
.map(s => s.conclusion)[0];

console.log(`${checkName} conclusion: ${conclusion}`)
return conclusion;
}

/**
* Assess whether or not a PR is ready for review.
* This is needed because some things that we need to evaluate are not filterable on
Expand Down Expand Up @@ -575,6 +607,25 @@ export class PullRequestLinter {
],
});

const codecovTests: Test[] = [];
for (const c of CODECOV_CHECKS) {
const checkName = `${CODECOV_PREFIX}${c}`;
const status = await this.checkRunConclusion(sha, checkName);
codecovTests.push({
test: () => {
const result = new TestResult();
const message = status == null ? `${checkName} has not started yet` : `${checkName} job is in status: ${status}`;
result.assessFailure(status !== 'success', message);
return result;
}
})
}

validationCollector.validateRuleSet({
exemption: shouldExemptCodecov,
testRuleSet: codecovTests,
});

console.log("Deleting PR Linter Comment now");
await this.deletePRLinterComment();
try {
Expand Down Expand Up @@ -656,6 +707,10 @@ function fixContainsIntegTest(pr: GitHubPr, files: GitHubFile[]): TestResult {
return result;
}

function shouldExemptCodecov(pr: GitHubPr): boolean {
return hasLabel(pr, Exemption.CODECOV);
}

function shouldExemptReadme(pr: GitHubPr): boolean {
return hasLabel(pr, Exemption.README);
}
Expand Down
13 changes: 13 additions & 0 deletions tools/@aws-cdk/prlint/test/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,18 @@ function configureMock(pr: Subset<linter.GitHubPr>, prFiles?: linter.GitHubFile[
},
};

const checksClient = {
listForRef() {
return {
data: { check_runs: linter.CODECOV_CHECKS.map(c => ({
name: `${linter.CODECOV_PREFIX}${c}`,
conclusion: 'success',
started_at: '1'
}))},
}
}
}

const searchClient = {
issuesAndPullRequests() {},
};
Expand All @@ -1204,6 +1216,7 @@ function configureMock(pr: Subset<linter.GitHubPr>, prFiles?: linter.GitHubFile[
issues: issuesClient as any,
search: searchClient as any,
repos: reposClient as any,
checks: checksClient as any,
paginate: (method: any, args: any) => { return method(args).data; },
} as any,
});
Expand Down
Loading