Skip to content

Commit b49c116

Browse files
authored
chore(prlint): fail prlinter on codecov failures, with exemption label (#32674)
Our current process for codecov statuses is that they block the merge of a PR if the are failing. This PR attempts to move this failure further forward in the PR process, so that the PRlinter fails and alerts the author. It also means that we can include an exemption label to merge a PR that we allow to be in breach of the codecov report. Currently, to do so we must change the branch protection rules. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3717f40 commit b49c116

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

tools/@aws-cdk/prlint/lint.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ export type GitHubPr =
1010
Endpoints['GET /repos/{owner}/{repo}/pulls/{pull_number}']['response']['data'];
1111

1212
export const CODE_BUILD_CONTEXT = 'AWS CodeBuild us-east-1 (AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv)';
13-
13+
export const CODECOV_PREFIX = 'codecov/';
14+
15+
export const CODECOV_CHECKS = [
16+
'patch',
17+
'patch/packages/aws-cdk',
18+
'patch/packages/aws-cdk-lib/core',
19+
'project',
20+
'project/packages/aws-cdk',
21+
'project/packages/aws-cdk-lib/core'
22+
];
1423
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.';
1524

1625
/**
@@ -24,6 +33,7 @@ enum Exemption {
2433
CLI_INTEG_TESTED = 'pr-linter/cli-integ-tested',
2534
REQUEST_CLARIFICATION = 'pr/reviewer-clarification-requested',
2635
REQUEST_EXEMPTION = 'pr-linter/exemption-requested',
36+
CODECOV = "pr-linter/exempt-codecov",
2737
}
2838

2939
export interface GithubStatusEvent {
@@ -336,14 +346,23 @@ export class PullRequestLinter {
336346
* @param sha the commit sha to evaluate
337347
*/
338348
private async codeBuildJobSucceeded(sha: string): Promise<boolean> {
339-
const statuses = await this.client.repos.listCommitStatusesForRef({
349+
return this.checkStatusSucceeded(sha, CODE_BUILD_CONTEXT);
350+
}
351+
352+
/**
353+
* Check a specific status check to see if it is successful for the given commit
354+
*
355+
* @param sha the commit sha to evaluate
356+
*/
357+
private async checkStatusSucceeded(sha: string, context: string): Promise<boolean> {
358+
const statuses = await this.client.paginate(this.client.repos.listCommitStatusesForRef, {
340359
owner: this.prParams.owner,
341360
repo: this.prParams.repo,
342361
ref: sha,
343362
});
344-
let status = statuses.data.filter(status => status.context === CODE_BUILD_CONTEXT).map(status => status.state);
345-
console.log("CodeBuild Commit Statuses: ", status);
346-
return statuses.data.some(status => status.context === CODE_BUILD_CONTEXT && status.state === 'success');
363+
let status = statuses.filter(status => status.context === context).map(status => status.state);
364+
console.log(`${context} statuses: ${status}`);
365+
return statuses.some(status => status.context === context && status.state === 'success');
347366
}
348367

349368
public async validateStatusEvent(pr: GitHubPr, status: StatusEvent): Promise<void> {
@@ -575,6 +594,24 @@ export class PullRequestLinter {
575594
],
576595
});
577596

597+
const codecovTests: Test[] = [];
598+
for (const c of CODECOV_CHECKS) {
599+
const checkName = `${CODECOV_PREFIX}${c}`;
600+
const succeeded = await this.checkStatusSucceeded(sha, checkName);
601+
codecovTests.push({
602+
test: () => {
603+
const result = new TestResult();
604+
result.assessFailure(!succeeded, `${checkName} job is not succeeding`);
605+
return result;
606+
}
607+
})
608+
}
609+
610+
validationCollector.validateRuleSet({
611+
exemption: shouldExemptCodecov,
612+
testRuleSet: codecovTests,
613+
});
614+
578615
console.log("Deleting PR Linter Comment now");
579616
await this.deletePRLinterComment();
580617
try {
@@ -656,6 +693,10 @@ function fixContainsIntegTest(pr: GitHubPr, files: GitHubFile[]): TestResult {
656693
return result;
657694
}
658695

696+
function shouldExemptCodecov(pr: GitHubPr): boolean {
697+
return hasLabel(pr, Exemption.CODECOV);
698+
}
699+
659700
function shouldExemptReadme(pr: GitHubPr): boolean {
660701
return hasLabel(pr, Exemption.README);
661702
}

tools/@aws-cdk/prlint/test/lint.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,13 @@ function configureMock(pr: Subset<linter.GitHubPr>, prFiles?: linter.GitHubFile[
11821182
const reposClient = {
11831183
listCommitStatusesForRef() {
11841184
return {
1185-
data: [{
1186-
context: linter.CODE_BUILD_CONTEXT,
1187-
state: 'success',
1188-
}],
1185+
data: [
1186+
{
1187+
context: linter.CODE_BUILD_CONTEXT,
1188+
state: 'success',
1189+
},
1190+
...(linter.CODECOV_CHECKS.map(c => ({ context: `${linter.CODECOV_PREFIX}${c}`, state: 'success'})))
1191+
],
11891192
};
11901193
},
11911194
};

0 commit comments

Comments
 (0)