-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer-github-issue-labels.sh
executable file
·56 lines (43 loc) · 2.46 KB
/
transfer-github-issue-labels.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
# This script uses the GitHub Labels REST API
# https://developer.github.com/v3/issues/labels/
# Source: https://douglascayers.com/2019/08/01/how-to-export-and-import-github-issue-labels-between-projects/
# Call this script via:
# bash transfer-github-issue-labels.sh PERSONAL_ACCESS_TOKEN gh_owner gh_repo_name
# Provide a personal access token that can
# access the source and target repositories.
# This is how you authorize with the GitHub API.
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
GH_TOKEN=$1
# The source repository whose labels to copy.
SRC_GH_USER=erikr
SRC_GH_REPO=dotfiles
# The target repository to add or update labels.
TGT_GH_USER=$2
TGT_GH_REPO=$3
# Headers used in curl commands
GH_ACCEPT_HEADER="Accept: application/vnd.github.v3+json"
GH_AUTH_HEADER="Authorization: token $GH_TOKEN"
# Bash for-loop over JSON array with jq
# https://starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/
sourceLabelsJson64=$(curl --silent -H "$GH_ACCEPT_HEADER" -H "$GH_AUTH_HEADER" https://api.github.com/repos/${SRC_GH_USER}/${SRC_GH_REPO}/labels | jq '[ .[] | { "name": .name, "color": .color, "description": .description } ]' | jq -r '.[] | @base64' )
# for each label from source repo,
# invoke github api to create or update
# the label in the target repo
for sourceLabelJson64 in $sourceLabelsJson64; do
# base64 decode the json
sourceLabelJson=$(echo ${sourceLabelJson64} | base64 --decode | jq -r '.')
# try to create the label
# POST /repos/:owner/:repo/labels { name, color, description }
# https://developer.github.com/v3/issues/labels/#create-a-label
createLabelResponse=$(echo $sourceLabelJson | curl --silent -X POST -d @- -H "$GH_ACCEPT_HEADER" -H "$GH_AUTH_HEADER" https://api.github.com/repos/${TGT_GH_USER}/${TGT_GH_REPO}/labels)
# if creation failed then the response doesn't include an id and jq returns 'null'
createdLabelId=$(echo $createLabelResponse | jq -r '.id')
# if label wasn't created maybe it's because it already exists, try to update it
if [ "$createdLabelId" == "null" ]
then
updateLabelResponse=$(echo $sourceLabelJson | curl --silent -X PATCH -d @- -H "$GH_ACCEPT_HEADER" -H "$GH_AUTH_HEADER" https://api.github.com/repos/${TGT_GH_USER}/${TGT_GH_REPO}/labels/$(echo $sourceLabelJson | jq -r '.name | @uri'))
echo "Update label response:\n"$updateLabelResponse"\n"
else
echo "Create label response:\n"$createLabelResponse"\n"
fi
done