Skip to content

Commit aac6212

Browse files
authored
chore: Adapts Issues GitHub action (#437)
1 parent 0ac493e commit aac6212

File tree

1 file changed

+114
-13
lines changed

1 file changed

+114
-13
lines changed

.github/workflows/issues.yml

Lines changed: 114 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
---
22
name: Create JIRA ticket for new issues
33

4+
# Creates and updates jira tickets that sync with GitHub Issues events.
45
on:
56
issues:
6-
types: [opened]
7-
8-
permissions:
9-
issues: write
10-
contents: read
7+
types: [opened, reopened, closed]
118
jobs:
129
jira_task:
1310
name: Create Jira issue
11+
if: github.event.action == 'opened'
1412
runs-on: ubuntu-latest
13+
env:
14+
GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }}
1515
steps:
1616
- name: Create JIRA ticket
1717
id: create
1818
shell: bash
1919
env:
2020
ISSUE_NUMBER: ${{ github.event.issue.number }}
21-
ISSUE_BODY: ${{ github.event.issue.body }}
2221
ISSUE_URL: ${{ github.event.issue.html_url }}
2322
JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
2423
JIRA_ASSIGNEE: ${{ secrets.ASSIGNEE_JIRA_TICKET }}
2524
run: |
26-
2725
json_response=$(curl --request POST \
2826
--url 'https://jira.mongodb.org/rest/api/2/issue' \
2927
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
@@ -39,17 +37,14 @@
3937
"id": "12"
4038
},
4139
"customfield_12751": [{
42-
"id": "22222"
43-
}],
40+
"id": "25550"
41+
}],
4442
"description": "This ticket tracks the following GitHub issue: '"${ISSUE_URL}"'.",
4543
"components": [
4644
{
4745
"id": "32194"
4846
}
49-
],
50-
"assignee": {
51-
"name": "'"${JIRA_ASSIGNEE}"'"
52-
}
47+
]
5348
}
5449
}')
5550
@@ -65,3 +60,109 @@
6560
issue-number: ${{ github.event.issue.number }}
6661
body: |
6762
Thanks for opening this issue. The ticket [${{ steps.create.outputs.jira-ticket-id }}](https://jira.mongodb.org/browse/${{ steps.create.outputs.jira-ticket-id }}) was created for internal tracking.
63+
reopen_jira_ticket:
64+
name: Reopen JIRA ticket
65+
if: github.event.action == 'reopened'
66+
runs-on: ubuntu-latest
67+
env:
68+
GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }}
69+
steps:
70+
- name: Reopened JIRA ticket if exists
71+
run: |
72+
ISSUE_NUMBER=${{ github.event.issue.number }}
73+
JIRA_API_TOKEN=${{ secrets.JIRA_API_TOKEN }}
74+
75+
JIRA_QUERY="project = CLOUDP AND issuetype = Story AND resolution = Declined AND text ~ \"HELP: GitHub Issue n. $ISSUE_NUMBER\""
76+
77+
# URL encode the query
78+
JIRA_URL=$(echo "$JIRA_QUERY" | jq -s -R -r @uri)
79+
80+
json_response=$(curl -s --request GET \
81+
--url "https://jira.mongodb.org/rest/api/2/search?fields=id&jql=${JIRA_URL}" \
82+
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
83+
--header 'Accept: application/json')
84+
85+
JIRA_TICKET_ID=$(echo "${json_response}" | jq -r '.issues[0].id')
86+
if [ -z "$JIRA_TICKET_ID" ]; then
87+
echo "JIRA_TICKET_ID is not defined. Exiting the script."
88+
exit 1
89+
fi
90+
91+
JIRA_REOPENED_URL="https://jira.mongodb.org/rest/api/2/issue/${JIRA_TICKET_ID}/transitions"
92+
93+
json_response=$(curl -s --request POST \
94+
--url "${JIRA_REOPENED_URL}" \
95+
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
96+
--header 'Accept: application/json' \
97+
--header 'Content-Type: application/json' \
98+
--data '{
99+
"transition": {
100+
"id": 3
101+
},
102+
"update": {
103+
"comment": [
104+
{
105+
"add": {
106+
"body": "The related GitHub issue was reopened. Updated via automated process."
107+
}
108+
}
109+
]
110+
}
111+
}')
112+
echo "Response: ${json_response}"
113+
close_jira_ticket:
114+
name: Close JIRA ticket
115+
if: github.event.action == 'closed'
116+
runs-on: ubuntu-latest
117+
env:
118+
GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }}
119+
steps:
120+
- name: Close JIRA ticket if exists
121+
run: |
122+
ISSUE_NUMBER=${{ github.event.issue.number }}
123+
JIRA_API_TOKEN=${{ secrets.JIRA_API_TOKEN }}
124+
125+
JIRA_QUERY="project = CLOUDP AND issuetype = Story AND resolution = Unresolved AND text ~ \"HELP: GitHub Issue n. $ISSUE_NUMBER\""
126+
127+
# URL encode the query
128+
JIRA_URL=$(echo "$JIRA_QUERY" | jq -s -R -r @uri)
129+
130+
json_response=$(curl -s --request GET \
131+
--url "https://jira.mongodb.org/rest/api/2/search?fields=id&jql=${JIRA_URL}" \
132+
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
133+
--header 'Accept: application/json')
134+
135+
JIRA_TICKET_ID=$(echo "${json_response}" | jq -r '.issues[0].id')
136+
if [ -z "$JIRA_TICKET_ID" ]; then
137+
echo "JIRA_TICKET_ID is not defined. Exiting the script."
138+
exit 1
139+
fi
140+
141+
JIRA_CLOSE_URL="https://jira.mongodb.org/rest/api/2/issue/${JIRA_TICKET_ID}/transitions"
142+
143+
json_response=$(curl -s --request POST \
144+
--url "${JIRA_CLOSE_URL}" \
145+
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
146+
--header 'Accept: application/json' \
147+
--header 'Content-Type: application/json' \
148+
--data '{
149+
"transition": {
150+
"id": 1371
151+
},
152+
"update": {
153+
"comment": [
154+
{
155+
"add": {
156+
"body": "The related GitHub issue was closed. Resolved via automated process."
157+
}
158+
}
159+
]
160+
},
161+
"fields": {
162+
"resolution": {
163+
"name": "Declined"
164+
}
165+
}
166+
}')
167+
echo "Response: ${json_response}"
168+

0 commit comments

Comments
 (0)