Skip to content

Commit 47106d0

Browse files
das-28: Add script to handle issue closure
1 parent 3f6a4da commit 47106d0

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

scripts/on-issue-closed.sh

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
get_issue_labels() {
5+
curl -s -H "Authorization: token $GH_TOKEN" \
6+
"$API_URL/repos/$REPO/issues/$ISSUE_NUMBER" |
7+
jq -r '.labels[].name'
8+
}
9+
10+
remove_label_from_issue() {
11+
curl -s -X DELETE -H "Authorization: token $GH_TOKEN" \
12+
"$API_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels/$(urlencode "$LABEL_NAME")"
13+
}
14+
15+
reopen_issue() {
16+
curl -s -X PATCH -H "Authorization: token $GH_TOKEN" \
17+
-H "Content-Type: application/json" \
18+
-d '{"state": "open"}' \
19+
"$API_URL/repos/$REPO/issues/$ISSUE_NUMBER"
20+
}
21+
22+
urlencode() {
23+
local str="$1"
24+
jq -nr --arg v "$str" '$v|@uri'
25+
}
26+
27+
get_project_fields() {
28+
jq -n --arg org "$OWNER" --argjson number "$PROJECT_NUMBER" '{
29+
query: "query ($org: String!, $number: Int!) {
30+
organization(login: $org) {
31+
projectV2(number: $number) {
32+
id
33+
fields(first: 50) {
34+
nodes {
35+
... on ProjectV2SingleSelectField {
36+
id
37+
name
38+
options { id name }
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}",
45+
variables: { org: $org, number: $number }
46+
}'
47+
}
48+
49+
build_get_project_item_query() {
50+
jq -n --arg owner "$OWNER" --arg name "$(basename "$REPO")" --argjson issue "$ISSUE_NUMBER" '{
51+
query: "query($owner: String!, $name: String!, $issue: Int!) {
52+
repository(owner: $owner, name: $name) {
53+
issue(number: $issue) {
54+
projectItems(first: 10) {
55+
nodes {
56+
id
57+
project { id title }
58+
}
59+
}
60+
}
61+
}
62+
}",
63+
variables: {
64+
owner: $owner,
65+
name: $name,
66+
issue: $issue
67+
}
68+
}'
69+
}
70+
71+
get_project_item_id_for_issue() {
72+
local query
73+
query=$(build_get_project_item_query)
74+
75+
curl -s -H "Authorization: bearer $GH_TOKEN" -H "Content-Type: application/json" \
76+
-d "$query" "$API_URL/graphql" | jq -r '.data.repository.issue.projectItems.nodes[0].id'
77+
}
78+
79+
build_mutation_payload() {
80+
local PROJECT_ID="$1"
81+
local itemId="$2"
82+
local fieldId="$3"
83+
local optionId="$4"
84+
85+
jq -n --arg projectId "$PROJECT_ID" \
86+
--arg itemId "$itemId" \
87+
--arg fieldId "$fieldId" \
88+
--arg optionId "$optionId" \
89+
'{
90+
query: "mutation ($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
91+
updateProjectV2ItemFieldValue(input: {
92+
projectId: $projectId,
93+
itemId: $itemId,
94+
fieldId: $fieldId,
95+
value: { singleSelectOptionId: $optionId }
96+
}) {
97+
projectV2Item { id }
98+
}
99+
}",
100+
variables: {
101+
projectId: $projectId,
102+
itemId: $itemId,
103+
fieldId: $fieldId,
104+
optionId: $optionId
105+
}
106+
}'
107+
}
108+
109+
move_issue_to_column() {
110+
echo "Checking labels for issue #$ISSUE_NUMBER..."
111+
112+
if echo "$(get_issue_labels)" | grep -q "$LABEL_NAME"; then
113+
echo "Label '$LABEL_NAME' found."
114+
115+
remove_label_from_issue
116+
echo "Label '$LABEL_NAME' removed."
117+
118+
reopen_issue
119+
echo "Issue reopened."
120+
121+
sleep 10s
122+
echo "🔍 Fetching project columns..."
123+
FIELDS_RESPONSE=$(curl -s -H "Authorization: bearer $GH_TOKEN" -X POST -d "$(get_project_fields)" "$API_URL/graphql")
124+
125+
126+
PROJECT_ID=$(echo "$FIELDS_RESPONSE" | jq -r '.data.organization.projectV2.id')
127+
STATUS_FIELD=$(echo "$FIELDS_RESPONSE" | jq -c '.data.organization.projectV2.fields.nodes[] | select(.name == "Status")')
128+
STATUS_FIELD_ID=$(echo "$STATUS_FIELD" | jq -r '.id')
129+
TARGET_COLUMN_ID=$(echo "$STATUS_FIELD" | jq -r --arg col "$TARGET_COLUMN_NAME" '.options[] | select(.name == $col) | .id')
130+
131+
echo "Project ID: $PROJECT_ID"
132+
echo "Field 'Status': $STATUS_FIELD_ID"
133+
echo "Target column: $TARGET_COLUMN_NAME ($TARGET_COLUMN_ID)"
134+
135+
ITEM_ID=$(get_project_item_id_for_issue)
136+
echo "Project card ID: $ITEM_ID"
137+
138+
echo "Moving card to '$TARGET_COLUMN_NAME'..."
139+
curl -s -X POST "$API_URL/graphql" \
140+
-H "Authorization: bearer $GH_TOKEN" \
141+
-H "Content-Type: application/json" \
142+
-d "$(build_mutation_payload "$PROJECT_ID" "$ITEM_ID" "$STATUS_FIELD_ID" "$TARGET_COLUMN_ID")"
143+
144+
echo "Card moved successfully!"
145+
else
146+
echo "Label '$LABEL_NAME' not found on issue #$ISSUE_NUMBER. No action taken."
147+
fi
148+
}
149+
150+
move_issue_to_column

0 commit comments

Comments
 (0)