-
Notifications
You must be signed in to change notification settings - Fork 21
211 lines (202 loc) · 8.18 KB
/
pr-tidying.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
name: Pull Request Tidying
on:
pull_request_target:
types:
- opened
- reopened
- ready_for_review
- unassigned
- edited
- unlocked
permissions:
contents: read
pull-requests: write
jobs:
default-assignee:
# Disabled for now
if: false
name: Assign contributor
continue-on-error: true
permissions:
pull-requests: write
runs-on: ubuntu-latest
outputs:
check-passed: ${{ steps.check.outputs.result == 'passed' }}
fixed: ${{ steps.fix.outputs.result == 'fixed' }}
env:
DEFAULT_ASSIGNEE: ${{ github.actor }}
steps:
- uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
disable-sudo: true
egress-policy: audit
- name: Check if PR already has assignee
id: check
run: |
if [[ -n $CURRENT_ASSIGNEE ]]; then
echo "result=passed" >> $GITHUB_OUTPUT
fi
env:
CURRENT_ASSIGNEE: ${{ github.event.pull_request.assignee.login || '' }}
- id: skip-edited-event
if: github.event.action == 'edited'
run: |
echo "Fix will be skipped because the triggering action is an edit event"
exit 1
- id: prevent-reassignment
if: github.event.action == 'unassigned' && env.DEFAULT_ASSIGNEE == github.event.assignee.login
run: |
echo "Fix will be skipped to avoid reassigning the unassigned user"
exit 1
- name: Add assignee
id: fix
if: steps.check.outputs.result != 'passed'
run: |
level=$(gh api /repos/$REPO/collaborators/$AUTHOR/permission --jq .permission)
if [[ $level = "write" || $level = "admin" ]]; then
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-assignee "$AUTHOR"
echo "result=fixed" >> $GITHUB_OUTPUT
fi
env:
REPO: ${{ github.repository }}
AUTHOR: ${{ env.DEFAULT_ASSIGNEE }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
issue-in-title:
# Disabled for now
if: false
name: Add issue references to title
continue-on-error: true
runs-on: ubuntu-latest
outputs:
check-passed: ${{ steps.check.outputs.result == 'passed' }}
fixed: ${{ steps.fix.outputs.result == 'fixed' }}
steps:
- uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
disable-sudo: true
egress-policy: audit
- name: Check the current PR title
id: check
run: |
regex='\s\([#][0-9]+\s?\)$'
if [[ $PR_TITLE =~ $regex ]]; then
echo "result=passed" >> $GITHUB_OUTPUT
fi
- id: skip-unassigned-event
if: github.event.action == 'unassigned'
run: |
echo "Fix will be skipped because the triggering action is an unassignment event"
exit 1
- id: skip-non-title-edits
if: github.event.action == 'edited' && github.event.changes.title.from == github.event.pull_request.title
run: |
echo "Fix will be skipped because the triggering edit action did not modify the PR title"
exit 1
- name: Parse issue(s) from Ticket heading
id: parse
if: steps.check.outputs.result != 'passed'
run: |
regex='^[#][#][#] Ticket (([#][0-9]+\s?)+)'
line=$(gh pr view "$PR_NUMBER" --json body --jq .body | head -n 1)
issues=''
if [[ $line =~ $regex ]]; then
issues="${BASH_REMATCH[1]}"
fi
echo "issue-numbers=$(tr -s '[:blank:]' <<< $issues)" >> $GITHUB_OUTPUT
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- id: skip-empty-issues
if: steps.parse.outputs.issue-numbers == ''
run: |
echo "Fix will be skipped because no issue numbers were parsed"
exit 1
- name: Format the new title
id: new-title
run: echo result="$ORIGINAL_TITLE (issue $ISSUE_NUMBERS)" >> $GITHUB_OUTPUT
env:
ORIGINAL_TITLE: ${{ github.event.pull_request.title }}
ISSUE_NUMBERS: ${{ steps.parse.outputs.issue-numbers }}
- id: skip-prevent-revert
if: github.event.action == 'edited' && github.event.changes.title.from == steps.new-title.outputs.result
run: |
echo "Fix will be skipped to avoid setting the title back to its previous value"
exit 1
- name: Update PR title
id: fix
if: steps.check.outputs.result != 'passed' && steps.parse.outputs.issue-numbers != ''
run: |
gh pr edit "$PR_NUMBER" --title "$ORIGINAL_TITLE (issue $ISSUE_NUMBERS)"
echo "result=fixed" >> $GITHUB_OUTPUT
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
ORIGINAL_TITLE: ${{ github.event.pull_request.title }}
ISSUE_NUMBERS: ${{ steps.parse.outputs.issue-numbers }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
comment:
name: Create/update PR comment
# Disabled for now
if: always() && github.actor != 'dependabot[bot]' && false
needs:
- default-assignee
- issue-in-title
runs-on: ubuntu-latest
steps:
- uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
disable-sudo: true
egress-policy: audit
- name: Create comment file
run: |
COMMENT_FILE=$(mktemp -t comment.md.XXXXX)
echo "COMMENT_FILE=$COMMENT_FILE" >> $GITHUB_ENV
cat >> $COMMENT_FILE << 'ENDOFCOMMENT'
## PR Helper Bot 🤖
**Thanks for your pull request!**
My job is to check for any missing conventions and automatically tidy things up (if I can).
You'll find the results below, along with any suggested actions for you to take.
<details>
<summary>Result key</summary>
✅ = Already passing
🛠️ = Fixed (now passing)
⚠️ = Consider fixing
</details>
| Convention | Result |
|:-------------------------------------|:------:|
| PR has assignee (defaults to author) | ${{ env.DEFAULT_ASSIGNEE }} |
| PR title ends with `(#issue)` | ${{ env.ISSUE_IN_TITLE }} |
*Pusher: @${{ env.GH_ACTOR }}, Action: `${{ env.GH_ACTION }}`, Workflow: [`${{ env.GH_WORKFLOW }}`](${{ env.GH_SERVER}}/${{ env.GH_REPO }}/actions/runs/${{ env.GH_RUN_ID }})*
ENDOFCOMMENT
env:
DEFAULT_ASSIGNEE: ${{ (needs.default-assignee.outputs.check-passed && '✅' ) || (needs.default-assignee.outputs.fixed && '🛠️') || '⚠️' }}
ISSUE_IN_TITLE: ${{ (needs.issue-in-title.outputs.check-passed && '✅' ) || (needs.issue-in-title.outputs.fixed && '🛠️') || '⚠️' }}
GH_ACTOR: ${{ github.actor }}
GH_ACTION: ${{ github.event_name }}
GH_WORKFLOW: ${{ github.workflow }}
GH_SERVER: ${{ github.server_url }}
GH_REPO: ${{ github.repository }}
GH_RUN_ID: ${{ github.run_id }}
- name: Output comment body to step summary
run: cat $COMMENT_FILE >> $GITHUB_STEP_SUMMARY
- name: Write the comment body
id: comment-body
run: |
CONTENT=$(cat $COMMENT_FILE)
echo "COMMENT_CONTENT<<ENDOFREPORT" >> $GITHUB_OUTPUT
echo "$CONTENT" >> $GITHUB_OUTPUT
echo "ENDOFREPORT" >> $GITHUB_OUTPUT
- name: Find previous report comment
id: find-comment
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'PR Helper Bot 🤖'
- name: Create or update comment
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: ${{ steps.comment-body.outputs.COMMENT_CONTENT }}
edit-mode: replace