-
Notifications
You must be signed in to change notification settings - Fork 292
100 lines (90 loc) · 4.25 KB
/
Copy pathpr-kind-label.yaml
File metadata and controls
100 lines (90 loc) · 4.25 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
name: Apply kind/area labels and milestone from PR body
on:
pull_request_target:
types: [opened, edited, reopened]
branches:
- main
jobs:
kind-labels:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Sync kind labels
env:
GH_TOKEN: ${{ github.token }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
# Extract /kind values from PR body
kinds=$(printf '%s' "$PR_BODY" | tr -d '\r' | sed -n 's|^/kind[[:space:]][[:space:]]*||p' | \
sed 's|[[:space:]]*$||' | sed '/^$/d' | sort -u)
# Validate every requested label before mutating. Removing first and
# failing to add a mistyped label would leave the PR with no kind/*.
if [ -n "$kinds" ]; then
existing=$(gh label list --repo "$REPO" --limit 200 --json name -q '.[].name')
unknown=$(printf '%s\n' "$kinds" | while IFS= read -r kind; do
printf '%s\n' "$existing" | grep -qxF "kind/$kind" || printf "kind/%s " "$kind"
done)
if [ -n "$unknown" ]; then
echo "::warning::Unknown label(s) ${unknown% }, skipping kind sync"
exit 0
fi
fi
# Remove existing kind/* labels
gh pr view "$PR_NUMBER" --repo "$REPO" --json labels \
-q '.labels[].name | select(startswith("kind/"))' | \
while IFS= read -r label; do
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "$label" || echo "::warning::Failed to remove label '$label'"
done
printf '%s\n' "$kinds" | \
while IFS= read -r kind; do
[ -z "$kind" ] && continue
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "kind/$kind" || \
echo "::warning::Label 'kind/$kind' not found in repository"
done
- name: Set area labels
env:
GH_TOKEN: ${{ github.token }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
# Absence of a directive is a no-op. Area labels are also set by the
# /area comment command and clearing here would wipe those.
areas=$(printf '%s' "$PR_BODY" | tr -d '\r' | sed -n 's|^/area[[:space:]][[:space:]]*||p' | \
sed 's|[[:space:]]*$||' | sed '/^$/d' | sort -u)
[ -z "$areas" ] && exit 0
existing=$(gh label list --repo "$REPO" --limit 200 --json name -q '.[].name')
unknown=$(printf '%s\n' "$areas" | while IFS= read -r area; do
printf '%s\n' "$existing" | grep -qxF "area/$area" || printf "area/%s " "$area"
done)
if [ -n "$unknown" ]; then
echo "::warning::Unknown label(s) ${unknown% }, skipping area sync"
exit 0
fi
# Remove existing area/* labels
gh pr view "$PR_NUMBER" --repo "$REPO" --json labels \
-q '.labels[].name | select(startswith("area/"))' | \
while IFS= read -r label; do
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "$label" || echo "::warning::Failed to remove label '$label'"
done
printf '%s\n' "$areas" | \
while IFS= read -r area; do
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "area/$area" || \
echo "::warning::Label 'area/$area' not found in repository"
done
- name: Set milestone
env:
GH_TOKEN: ${{ github.token }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
# Use the last /milestone directive in the PR body
milestone=$(printf '%s' "$PR_BODY" | tr -d '\r' | sed -n 's|^/milestone[[:space:]][[:space:]]*||p' | \
sed 's|[[:space:]]*$||' | sed '/^$/d' | tail -n 1)
[ -z "$milestone" ] && exit 0
gh pr edit "$PR_NUMBER" --repo "$REPO" --milestone "$milestone" || \
echo "::warning::Milestone '$milestone' not found in repository"