From b9d2ac32d54110636a3abd7863ed00af030fec84 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 13 Mar 2023 14:59:13 -0400 Subject: [PATCH] chore(ci): verify the PR title as part of linting (#18163) This verifies the PR title as part of the lint step. --- .github/workflows/ci.generate.ts | 6 ++++ .github/workflows/ci.yml | 3 ++ tools/verify_pr_title.js | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tools/verify_pr_title.js diff --git a/.github/workflows/ci.generate.ts b/.github/workflows/ci.generate.ts index 06d8e0a02a..46a2bc632e 100644 --- a/.github/workflows/ci.generate.ts +++ b/.github/workflows/ci.generate.ts @@ -473,6 +473,12 @@ const ci = { run: "deno run --unstable --allow-write --allow-read --allow-run ./tools/format.js --check", }, + { + name: "Lint PR title", + if: "matrix.job == 'lint' && github.event_name == 'pull_request'", + run: + "deno run ./tools/verify_pr_title.js '${{ github.event.pull_request.title }}'", + }, { name: "lint.js", if: "matrix.job == 'lint'", diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 474da1deb1..ce99b96c63 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -285,6 +285,9 @@ jobs: - name: test_format.js if: '!(github.event_name == ''pull_request'' && matrix.skip_pr) && (steps.exit_early.outputs.EXIT_EARLY != ''true'' && (matrix.job == ''lint''))' run: deno run --unstable --allow-write --allow-read --allow-run ./tools/format.js --check + - name: Lint PR title + if: '!(github.event_name == ''pull_request'' && matrix.skip_pr) && (steps.exit_early.outputs.EXIT_EARLY != ''true'' && (matrix.job == ''lint'' && github.event_name == ''pull_request''))' + run: 'deno run ./tools/verify_pr_title.js ''${{ github.event.pull_request.title }}''' - name: lint.js if: '!(github.event_name == ''pull_request'' && matrix.skip_pr) && (steps.exit_early.outputs.EXIT_EARLY != ''true'' && (matrix.job == ''lint''))' run: deno run --unstable --allow-write --allow-read --allow-run ./tools/lint.js diff --git a/tools/verify_pr_title.js b/tools/verify_pr_title.js new file mode 100644 index 0000000000..be877bbfd8 --- /dev/null +++ b/tools/verify_pr_title.js @@ -0,0 +1,47 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +const prTitle = Deno.args[0]; + +if (prTitle == null) { + Deno.exit(0); // not a PR +} + +console.log("PR title:", prTitle); + +// This is a release PR, so it's valid. +if (/^[^\s]+\.[^\s]+\.[^\s]+$/.test(prTitle)) { + console.log("Valid."); + Deno.exit(0); +} + +const validPrefixes = [ + "chore", + "fix", + "feat", + "perf", + "ci", + "cleanup", + "docs", + "bench", + "build", + "refactor", + "test", + // allow Revert PRs because it allows us to remove the landed + // commit from the generated changelog + "Revert ", +]; + +if (validPrefixes.some((prefix) => prTitle.startsWith(prefix))) { + console.log("Valid."); +} else { + console.error( + "The PR title must start with one of the following prefixes:\n", + ); + for (const prefix of validPrefixes) { + console.error(` - ${prefix}`); + } + console.error( + "\nPlease fix the PR title according to https://www.conventionalcommits.org " + + "then push an empty commit to reset the CI.", + ); + Deno.exit(1); +}