mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
tools: lint PR commit messages without approval
Use pull_request_target to run commit message linting without fork workflow approval. Fetch the first commit through the API and pass its message to a pinned validator as JSON on stdin. Capture validator output and report failures through escaped annotations, using only read access to pull requests. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65875 Reviewed-By: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
0d14c0e507
commit
e5bbcfdd78
2 files changed
+33
-29
No files matched your search
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"problemMatcher": [
|
||||
{
|
||||
"owner": "core-validate-commit",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^not ok \\d+ (.*)$",
|
||||
"message": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
name: First commit message adheres to guidelines
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
pull_request_target:
|
||||
branches:
|
||||
- main
|
||||
|
||||
@@ -9,27 +9,44 @@ env:
|
||||
NODE_VERSION: lts/*
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
lint-commit-message:
|
||||
name: lint-commit-message
|
||||
runs-on: ubuntu-slim
|
||||
steps:
|
||||
- name: Compute number of commits in the PR
|
||||
id: nb-of-commits
|
||||
run: |
|
||||
echo "plusOne=$((${{ github.event.pull_request.commits }} + 1))" >> $GITHUB_OUTPUT
|
||||
echo "minusOne=$((${{ github.event.pull_request.commits }} - 1))" >> $GITHUB_OUTPUT
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
fetch-depth: ${{ steps.nb-of-commits.outputs.plusOne }}
|
||||
persist-credentials: false
|
||||
- run: git reset HEAD^2
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
- name: Validate commit message
|
||||
run: |
|
||||
echo "::add-matcher::.github/workflows/commit-lint-problem-matcher.json"
|
||||
git rev-parse HEAD~${{ steps.nb-of-commits.outputs.minusOne }} | xargs npx -q core-validate-commit --no-validate-metadata --tap
|
||||
- name: Validate first commit message
|
||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||
with:
|
||||
script: |
|
||||
const { data: [commit] } = await github.rest.pulls.listCommits({
|
||||
...context.repo,
|
||||
pull_number: context.issue.number,
|
||||
per_page: 1,
|
||||
});
|
||||
if (!commit) {
|
||||
throw new Error('No commits found in pull request');
|
||||
}
|
||||
const { exitCode, stdout, stderr } = await exec.getExecOutput('npx', [
|
||||
'-q', '--yes', '--ignore-scripts', 'core-validate-commit@6.0.0',
|
||||
'--no-validate-metadata', '--tap', '-',
|
||||
], {
|
||||
cwd: process.env.RUNNER_TEMP,
|
||||
input: Buffer.from(JSON.stringify([{ id: commit.sha, message: commit.commit.message }])),
|
||||
silent: true,
|
||||
ignoreReturnCode: true,
|
||||
});
|
||||
if (exitCode !== 0) {
|
||||
core.setFailed(stdout + stderr || 'Commit message validation failed');
|
||||
} else {
|
||||
core.info('First commit message passes validation');
|
||||
}
|
||||
Reference in new issue
Block a user