Skip to content

Commit 4a789b6

Browse files
authored
[Infra] Add PR traceability marker check (#29)
Signed-off-by: Douglas Chiang <[email protected]>
1 parent 9a04bc5 commit 4a789b6

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: PR Validation
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
- edited
9+
10+
permissions:
11+
contents: read
12+
pull-requests: write
13+
14+
concurrency:
15+
group: pr-validation-${{ github.event.pull_request.number }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
validate_title:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checking the presence of the traceability tag in the PR title
23+
env:
24+
PR_TITLE: ${{ github.event.pull_request.title }}
25+
PR_NUMBER: ${{ github.event.pull_request.number }}
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
REPO: ${{ github.repository }}
28+
run: |
29+
set -euo pipefail
30+
31+
# Trim leading/trailing whitespace from the PR title and update if necessary
32+
TRIMMED_TITLE=$(printf '%s' "$PR_TITLE" | awk '{$1=$1; print}')
33+
if [ "$TRIMMED_TITLE" != "$PR_TITLE" ]; then
34+
echo "Updating PR title to: '$TRIMMED_TITLE'"
35+
curl -sS -X PATCH \
36+
-H "Authorization: Bearer $GITHUB_TOKEN" \
37+
-H "Accept: application/vnd.github+json" \
38+
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER" \
39+
-d "$(jq -n --arg title "$TRIMMED_TITLE" '{title:$title}')"
40+
else
41+
echo "PR title already normalized."
42+
fi
43+
44+
error_msg() {
45+
title=$(printf '%s' "$TRIMMED_TITLE" | sed -E \
46+
-e 's/\[[^]]*\]?[[:space:]]*//g' \
47+
-e 's/(^|[[:space:]])[^[:space:]]*\][[:space:]]*/\1/g' \
48+
-e 's/\][[:space:]]*//g' \
49+
-e 's/[[:space:]]+/ /g' \
50+
-e 's/^[[:space:]]+//; s/[[:space:]]+$//')
51+
echo "No recognized traceability tag / there are multiple tags"
52+
echo "Please start the PR title with one of the following tags: [New], [Breaking], [Infra], [BugFix]"
53+
echo "e.g. [New] $title"
54+
}
55+
56+
if echo "$TRIMMED_TITLE" | grep -Eq '^\[(New|Breaking|Infra|BugFix)\]'; then
57+
count=$(grep -o '\[[^]]\+\]' <<< "$TRIMMED_TITLE" | wc -l)
58+
if [ "$count" -eq 1 ]; then
59+
echo "Traceability tag found."
60+
exit 0
61+
else
62+
error_msg
63+
exit 1
64+
fi
65+
else
66+
error_msg
67+
exit 1
68+
fi

0 commit comments

Comments
 (0)