-
Notifications
You must be signed in to change notification settings - Fork 10
134 lines (110 loc) · 4.59 KB
/
scan-pull-request.yml
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
name: Scan Pull Request
on:
pull_request:
types:
- edited
- opened
- reopened
- synchronize
workflow_dispatch:
permissions:
contents: read
pull-requests: write
repository-projects: read
jobs:
lint-pr-title:
name: 🏷️ Lint PR Title
runs-on: ubuntu-latest
steps:
- name: Lint title
uses: amannn/action-semantic-pull-request@v5
id: lint-title
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Create error comment
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ always() && steps.lint-title.outputs.error_message != null }}
with:
header: lint-title-error-comment
message: |
We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like this pull request's title needs to be adjusted.
Details:
```
${{ steps.lint-title.outputs.error_message }}
```
- name: Delete error comment
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ steps.lint-title.outputs.error_message == null }}
with:
header: lint-title-error-comment
delete: true
set-labels:
name: 🏷️ Set Labels
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set labels
uses: actions/labeler@v5
with:
configuration-path: ./.github/config/labeler.yml
sync-labels: true
- name: Assign Conventional Commit label
shell: bash
env:
PR_CURRENT_LABELS_JSON: ${{ toJson(github.event.pull_request.labels) }}
PR_TITLE: ${{ github.event.pull_request.title }}
GITHUB_TOKEN: ${{ github.token }}
run: |
# Create a mapping between Conventional Commit prefixes and our labels:
label_map='{
"build": "type: build",
"chore": "type: chore",
"ci": "type: ci",
"docs": "type: docs",
"feat": "type: feature",
"fix": "type: bugfix",
"perf": "type: performance",
"refactor": "type: refactor",
"revert": "type: reversion",
"style": "type: style",
"test": "type: test"
}'
# Strip any surrounding whitespace from the sanitized PR title:
pr_title="$(echo "$PR_TITLE" | tr -d '\n' | xargs)"
# Parse the existing labels:
pr_current_labels=$(echo "$PR_CURRENT_LABELS_JSON" | jq '.[].name')
# Determine the Conventional Commit type based upon the PR title:
commit_type="$(echo "$pr_title" | cut -d: -f1 | sed 's/(.*)//g; s/!//g')"
echo "Detected Conventional Commit type: '$commit_type'"
if [[ -z "$commit_type" ]]; then
echo "Commit type could not be extracted from PR title: '$pr_title'"
exit 1
fi
# Pull the appropriate label based on the detected Conventional Commit type:
label_to_apply="$(echo "$label_map" | jq -r --arg type "$commit_type" '.[$type] // empty')"
if [[ -z "$label_to_apply" ]]; then
echo "Unrecognized Conventional Commit type: '$commit_type'"
exit 1
fi
echo "Mapping Conventional Commit type '$commit_type' to label: '$label_to_apply'"
# Determine whether any outdated Conventional Commit labels need to be
# removed:
labels_to_remove_csv=$(echo "$PR_CURRENT_LABELS_JSON" | jq -r --argjson label_map "$label_map" --arg current_label "$label_to_apply" '.[].name | select(. != $current_label and (. as $existing | $label_map | any(.[]; . == $existing)))' | paste -sd, -)
echo "Removing incorrect Conventional Commit labels: '$labels_to_remove_csv'"
# If the label to add is already applied, skip it:
labels_to_add_csv=""
if echo "$pr_current_labels" | grep -qw "$label_to_apply"; then
echo "Label already exists on the PR: '$label_to_apply'"
else
echo "Label should be added to the PR: '$label_to_apply'"
labels_to_add_csv+="$label_to_apply"
fi
# Apply the label changes:
if [[ -n "$labels_to_remove_csv" || -n "$labels_to_add_csv" ]]; then
gh pr edit \
"${{ github.event.pull_request.number }}" \
${labels_to_add_csv:+--add-label "$labels_to_add_csv"} \
${labels_to_remove_csv:+--remove-label "$labels_to_remove_csv"}
else
echo "No label changes needed"
fi