This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
notify.yml
281 lines (262 loc) · 11.1 KB
/
notify.yml
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
parameters:
token_name:
description: The name of environment variable containing CircleCI API Token. Required for all projects.
default: "CIRCLE_TOKEN"
type: string
job_type:
description: Indicates if job should be treated as build or deployment in Jira dev panel. Note that Deployments require additional details
default: "build"
type: enum
enum: ["build", "deployment"]
environment:
description: For deployments. Indicates the name of target environment. Default is the CircleCI Job Name.
default: "${CIRCLE_JOB}"
type: string
environment_type:
description: Indicates the category of target environment as defined by Atlassian
type: enum
enum: ["production", "staging", "testing", "development", "unmapped"]
default: "development"
state_path:
description: Relative or absolute path to a store build state for orb.
default: "./circleci-orb-jira.status"
type: string
service_id:
description: Specify the JSD service ID for the project this notification targets.
type: string
default: ''
issue_regexp:
description: Override the default project key regexp if your project keys follow a different format.
default: "[A-Z]{2,30}-[0-9]+"
type: string
steps:
- jq/install:
when: always
- run:
name: JIRA - Setting Failure Condition
command: |
echo 'JIRA_BUILD_STATUS="failed"' > <<parameters.state_path>>
when: on_fail
- run:
name: JIRA - Setting Success Condition
command: |
echo 'JIRA_BUILD_STATUS="successful"' > <<parameters.state_path>>
when: on_success
- run:
when: always
name: Update status in Atlassian Jira
command: |
: ${<<parameters.token_name>>:?"Please provide a CircleCI API token for this orb to work!"} >&2
if [[ $(echo $CIRCLE_REPOSITORY_URL | grep github.com) ]]; then
VCS_TYPE=github
else
VCS_TYPE=bitbucket
fi
run () {
verify_api_key
parse_jira_key_array
HAS_JSD_SERVICE_ID="<< parameters.service_id >>"
# If you have either an issue key or a service ID
if [[ -n "${ISSUE_KEYS}" || -n "${HAS_JSD_SERVICE_ID}" ]]; then
check_workflow_status
generate_json_payload_<<parameters.job_type>>
post_to_jira
else
# If no service is or issue key is found.
echo "No Jira issue keys found in commit subjects or branch name, skipping."
echo "No service ID selected. Please add the service_id parameter for JSD deployments."
exit 0
fi
}
verify_api_key () {
URL="https://circleci.com/api/v2/me?circle-token=${<<parameters.token_name>>}"
fetch $URL /tmp/me.json
jq -e '.login' /tmp/me.json
}
fetch () {
URL="$1"
OFILE="$2"
RESP=$(curl -w "%{http_code}" -s <<# parameters.token_name >> --user "${<<parameters.token_name>>}:" <</parameters.token_name>> \
-o "${OFILE}" \
"${URL}")
echo "${RESP}:${URL}"
if [[ "$RESP" != "20"* ]]; then
echo "Curl failed with code ${RESP}. full response below."
cat $OFILE
exit 1
fi
}
parse_jira_key_array () {
# must save as ISSUE_KEYS='["CC-4"]'
fetch https://circleci.com/api/v1.1/project/${VCS_TYPE}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM} /tmp/job_info.json
# see https://jqplay.org/s/TNq7c5ctot
ISSUE_KEYS=$(cat /tmp/job_info.json | jq '[.all_commit_details[].subject | scan("(<<parameters.issue_regexp>>)") | .[] ] + [.all_commit_details[].branch | scan("(<<parameters.issue_regexp>>)") | .[] ] + [if .branch then .branch else "" end | scan("(<<parameters.issue_regexp>>)") | . [] ]')
if [ -z "$ISSUE_KEYS" ]; then
# No issue keys found.
echo "No issue keys found. This build does not contain a match for a Jira Issue. Please add your issue ID to the commit message or within the branch name."
exit 0
fi
}
check_workflow_status () {
URL="https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}"
fetch $URL /tmp/workflow.json
export WORKFLOW_STATUS=$(jq -r '.status' /tmp/workflow.json)
export CIRCLE_PIPELINE_NUMBER=$(jq -r '.pipeline_number' /tmp/workflow.json)
echo "This job is passing, however another job in workflow is ${WORKFLOW_STATUS}"
if [ "<<parameters.job_type>>" != "deployment" ]; then
# deployments are special, cause they pass or fail alone.
# but jobs are stuck togehter, and they must respect status of workflow
if [[ "$WORKFLOW_STATUS" == "fail"* ]]; then
export JIRA_BUILD_STATUS="failed"
fi
fi
}
generate_json_payload_build () {
iso_time=$(date '+%Y-%m-%dT%T%z'| sed -e 's/\([0-9][0-9]\)$/:\1/g')
echo {} | jq \
--arg time_str "$(date +%s)" \
--arg lastUpdated "${iso_time}" \
--arg pipelineNumber "${CIRCLE_PIPELINE_NUMBER}" \
--arg projectName "${CIRCLE_PROJECT_REPONAME}" \
--arg state "${JIRA_BUILD_STATUS}" \
--arg jobName "${CIRCLE_JOB}" \
--arg buildNumber "${CIRCLE_BUILD_NUM}" \
--arg url "${CIRCLE_BUILD_URL}" \
--arg workflowUrl "https://circleci.com/workflow-run/${CIRCLE_WORKFLOW_ID}" \
--arg commit "${CIRCLE_SHA1}" \
--arg refUri "${CIRCLE_REPOSITORY_URL}/tree/${CIRCLE_BRANCH}" \
--arg repositoryUri "${CIRCLE_REPOSITORY_URL}" \
--arg branchName "${CIRCLE_BRANCH}" \
--arg workflowId "${CIRCLE_WORKFLOW_ID}" \
--arg repoName "${CIRCLE_PROJECT_REPONAME}" \
--arg display "${CIRCLE_PROJECT_REPONAME}" \
--arg description "${CIRCLE_PROJECT_REPONAME} #${CIRCLE_BUILD_NUM} ${CIRCLE_JOB}" \
--argjson issueKeys "${ISSUE_KEYS}" \
'
($time_str | tonumber) as $time_num |
{
"builds": [
{
"schemaVersion": "1.0",
"pipelineId": $projectName,
"buildNumber": $pipelineNumber,
"updateSequenceNumber": $time_str,
"displayName": $display,
"description": $description,
"url": $workflowUrl,
"state": $state,
"lastUpdated": $lastUpdated,
"issueKeys": $issueKeys
}
]
}
' > /tmp/jira-status.json
}
generate_json_payload_deployment () {
echo "Update Jira with status: ${JIRA_BUILD_STATUS} for ${CIRCLE_PIPELINE_NUMBER}"
iso_time=$(date '+%Y-%m-%dT%T%z'| sed -e 's/\([0-9][0-9]\)$/:\1/g')
echo {} | jq \
--arg time_str "$(date +%s)" \
--arg lastUpdated "${iso_time}" \
--arg state "${JIRA_BUILD_STATUS}" \
--arg buildNumber "${CIRCLE_BUILD_NUM}" \
--arg pipelineNumber "${CIRCLE_PIPELINE_NUMBER}" \
--arg projectName "${CIRCLE_PROJECT_REPONAME}" \
--arg url "${CIRCLE_BUILD_URL}" \
--arg commit "${CIRCLE_SHA1}" \
--arg refUri "${CIRCLE_REPOSITORY_URL}/tree/${CIRCLE_BRANCH}" \
--arg repositoryUri "${CIRCLE_REPOSITORY_URL}" \
--arg branchName "${CIRCLE_BRANCH}" \
--arg workflowId "${CIRCLE_WORKFLOW_ID}" \
--arg workflowUrl "https://circleci.com/workflow-run/${CIRCLE_WORKFLOW_ID}" \
--arg repoName "${CIRCLE_PROJECT_REPONAME}" \
--arg pipelineDisplay "#${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_PROJECT_REPONAME}" \
--arg deployDisplay "#${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_PROJECT_REPONAME} - <<parameters.environment>>" \
--arg description "${CIRCLE_PROJECT_REPONAME} #${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_JOB} <<parameters.environment>>" \
--arg envId "${CIRCLE_WORKFLOW_ID}-<<parameters.environment>>" \
--arg envName "<<parameters.environment>>" \
--arg envType "<<parameters.environment_type>>" \
--argjson issueKeys "${ISSUE_KEYS}" \
'
($time_str | tonumber) as $time_num |
{
"deployments": [
{
"schemaVersion": "1.0",
"pipeline": {
"id": $repoName,
"displayName": $pipelineDisplay,
"url": $workflowUrl
},
"deploymentSequenceNumber": $pipelineNumber,
"updateSequenceNumber": $time_str,
"displayName": $deployDisplay,
"description": $description,
"url": $url,
"state": $state,
"lastUpdated": $lastUpdated,
"associations": [
{
"associationType": "issueKeys",
"values": $issueKeys
},
{
"associationType": "serviceIdOrKeys",
"values": ["<< parameters.service_id >>"]
}
],
"environment":{
"id": $envId,
"displayName": $envName,
"type": $envType
}
}
]
}
' > /tmp/jira-status.json
}
post_to_jira () {
HTTP_STATUS=$(curl \
-u "${<<parameters.token_name>>}:" \
-s -w "%{http_code}" -o /tmp/curl_response.txt \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST "https://circleci.com/api/v1.1/project/${VCS_TYPE}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/jira/<<parameters.job_type>>" --data @/tmp/jira-status.json)
echo "Results from Jira: "
if [ "${HTTP_STATUS}" != "200" ];then
echo "Error calling Jira, result: ${HTTP_STATUS}" >&2
jq '.' /tmp/curl_response.txt
exit 0
fi
case "<<parameters.job_type>>" in
"build")
if jq -e '.unknownIssueKeys[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: unknown issue key"
jq '.' /tmp/curl_response.txt
exit 0
fi
;;
"deployment")
if jq -e '.unknownAssociations[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: unknown association"
jq '.' /tmp/curl_response.txt
exit 0
fi
if jq -e '.rejectedDeployments[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: Deployment rejected"
jq '.' /tmp/curl_response.txt
exit 0
fi
;;
esac
# If reached this point, the deployment was a success.
echo
jq '.' /tmp/curl_response.txt
echo
echo
echo "Success!"
}
# kick off
source <<parameters.state_path>>
run
rm -f <<parameters.state_path>>