forked from canonical/sync-issues-github-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yaml
283 lines (254 loc) · 11 KB
/
action.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
name: 'Sync github issues to jira'
inputs:
JIRA_URL:
description: Jira URL domain to be stored as a secret.
required: true
JIRA_USERNAME:
description: Jira username to be stored as a secret. Provide with Jira API token.
required: false
JIRA_API_TOKEN:
description: Jira API token to be stored as a secret. Provide with Jira username.
required: false
JIRA_AUTHORIZATION:
description: Jira authorization to be stored as a secret. Mutually exclusive with JIRA_USERNAME and JIRA_API_TOKEN.
required: false
JIRA_COMPONENT:
description: Jira component to be stored as a secret.
required: true
JIRA_PROJECT_KEY:
description: Jira project key to be stored as a secret.
required: true
JIRA_EPIC_KEY:
description: Jira epic key to be stored as a secret.
required: true
JIRA_ISSUE_TYPE:
description: Jira issue type.
required: false
default: "Roadmap Item"
GITHUB_TOKEN:
description: 'GITHUB_TOKEN to be stored as a secret'
required: false
label:
description: 'Label which will trigger a Jira import.'
required: true
default: 'jira'
runs:
using: "composite"
steps:
#- name: Dump GitHub context
# run: echo '${{ toJSON(github) }}'
# shell: bash
- name: restrict action to labelled issues and issue comments
shell: bash
run: |
set -eu
echo "NeedsJiraUpdate=false" >> $GITHUB_ENV
if [ ${{ github.event_name }} != "issues" ] && [ ${{ github.event_name }} != "issue_comment" ]; then
echo "This action only work on issue events. Please use on: issues or issue_comment to use this action."
exit 1
fi
if [ ${{ github.event.issue.pull_request }} ]; then
echo "This action only work on issues, not pull requests."
exit 0
fi
# Issue creation with label will trigger 2 events and run twice: one create, one labelled.
# let just focus on labelling then for creating issues Jira-side.
if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then
echo "Ignoring creation of issues as a label will trigger a second event."
exit 0
fi
# We only operate on labelled issues or issues that are just unlabeled with our desired label
## check if one label of labels is our jira label
toconsider=${{ contains(github.event.issue.labels.*.name, inputs.label) }}
## second chance, this has just been unlabeled and needs to be deleted on Jira
if [ ${{ github.event.action }} == "unlabeled" ] && [ ${{ github.event.label.name }} == ${{ inputs.label }} ]; then
toconsider=true
fi
if [ "${toconsider}" == false ]; then
echo "Our desired label '${{ inputs.label }}' is not found on the issue, skipping"
exit 0
fi
# And finally, for the "labeled" event, we are only interested if the new added label is our desired one.
if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != ${{ inputs.label }} ]; then
echo "Not interested in this action, skipping"
exit 0
fi
# Exit if the issue already has an OWS label.
# TODO: Support updates to Jira issues.
labels=$(jq -r '.issue.labels[].name' "$GITHUB_EVENT_PATH")
for lbl in $labels; do
if [[ "$lbl" == OWS* ]]; then
echo "Issue has a label bestarting with OWS, skipping"
exit 0
fi
done
# last one wins
echo "NeedsJiraUpdate=true" >> $GITHUB_ENV
- name: "Convert markdown to jira"
if: ${{ env.NeedsJiraUpdate == 'true' }}
id: convert_markdown
shell: bash
run: |
set -eu
# Install markdown to jira converter
sudo apt update
sudo apt install -y pandoc jq
# Convert mardown to jira.
TMPDIR=$(mktemp -d)
trap 'rm -rf -- "$TMPDIR"' EXIT
echo '${{ github.event.issue.body }}' > $TMPDIR/body.md
sed -i 's/`//g' $TMPDIR/body.md
pandoc -f markdown -t jira $TMPDIR/body.md -o $TMPDIR/description.txt
description=$(cat $TMPDIR/description.txt)
description_escaped=$(jq -Rs . <<< "$description")
echo "description=${description_escaped}" >> $GITHUB_OUTPUT
- name: "Create issue in jira"
if: ${{ env.NeedsJiraUpdate == 'true' }}
id: create_jira_issue
shell: bash
env:
JIRA_USERNAME: '${{ inputs.JIRA_USERNAME }}'
JIRA_API_TOKEN: '${{ inputs.JIRA_API_TOKEN }}'
JIRA_AUTHORIZATION: '${{ inputs.JIRA_AUTHORIZATION }}'
run: |
set -eu
if [[ -z "${JIRA_AUTHORIZATION}" ]]; then
jira_auth=$(echo -n "${JIRA_USERNAME}:${JIRA_API_TOKEN}" | base64 | tr -d '\n')
else
jira_auth="${JIRA_AUTHORIZATION}"
fi
jira_auth_header="Authorization: Basic $jira_auth"
echo "Creating issue ${{ github.event.issue.title }} and linking it as a dependency to ${{ inputs.DEPENDENT_ISSUE }}"
create_body='{
"fields": {
"project": {
"key": "${{ inputs.JIRA_PROJECT_KEY }}"
},
"summary": "${{ github.event.issue.title }}",
"description": ${{ steps.convert_markdown.outputs.description }},
"issuetype": {
"name": "${{ inputs.JIRA_ISSUE_TYPE }}"
},
"components": [
{
"name": "${{ inputs.JIRA_COMPONENT }}"
}
],
"customfield_15140": "${{ inputs.JIRA_EPIC_KEY }}"
}
}'
create_response=$(curl --silent -w "%{http_code}" -o /tmp/create_issue_response.json -H "$jira_auth_header" -X POST -H "Content-Type: application/json" --data "${create_body}" "${{ inputs.JIRA_URL }}/rest/api/2/issue/")
http_status=$(tail -n1 <<< "$create_response")
if [[ $http_status != 2* ]]; then
echo "Creating issue failed with status $http_status"
exit 1
fi
new_issue_key=$(jq -r '.key' /tmp/create_issue_response.json)
echo "jira_issue_key=${new_issue_key}" >> $GITHUB_OUTPUT
echo "created new issue in Jira: ${new_issue_key}"
# Yes, that's indeed neccessary. Otherwise the link request will fail.
echo "Waiting 10s to give the Jira API some to process the new issue before linking it subsequently"
sleep 10
- name: "Link issue to Github"
if: ${{ env.NeedsJiraUpdate == 'true' }}
id: link_to_github
shell: bash
env:
JIRA_USERNAME: '${{ inputs.JIRA_USERNAME }}'
JIRA_API_TOKEN: '${{ inputs.JIRA_API_TOKEN }}'
JIRA_AUTHORIZATION: '${{ inputs.JIRA_AUTHORIZATION }}'
run: |
set -eu
if [[ -z "${JIRA_AUTHORIZATION}" ]]; then
jira_auth=$(echo -n "${JIRA_USERNAME}:${JIRA_API_TOKEN}" | base64 | tr -d '\n')
else
jira_auth="${JIRA_AUTHORIZATION}"
fi
jira_auth_header="Authorization: Basic $jira_auth"
echo "Linking Jira issue to GitHub issue"
gh_link_body='{
"object": {
"url": "${{ github.event.issue.html_url }}",
"title": "GitHub Issue"
},
"globalId": "github:${{ github.event.issue.html_url }}",
"relationship": "links to",
"type": {
"name": "GitHub Issue",
"inward": "links to",
"outward": "is linked to"
}
}'
link_gh_response=$(curl --silent -w "%{http_code}" -o /tmp/link_gh_response.json -H "$jira_auth_header" -X POST -H "Content-Type: application/json" --data "${gh_link_body}" "${{ inputs.JIRA_URL }}/rest/api/2/issue/${{ steps.create_jira_issue.outputs.jira_issue_key }}/remotelink")
http_status=$(tail -n1 <<< "$link_gh_response")
if [[ $http_status != 2* ]]; then
echo "Linking github issue failed with status $http_status"
exit 1
fi
echo "Linked ${{ steps.create_jira_issue.outputs.jira_issue_key }} to GitHub issue ${{ github.event.issue.number }}"
# - name: "Link issue to Epic"
# if: ${{ env.NeedsJiraUpdate == 'true' }}
# id: link_to_epic
# shell: bash
# env:
# JIRA_USERNAME: '${{ inputs.JIRA_USERNAME }}'
# JIRA_API_TOKEN: '${{ inputs.JIRA_API_TOKEN }}'
# JIRA_AUTHORIZATION: '${{ inputs.JIRA_AUTHORIZATION }}'
# run: |
# set -eu
#
# if [[ -z "${JIRA_AUTHORIZATION}" ]]; then
# jira_auth=$(echo -n "${JIRA_USERNAME}:${JIRA_API_TOKEN}" | base64 | tr -d '\n')
# else
# jira_auth="${JIRA_AUTHORIZATION}"
# fi
# jira_auth_header="Authorization: Basic $jira_auth"
#
# echo "Linking Jira issue to epic"
# epic_link_body='{
# "type": {
# "name": "Dependency",
# "inward": "has dependency",
# "outward": "depends on"
# },
# "inwardIssue": {
# "key": "${{ inputs.JIRA_EPIC_KEY }}"
# },
# "outwardIssue": {
# "key": "${{ steps.create_jira_issue.outputs.jira_issue_key }}"
# }
# }'
# link_epic_response=$(curl --silent -w "%{http_code}" -o /tmp/link_epic_response.json -H "$jira_auth_header" -X POST -H "Content-Type: application/json" --data "${epic_link_body}" "${{ inputs.JIRA_URL }}/rest/api/2/issueLink")
#
# http_status=$(tail -n1 <<< "$link_epic_response")
# if [[ $http_status != 2* ]]; then
# echo "Linking epic failed with status $http_status"
# exit 1
# fi
# echo "Linked jira issue ${{ steps.create_jira_issue.outputs.jira_issue_key }} to jira epic ${{ inputs.JIRA_EPIC_KEY }}"
- name: Adding label to GitHub issue
if: ${{ env.NeedsJiraUpdate == 'true' }}
shell: bash
run: |
issue_number=${{ github.event.issue.number }}
owner=${{ github.repository_owner }}
repo=${{ github.event.repository.name }}
echo "Adding label ${{ steps.create_jira_issue.outputs.jira_issue_key }} to issue ${issue_number}"
curl --silent -X POST \
-H 'Authorization: Bearer ${{ inputs.GITHUB_TOKEN }}' \
-H "Accept: application/vnd.github+json" \
-d '{"labels":["${{ steps.create_jira_issue.outputs.jira_issue_key }}"]}' \
"https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}/labels"
- name: Remove specific label from issue
if: ${{ env.NeedsJiraUpdate == 'true' }}
shell: bash
run: |
label_to_remove="${{ inputs.label }}"
issue_number=${{ github.event.issue.number }}
owner=${{ github.repository_owner }}
repo=${{ github.event.repository.name }}
echo "Removing label ${label_to_remove} from issue ${issue_number}"
curl --silent -X DELETE \
-H 'Authorization: Bearer ${{ inputs.GITHUB_TOKEN }}' \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}/labels/${label_to_remove}"