forked from hmcts/auto-shutdown
-
Notifications
You must be signed in to change notification settings - Fork 0
319 lines (315 loc) · 14.7 KB
/
parsegithubissue.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
name: Auto Parse github form - master
run-name: ${{ github.actor }} - issue:${{ github.event.issue.number }}
on:
issues:
types:
- opened
- edited
- labeled
workflow_dispatch:
env:
GH_TOKEN: ${{ secrets.PLATFORM_USER_TOKEN }}
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
APPROVAL_STATE: 'Pending Approval'
APPROVAL_COMMENT: 'Pending Approval'
SLACK_TOKEN: ${{ secrets.AUTO_SHUTDOWN_OAUTH_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
permissions:
id-token: write
jobs:
process-request:
permissions: write-all
runs-on: ubuntu-latest
if: github.event.issue.user.login != 'renovate[bot]' && github.actor != 'hmcts-platform-operations' && !contains(github.event.issue.labels.*.name, 'cancel') && !contains(github.event.issue.labels.*.name, 'pull-request')
steps:
#Adds label for user feedback
- name: add processing label
run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
LABELS: processing
#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: ${{ github.event.issue.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')
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: approved,auto-approved
#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_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: ${{ github.event.issue.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
if: env.APPROVAL_STATE == 'Denied'
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: Processing,Pending approval
#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')
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: Cost Error,Pending approval,Very high cost
#Allows workflow to access repo
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '0'
token: ${{ env.GH_TOKEN }}
#Get issue body
- name: Get issue body
id: get_issue_body
run: |
gh issue view --repo ${{ github.repository }} ${{ github.event.issue.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 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='${{ github.event.issue.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
if: env.PROCESS_SUCCESS == 'true' && contains(github.event.issue.labels.*.name, 'Error in form')
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: Error in form
# Add form error label if process_success not equal to true
- name: Add error label
if: env.PROCESS_SUCCESS != 'true'
run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
LABELS: Error in form
# 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: ${{ github.event.issue.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 approver == requester
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 }}
SLACK_TOKEN: ${{ secrets.AUTO_SHUTDOWN_OAUTH_TOKEN }}
ISSUE_TITLE: ${{ github.event.issue.title }}
CHANGE_JIRA_ID: ${{ env.CHANGE_JIRA_ID }}
REQUEST_URL: ${{ env.REQUEST_URL }}
BUSINESS_AREA_ENTRY: ${{ env.BUSINESS_AREA_ENTRY }}
TEAM_NAME: ${{ env.TEAM_NAME }}
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.issue.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' && !contains(github.event.issue.labels.*.name, 'approved')
uses: peter-evans/create-or-update-comment@v4.0.0
with:
issue-number: ${{ github.event.issue.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: ${{ github.event.issue.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'
run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
LABELS: Cost Error
#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'
run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
LABELS: auto-approved
#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: ${{ github.event.issue.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'
run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
LABELS: Pending approval,Very high cost
#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: ${{ github.event.issue.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_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: ${{ github.event.issue.number }}
body: |
${{ env.APPROVAL_COMMENT }}
#Commit issue submission to master if approved or auto-approved.
- name: Commit to master
if: env.PROCESS_SUCCESS == 'true' && env.APPROVAL_STATE == 'Approved' || env.PROCESS_SUCCESS == 'true' && env.APPROVAL_STATE == 'auto-approved'
run: |
git config user.name hmcts-platform-operations
git config user.email github-platform-operations@hmcts.net
git add .
git commit -m parseissue-${{ github.event.issue.number }}
git push
commit_tree_url=$(gh browse -c -n)
commit_url=${commit_tree_url/tree/commit}
echo "COMMIT_URL=$(echo $commit_url)" >> $GITHUB_ENV
#Remove processing label
- name: Remove processing label
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: Processing
#Remove pending approval label if approved
- name: Remove processing label
if: env.APPROVAL_STATE == 'Approved'
run: gh issue edit "$NUMBER" --remove-label "$LABELS"
env:
LABELS: Pending approval
#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: ${{ github.event.issue.number }}
comment: |
Success! Your [commit](${{ env.COMMIT_URL }}) has been added to the json exclusions file.
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 }}
SLACK_TOKEN: ${{ secrets.AUTO_SHUTDOWN_OAUTH_TOKEN }}
ISSUE_TITLE: ${{ github.event.issue.title }}
CHANGE_JIRA_ID: ${{ env.CHANGE_JIRA_ID }}
REQUEST_URL: ${{ env.REQUEST_URL }}
BUSINESS_AREA_ENTRY: ${{ env.BUSINESS_AREA_ENTRY }}
TEAM_NAME: ${{ env.TEAM_NAME }}
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.issue.user.login }}
APPROVAL_COMMENT: ${{ env.APPROVAL_COMMENT }}
REVIEWER: ${{ github.actor }}