-
Notifications
You must be signed in to change notification settings - Fork 7
77 lines (69 loc) · 3.04 KB
/
pr-conventional-title.yml
File metadata and controls
77 lines (69 loc) · 3.04 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
---
name: Pull Request Conventional Title
on:
pull_request:
types:
- opened
- edited
- synchronize
permissions: {}
jobs:
validate-pr-title:
runs-on: ubuntu-latest
permissions:
# We need `pull-requests: write` to be able to post comments on PRs
pull-requests: write
steps:
- uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
with:
disable-sudo-and-containers: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
- uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1
id: pr-title
with:
subjectPattern: ^(?![A-Z]).+$
subjectPatternError: |
The subject "{subject}" found in the pull request title "{title}"
didn't match the configured pattern. Please ensure that the subject
doesn't start with an uppercase character.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add a PR comment with semantic title suggestions
if: always() && steps.pr-title.outputs.error_message != null
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -Eeuo pipefail
MARKER="<!-- pr-title-lint-error -->"
BODY_HEADER="Hey there and thank you for opening this pull request! 👋🏼\n\nWe require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted.\n\n:warning: Details\n\n${{ steps.pr-title.outputs.error_message }}"
FULL_BODY="$MARKER\n\n$BODY_HEADER"
EXISTING_ID=$(gh api repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments --jq \
".[] | select(.body|contains(\"$MARKER\")) | .id" | head -n1 || true)
if [ -n "${EXISTING_ID}" ]; then
echo "Updating existing sticky comment (${EXISTING_ID})"
gh api repos/${GITHUB_REPOSITORY}/issues/comments/${EXISTING_ID} -X PATCH -f body="${FULL_BODY}"
else
echo "Creating new sticky comment"
gh api repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments -f body="${FULL_BODY}"
fi
- name: Remove PR comment
if: steps.pr-title.outputs.error_message == null
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -Eeuo pipefail
MARKER="<!-- pr-title-lint-error -->"
IDS=$(gh api repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments --jq \
".[] | select(.body|contains(\"$MARKER\")) | .id" || true)
if [ -z "${IDS}" ]; then
echo "No sticky comment to remove."
exit 0
fi
for id in $IDS; do
echo "Deleting sticky comment $id"
gh api repos/${GITHUB_REPOSITORY}/issues/comments/${id} -X DELETE
done