Skip to content

Commit bcc31bb

Browse files
authored
Update github-add-repo-admin.sh (#9)
1 parent dedf034 commit bcc31bb

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

‎github-add-repo-admin/github-add-repo-admin.sh‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ ORG=${ORG:-''}
99
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
1010
REPO_ADMIN=${REPO_ADMIN:-''}
1111

12+
# Check if GITHUB_TOKEN is set
13+
if [ -z "${GITHUB_TOKEN}" ]; then
14+
echo "GITHUB_TOKEN is empty. Please set your token and try again"
15+
exit 1
16+
fi
17+
18+
# Validate GITHUB_TOKEN by calling GitHub API
19+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" "${API_URL_PREFIX}/user")
20+
21+
if [ "$RESPONSE" -ne 200 ]; then
22+
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
23+
exit 1
24+
fi
25+
1226
get_repo_pagination () {
1327
repo_pages=$(curl -H "Authorization: token ${GITHUB_TOKEN}" -I "${API_URL_PREFIX}/orgs/${ORG}/repos?per_page=100" | grep -Eo '&page=[0-9]+' | grep -Eo '[0-9]+' | tail -1;)
1428
echo "${repo_pages:-1}"
@@ -23,8 +37,13 @@ process_repos () {
2337

2438
for i in $(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "${API_URL_PREFIX}/orgs/${ORG}/repos?page=${PAGE}&per_page=100&sort=full_name" | jq -r 'sort_by(.name) | .[] | .name'); do
2539

40+
echo "processing repo ${i}"
41+
2642
# Give internal repo admin team permissions on the repo
2743
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${REPO_ADMIN}/repos/${ORG}/${i}" -d '{"permission":"admin"}';
44+
45+
# Add delay to prevent hitting github rate limit
46+
sleep 5
2847

2948
done
3049
done

0 commit comments

Comments
 (0)