-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_branches.sh
executable file
·76 lines (67 loc) · 1.53 KB
/
migrate_branches.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
#!/usr/bin/env bash
set -euo pipefail
function usage {
echo
echo "usage:"
echo
echo " OWNER=Jell SOURCE_BRANCH=master TARGET_BRANCH=main $0"
echo
}
if [[ ${OWNER:-""} == "" ]]
then
usage
echo "missing OWNER"
exit 1
fi
if [[ ${SOURCE_BRANCH:-""} == "" ]]
then
usage
echo "missing SOURCE_BRANCH"
exit 1
fi
if [[ ${TARGET_BRANCH:-""} == "" ]]
then
usage
echo "missing TARGET_BRANCH"
exit 1
fi
function get_repos_at_page {
# avoid rate limiting
sleep 0.5
PAGE="$1"
curl -fLsS \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: token $GITHUB_TOKEN" \
--get -d "per_page=100&page=${PAGE}" \
"https://api.github.com/user/repos" \
| jq -rc
}
function migrate_repos {
REPOS="$1"
for REPO in $(echo "$REPOS" | jq -r ".[] | select( .owner.login == \"$OWNER\" )
| select( .archived == false )
| select( .default_branch == \"$SOURCE_BRANCH\")
| .full_name")
do
echo "migrating ${REPO}..."
# avoid rate limiting
sleep 0.5
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${REPO}/branches/${SOURCE_BRANCH}/rename" \
-d "{\"new_name\":\"${TARGET_BRANCH}\"}"
done
}
PAGE=0
while :
do
PAGE=$((PAGE + 1))
REPOS=$(get_repos_at_page $PAGE)
migrate_repos "$REPOS"
if [[ $REPOS == "[]" ]]
then
break
fi
done