-
Notifications
You must be signed in to change notification settings - Fork 0
143 lines (131 loc) · 5.99 KB
/
Copy pathcommitlint.yml
File metadata and controls
143 lines (131 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
name: Commit Lint
on: # yamllint disable-line rule:truthy
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop
permissions:
contents: read
jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Lint commit messages (push)
if: github.event_name == 'push'
uses: wagoid/commitlint-github-action@b948419dd99f3fd78a6548d48f94e3df7f6bf3ed # v6.2.1
with:
configFile: commitlint.config.cjs
# v6 lints the push range (before..after) automatically. Do NOT pass
# legacy v5 `from`/`to` inputs - they were removed in v6 and now
# produce an "Unexpected input(s)" warning. See ADR-008.
- name: Lint commit messages (PR)
if: github.event_name == 'pull_request'
uses: wagoid/commitlint-github-action@b948419dd99f3fd78a6548d48f94e3df7f6bf3ed # v6.2.1
with:
configFile: commitlint.config.cjs
# v6 lints the PR range (base..head) automatically.
lint-pr-title:
name: Lint PR title (squash-merge subject)
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
contents: read
pull-requests: write
steps:
- name: Normalize dependabot PR metadata
if: github.actor == 'dependabot[bot]' && github.event_name == 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
# Squash-merge uses the PR title+body as the commit message; commitlint
# lints that on push to main. Dependabot's auto title has a doubled
# scope (prefix "build(deps)" + include "scope") and its body is ~65KB
# of release notes, both of which break commitlint. Normalize.
# NOTE: body is a fixed multi-line literal (a real blank line separates
# the two paragraphs); a literal "\n" would be stored verbatim by the
# API and still produce an over-length line on squash merge.
NORMALIZED_TITLE=$(printf '%s' "$PR_TITLE" | sed -E 's/^([a-z]+)\(([a-z]+)\)\(([a-z-]+)\):/\1(\2):/')
NORMALIZED_BODY="Automated dependency update from dependabot.
Bumps a grouped set of packages (see commit history for the full list of version changes)."
gh pr edit "$PR_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--title "$NORMALIZED_TITLE" \
--body "$NORMALIZED_BODY"
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '20'
- name: Install commitlint
run: |
# The wagoid action's Docker image bundles commitlint + the
# config-conventional preset; here we install them via npm so
# the same config (commitlint.config.cjs) is available.
npm install --no-save --no-audit --no-fund \
@commitlint/cli@19 \
@commitlint/config-conventional@19
- name: Lint PR title against commitlint
if: github.actor != 'dependabot[bot]'
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
# Defends against bots (Jules, Dependabot, Copilot) that bypass
# local commit-msg hooks. The PR title is the squash-merge subject
# for the merged commit; it MUST be a valid conventional commit.
# See ADR-008 (PR #505 run 27086771117).
# Fetch the live title from the API, NOT github.event.pull_request.title:
# reruns replay the original event payload, so an edited PR title
# would be checked against a stale value and keep failing forever.
PR_TITLE="$(gh pr view "$PR_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--json title --jq '.title')"
printf '%s\n' "$PR_TITLE" > pr-title.txt
if ! npx commitlint \
--config commitlint.config.cjs \
--edit pr-title.txt; then
echo "::error::PR title is not a valid conventional commit."
echo "::error::Required format: type(scope): subject (max 150 chars)"
echo "::error::Got: $PR_TITLE"
exit 1
fi
echo "PR title is a valid conventional commit."
- name: Enforce PR body length for squash merge compatibility
if: github.actor != 'dependabot[bot]'
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
# On squash merge, GitHub concatenates PR title + body as the
# commit message. Enforce body limits here since commitlint
# body-max-length is disabled (squash merges produce long bodies
# from PR descriptions by design).
# Fetch the live body from the API (same reconsideration as the
# title lint above): reruns replay the stale event payload.
PR_BODY="$(gh pr view "$PR_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--json body --jq '.body // ""')"
BODY_LEN=${#PR_BODY}
if [ "$BODY_LEN" -gt 2000 ]; then
echo "::error::PR body is ${BODY_LEN} chars (max 2000)."
echo "::error::On squash merge, long bodies become commit messages."
echo "::error::Please shorten the PR description."
exit 1
fi
if printf '%s' "$PR_BODY" | grep -qE '.{101}'; then
echo "::warning::PR body has lines exceeding 100 characters."
echo "::warning::This may trigger commitlint body-max-line-length on squash merge."
fi
echo "PR body length check passed (${BODY_LEN} chars)."