forked from canonical/data-platform-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (171 loc) · 6.8 KB
/
sync_issue_to_jira.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
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
# Usage documentation: sync_issue_to_jira.md
on:
workflow_call:
inputs:
jira-base-url:
description: URL of Jira instance (e.g. "https://warthogs.atlassian.net"). Do not include trailing slash
required: true
type: string
jira-project-key:
description: Jira project key (e.g. "DPE")
required: true
type: string
jira-component-names:
description: Comma separated list of Jira component names (e.g. "mysql-k8s,mysql-router-k8s")
required: false
type: string
secrets:
jira-api-token:
# https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
description: Jira API token
required: true
jira-user-email:
description: User email address for Jira API token
required: true
jobs:
create-jira-issue:
name: Create Jira issue
if: ${{ github.event.action == 'opened' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Login to Jira API
uses: atlassian/gajira-login@v3
env:
JIRA_BASE_URL: ${{ inputs.jira-base-url }}
JIRA_USER_EMAIL: ${{ secrets.jira-user-email }}
JIRA_API_TOKEN: ${{ secrets.jira-api-token }}
- name: Determine Jira issue type
id: issue-type
run: |
if '${{ contains(github.event.issue.labels.*.name, 'bug') }}'
then
echo "type=Bug" >> "$GITHUB_OUTPUT"
else
echo "type=Story" >> "$GITHUB_OUTPUT"
fi
- name: Create components JSON
id: components
shell: python
run: |
import json
import os
components = [
{"name": component}
for component in "${{ inputs.jira-component-names }}".split(",")
if component
]
output = f"components={json.dumps(components)}"
print(output)
with open(os.environ["GITHUB_OUTPUT"], "a") as file:
file.write(output)
- name: Create Jira issue
id: create
uses: atlassian/gajira-create@v3
with:
project: ${{ inputs.jira-project-key }}
issuetype: ${{ steps.issue-type.outputs.type }}
summary: ${{ github.event.issue.title }}
fields: '{"components": ${{ steps.components.outputs.components }}, "assignee": null}'
- name: Add GitHub issue URL to Jira issue
run: |
curl --request POST \
--url '${{ inputs.jira-base-url }}/rest/api/3/issue/${{ steps.create.outputs.issue }}/remotelink' \
--user '${{ secrets.jira-user-email }}:${{ secrets.jira-api-token }}' \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{"object": {"url": "${{ github.event.issue.html_url }}", "title": "Issue #${{ github.event.issue.number }} · ${{ github.repository }}"}}'
- name: Comment Jira issue URL on GitHub issue
run: gh issue comment '${{ github.event.issue.number }}' --body '${{ inputs.jira-base-url }}/browse/${{ steps.create.outputs.issue }}' --repo '${{ github.repository }}'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update-jira-issue:
name: Update Jira issue
if: ${{ github.event.action != 'opened' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Get Jira issue key
id: get-jira-issue-key
shell: python
run: |
import json
import os
import subprocess
def run_gh_cli(*args):
"""Run command with GitHub CLI"""
output = subprocess.run(
[
"gh",
"api",
"-H",
"Accept: application/vnd.github+json",
"-H",
"X-GitHub-Api-Version: 2022-11-28",
*args,
],
check=True,
capture_output=True,
encoding="utf-8",
).stdout
return json.loads(output)
# First five issue comments
github_issue_comments = run_gh_cli(
"${{ github.event.issue.comments_url }}",
"--method",
"GET",
"--raw-field",
"per_page=5",
)
for comment in github_issue_comments:
user = comment["user"]
if user["login"] == "github-actions[bot]":
assert user["id"] == 41898282
assert user["type"] == "Bot"
# Example: https://warthogs.atlassian.net/browse/DPE-994
jira_issue_link = comment["body"]
if not jira_issue_link.startswith("${{ inputs.jira-base-url }}"):
continue
# Example: DPE-994
jira_issue_key = jira_issue_link.split("/")[-1]
output = f"jira_issue_key={jira_issue_key}"
print(output)
with open(os.environ["GITHUB_OUTPUT"], "a") as file:
file.write(output)
break
else:
print("Jira issue not found")
exit(1)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine Jira issue transition ID
id: transition-id
run: |
if '${{ github.event.action == 'closed' && github.event.issue.state_reason == 'completed' }}'
then
echo "name=Done" >> "$GITHUB_OUTPUT"
echo "id=61" >> "$GITHUB_OUTPUT"
elif '${{ github.event.action == 'closed' && github.event.issue.state_reason == 'not_planned' }}'
then
echo "name=Rejected" >> "$GITHUB_OUTPUT"
echo "id=71" >> "$GITHUB_OUTPUT"
elif '${{ github.event.action == 'reopened'}}'
then
echo "name=Untriaged" >> "$GITHUB_OUTPUT"
echo "id=81" >> "$GITHUB_OUTPUT"
else
echo 'github.event.action=${{ github.event.action }}'
echo 'github.event.issue.state_reason=${{ github.event.issue.state_reason }}'
echo "Unknown transition"
exit 1
fi
- name: Transition issue to ${{ steps.transition-id.outputs.name }}
run: |
curl --request POST \
--url '${{ inputs.jira-base-url }}/rest/api/3/issue/${{ steps.get-jira-issue-key.outputs.jira_issue_key }}/transitions' \
--user '${{ secrets.jira-user-email }}:${{ secrets.jira-api-token }}' \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '{"transition": {"id": "${{ steps.transition-id.outputs.id }}"}}'