Skip to content

JordanHoey96 - PR:900 #243

JordanHoey96 - PR:900

JordanHoey96 - PR:900 #243

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 }}