-
Notifications
You must be signed in to change notification settings - Fork 0
79 lines (72 loc) · 3 KB
/
Copy pathcommitlint.yml
File metadata and controls
79 lines (72 loc) · 3 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
name: Commitlint
on:
pull_request:
push:
branches:
- main
permissions:
contents: read
jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Validate commit messages
shell: bash
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
from="${{ github.event.pull_request.base.sha }}"
to="${{ github.event.pull_request.head.sha }}"
else
# Push to main (typically maintainer-only). github.event.before is the pre-push
# tip and github.event.after is the new tip. After a force push that rewrites
# history (e.g. amend/rebase to fix commit messages), before is often not an
# ancestor of after, so commitlint's --from/--to range is invalid and git
# errors with "Invalid revision range". Fetch the pre-push tip when possible,
# then lint from merge-base(before, after)..after so rewritten commits are
# still checked.
from="${{ github.event.before }}"
to="${{ github.event.after }}"
if [ "$from" = "0000000000000000000000000000000000000000" ]; then
from="origin/main"
else
# before may no longer be on main after a force push, but GitHub often
# still has the object. Fetch with full history (not --depth=1): a shallow
# fetch leaves before disconnected and merge-base fails; with set -e that
# aborts the step before the origin/main fallback runs.
if ! git cat-file -e "$from^{commit}" 2>/dev/null; then
git fetch origin "$from" 2>/dev/null || true
fi
if git cat-file -e "$from^{commit}" 2>/dev/null \
&& ! git merge-base --is-ancestor "$from" "$to" 2>/dev/null; then
fork_point="$(git merge-base "$from" "$to" 2>/dev/null || true)"
if [ -n "$fork_point" ] && git cat-file -e "$fork_point^{commit}" 2>/dev/null; then
from="$fork_point"
else
git fetch origin "$from" 2>/dev/null || true
fork_point="$(git merge-base "$from" "$to" 2>/dev/null || true)"
if [ -n "$fork_point" ] && git cat-file -e "$fork_point^{commit}" 2>/dev/null; then
from="$fork_point"
else
from="origin/main"
fi
fi
fi
if ! git cat-file -e "$from^{commit}" 2>/dev/null; then
from="origin/main"
fi
fi
fi
pnpm exec commitlint --from "$from" --to "$to"