Skip to content

Commit 936714f

Browse files
committed
refactor: standardize shebang and improve GITHUB_TOKEN validation across scripts
1 parent 42a6622 commit 936714f

5 files changed

Lines changed: 94 additions & 45 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env /bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44
###
@@ -16,9 +16,9 @@ if [ -z "${GITHUB_TOKEN}" ]; then
1616
fi
1717

1818
# 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")
19+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
2020

21-
if [ "$RESPONSE" -ne 200 ]; then
21+
if [ "${RESPONSE}" -ne 200 ]; then
2222
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
2323
exit 1
2424
fi
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44
###
@@ -8,18 +8,18 @@ GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ENTERPRISE=${ENTERPRISE:-''}
99
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
1010

11-
if [ -z "${GITHUB_TOKEN}" ]
12-
then
13-
echo "GITHUB_TOKEN is empty. Please set your token and try again"
14-
exit 1
11+
# Check if GITHUB_TOKEN is set
12+
if [ -z "${GITHUB_TOKEN}" ]; then
13+
echo "GITHUB_TOKEN is empty. Please set your token and try again"
14+
exit 1
1515
fi
1616

1717
get_licenses () {
1818
PAYLOAD=$(curl -s -L -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "${API_URL_PREFIX}/enterprises/${ENTERPRISE}/consumed-licenses")
19-
SEATS_CONSUMED=$(echo "$PAYLOAD" | jq -r .total_seats_consumed)
20-
SEATS_PURCHASED=$(echo "$PAYLOAD" | jq -r .total_seats_purchased)
21-
echo "Total seats consumed: $SEATS_CONSUMED"
22-
echo "Total seats purchased: $SEATS_PURCHASED"
19+
SEATS_CONSUMED=$(echo "${PAYLOAD}" | jq -r .total_seats_consumed)
20+
SEATS_PURCHASED=$(echo "${PAYLOAD}" | jq -r .total_seats_purchased)
21+
echo "Total seats consumed: ${SEATS_CONSUMED}"
22+
echo "Total seats purchased: ${SEATS_PURCHASED}"
2323
}
2424

2525
get_licenses

‎github-import-repo/github-import-repo.sh‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env /bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44
###
@@ -8,16 +8,29 @@ GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
99
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
1010
GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'}
11-
SRC_REPO=$1
12-
DEST_REPO=$2
11+
SRC_REPO=${1:-''}
12+
DEST_REPO=${2:-''}
1313
OWNER_USERNAME=${OWNER_USERNAME:-''}
1414

15+
# Check if GITHUB_TOKEN is set
16+
if [ -z "${GITHUB_TOKEN}" ]; then
17+
echo "GITHUB_TOKEN is empty. Please set your token and try again"
18+
exit 1
19+
fi
20+
21+
# Validate GITHUB_TOKEN by calling GitHub API
22+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
23+
24+
if [ "${RESPONSE}" -ne 200 ]; then
25+
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
26+
exit 1
27+
fi
1528

1629
# Create repo
17-
curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.nebula-preview+json" "${API_URL_PREFIX}/orgs/${ORG}/repos" -d '{"name":"'${DEST_REPO}'", "visibility":"internal"}';
30+
curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.nebula-preview+json" "${API_URL_PREFIX}/orgs/${ORG}/repos" -d '{"name":"'"${DEST_REPO}"'", "visibility":"internal"}'
1831

1932
# Grant Admin permissions on new repo
20-
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/repos/${ORG}/${DEST_REPO}/collaborators/${OWNER_USERNAME}" -d '{"permission":"admin"}';
33+
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/repos/${ORG}/${DEST_REPO}/collaborators/${OWNER_USERNAME}" -d '{"permission":"admin"}'
2134

2235
# Clone old repo locally
2336
git clone --bare "${GIT_URL_PREFIX}/${ORG}/${SRC_REPO}.git"

