forked from dougwaldron/jira-issues-importer
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjira-commenter.sh
More file actions
executable file
·105 lines (87 loc) · 5.03 KB
/
jira-commenter.sh
File metadata and controls
executable file
·105 lines (87 loc) · 5.03 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
#!/usr/bin/env bash
set -euo pipefail
# Required variables
: "${JIRA_MIGRATION_JIRA_PROJECT_NAME:? Missing Jira project name to process (e.g., INFRA)}"
: "${JIRA_MIGRATION_JIRA_PROJECT_DESC:? Missing Jira project description (e.g., Jenkins Infrastructure project)}"
: "${JIRA_MIGRATION_JIRA_USER:? Missing Jira user to be comment author (e.g., jenkins-infra-bot)}"
: "${JIRA_MIGRATION_JIRA_TOKEN:? Missing Jira token for authentication (e.g., your-jira-token)}"
: "${JIRA_MIGRATION_JIRA_URL:? Missing Jira base URL (e.g., https://issues.jenkins.io)}"
: "${JIRA_MIGRATION_GITHUB_NAME:? Missing GitHub org name (e.g., jenkins-infra)}"
: "${JIRA_MIGRATION_GITHUB_REPO:? Missing GitHub repo name (e.g., helpdesk)}"
: "${IS_USER_JIRA_ADMIN:=false}" # whether the user is a Jira admin
: "${DONT_NOTIFY_JIRA_USERS:=false}" # whether to notify Jira users after labeling and updating issues (need admin rights)
: "${ARCHIVE_JIRA_ISSUES:=false}" # whether to archive Jira issues after commenting and labeling (need admin rights)
: "${JIRA_GITHUB_MAPPING_FILE:=jira-keys-to-github-id.txt}" # with each line containing <JENKINS-ISSUE-KEY>:<GITHUB-ISSUE-KEY>, ex: "INFRA-545:415"
: "${COMMENTS_FILE:=jira-comments.txt}" # with each line containing <JENKINS-ISSUE-KEY>:<GITHUB-ISSUE-KEY>:<JIRA-COMMENT-ID>:<JIRA-COMMENT-SELF-LINK>, ex: "INFRA-545:415:457400:https://issues.jenkins.io/rest/api/2/issue/224778/comment/457400"
: "${EXPORTED_LABEL:=issue-exported-to-github}" # label to add to issues that have been exported
: "${JIRA_MIGRATION_ADDITIONAL_INFO:=}" # additional info to add to the comment body
github_issues_link="https://github.com/${JIRA_MIGRATION_GITHUB_NAME}/${JIRA_MIGRATION_GITHUB_REPO}/issues/"
notify_users_param=""
archive_issues=false
if [[ "${IS_USER_JIRA_ADMIN}" = true ]]; then
if [[ "${DONT_NOTIFY_JIRA_USERS}" = true ]]; then
notify_users_param="notifyUsers=false"
fi
if [[ "${ARCHIVE_JIRA_ISSUES}" = true ]]; then
archive_issues=true
fi
else
echo "Warning: User is not a Jira admin, can't set notifyUsers parameter or archive issues."
fi
echo "Check ${JIRA_MIGRATION_JIRA_URL} connectivity"
response_code=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}" "${JIRA_MIGRATION_JIRA_URL}/rest/api/2/myself")
if [[ $response_code == 200 ]]; then
echo "Connected to Jira successfully."
else
echo "Error: Unable to connect to JIRA. Please check your credentials and Jira URL."
exit 1
fi
while IFS=':' read -ra mapping; do
github_issue_id=${mapping[1]}
jira_issue_key=${mapping[0]}
echo '-------------------------'
echo "Processing issue ${JIRA_MIGRATION_JIRA_URL}/browse/${jira_issue_key} (${github_issues_link}${github_issue_id})"
issue_api_url="${JIRA_MIGRATION_JIRA_URL}/rest/api/2/issue/${jira_issue_key}"
commenting_api_url="${issue_api_url}/comment"
# Add export comment to Jira issue and pin it
body="All issues for ${JIRA_MIGRATION_JIRA_PROJECT_DESC} have been migrated to [GitHub|${github_issues_link}]\n\nHere is the link to this issue on GitHub: ${github_issues_link}${github_issue_id}\n\nTo find related issues use this search: ${github_issues_link}?q=%22${jira_issue_key}%22${JIRA_MIGRATION_ADDITIONAL_INFO}\n\n(_Note: this is an automated bulk comment_)"
result=$(curl \
--silent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}" \
-X POST \
--data "{\"body\": \"${body}\"}" \
"${commenting_api_url}?${notify_users_param}")
echo "${result}" | jq > "issue_comment_on_${jira_issue_key}_${github_issue_id}.json"
comment_id=$(echo "${result}" | jq -r '.id')
comment_self_link=$(echo "${result}" | jq -r '.self')
comment_api_url="${commenting_api_url}/${comment_id}"
echo "Added comment id: ${comment_id}, link: ${comment_self_link}"
echo "${jira_issue_key}:${github_issue_id}:${comment_id}:${comment_self_link}" >> "${COMMENTS_FILE}"
echo "Pin comment id ${comment_id} to the top of the issue ${jira_issue_key}"
pin_url="${comment_api_url}/pin"
curl "${pin_url}" \
--silent \
-o /dev/null \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}" \
--data-raw true
# Add exported label to Jira issue
echo "Add label '${EXPORTED_LABEL}' to issue ${jira_issue_key}"
editmeta_data="{ \"update\": { \"labels\": [{\"add\": \"${EXPORTED_LABEL}\" }] } }"
curl "${issue_api_url}?${notify_users_param}" \
--silent \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}" \
--data "${editmeta_data}"
if [[ "${archive_issues}" = true ]]; then
echo "Archive the issue ${jira_issue_key}"
curl "${issue_api_url}/archive?${notify_users_param}" \
--silent \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}"
fi
done <"${JIRA_GITHUB_MAPPING_FILE}"