From 6fb9493672d60612f5122d7f70796a4d20fda472 Mon Sep 17 00:00:00 2001 From: Scott Schreckengaust Date: Wed, 29 Nov 2023 19:51:55 +0000 Subject: [PATCH] feat: add commitlint github workflow Signed-off-by: Scott Schreckengaust --- .gitattributes | 1 + .github/workflows/commitlint.yml | 32 +++++++++++++++++ .gitignore | 1 + .projen/files.json | 1 + .projenrc.ts | 2 ++ projenrc/github-workflows.ts | 61 ++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+) create mode 100644 .github/workflows/commitlint.yml diff --git a/.gitattributes b/.gitattributes index f4fcbaa5..2c867fb6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,7 @@ /.github/workflows/auto-approve.yml linguist-generated /.github/workflows/bandit.yml linguist-generated /.github/workflows/build.yml linguist-generated +/.github/workflows/commitlint.yml linguist-generated /.github/workflows/github-merit-badger.yml linguist-generated /.github/workflows/monthly-repo-metrics.yml linguist-generated /.github/workflows/ort-toolkit.yml linguist-generated diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml new file mode 100644 index 00000000..90e9af3f --- /dev/null +++ b/.github/workflows/commitlint.yml @@ -0,0 +1,32 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: commitlint +on: + pull_request: {} + workflow_dispatch: {} + push: {} +jobs: + commitlint: + name: commitlint/ci + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + security-events: write + actions: read + if: (github.actor != 'dependabot[bot]') + steps: + - name: Checkout project + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - name: Setup Node + uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 + with: + node-version: 20.x + - name: Install CommitLint + run: npm install -g @commitlint/config-conventional commitlint + - name: Validate Current Commit + if: github.event_name == 'push' + run: npx commitlint --from HEAD~1 --to HEAD --verbose + - name: Validate PR commits with commitlint + if: github.event_name == 'pull_request' + run: npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose diff --git a/.gitignore b/.gitignore index f0218b86..cb7e6abb 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ tsconfig.json !/.github/workflows/ort-toolkit.yml !/.github/workflows/semgrep.yml !/.github/workflows/bandit.yml +!/.github/workflows/commitlint.yml diff --git a/.projen/files.json b/.projen/files.json index 2a2005fc..6846115a 100644 --- a/.projen/files.json +++ b/.projen/files.json @@ -6,6 +6,7 @@ ".github/workflows/auto-approve.yml", ".github/workflows/bandit.yml", ".github/workflows/build.yml", + ".github/workflows/commitlint.yml", ".github/workflows/github-merit-badger.yml", ".github/workflows/monthly-repo-metrics.yml", ".github/workflows/ort-toolkit.yml", diff --git a/.projenrc.ts b/.projenrc.ts index 84d473bd..e3d2cd44 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -20,6 +20,7 @@ import { buildOrtToolkitWorkflow, runSemGrepWorkflow, runBanditWorkflow, + runCommitLintWorkflow, } from './projenrc/github-workflows'; // Constants @@ -97,6 +98,7 @@ buildAutoApproveWorkflow(project); buildOrtToolkitWorkflow(project); runSemGrepWorkflow(project); runBanditWorkflow(project); +runCommitLintWorkflow(project); // Add specific overrides https://projen.io/github.html#actions-versions project.github?.actions.set('actions/checkout@v3', 'actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744'); diff --git a/projenrc/github-workflows.ts b/projenrc/github-workflows.ts index 1682816a..ce946878 100644 --- a/projenrc/github-workflows.ts +++ b/projenrc/github-workflows.ts @@ -429,3 +429,64 @@ export function runBanditWorkflow(project: AwsCdkConstructLibrary) { } } } + +/** + * https://commitlint.js.org/#/guides-ci-setup + * Runs commitlint on the repository. + * @param project AwsCdkConstructLibrary + */ +export function runCommitLintWorkflow(project: AwsCdkConstructLibrary) { + const commitlint: Job = { + name: 'commitlint/ci', + runsOn: ['ubuntu-latest'], + permissions: { + contents: JobPermission.READ, + pullRequests: JobPermission.READ, + securityEvents: JobPermission.WRITE, + actions: JobPermission.READ, + }, + if: "(github.actor != 'dependabot[bot]')", + + steps: [ + { + name: 'Checkout project', + uses: 'actions/checkout@v3', + }, + { + name: 'Setup Node', + uses: 'actions/setup-node@v3', + with: { + 'node-version': '20.x', + }, + }, + { + name: 'Install CommitLint', + run: 'npm install -g @commitlint/config-conventional commitlint', + }, + { + name: 'Validate Current Commit', + if: "github.event_name == 'push'", + run: 'npx commitlint --from HEAD~1 --to HEAD --verbose', + }, + { + name: 'Validate PR commits with commitlint', + if: "github.event_name == 'pull_request'", + run: 'npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose', + }, + ], + }; + + if (project.github) { + const workflow = project.github.addWorkflow('commitlint'); + if (workflow) { + workflow.on({ + pullRequest: {}, + workflowDispatch: {}, + push: {}, + }); + workflow.addJobs({ + commitlint: commitlint, + }); + } + } +}