From 277c2303f965680aed7613eb512365c58aa92b6b Mon Sep 17 00:00:00 2001 From: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> Date: Wed, 13 Apr 2022 17:00:08 +0900 Subject: [PATCH] feat: Add `ignoreLabels` option to opt-out of validation for certain PRs (#174) --- .../lint-pr-title-preview-ignoreLabels.yml | 27 +++++++++++++++++++ README.md | 7 +++++ action.yml | 3 +++ src/index.js | 16 ++++++++++- src/parseConfig.js | 8 +++++- 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/lint-pr-title-preview-ignoreLabels.yml diff --git a/.github/workflows/lint-pr-title-preview-ignoreLabels.yml b/.github/workflows/lint-pr-title-preview-ignoreLabels.yml new file mode 100644 index 000000000..537f017a9 --- /dev/null +++ b/.github/workflows/lint-pr-title-preview-ignoreLabels.yml @@ -0,0 +1,27 @@ +name: 'Lint PR title preview (current branch, ignoreLabels enabled)' +on: + pull_request: + types: + - opened + - edited + - synchronize + - labeled + - unlabeled + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 + - run: yarn install + - run: yarn build + - uses: ./ + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + ignoreLabels: | + bot + ignore-semantic-pull-request diff --git a/README.md b/README.md index 0d5a3dd2a..605f93b10 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ The action works without configuration, however you can provide options for cust validateSingleCommitMatchesPrTitle: true # If you use GitHub Enterprise, you can set this to the URL of your server githubBaseUrl: https://github.myorg.com/api/v3 + # If the PR contains one of these labels, the validation is skipped. + # Multiple labels can be separated by newlines. + # If you want to rerun the validation when labels change, you might want + # to use the `labeled` and `unlabeled` event triggers in your workflow. + ignoreLabels: | + bot + ignore-semantic-pull-request ``` ## Event triggers diff --git a/action.yml b/action.yml index 0bacf5d70..c4699ab92 100644 --- a/action.yml +++ b/action.yml @@ -35,3 +35,6 @@ inputs: githubBaseUrl: description: "If you use Github Enterprise, you can set this to the URL of your server (e.g. https://github.myorg.com/api/v3)" required: false + ignoreLabels: + description: "If the PR contains one of these labels, the validation is skipped. Multiple labels can be separated by newlines. If you want to rerun the validation when labels change, you might want to use the `labeled` and `unlabeled` event triggers in your workflow." + required: false diff --git a/src/index.js b/src/index.js index 545b35e90..b53f621a9 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,8 @@ module.exports = async function run() { subjectPatternError, validateSingleCommit, validateSingleCommitMatchesPrTitle, - githubBaseUrl + githubBaseUrl, + ignoreLabels } = parseConfig(); const client = github.getOctokit(process.env.GITHUB_TOKEN, { @@ -41,6 +42,19 @@ module.exports = async function run() { pull_number: contextPullRequest.number }); + // Ignore errors if specified labels are added. + if (ignoreLabels) { + const labelNames = pullRequest.labels.map((label) => label.name); + for (const labelName of labelNames) { + if (ignoreLabels.includes(labelName)) { + core.info( + `Validation was skipped because the PR label "${labelName}" was found.` + ); + return; + } + } + } + // Pull requests that start with "[WIP] " are excluded from the check. const isWip = wip && /^\[WIP\]\s/.test(pullRequest.title); diff --git a/src/parseConfig.js b/src/parseConfig.js index 9ff23731e..773b0d989 100644 --- a/src/parseConfig.js +++ b/src/parseConfig.js @@ -52,6 +52,11 @@ module.exports = function parseConfig() { githubBaseUrl = ConfigParser.parseString(process.env.INPUT_GITHUBBASEURL); } + let ignoreLabels; + if (process.env.INPUT_IGNORELABELS) { + ignoreLabels = ConfigParser.parseEnum(process.env.INPUT_IGNORELABELS); + } + return { types, scopes, @@ -61,6 +66,7 @@ module.exports = function parseConfig() { subjectPatternError, validateSingleCommit, validateSingleCommitMatchesPrTitle, - githubBaseUrl + githubBaseUrl, + ignoreLabels }; };