-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcommon.sh
More file actions
254 lines (228 loc) · 6.56 KB
/
common.sh
File metadata and controls
254 lines (228 loc) · 6.56 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env bash
# The output appears in a dedicated emphasized GitHub step box.
# Multiple lines are not supported.
# Markdown is not supported.
gh_log() {
local LEVEL="$1"
shift
if [ "$CI" = "false" ]; then
case "$LEVEL" in
"debug")
printf "\e[1;90m"
;;
"error")
printf "\e[1;31m"
;;
"warning")
printf "\e[1;33m"
;;
*)
printf "\e[1;32m"
;;
esac
fi
echo "::$LEVEL::$*"
if [ "$CI" = "false" ]; then printf "\e[0m"; fi
}
export -f gh_log
# Sets a step output value.
# Examples:
# gh_output name value
# gh_output name <<EOF
# value
# EOF
gh_output() {
local NAME="$1"
if [ "$#" -eq 1 ]; then
echo "$NAME<<END_OF_VALUE"
cat
echo "END_OF_VALUE"
else
shift
echo "$NAME=$*"
fi >>"$GITHUB_OUTPUT"
}
export -f gh_output
check_not_empty() {
for V in "$@"; do
typeset -n VAR="$V"
if [ -z "${VAR:-}" ]; then
gh_log error "Variable $V is not set or empty"
exit 1
fi
done
}
export -f check_not_empty
check_not_empty GITHUB_STEP_SUMMARY
# The output appears in the GitHub step summary box.
# Multiple lines are supported.
# Markdown is supported.
# Examples:
# gh_summary "Markdown summary"
# gh_summary <<EOF
# Markdown summary
# EOF
gh_summary() {
if [ "$CI" = "false" ]; then printf "\e[94m"; fi
if [ "$#" -eq 0 ]; then
cat # for the data passed via the pipe
else
echo -e "$@" # for the data passed as arguments
fi >>"$GITHUB_STEP_SUMMARY"
if [ "$CI" = "false" ]; then printf "\e[0m"; fi
}
export -f gh_summary
##########
## JIRA ##
##########
jira_check_not_empty() {
check_not_empty \
JIRA_USER \
JIRA_TOKEN \
JIRA_BASE_URL
}
export -f jira_check_not_empty
# Retrieves issues from JIRA using JQL and stores them in JSON format in the OUTPUT_FILE.
# If additional fields are needed, they can be added to the "fields" array.
#
# Example:
# get_issues "project = ROX AND fixVersion = 4.9.0" "issues.json"
get_issues() {
jira_check_not_empty
local JQL="$1"
local OUTPUT_FILE="$2"
# Initializations
PAGE_SIZE=20
next_page_token=""
echo "[]" > "${OUTPUT_FILE}"
# Paginate through the results
while true; do
cat <<EOF > /tmp/request.json
{
"jql": "$JQL",
"maxResults": "${PAGE_SIZE}",
"nextPageToken": "${next_page_token}",
"fields": [
"key",
"assignee",
"issuetype",
"status",
"summary"
]
}
EOF
response=$(curl --fail -sSL --request POST \
--url "https://${JIRA_BASE_URL}/rest/api/3/search/jql" \
--user "${JIRA_USER}:${JIRA_TOKEN}" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data-binary @/tmp/request.json \
--retry 5 --retry-all-errors
)
if [ -z "${response}" ]; then
gh_log error "Failed to retrieve issues from JIRA."
exit 1
fi
# Append the issues from the current page to $OUTPUT_FILE
# Need to use a temporary file as jq does not support in-place editing.
jq --argjson new_issues "$(echo "$response" | jq '.issues')" '. + $new_issues' "${OUTPUT_FILE}" > "${OUTPUT_FILE}.tmp"
mv "${OUTPUT_FILE}.tmp" "${OUTPUT_FILE}"
if [[ "$(echo "${response}" | jq '.isLast')" = "true" ]]; then
# No more pages, exit the loop.
break
else
next_page_token="$(echo "${response}" | jq -r '.nextPageToken')"
fi
done
}
export -f get_issues
# Retrieves a JIRA issue with ISSUE_KEY and returns it in JSON format.
#
# Example:
# get_issue "ROX-29997" > rox-29997.json
get_issue() {
jira_check_not_empty
local ISSUE_KEY="$1"
curl --fail -sSL --request GET \
--user "${JIRA_USER}:${JIRA_TOKEN}" \
--header "Accept: application/json" \
--retry 5 --retry-all-errors \
"https://${JIRA_BASE_URL}/rest/api/3/issue/${ISSUE_KEY}"
}
export -f get_issue
# Adds COMMENT to a JIRA issue with ISSUE_KEY.
#
# Example:
# comment_on_issue "ROX-29997" "Please update the status of this issue and notify the release engineer."
comment_on_issue() {
jira_check_not_empty
local ISSUE_KEY="$1"
local COMMENT="$2"
## Commenting on a single issue
cat <<EOF > /tmp/request.json
{
"body": {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "${COMMENT}"
}
]
}
]
}
}
EOF
curl --fail --output /dev/null -sSL --request POST \
--user "${JIRA_USER}:${JIRA_TOKEN}" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data-binary @/tmp/request.json \
"https://${JIRA_BASE_URL}/rest/api/3/issue/${ISSUE_KEY}/comment"
gh_log debug "Commented on issue ${ISSUE_KEY}"
}
export -f comment_on_issue
get_jira_release() {
jira_check_not_empty
local PROJECT="$1"
local VERSION="$2"
curl --fail -sSL --request GET \
--user "${JIRA_USER}:${JIRA_TOKEN}" \
--header "Accept: application/json" \
--retry 5 --retry-all-errors \
"https://${JIRA_BASE_URL}/rest/api/3/project/${PROJECT}/versions" | jq -r ".[] | select(.name == \"${VERSION}\")"
}
export -f get_jira_release
# Fetches the CHANGELOG from a release branch
# and returns the content of the section for a given version.
fetch_changelog() {
RELEASE_BRANCH="$1"
VERSION="$2"
check_not_empty \
RELEASE_BRANCH \
VERSION
ESCAPED_VERSION="${VERSION//./\.}"
CHANGELOG="$(gh api \
-H "Accept: application/vnd.github.v3.raw" \
"/repos/${GITHUB_REPOSITORY}/contents/CHANGELOG.md?ref=${RELEASE_BRANCH}"
)"
echo "$CHANGELOG" | sed -n "/^## \[$ESCAPED_VERSION]$/,/^## \[/p" | sed '1d;$d' | sed '/./,$!d'
}
export -f fetch_changelog
# bash trick to check if the script is sourced.
if ! (return 0 2>/dev/null); then # called
SCRIPT="$1"
check_not_empty \
SCRIPT \
GITHUB_REPOSITORY \
GITHUB_REF_NAME
URL="/repos/$GITHUB_REPOSITORY/contents/.github/workflows/scripts/$SCRIPT.sh?ref=$GITHUB_REF_NAME"
shift
gh_log debug "Executing '$SCRIPT.sh' from '$GITHUB_REPOSITORY' $GITHUB_REF_NAME branch with: ${*@Q}"
gh api -H "Accept: application/vnd.github.v3.raw" "$URL" | bash -s -- "$@"
fi