‎github-monthly-issues-report/github-monthly-issues-report.sh‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env /bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44
###
@@ -11,6 +11,20 @@ API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
1111
MONTH_START=${MONTH_START:-''}
1212
MONTH_END=${MONTH_END:-''}
1313

14+
# Check if GITHUB_TOKEN is set
15+
if [ -z "${GITHUB_TOKEN}" ]; then
16+
echo "GITHUB_TOKEN is empty. Please set your token and try again"
17+
exit 1
18+
fi
19+
20+
# Validate GITHUB_TOKEN by calling GitHub API
21+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
22+
23+
if [ "${RESPONSE}" -ne 200 ]; then
24+
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
25+
exit 1
26+
fi
27+
1428
get_public_pagination () {
1529
public_pages=$(curl -H "Authorization: token ${GITHUB_TOKEN}" -I "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues?state=all&labels=Linked%20[AC]&per_page=100" | grep -Eo '&page=[0-9]+' | grep -Eo '[0-9]+' | tail -1;)
1630
echo "${public_pages:-1}"
@@ -22,15 +36,15 @@ limit_public_pagination () {
2236

2337
repo_issues () {
2438
for PAGE in $(limit_public_pagination); do
25-
for i in $(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues?state=all&labels=Linked%20[AC]&page=${PAGE}&per_page=100" | jq -r 'map(select(.created_at | . >= "'$MONTH_START'T00:00" and . <= "'$MONTH_END'T23:59")) | sort_by(.number) | .[] | .number'); do
39+
for i in $(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues?state=all&labels=Linked%20[AC]&page=${PAGE}&per_page=100" | jq -r 'map(select(.created_at | . >= "'"${MONTH_START}"'T00:00" and . <= "'"${MONTH_END}"'T23:59")) | sort_by(.number) | .[] | .number'); do
2640
ISSUE_PAYLOAD=$(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues/${i}" -H "Accept: application/vnd.github.mercy-preview+json")
2741
ISSUE_TIMELINE_PAYLOAD=$(curl -H "Authorization: token ${GITHUB_TOKEN}" -s "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues/${i}/timeline" -H "Accept: application/vnd.github.mockingbird-preview+json" | jq -r '.[] | select(.label.name=="Linked [AC]" or .label.name=="linked")')
2842

29-
ISSUE_AUTHOR=$(echo "$ISSUE_PAYLOAD" | jq -r .user.login)
30-
ISSUE_TITLE=$(echo "$ISSUE_PAYLOAD" | jq -r .title | tr '"' "'")
31-
ISSUE_HTML_URL=$(echo "$ISSUE_PAYLOAD" | jq -r .html_url)
43+
ISSUE_AUTHOR=$(echo "${ISSUE_PAYLOAD}" | jq -r .user.login)
44+
ISSUE_TITLE=$(echo "${ISSUE_PAYLOAD}" | jq -r .title | tr '"' "'")
45+
ISSUE_HTML_URL=$(echo "${ISSUE_PAYLOAD}" | jq -r .html_url)
3246

33-
ISSUE_TIMELINE_LABELED_BY=$(echo "$ISSUE_TIMELINE_PAYLOAD" | jq -s 'first(.[]| .actor.login)' | jq -r)
47+
ISSUE_TIMELINE_LABELED_BY=$(echo "${ISSUE_TIMELINE_PAYLOAD}" | jq -s 'first(.[]| .actor.login)' | jq -r)
3448

3549
cat >> test.json << EOF
3650
{
@@ -46,26 +60,26 @@ EOF
4660
}
4761

4862
author_json () {
49-
AUTHORS=$(cat test.json| jq -r '.author' | sort | uniq -c | awk -F " " '{print "{\"author\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .author)
63+
AUTHORS=$(cat test.json | jq -r '.author' | sort | uniq -c | awk -F " " '{print "{\"author\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .author)
5064
for AUTHOR in ${AUTHORS}; do
51-
TEST_PAYLOAD=$(cat test.json| jq -r '.author' | sort | uniq -c | awk -F " " '{print "{\"author\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .)
52-
TEST_PAYLOAD_AUTHOR=$(echo "$TEST_PAYLOAD" | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .author')
53-
TEST_PAYLOAD_AUTHOR_COUNT=$(echo "$TEST_PAYLOAD" | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .count')
65+
TEST_PAYLOAD=$(cat test.json | jq -r '.author' | sort | uniq -c | awk -F " " '{print "{\"author\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .)
66+
TEST_PAYLOAD_AUTHOR=$(echo "${TEST_PAYLOAD}" | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .author')
67+
TEST_PAYLOAD_AUTHOR_COUNT=$(echo "${TEST_PAYLOAD}" | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .count')
5468
#TEST_PAYLOAD_AUTHOR_ISSUE_TITLE=$(cat test.json | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .title')
5569
TEST_PAYLOAD_AUTHOR_ISSUE_URL=$(cat test.json | jq -r --arg AUTHOR "${AUTHOR}" 'select(.author==$AUTHOR) | .title, .issue_url')
56-
echo -e "<a href="https://github.com/${TEST_PAYLOAD_AUTHOR}">${TEST_PAYLOAD_AUTHOR}</a> - ${TEST_PAYLOAD_AUTHOR_COUNT}"
70+
echo -e "<a href=\"https://github.com/${TEST_PAYLOAD_AUTHOR}\">${TEST_PAYLOAD_AUTHOR}</a> - ${TEST_PAYLOAD_AUTHOR_COUNT}"
5771
done | sort -n -k 4,4 -r >> output.txt
5872
}
5973

6074
contributor_json () {
61-
CONTRIBUTORS=$(cat test.json| jq -r '.contributor' | sort | uniq -c | awk -F " " '{print "{\"contributor\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .contributor)
75+
CONTRIBUTORS=$(cat test.json | jq -r '.contributor' | sort | uniq -c | awk -F " " '{print "{\"contributor\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .contributor)
6276
for CONTRIBUTOR in ${CONTRIBUTORS}; do
63-
TEST_PAYLOAD=$(cat test.json| jq -r '.contributor' | sort | uniq -c | awk -F " " '{print "{\"contributor\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .)
64-
TEST_PAYLOAD_CONTRIBUTOR=$(echo "$TEST_PAYLOAD" | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .contributor')
65-
TEST_PAYLOAD_CONTRIBUTOR_COUNT=$(echo "$TEST_PAYLOAD" | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .count')
77+
TEST_PAYLOAD=$(cat test.json | jq -r '.contributor' | sort | uniq -c | awk -F " " '{print "{\"contributor\":""\""$2"\""",\"count\":" $1"}"}' | jq -r .)
78+
TEST_PAYLOAD_CONTRIBUTOR=$(echo "${TEST_PAYLOAD}" | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .contributor')
79+
TEST_PAYLOAD_CONTRIBUTOR_COUNT=$(echo "${TEST_PAYLOAD}" | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .count')
6680
#TEST_PAYLOAD_CONTRIBUTOR_ISSUE_TITLE=$(cat test.json | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .title' )
6781
TEST_PAYLOAD_CONTRIBUTOR_ISSUE_URL=$(cat test.json | jq -r --arg CONTRIBUTOR "${CONTRIBUTOR}" 'select(.contributor==$CONTRIBUTOR) | .issue_url')
68-
echo -e "<a href="https://github.com/${TEST_PAYLOAD_CONTRIBUTOR}">${TEST_PAYLOAD_CONTRIBUTOR}</a> - ${TEST_PAYLOAD_CONTRIBUTOR_COUNT}"
82+
echo -e "<a href=\"https://github.com/${TEST_PAYLOAD_CONTRIBUTOR}\">${TEST_PAYLOAD_CONTRIBUTOR}</a> - ${TEST_PAYLOAD_CONTRIBUTOR_COUNT}"
6983
done | sort -n -k 4,4 -r >> output.txt
7084
rm -Rf test.json
7185
}
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env /bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44
###
@@ -8,34 +8,56 @@ GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
99
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
1010
TEMPLATE_REPO=${TEMPLATE_REPO:-''}
11-
REPO_NAME=$1
11+
REPO_NAME=${1:-''}
1212
CD_USERNAME=${CD_USERNAME:-''}
1313
CD_GITHUB_TOKEN=${CD_GITHUB_TOKEN:-''}
1414
REPO_ADMIN=${REPO_ADMIN:-'admins example'}
1515
REPO_WRITE=${REPO_WRITE:-'write example2'}
1616

17-
# Check if GITHUB_TOKEN is empty
18-
if [ -z "${GITHUB_TOKEN}" ]
19-
then
20-
echo "GITHUB_TOKEN is empty. Please set your token and try again"
21-
exit 1
17+
# Check if GITHUB_TOKEN is set
18+
if [ -z "${GITHUB_TOKEN}" ]; then
19+
echo "GITHUB_TOKEN is empty. Please set your token and try again"
20+
exit 1
21+
fi
22+
23+
# Validate GITHUB_TOKEN by calling GitHub API
24+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
25+
26+
if [ "${RESPONSE}" -ne 200 ]; then
27+
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
28+
exit 1
29+
fi
30+
31+
# Check if CD_GITHUB_TOKEN is set
32+
if [ -z "${CD_GITHUB_TOKEN}" ]; then
33+
echo "CD_GITHUB_TOKEN is empty. Please set your token and try again"
34+
exit 1
35+
fi
36+
37+
# Validate CD_GITHUB_TOKEN by calling GitHub API
38+
CD_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${CD_GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
39+
40+
if [ "${CD_RESPONSE}" -ne 200 ]; then
41+
echo "Error: CD_GITHUB_TOKEN is invalid or does not have required permissions."
42+
exit 1
2243
fi
2344

2445
# Create repo from template
25-
curl -s -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.baptiste-preview+json" "${API_URL_PREFIX}/repos/${ORG}/${TEMPLATE_REPO}/generate" -d '{"name":"'${REPO_NAME}'", "owner":"'${ORG}'", "private":true, "include_all_branches":true}';
46+
curl -s -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.baptiste-preview+json" "${API_URL_PREFIX}/repos/${ORG}/${TEMPLATE_REPO}/generate" -d '{"name":"'"${REPO_NAME}"'", "owner":"'"${ORG}"'", "private":true, "include_all_branches":true}'
2647

2748
# Give internal teams permissions on the new repo
2849
for ADMIN_TEAM in ${REPO_ADMIN}; do
29-
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${ADMIN_TEAM}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"admin"}';
50+
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${ADMIN_TEAM}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"admin"}'
3051
done
3152

3253
for WRITE_TEAM in ${REPO_WRITE}; do
33-
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${WRITE_TEAM}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"push"}';
54+
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${WRITE_TEAM}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"push"}'
3455
done
56+
3557
# Give cd user write permissions on the new repo
36-
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/repos/${ORG}/${REPO_NAME}/collaborators/${CD_USERNAME}" -d '{"permission":"push"}';
58+
curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/repos/${ORG}/${REPO_NAME}/collaborators/${CD_USERNAME}" -d '{"permission":"push"}'
3759

3860
# Accept the invite automatically
3961
CD_INVITES=$(curl -s -H "Authorization: token ${CD_GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/user/repository_invitations")
40-
CD_INVITES_INVITE_ID=$(echo "$CD_INVITES" | jq -r --arg REPO_NAME "${REPO_NAME}" 'select(.[].repository.name==$REPO_NAME) | .[].id')
62+
CD_INVITES_INVITE_ID=$(echo "${CD_INVITES}" | jq -r --arg REPO_NAME "${REPO_NAME}" 'select(.[].repository.name==$REPO_NAME) | .[].id')
4163
curl -s -X PATCH -H "Authorization: token ${CD_GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/user/repository_invitations/${CD_INVITES_INVITE_ID}"

0 commit comments

Comments
 (0)