-
Notifications
You must be signed in to change notification settings - Fork 3
382 lines (342 loc) · 15.5 KB
/
parsegithubissue-pr.yaml
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
name: Auto Parse github form - pull request
run-name: ${{ github.actor }} - PR:${{ github.event.pull_request.number }}
on:
pull_request:
env:
GH_TOKEN: ${{ secrets.PLATFORM_USER_TOKEN }}
SLACK_WEBHOOK: ${{ secrets.TEST_CHANNEL_SLACK_WEBHOOK }}
APPROVAL_STATE: 'Pending Approval'
APPROVAL_COMMENT: 'Pending Approval'
SLACK_TOKEN: ${{ secrets.AUTO_SHUTDOWN_OAUTH_TOKEN }}
permissions:
id-token: write
jobs:
process-request:
permissions: write-all
runs-on: ubuntu-latest
if: github.event.pull_request.user.login != 'renovate[bot]' && github.event.pull_request.user.login != 'hmcts-platform-operations'
steps:
#Allows workflow to access repo
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '0'
token: ${{ env.GH_TOKEN }}
ref: ${{ github.head_ref }}
- name: Get the date
run: |
START_DATE=$(date +%d-%m-%Y -d "+30 days")
echo "START_DATE=$START_DATE" >> $GITHUB_ENV
END_DATE=$(date +%d-%m-%Y -d "+31 days")
echo "END_DATE=$END_DATE" >> $GITHUB_ENV
echo "START_DATE is $START_DATE"
- uses: JasonEtco/create-an-issue@v2
id: create-issue
env:
GITHUB_TOKEN: ${{ env.GH_TOKEN }}
TITLE: 'PR-${{ github.event.pull_request.number }}'
JUSTIFICATION: 'Development'
PULL_REQUEST: 'PR-${{ github.event.pull_request.number }}'
BUSINESS_AREA: 'CFT'
TEAM_NAME: 'PlatOps'
ENVIRONMENT: 'AAT / Staging'
START_DATE: '${{ env.START_DATE }}'
END_DATE: '${{ env.END_DATE }}'
POST_11PM: 'No'
with:
filename: .github/ISSUE_TEMPLATE/1-pull-request-template.md
update_existing: true
search_existing: all
#Adds label for user feedback
- name: add processing label
uses: actions-ecosystem/action-add-labels@v1
with:
labels: 'Processing'
number: ${{ steps.create-issue.outputs.number }}
#Comment if approver is equal to requester
- name: Cannot self approve comment
if: contains(github.event.issue.labels.*.name, 'approved') && github.event.issue.user.login == github.actor || contains(github.event.issue.labels.*.name, 'auto-approved') && github.event.issue.user.login == github.actor
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
Requester cannot also be approver - please seek a second approver.
#Remove any approval labels if approver is equal to requester
- name: Remove approved label
if: contains(github.event.issue.labels.*.name, 'approved') && github.event.issue.user.login == github.actor || contains(github.event.issue.labels.*.name, 'auto-approved')
uses: actions-ecosystem/action-remove-labels@v1
with:
labels: |
approved
auto-approved
number: ${{ steps.create-issue.outputs.number }}
#End workflow if user is equal to requester
- name: exit flow if approver == requester
if: contains(github.event.issue.labels.*.name, 'approved') && github.event.issue.user.login == github.actor || contains(github.event.issue.labels.*.name, 'auto-approved') && github.event.issue.user.login == github.actor
run: exit 1
#if denied label is applied, set approval environment variables appropriately
- name: set denied status
if: contains(github.event.issue.labels.*.name, 'denied')
run: |
echo "APPROVAL_STATE=Denied" >> $GITHUB_ENV
echo "APPROVAL_COMMENT=Denied by ${{ github.actor }}" >> $GITHUB_ENV
#if approval state == denied, close issue
- name: Close Issue with denied
if: env.APPROVAL_STATE == 'Denied'
uses: peter-evans/close-issue@v3
with:
issue-number: ${{ steps.create-issue.outputs.number }}
comment: |
Your request has been denied by ${{ github.actor }}, most likely due to associated costs being too high.
Closing issue.
#if approval state == denied, remove processing and pending approval labels
- name: Remove labels
uses: actions-ecosystem/action-remove-labels@v1
if: env.APPROVAL_STATE == 'Denied'
with:
labels: |
Processing
Pending approval
number: ${{ steps.create-issue.outputs.number }}
#Remove labels from a previous run if present
- name: Remove labels
if: contains(github.event.issue.labels.*.name, 'Cost Error') || contains(github.event.issue.labels.*.name, 'Pending Approval') || contains(github.event.issue.labels.*.name, 'Success') || contains(github.event.issue.labels.*.name, 'Very high cost')
uses: actions-ecosystem/action-remove-labels@v1
with:
labels: |
Cost Error
Pending approval
Success
Very high cost
#Get issue body
- name: Get issue body
id: get_issue_body
run: |
gh issue view --repo ${{ github.repository }} ${{ steps.create-issue.outputs.number }} --json body | jq -r '.body' > body.txt
sed -i "s/'//g" body.txt
cat body.txt
#Create a multiline variable so it can be read by parser
- name: multiline github var
run: |
echo 'ISSUE_BODY<<EOF' >> $GITHUB_ENV
while read LINE; do
echo "$LINE" >> $GITHUB_ENV;
done < body.txt
echo 'EOF' >> $GITHUB_ENV
rm body.txt
#Parse issue form that trigger workflow
- name: Read submitted issue
id: parse_issue
uses: stefanbuck/github-issue-parser@v3.1.1
with:
issue-body: ${{ env.ISSUE_BODY }}
template-path: .github/ISSUE_TEMPLATE/3-skip-auto-shutdown-request.yaml
#Save parsed date into environment variables
- name: Save parsed jsonString data
run: |
echo '${{ steps.parse_issue.outputs.jsonString }}' > NEW_DATA.json
echo '${{ steps.parse_issue.outputs.jsonString }}'
echo NEW_DATA=$(cat NEW_DATA.json | jq -c | jq '.form_environment |= split(",")') >> $GITHUB_ENV
rm NEW_DATA.json
echo GITHUB_REPO='${{ github.repository }}' >> $GITHUB_ENV
echo ISSUE_NUMBER='${{ steps.create-issue.outputs.number }}' >> $GITHUB_ENV
#Setup Python
- uses: actions/setup-python@v5
with:
python-version: '3.11'
# Install Python requirements
- name: Install Python Requirements
run: |
pip3 install -r requirements.txt
# Run custom python script to handle form data / apply form verification
- name: process form data
continue-on-error: true
run: |
python ./scripts/file_handling.py
# Remove label if present
- name: Remove error labels
uses: actions-ecosystem/action-remove-labels@v1
if: env.PROCESS_SUCCESS == 'true' && contains(github.event.issue.labels.*.name, 'Error in form')
with:
labels: |
Error in form
number: ${{ steps.create-issue.outputs.number }}
# Add form error label if process_success not equal to true
- name: Add error label
if: env.PROCESS_SUCCESS != 'true'
uses: actions-ecosystem/action-add-labels@v1
with:
labels: 'Error in form'
number: ${{ steps.create-issue.outputs.number }}
# Provide any error messages to user as a comment
- name: Create Issue Comment
if: env.PROCESS_SUCCESS != 'true'
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
${{ env.ISSUE_COMMENT }}
Please edit this issue and select "Update comment" to resubmit.
#End workflow if for processing finds an error
- name: exit flow if an error occurred
if: env.PROCESS_SUCCESS != 'true'
run: exit 1
# Login to Azure CLI (for cost analysis)
- name: 'Az CLI login'
uses: azure/login@v2
with:
client-id: 2b6fa9d7-7dba-4600-a58a-5e25554997aa # DTS AKS Auto-Shutdown
tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET
allow-no-subscriptions: true
#Run bash script to get Azure resouce details for cost analysis
- name: get aks resource details
if: env.PROCESS_SUCCESS == 'true'
continue-on-error: true
run: ./scripts/aks/resource-details.sh
#Run cost calculator python script
- name: process costs
if: env.PROCESS_SUCCESS == 'true'
continue-on-error: true
run: |
python ./scripts/cost-calculator.py || echo "Calculating cost failed"
#Send slack message if approval_state == denied
- name: Send denied slack message
if: env.APPROVAL_STATE == 'Denied'
run: ./scripts/aks/send-slack-message.sh
env:
SLACK_WEBHOOK: ${{ env.SLACK_WEBHOOK }}
ISSUE_TITLE: 'PR-${{ github.event.pull_request.number }}'
CHANGE_JIRA_ID: ${{ env.CHANGE_JIRA_ID }}
REQUEST_URL: ${{ env.REQUEST_URL }}
BUSINESS_AREA_ENTRY: ${{ env.BUSINESS_AREA_ENTRY }}
START_DATE: ${{ env.START_DATE }}
END_DATE: ${{ env.END_DATE }}
COST_DETAILS_FORMATTED: ${{ env.COST_DETAILS_FORMATTED }}
ENVIRONMENT: ${{ env.ENVIRONMENT }}
JUSTIFICATION: ${{ env.JUSTIFICATION }}
REQUESTER: ${{ github.event.pull_request.user.login }}
APPROVAL_COMMENT: ${{ env.APPROVAL_COMMENT }}
REVIEWER: ${{ github.actor }}
#End workflow if approval state == denied
- name: exit flow if denied
if: env.APPROVAL_STATE == 'Denied'
run: exit 1
#Add output of cost calculator as a comment to user
- name: Add cost details as a comment
if: env.PROCESS_SUCCESS == 'true' && env.ERROR_IN_COSTS != 'true'
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
Total estimated cost of skipping shutdown for the provided dates is **£${{ env.COST_DETAILS_FORMATTED }}**
#If cost calculator fails, provide feedback to user as a comment
- name: Add cost error details as a comment
if: env.PROCESS_SUCCESS == 'true' && env.ERROR_IN_COSTS == 'true'
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
We have been unable to estimate the cost of this skip request.
#If cost calculator finds an error, add cost error label
- name: Add costs error label
if: env.ERROR_IN_COSTS == 'true'
uses: actions-ecosystem/action-add-labels@v1
with:
labels: 'Cost Error'
number: ${{ steps.create-issue.outputs.number }}
#Set approval_state environment var to auto-approved if cost value is less than £5,000
- name: set approved status
if: env.COST_DETAILS < 5000
run: |
echo "APPROVAL_STATE=auto-approved" >> $GITHUB_ENV
echo "APPROVAL_COMMENT=Auto approved" >> $GITHUB_ENV
#Add auto approved label if approval_state == auto-approved
- name: Add auto-approved label
if: env.APPROVAL_STATE == 'auto-approved'
uses: actions-ecosystem/action-add-labels@v1
with:
labels: 'auto-approved'
number: ${{ steps.create-issue.outputs.number }}
#Add auto-approval comment if approval_state == auto-approved
- name: Add auto approval comment
if: env.APPROVAL_STATE == 'auto-approved'
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
You request has been automatically approved.
#If approval_state == Pending Approval add appropriate labels
- name: Add very high cost label
if: env.APPROVAL_STATE == 'Pending Approval'
uses: actions-ecosystem/action-add-labels@v1
with:
labels: |
Pending approval
Very high cost
number: ${{ steps.create-issue.outputs.number }}
#If approval_state == Pending Approval && workflow is running at submission time, not approval time.
- name: Very High Cost approval comment
if: env.APPROVAL_STATE == 'Pending Approval' && github.event.issue.user.login == github.actor
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
Due to the level of cost associated with this request, an approval will be required from any additional person. Note, you cannot approve your own request.
An approval is provided by adding the "Approved" label to this GitHub issue. See [Review Process](https://github.com/hmcts/auto-shutdown?tab=readme-ov-file#shutdown-exclusion-request---review-process) for additional information.
#If approved label is applued, set approval variables.
- name: set approved status
if: contains(github.event.issue.labels.*.name, 'approved')
run: |
echo "APPROVAL_STATE=Approved" >> $GITHUB_ENV
echo "APPROVAL_COMMENT=Approved by ${{ github.actor }}" >> $GITHUB_ENV
#Add approval comment if approval_state == Approved
- name: Add auto approval comment
if: env.APPROVAL_STATE == 'Approved'
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ steps.create-issue.outputs.number }}
body: |
${{ env.APPROVAL_COMMENT }}
#Remove processing label
- name: Remove processing label
uses: actions-ecosystem/action-remove-labels@v1
with:
labels: |
Processing
number: ${{ steps.create-issue.outputs.number }}
#Remove pending approval label if approved
- name: Remove processing label
if: env.APPROVAL_STATE == 'Approved'
uses: actions-ecosystem/action-remove-labels@v1
with:
labels: |
Pending approval
number: ${{ steps.create-issue.outputs.number }}
#Close issue if approved
- name: Close Issue with success
if: env.PROCESS_SUCCESS == 'true' && env.APPROVAL_STATE == 'Approved' || env.PROCESS_SUCCESS == 'true' && env.APPROVAL_STATE == 'auto-approved'
uses: peter-evans/close-issue@v3
with:
issue-number: ${{ steps.create-issue.outputs.number }}
comment: |
Success!
Note: Since this is a pull request, no changes have been made to the exclusion list json.
Auto-closing issue.
#Send slack message if form parse process is successfull.
- name: Send slack message
if: env.PROCESS_SUCCESS == 'true'
run: ./scripts/aks/send-slack-message.sh
env:
SLACK_WEBHOOK: ${{ env.SLACK_WEBHOOK }}
ISSUE_TITLE: 'PR-${{ github.event.pull_request.number }}'
CHANGE_JIRA_ID: ${{ env.CHANGE_JIRA_ID }}
REQUEST_URL: ${{ env.REQUEST_URL }}
BUSINESS_AREA_ENTRY: ${{ env.BUSINESS_AREA_ENTRY }}
START_DATE: ${{ env.START_DATE }}
END_DATE: ${{ env.END_DATE }}
COST_DETAILS_FORMATTED: ${{ env.COST_DETAILS_FORMATTED }}
ENVIRONMENT: ${{ env.ENVIRONMENT }}
JUSTIFICATION: ${{ env.JUSTIFICATION }}
REQUESTER: ${{ github.event.pull_request.user.login }}
APPROVAL_COMMENT: ${{ env.APPROVAL_COMMENT }}
REVIEWER: ${{ github.actor }}