forked from hmcts/auto-shutdown
-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (127 loc) · 6.67 KB
/
add-to-dashboard.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
name: Add issue to Projects Dashboard
run-name: ${{ github.actor }} - issue:${{ github.event.issue.number }}
on:
issues:
# types: [opened, edited, labeled, reopened, unlabeled]
types: [closed, labeled]
workflow_dispatch:
env:
GH_TOKEN: ${{ secrets.PLATFORM_USER_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
PROJECT_NUMBER: 29
PROJECT_OWNER: hmcts
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, 'pull-request')
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Get issue body and extract values
id: get_issue_body
run: |
gh issue view ${{ github.event.issue.number }} --json body | jq -r '.body' > body.txt
sed -i "s/'//g" body.txt
START_DATE=$(sed -n '/^### Skip shutdown start date/,/^###/p' body.txt | tail -n +2 | head -n -1 | tr -d '[:space:]')
END_DATE=$(sed -n '/^### Skip shutdown end date/,/^###/p' body.txt | tail -n +2 | head -n -1 | tr -d '[:space:]')
BUSINESS_AREA=$(sed -n '/^### Business area/,/^###/p' body.txt | tail -n +2 | head -n -1 | tr -d '[:space:]')
TEAM_NAME=$(sed -n '/^### Team\/Application Name/,/^###/p' body.txt | tail -n +2 | head -n -1 | tr -d '[:space:]')
ENVIRONMENTS_REQUESTED=$(sed -n '/^### Environment/,/^###/p' body.txt | tail -n +2 | head -n -1 | tr -d '[:space:]')
PAST_11PM=$(sed -n '/^### Do you need this exclusion past 11pm?/,$p' body.txt | tail -n +2 | sed '/^###/d' | xargs)
echo "start_date=$START_DATE" >> $GITHUB_OUTPUT
echo "end_date=$END_DATE" >> $GITHUB_OUTPUT
echo "business_area=$BUSINESS_AREA" >> $GITHUB_OUTPUT
echo "team_name=$TEAM_NAME" >> $GITHUB_OUTPUT
echo "environments_requested=$ENVIRONMENTS_REQUESTED" >> $GITHUB_OUTPUT
echo "past_11pm=$PAST_11PM" >> $GITHUB_OUTPUT
- name: Update GitHub Project
if: "!contains(github.event.issue.labels.*.name, 'cancel')"
run: |
ITEM_ID=$(gh project item-add $PROJECT_NUMBER --owner $PROJECT_OWNER --url ${{ github.event.issue.html_url }} --format json | jq -r '.id')
echo "Item ID: $ITEM_ID"
# Get project ID
PROJECT_ID=$(gh project view $PROJECT_NUMBER --owner $PROJECT_OWNER --format json | jq -r '.id')
echo "Project ID: $PROJECT_ID"
# Convert dates to ISO 8601 format
convert_date() {
local input_date="$1"
local day=$(echo $input_date | cut -d'-' -f1)
local month=$(echo $input_date | cut -d'-' -f2)
local year=$(echo $input_date | cut -d'-' -f3)
echo "$year-$month-$day"
}
START_DATE=$(convert_date "${{ steps.get_issue_body.outputs.start_date }}")
END_DATE=$(convert_date "${{ steps.get_issue_body.outputs.end_date }}")
echo "Converted Start Date: $START_DATE"
echo "Converted End Date: $END_DATE"
FIELDS=$(gh project field-list $PROJECT_NUMBER --owner $PROJECT_OWNER --format json)
echo "All fields:"
echo "$FIELDS" | jq '.'
# Extract StartDate and EndDate field IDs
START_DATE_FIELD_ID=$(echo "$FIELDS" | jq -r '.fields[] | select(.name == "StartDate") | .id')
END_DATE_FIELD_ID=$(echo "$FIELDS" | jq -r '.fields[] | select(.name == "EndDate") | .id')
echo "StartDate Field ID: $START_DATE_FIELD_ID"
echo "EndDate Field ID: $END_DATE_FIELD_ID"
# Update StartDate field
gh project item-edit --project-id "$PROJECT_ID" --id "$ITEM_ID" --field-id "$START_DATE_FIELD_ID" --date "$START_DATE"
# Update EndDate field
gh project item-edit --project-id "$PROJECT_ID" --id "$ITEM_ID" --field-id "$END_DATE_FIELD_ID" --date "$END_DATE"
echo "Project update completed"
env:
GH_TOKEN: ${{ env.GH_TOKEN }}
- name: Update issue title
if: "!contains(github.event.issue.labels.*.name, 'cancel')"
run: |
CURRENT_TITLE=$(gh issue view ${{ github.event.issue.number }} --json title -q .title)
# Check if past 11pm is requested
if [[ "${{ steps.get_issue_body.outputs.past_11pm }}" == "No" ]]; then
PAST_11PM="🕒"
else
PAST_11PM=""
fi
if [[ "$CURRENT_TITLE" == *"- ❌"* ]]; then
CURRENT_TITLE=$(echo "$CURRENT_TITLE" | sed 's/- ❌//g')
gh issue edit ${{ github.event.issue.number }} --title "$CURRENT_TITLE"
fi
NEW_INFO="${{ steps.get_issue_body.outputs.business_area }} - ${{ steps.get_issue_body.outputs.team_name }} - ${{ steps.get_issue_body.outputs.environments_requested }} $PAST_11PM"
if [[ "$CURRENT_TITLE" != *"$NEW_INFO"* ]]; then
NEW_TITLE="${CURRENT_TITLE} - ${NEW_INFO}"
gh issue edit ${{ github.event.issue.number }} --title "$NEW_TITLE"
fi
- name: Run if cancel label added
if: contains(github.event.issue.labels.*.name, 'cancel')
run: |
CURRENT_TITLE=$(gh issue view ${{ github.event.issue.number }} --json title -q .title)
if [[ "$CURRENT_TITLE" != *"- ❌"* ]]; then
NEW_TITLE="${CURRENT_TITLE} - ❌"
gh issue edit ${{ github.event.issue.number }} --title "$NEW_TITLE"
fi
ITEM_ID=$(gh project item-add $PROJECT_NUMBER --owner $PROJECT_OWNER --url ${{ github.event.issue.html_url }} --format json | jq -r '.id')
echo "Item ID: $ITEM_ID"
gh project item-delete $PROJECT_NUMBER --owner $PROJECT_OWNER --id $ITEM_ID
- name: Keep last 10 issues on project board
run: |
# Get project ID
PROJECT_ID=$(gh project view $PROJECT_NUMBER --owner $PROJECT_OWNER --format json | jq -r '.id')
echo "Project ID: $PROJECT_ID"
# Fetch all items from the project
ALL_ITEMS=$(gh project item-list $PROJECT_NUMBER --owner $PROJECT_OWNER --format json)
# Sort items by creation date (newest first) and get IDs of items to remove
ITEMS_TO_REMOVE=$(echo "$ALL_ITEMS" | jq -r '.items | sort_by(.createdAt) | reverse | .[10:] | .[].id')
# Count items to remove
REMOVE_COUNT=$(echo "$ITEMS_TO_REMOVE" | wc -w)
echo "Items to remove: $REMOVE_COUNT"
# Remove each item
for ITEM_ID in $ITEMS_TO_REMOVE; do
echo "Removing item $ITEM_ID"
gh project item-delete $PROJECT_NUMBER --owner $PROJECT_OWNER --id $ITEM_ID
done
echo "Cleanup completed. Kept the 10 most recent items."
env:
GH_TOKEN: ${{ env.GH_TOKEN }}