-
Notifications
You must be signed in to change notification settings - Fork 53
/
transfer-issues.sh
executable file
·167 lines (127 loc) · 4.59 KB
/
transfer-issues.sh
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
#!/bin/bash
COUNT=${COUNT:-1}
STATES=${STATES:-"[OPEN, CLOSED]"}
ISSUE_NUMBER=${ISSUE_NUMBER:-}
set -xue
# Required environment variables
# GITHUB_TOKEN: GitHub token with repo scope
# FROM_NAME: Repository name to transfer issues from
# Optional environment variables
# ISSUE_NUMBER: Issue number to transfer (will nullify COUNT)
# COUNT: Number of issues to transfer
# The label ID for https://github.com/mozilla/addons/labels/migration%3A2024
MIGRATION_LABEL_ID="LA_kwDOAn4H8M8AAAABmbq8hA"
MIGRATION_NO_JIRA_ID="LA_kwDOAPqAY88AAAABm5JiFA"
REPOSITORY_OWNER="mozilla"
TO_NAME="addons"
ALLOWED_REPOS="addons-server,addons-frontend"
FROM_REPO="$REPOSITORY_OWNER/$FROM_NAME"
TO_REPO="$REPOSITORY_OWNER/$TO_NAME"
echo """
FROM_NAME: $FROM_NAME
REPOSITORY_OWNER: $REPOSITORY_OWNER
TO_NAME: $TO_NAME
ALLOWED_REPOS: $ALLOWED_REPOS
FROM_REPO: $FROM_REPO
TO_REPO: $TO_REPO
ISSUE_NUMBER: $ISSUE_NUMBER
COUNT: $COUNT
STATES: $STATES
"""
auth_status=$(gh auth status 2>&1)
if [[ "$auth_status" == *"You are not logged into any GitHub hosts."* ]]; then
echo "Error: $auth_status"
exit 1
fi
if [[ ! "$ALLOWED_REPOS" =~ (^|,)"$FROM_NAME"(,|$) ]]; then
echo "Invalid FROM_NAME "$FROM_NAME". Exiting..."
exit 1
fi
rate_limit=$(gh api /rate_limit)
echo "rate_limit: $rate_limit"
graphql_remaining=$(echo $rate_limit | jq -r '.resources.graphql.remaining')
core_remaining=$(echo $rate_limit | jq -r '.resources.core.remaining')
rate_limit_remaining=$(echo $rate_limit | jq -r '.rate.remaining')
if [[ "$graphql_remaining" -eq 0 || "$core_remaining" -eq 0 || "$rate_limit_remaining" -eq 0 ]]; then
echo "Rate limit exceeded. Exiting..."
exit 1
fi
if [[ -n "$ISSUE_NUMBER" ]]; then
echo "Transferring issue $ISSUE_NUMBER from \"$FROM_REPO\" to \"$TO_REPO\""
issues_query="""
query {
repository(owner: \"$REPOSITORY_OWNER\", name: \"$FROM_NAME\") {
issue(number: $ISSUE_NUMBER) {
id
body
url
}
}
}
"""
issues=$(gh api graphql -f query="$issues_query" --jq '.data.repository.issue')
else
echo "Transferring $COUNT issues from \"$FROM_REPO\" to \"$TO_REPO\""
issues_query="""
query {
repository(owner: \"$REPOSITORY_OWNER\", name: \"$FROM_NAME\") {
issues(first: $COUNT, states: $STATES, orderBy: {field: CREATED_AT, direction: ASC}) {
nodes {
id
body
url
}
}
}
}
"""
issues=$(gh api graphql -f query="$issues_query" --jq '.data.repository.issues.nodes[]')
fi
if [[ -z "$issues" ]]; then
echo "No issues found. Exiting..."
exit 1
fi
# echo each issue .url property
echo "$issues" | jq -r '.url'
transfer_mutation="mutation {"
comment_mutation="mutation {"
new_issues_counter=1
comment_counter=1
repository_id=$(gh repo view "$TO_REPO" --json id --jq '.id')
while IFS= read -r issue; do
issue_id=$(echo "$issue" | jq -r '.id')
transfer_mutation+=" t${new_issues_counter}: transferIssue(input: { issueId: \"${issue_id}\", repositoryId: \"${repository_id}\", createLabelsIfMissing: true }) { issue { id url } }"
new_issues_counter=$((new_issues_counter+1))
jira_link=$(echo $issue \
| jq -r '.body' \
| awk -F "┆Issue is synchronized with this" '{if (NF>1) {print $2} else {print ""}}' \
| grep -o 'https://[^ )]*' || echo ''
)
# if jira_link is present, add to the mutation
if [ -n "$jira_link" ]; then
comment_body="Old Jira Ticket: $jira_link"
comment_mutation+=" t${comment_counter}: addComment(input: { subjectId: \"${issue_id}\", body: \"${comment_body}\" }) { __typename }"
else
comment_mutation+=" t${comment_counter}: addLabelsToLabelable(input: {labelableId: \"$issue_id\", labelIds: [\"$MIGRATION_NO_JIRA_ID\"]}) { __typename }"
fi
comment_counter=$((comment_counter+1))
done <<< "$issues"
transfer_mutation+=" }"
comment_mutation+=" }"
if [[ "$comment_counter" -gt 1 ]]; then
gh api graphql -f query="$comment_mutation"
fi
new_issues=$(gh api graphql -f query="$transfer_mutation" --jq '.data | keys[] as $k | {url: .[$k].issue.url, id: .[$k].issue.id}')
repo_label="repository:$FROM_NAME"
gh label create $repo_label -R "$TO_REPO" --force
label_id=$(gh api /repos/$TO_REPO/labels/$repo_label --jq '.node_id')
label_mutation="mutation {"
label_counter=1
while IFS= read -r issue; do
id=$(echo "$issue" | jq -r '.id')
label_mutation+=" l${label_counter}: addLabelsToLabelable(input: {labelableId: \"$id\", labelIds: [\"$label_id\", \"$MIGRATION_LABEL_ID\"]}) { __typename }"
label_counter=$((label_counter+1))
done <<< "$new_issues"
label_mutation+=" }"
gh api graphql -f query="$label_mutation"
echo "$new_issues" | jq -r '.url'