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(prlinter): support exempting checks from pr based on a label #6693

Merged
merged 6 commits into from
Mar 12, 2020
Merged
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
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