-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib-jira.sh
More file actions
executable file
·235 lines (198 loc) · 6.31 KB
/
lib-jira.sh
File metadata and controls
executable file
·235 lines (198 loc) · 6.31 KB
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
#!/usr/bin/env bash
# The following variable must be set before using this script
# export JIRA_BASE_URL="https://kosli-team.atlassian.net"
# export JIRA_USERNAME="tore@kosli.com"
# export JIRA_API_TOKEN="xx"
# export DEBUG="true"
function debug_log
{
if [ "${DEBUG}" == "true" ]; then
echo -e "$@" >&2
fi
}
function loud_curl
{
# curl that prints the server traceback if the response
# status code is not in the range 200-299
local -r method=$1; shift # eg GET/POST
local -r url=$1; shift
local -r jsonPayload=$1; shift
local -r userArg=$1;shift
local -r outputFile=$(mktemp)
set +e
if [ "${method}" == "GET" ]; then
HTTP_CODE=$(curl --header 'Content-Type: application/json' \
--user "${userArg}" \
--output "${outputFile}" \
--write-out "%{http_code}" \
--request "${method}" \
--silent \
${url})
else
HTTP_CODE=$(curl --header 'Content-Type: application/json' \
--user "${userArg}" \
--output "${outputFile}" \
--write-out "%{http_code}" \
--request "${method}" \
--silent \
--data "${jsonPayload}" \
${url})
fi
set -e
if [[ ${HTTP_CODE} -lt 200 || ${HTTP_CODE} -gt 299 ]] ; then
>&2 cat ${outputFile} # Request failed so send output to stderr
>&2 echo
rm ${outputFile}
exit 2
fi
cat ${outputFile} # Correct response send to stdout
echo
rm ${outputFile}
}
function loud_curl_jira
{
local -r userArg=""${JIRA_USERNAME}:${JIRA_API_TOKEN}""
loud_curl "$@" ${userArg}
}
function create_release
{
local -r projectId=$1; shift
local -r releaseName=$1; shift
local -r startDate=$(date -u "+%Y-%m-%d")
local -r url="${JIRA_BASE_URL}/rest/api/3/version"
local -r data='{
"description": "Release '${releaseName}'",
"name": "'${releaseName}'",
"projectId": '${projectId}',
"startDate": "'${startDate}'"
}'
debug_log "Created release:\n${data}"
loud_curl_jira POST "${url}" "${data}"
}
function set_release_to_released
{
local -r releaseId=$1; shift
local -r releaseDate=$(date -u "+%Y-%m-%d")
local -r url="${JIRA_BASE_URL}/rest/api/3/version/${releaseId}"
local -r data='{
"releaseDate": "'${releaseDate}'",
"released": true
}'
debug_log "Set release ${releaseId} to released:\n${data}"
loud_curl_jira PUT "${url}" "${data}"
}
function get_current_release_candidate
{
local -r projectId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/project/${projectId}/version?status=unreleased"
loud_curl_jira GET "${url}" {}
}
function get_release
{
local -r releaseId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/version/${releaseId}?expand=approvers"
loud_curl_jira GET "${url}" {}
}
function fetch_user_details
{
local -r accountId=$1; shift
local url="${JIRA_BASE_URL}/rest/api/3/user?accountId=${accountId}"
loud_curl_jira GET "${url}" {}
}
function get_approvers_in_release
{
local -r releaseId=$1; shift
get_release ${releaseId} | jq '.approvers'
}
function add_approver_name_and_email
{
# In list of approvers it only includes the accountId
# This function uses accountId to look up displayName and email and
# adds it to the input file.
local releaseJsonFile=$1; shift
local releaseJson=$(cat "${releaseJsonFile}")
local updatedApprovers accountId userJson email displayName
updatedApprovers=$(echo "${releaseJson}" | jq -c '.approvers[]' | while read -r approver; do
accountId=$(echo "${approver}" | jq -r '.accountId')
userJson=$(fetch_user_details "${accountId}")
email=$(echo "${userJson}" | jq -r '.emailAddress // empty')
displayName=$(echo "${userJson}" | jq -r '.displayName // empty')
echo "${approver}" | jq --arg email "${email}" --arg name "${displayName}" \
'. + {emailAddress: $email, displayName: $name}'
done | jq -s '.')
echo "${releaseJson}" | jq --argjson approvers "${updatedApprovers}" '.approvers = $approvers' > "${releaseJsonFile}"
}
function get_issues_in_release
{
local -r releaseId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/search?jql=fixVersion='${releaseId}'"
loud_curl_jira GET "${url}" {}
}
function get_issue_keys_in_release
{
local -r releaseId=$1; shift
get_issues_in_release ${releaseId} | jq -r '[.issues[].key] | join(" ")'
}
function add_issue_to_release()
{
local -r issueKey=$1; shift
local -r releaseId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/issue/${issueKey}"
local -r data='{
"fields": {
"fixVersions": [{
"id": "'${releaseId}'"
}]
}
}'
loud_curl_jira PUT "${url}" "${data}"
}
function get_issue
{
local -r issueKey=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/issue/${issueKey}"
loud_curl_jira GET "${url}" {}
}
function add_section_to_release
{
local -r releaseId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/version/${releaseId}"
local -r data='{
"myCustomField": "some custom value"
}'
debug_log "Set release ${releaseId} to released:\n${data}"
loud_curl_jira PUT "${url}" "${data}"
}
function add_trail_link_to_release
{
local -r releaseId=$1; shift
local -r trailUrl=$1;
local -r url="${JIRA_BASE_URL}/rest/api/3/version/${releaseId}/relatedwork"
local -r data='{
"category": "Audit",
"title": "Kosli Trail",
"url": "'${trailUrl}'"
}'
debug_log "Add related work to release ${releaseId}:\n${data}"
loud_curl_jira PUT "${url}" "${data}"
}
# Jira has no API to add an approver to a release. This must be done in the UX
# See https://community.developer.atlassian.com/t/add-approver-to-version-through-rest-api/76975
function add_approver_to_release
{
local -r approverId=$1; shift
local -r releaseId=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/version/${releaseId}"
local -r data='{
"approvers": [{
"accountId": "'${approverId}'"
}]
}'
loud_curl_jira PUT "${url}" "${data}"
}
function get_project_id
{
local -r projectKey=$1; shift
local -r url="${JIRA_BASE_URL}/rest/api/3/project/${projectKey}"
loud_curl_jira GET "${url}" {} | jq -r .id
}