Skip to content

Commit

Permalink
feat(prlinter): support exempting checks from pr based on a label (#6693
Browse files Browse the repository at this point in the history
)

* support exempting readme changes from pr based on a label

* added test exemption as well

* add logs when exempting PR's

* dummy to trigger build

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
iliapolo and mergify[bot] authored Mar 12, 2020
1 parent 55cfa75 commit 77cbe80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion tools/prlint/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# prlint (beta)

A linter that can run various checks to validate a PR adheres to our standards.
A linter that can run various checks to validate a PR adheres to our standards.

### Disclaimer

Expand Down Expand Up @@ -41,3 +41,4 @@ Error: Features must contain a change to a README file
```

Note that an **un-authenticated** GitHub client is created, unless you set the `GITHUB_TOKEN` env variable.

39 changes: 32 additions & 7 deletions tools/prlint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const GitHub = require("github-api")

const OWNER = "aws"
const REPO = "aws-cdk"
const EXEMPT_README = 'pr-linter/exempt-readme'
const EXEMPT_TEST = 'pr-linter/exempt-test'

class LinterError extends Error {
constructor(message) {
Expand All @@ -18,7 +20,7 @@ function createGitHubClient() {
} else {
console.log("Creating un-authenticated GitHub Client")
}

return new GitHub({'token': token});
}

Expand Down Expand Up @@ -60,17 +62,31 @@ function fixContainsTest(issue, files) {
};
};

function shouldExemptReadme(issue) {
return hasLabel(issue, EXEMPT_README);
}

function shouldExemptTest(issue) {
return hasLabel(issue, EXEMPT_TEST);
}

function hasLabel(issue, labelName) {
return issue.labels.some(function (l) {
return l.name === labelName;
})
}

async function mandatoryChanges(number) {

if (!number) {
throw new Error('Must provide a PR number')
}

const gh = createGitHubClient();

const issues = gh.getIssues(OWNER, REPO);
const repo = gh.getRepo(OWNER, REPO);

console.log(`⌛ Fetching PR number ${number}`)
const issue = (await issues.getIssue(number)).data;

Expand All @@ -79,12 +95,21 @@ async function mandatoryChanges(number) {

console.log("⌛ Validating...");

featureContainsReadme(issue, files);
featureContainsTest(issue, files);
fixContainsTest(issue, files);
if (shouldExemptReadme(issue)) {
console.log(`Not validating README changes since the PR is labeled with '${EXEMPT_README}'`)
} else {
featureContainsReadme(issue, files);
}

if (shouldExemptTest(issue)) {
console.log(`Not validating test changes since the PR is labeled with '${EXEMPT_TEST}'`)
} else {
featureContainsTest(issue, files);
fixContainsTest(issue, files);
}

console.log("✅ Success")

}

// we don't use the 'export' prefix because github actions
Expand Down

0 comments on commit 77cbe80

Please sign in to comment.