-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgbr
executable file
·287 lines (237 loc) · 6.38 KB
/
gbr
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
set -eo pipefail
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
usage() {
echo "Rename the 'master' branch for an entire account at once."
echo ""
echo "This tool will find all repositories on GitHub belonging to an account that:"
echo ""
echo " - aren't forks"
echo " - aren't archived or disabled"
echo " - have their default branch set to 'master'"
echo ""
echo "It will then rename the master branch to 'main' by default, and set that as the"
echo "default branch on GitHub instead of 'master'."
echo ""
echo "By default, the 'master' branch will be left untouched to prevent any accidental"
echo "data loss. If you'd like to delete those branches, set the '--delete' flag."
echo ""
echo "You must choose to either do a dry run ('--dry-run') or force the changes"
echo "('--force'). You must also specify one of '--org' or '--user' (but not both) to"
echo "select the account to collect repositories from."
echo ""
echo "${BOLD}USAGE${NORMAL}"
echo " $(basename "$0") [flags]"
echo ""
echo "${BOLD}FLAGS${NORMAL}"
echo " -u, --user string The username to collect repositories in"
echo " -o, --org string The organization to collect repositories in"
echo " -t, --team string The team within the organization (optional)"
echo " -b, --new-branch string The new name for the master branch (defaults to"
echo " 'main')"
echo " -d, --delete Delete the master branch from GitHub"
echo " -n, --dry-run Report what will happen, without making any changes"
echo " on GitHub"
echo " -f, --force Run for real, making changes on GitHub"
echo " -l, --list Print a list of all GitHub repositories that would"
echo " be changed and exit (implies a dry run)"
exit 1
}
if [ $# -lt 2 ]; then
usage
fi
ORG=
TEAM=
USER=
NEW_BRANCH=main
DELETE=0
DRY_RUN=0
FORCE=0
LIST_ONLY=0
while [ $# -gt 0 ] ; do
case $1 in
-u | --user)
USER="$2"
;;
-o | --org)
ORG="$2"
;;
-t | --team)
TEAM="$2"
;;
-b | --new-branch)
NEW_BRANCH="$2"
;;
-d | --delete)
DELETE=1
;;
-n | --dry-run)
DRY_RUN=1
;;
-f | --force)
FORCE=1
;;
-l | --list)
LIST_ONLY=1
DRY_RUN=1
;;
esac
shift
done
if [ "$FORCE" -ne 1 ] && [ "$DRY_RUN" -ne 1 ]; then
usage
fi
if [ "$FORCE" -eq 1 ] && [ "$DRY_RUN" -eq 1 ]; then
usage
fi
if [ "$LIST_ONLY" -eq 1 ] && [ "$DRY_RUN" -ne 1 ]; then
usage
fi
if [ -z "$USER" ] && [ -z "$ORG" ]; then
usage
fi
if [ -n "$USER" ] && [ -n "$ORG" ]; then
usage
fi
if [ -z "$ORG" ] && [ -n "$TEAM" ]; then
usage
fi
if [ -n "$USER" ] && [ -n "$TEAM" ]; then
usage
fi
REPOS_ENDPOINT=
ACCOUNT=
if [ -n "$USER" ]; then
REPOS_ENDPOINT="users/$USER/repos"
ACCOUNT=$USER
elif [ -n "$ORG" ] && [ -n "$TEAM" ]; then
REPOS_ENDPOINT="orgs/$ORG/teams/$TEAM/repos"
ACCOUNT=$ORG
else
REPOS_ENDPOINT="orgs/$ORG/repos"
ACCOUNT=$ORG
fi
if [ -n "$TEAM" ]; then
echo "${BOLD}==> Collecting relevant repositories for '$ORG/$TEAM'...${NORMAL}"
else
echo "${BOLD}==> Collecting relevant repositories for '$ACCOUNT'...${NORMAL}"
fi
PARAMS="?per_page=100"
REPOS=
while [ -n "$PARAMS" ]; do
RESPONSE=$(gh api "$REPOS_ENDPOINT$PARAMS" --include)
BODY=$(echo "$RESPONSE" | tail -1)
REPOS+=$(
echo "$BODY" |
jq "
map(
select(
.fork == false and
.archived == false and
.disabled == false and
.default_branch == \"master\"
) |
{
id: .id,
name: .name,
full_name: .full_name
}
)[]
"
)
set +o pipefail
# shellcheck disable=SC1004
PARAMS=$(
echo "$RESPONSE" |
grep -F Link: |
sed 's/Link: //' |
sed 's/, /\
/g' |
grep -F 'rel="next"' |
grep -o '\?.*>' |
sed -e 's/>$//'
)
set -o pipefail
done
if [ -z "$REPOS" ]; then
echo "No relevant repositories found."
exit
fi
if [ "$LIST_ONLY" -eq 1 ]; then
echo "$REPOS" | jq -r '.full_name'
exit
fi
TMP_DIR=/tmp/gbr/$ACCOUNT
echo "${BOLD}==> (Re)creating '$TMP_DIR'...${NORMAL}"
rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"
cd "$TMP_DIR"
FAILED_RENAMES=()
FAILED_DELETIONS=()
for REPO in $(echo "$REPOS" | jq '.name' | sed 's/"//g'); do
{
echo "${BOLD}==> Updating '$ACCOUNT/$REPO'...${NORMAL}"
git clone "[email protected]:$ACCOUNT/$REPO.git" "$TMP_DIR/$REPO"
cd "$TMP_DIR/$REPO"
echo "${BOLD}Setting '$NEW_BRANCH' to point to the head of 'master'...${NORMAL}"
git checkout -B "$NEW_BRANCH" origin/master --no-track
if [ "$DRY_RUN" -ne 1 ]; then
git push --set-upstream origin "$NEW_BRANCH"
git remote set-head origin "$NEW_BRANCH"
fi
echo "${BOLD}Changing default branch to '$NEW_BRANCH'...${NORMAL}"
if [ "$DRY_RUN" -ne 1 ]; then
gh api repos/:owner/:repo \
--method PATCH \
--raw-field default_branch="$NEW_BRANCH" \
>/dev/null
fi
echo "${BOLD}Changing open pull request target branches to '$NEW_BRANCH'...${NORMAL}"
for PR in $(gh pr list --base master --limit 9999 | cut -f 1); do
if [ "$DRY_RUN" -ne 1 ]; then
gh api "repos/:owner/:repo/pulls/$PR" \
--method PATCH \
--raw-field base="$NEW_BRANCH" \
>/dev/null
fi
echo -n "."
done
echo ""
if [ "$DELETE" -eq 1 ]; then
echo "${BOLD}Deleting 'master'...${NORMAL}"
if [ "$DRY_RUN" -ne 1 ]; then
git push origin --delete master || FAILED_DELETIONS+=("$REPO")
fi
fi
} || FAILED_RENAMES+=("$REPO")
done
echo "Done!"
if [ ${#FAILED_RENAMES[@]} -gt 0 ]; then
echo ""
echo "${BOLD}NOTE${NORMAL}"
echo ""
echo "Failed to rename the 'master' branch for the following repositories:"
echo ""
for REPO in "${FAILED_RENAMES[@]}"; do
echo " $ACCOUNT/$REPO"
done
echo ""
echo "Manual intervention is required."
fi
if [ ${#FAILED_DELETIONS[@]} -gt 0 ]; then
echo ""
echo "${BOLD}NOTE${NORMAL}"
echo ""
echo "Failed to delete the 'master' branch from the following repositories:"
echo ""
for REPO in "${FAILED_DELETIONS[@]}"; do
echo " $ACCOUNT/$REPO"
done
echo ""
echo "Manual intervention is required."
fi
echo ""
echo "${BOLD}NOTE${NORMAL}"
echo ""
echo "Any CI or other code that relies on branch names will need to be updated manually."