diff --git a/.github/workflows/.commitlintrc.cjs b/.github/workflows/.commitlintrc.cjs new file mode 100644 index 0000000000..39fcab6315 --- /dev/null +++ b/.github/workflows/.commitlintrc.cjs @@ -0,0 +1,38 @@ +// Parse UHD's area-prefixed subjects and apply repository message limits. +module.exports = { + parserPreset: { + parserOpts: { + headerPattern: /^(\S(?:.*\S)?): (\S(?:.*\S)?)$/, + headerCorrespondence: ['scope', 'subject'], + }, + }, + plugins: [ + { + rules: { + 'header-ideal-length': (parsed, when, value) => { + const isValid = parsed.header.length <= value; + return [ + when === 'never' ? !isValid : isValid, + `header should be ${value} characters or fewer`, + ]; + }, + 'no-fixup-commit': (parsed) => [ + !/^fixup!\s/i.test(parsed.header) + && !/:\s*fixup!\s/i.test(parsed.header), + 'fixup commits are not allowed', + ], + }, + }, + ], + defaultIgnores: false, + rules: { + 'scope-empty': [2, 'never'], + 'subject-empty': [2, 'never'], + 'header-ideal-length': [1, 'always', 50], + 'header-max-length': [2, 'always', 72], + 'body-empty': [2, 'never'], + 'body-leading-blank': [2, 'always'], + 'body-max-line-length': [2, 'always', 72], + 'no-fixup-commit': [2, 'always'], + }, +}; \ No newline at end of file diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml new file mode 100644 index 0000000000..4aa1ad9adc --- /dev/null +++ b/.github/workflows/commitlint.yml @@ -0,0 +1,56 @@ +name: Commit Message Lint + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: + contents: read + +jobs: + commitlint: + runs-on: ubuntu-latest + steps: + - name: Check out pull request head + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + persist-credentials: false + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 24 + + - name: Lint pull request commits + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -o pipefail + printf '## Commit message lint\n\n' >> "$GITHUB_STEP_SUMMARY" + output_file="$(mktemp)" + trap 'rm -f "$output_file"' EXIT + # Lint only commits introduced by this pull request. + if npx --yes --package @commitlint/cli@19.8.1 -- commitlint \ + --config .github/workflows/.commitlintrc.cjs \ + --from="$BASE_SHA" \ + --to="$HEAD_SHA" 2>&1 | tee "$output_file"; then + if [[ -s "$output_file" ]]; then + printf 'Commit message lint passed with warnings.\n\n' \ + >> "$GITHUB_STEP_SUMMARY" + while IFS= read -r line; do + printf ' %s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + done < "$output_file" + else + printf 'All pull request commit messages passed.\n' \ + >> "$GITHUB_STEP_SUMMARY" + fi + else + printf 'Commit message lint failed.\n\n' >> "$GITHUB_STEP_SUMMARY" + while IFS= read -r line; do + printf ' %s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + done < "$output_file" + exit 1 + fi \ No newline at end of file