forked from dougwaldron/jira-issues-importer
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathretrieve-remotelinks.sh
More file actions
executable file
·94 lines (69 loc) · 2.76 KB
/
retrieve-remotelinks.sh
File metadata and controls
executable file
·94 lines (69 loc) · 2.76 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
#!/usr/bin/env bash
set -euo pipefail
: "${JIRA_MIGRATION_JIRA_PROJECT_NAME:? Missing Jira project name (e.g., JENKINS)}"
: "${JIRA_MIGRATION_JIRA_URL:? Missing Jira base URL (e.g., https://issues.jenkins.io)}"
: "${JIRA_MIGRATION_JIRA_USER:? Missing Jira user}"
: "${JIRA_MIGRATION_JIRA_TOKEN:? Missing Jira token}"
: "${JIRA_MIGRATION_PARALLEL_COUNT:=100}"
input_file="jira_output/combined.xml"
remotelinks_file="combined-remotelinks.txt"
jira_base="${JIRA_MIGRATION_JIRA_URL}/rest/api/2/issue"
if ! command -v flock >/dev/null 2>&1; then
echo "Error: 'flock' is required but not installed on this system." >&2
echo "Install it first. On macOS: brew install flock" >&2
exit 1
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 "Error: Unable to connect to Jira"
exit 1
fi
echo "Connected to Jira successfully."
: > "${remotelinks_file}"
issues=$(grep '<key id=' "${input_file}" | sed "s/.*\(${JIRA_MIGRATION_JIRA_PROJECT_NAME}-[0-9][0-9]*\).*/\1/")
total=$(printf "%s\n" "${issues}" | grep -c .)
echo "Total issues: ${total}"
echo "Parallel count: ${JIRA_MIGRATION_PARALLEL_COUNT}"
# Export for subshells
export JIRA_MIGRATION_JIRA_TOKEN jira_base remotelinks_file total
# Progress counter using a file (atomic append)
progress_file=$(mktemp)
echo 0 > "${progress_file}"
update_progress() {
{
flock 200
# Read the current value
current=$(cat "$progress_file")
current=$((current + 1))
# Write the updated value
echo "$current" > "$progress_file"
} 200>"$progress_file.lock"
percent=$((100 * current / total))
printf "\r[%s/%s | %s%%] Processing..." "$current" "$total" "$percent" >&2
}
export progress_file
export -f update_progress
process_issue() {
issue="$1"
remotelinks_url="${jira_base}/${issue}/remotelink"
# Fetch remote links
remotelinks_json=$(curl -s \
-H "Authorization: Bearer ${JIRA_MIGRATION_JIRA_TOKEN}" \
"${remotelinks_url}")
printf "%s\n" "${remotelinks_json}" | jq -r --arg issue "${issue}" '
.[]? | select(.object.url != null) |
"\($issue):[\(.object.title)](\(.object.url))"
' >> "${remotelinks_file}"
update_progress
}
export -f process_issue
printf "%s\n" "${issues}" | xargs -n1 -P"${JIRA_MIGRATION_PARALLEL_COUNT}" bash -c 'process_issue "$@"' _
echo "Escaping #nnn references in remote link titles"
tmp=$(mktemp)
sed 's/#\([0-9][0-9]*\)/#\xE2\x80\x8B\1/g' "${remotelinks_file}" > "$tmp"
mv "$tmp" "${remotelinks_file}"
echo -e "\nDone. Output written to ${remotelinks_file}" >&2
rm -f "${progress_file}"