-
Notifications
You must be signed in to change notification settings - Fork 4
67 lines (58 loc) · 2.26 KB
/
Copy pathpr-title-checker.yml
File metadata and controls
67 lines (58 loc) · 2.26 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
name: PR Title Checker
on:
pull_request:
types: [opened, edited, synchronize, labeled]
permissions:
contents: read # allow checkout of the repo config by default
pull-requests: read # allow PR metadata reads by default
jobs:
check:
runs-on: ubuntu-latest
name: pr-title-checker
permissions:
contents: read # checkout .github/pr-title-checker-config.json
pull-requests: read # read PR title metadata from the event/CLI
issues: write # create/update the Invalid PR Title label on the PR issue
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # pinned from v6.0.2
with:
persist-credentials: false
- name: Validate PR title
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
set -euo pipefail
config=.github/pr-title-checker-config.json
if [ ! -f "$config" ]; then
echo "::error::PR title checker config is missing at $config."
exit 1
fi
read_config_value() {
local query=$1
local name=$2
local value
if ! value=$(jq -er "$query" "$config"); then
echo "::error::Failed to read $name from $config."
exit 1
fi
if [ -z "$value" ] || [ "$value" = "null" ]; then
echo "::error::$name in $config must be non-empty."
exit 1
fi
printf '%s' "$value"
}
regexp=$(read_config_value '.CHECKS.regexp' 'CHECKS.regexp')
label=$(read_config_value '.LABEL.name' 'LABEL.name')
color=$(read_config_value '.LABEL.color' 'LABEL.color')
failure=$(read_config_value '.MESSAGES.failure' 'MESSAGES.failure')
if [[ "$PR_TITLE" =~ $regexp ]]; then
gh pr edit "$PR_NUMBER" --remove-label "$label" >/dev/null 2>&1 || true
exit 0
fi
gh label create "$label" --color "$color" --force >/dev/null 2>&1 || true
gh pr edit "$PR_NUMBER" --add-label "$label" >/dev/null 2>&1 || true
echo "::error::$failure"
exit 1