Skip to content

Commit bb511bd

Browse files
committed
feat: enhance error handling by adding informative messages for missing environment variables
1 parent 936714f commit bb511bd

5 files changed

Lines changed: 106 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
###
5-
## GLOBAL VARIABLES
5+
# GLOBAL VARIABLES
66
###
77
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
@@ -15,6 +15,18 @@ if [ -z "${GITHUB_TOKEN}" ]; then
1515
exit 1
1616
fi
1717

18+
# Check if ORG is set
19+
if [ -z "${ORG}" ]; then
20+
echo "ORG is empty. Please set your organization and try again"
21+
exit 1
22+
fi
23+
24+
# Check if REPO_ADMIN is set
25+
if [ -z "${REPO_ADMIN}" ]; then
26+
echo "REPO_ADMIN is empty. Please set your team slug and try again"
27+
exit 1
28+
fi
29+
1830
# Validate GITHUB_TOKEN by calling GitHub API
1931
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
2032

@@ -24,7 +36,7 @@ if [ "${RESPONSE}" -ne 200 ]; then
2436
fi
2537

2638
get_repo_pagination () {
27-
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;)
39+
repo_pages=$(curl -s -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;)
2840
echo "${repo_pages:-1}"
2941
}
3042

@@ -35,12 +47,12 @@ limit_repo_pagination () {
3547
process_repos () {
3648
for PAGE in $(limit_repo_pagination); do
3749

38-
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
50+
for i in $(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/orgs/${ORG}/repos?page=${PAGE}&per_page=100&sort=full_name" | jq -r 'sort_by(.name) | .[] | .name'); do
3951

4052
echo "processing repo ${i}"
4153

4254
# Give internal repo admin team permissions on the repo
43-
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"}';
55+
curl -s -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"}';
4456

4557
# Add delay to prevent hitting github rate limit
4658
sleep 5

‎github-get-consumed-licenses/github-get-consumed-licenses.sh‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
###
5-
## GLOBAL VARIABLES
5+
# GLOBAL VARIABLES
66
###
77
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ENTERPRISE=${ENTERPRISE:-''}
@@ -14,6 +14,12 @@ if [ -z "${GITHUB_TOKEN}" ]; then
1414
exit 1
1515
fi
1616

17+
# Check if ENTERPRISE is set
18+
if [ -z "${ENTERPRISE}" ]; then
19+
echo "ENTERPRISE is empty. Please set your enterprise and try again"
20+
exit 1
21+
fi
22+
1723
get_licenses () {
1824
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")
1925
SEATS_CONSUMED=$(echo "${PAYLOAD}" | jq -r .total_seats_consumed)

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
###
5-
## GLOBAL VARIABLES
5+
# GLOBAL VARIABLES
66
###
77
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
@@ -18,6 +18,30 @@ if [ -z "${GITHUB_TOKEN}" ]; then
1818
exit 1
1919
fi
2020

21+
# Check if ORG is set
22+
if [ -z "${ORG}" ]; then
23+
echo "ORG is empty. Please set your organization and try again"
24+
exit 1
25+
fi
26+
27+
# Check if SRC_REPO is set
28+
if [ -z "${SRC_REPO}" ]; then
29+
echo "SRC_REPO is empty. Please provide source repository as first argument"
30+
exit 1
31+
fi
32+
33+
# Check if DEST_REPO is set
34+
if [ -z "${DEST_REPO}" ]; then
35+
echo "DEST_REPO is empty. Please provide destination repository as second argument"
36+
exit 1
37+
fi
38+
39+
# Check if OWNER_USERNAME is set
40+
if [ -z "${OWNER_USERNAME}" ]; then
41+
echo "OWNER_USERNAME is empty. Please set your owner username and try again"
42+
exit 1
43+
fi
44+
2145
# Validate GITHUB_TOKEN by calling GitHub API
2246
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
2347

@@ -27,10 +51,10 @@ if [ "${RESPONSE}" -ne 200 ]; then
2751
fi
2852

2953
# Create repo
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"}'
54+
curl -s -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"}'
3155

3256
# Grant Admin permissions on new repo
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"}'
57+
curl -s -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"}'
3458

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

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
###
5-
## GLOBAL VARIABLES
5+
# GLOBAL VARIABLES
66
###
77
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
@@ -17,6 +17,30 @@ if [ -z "${GITHUB_TOKEN}" ]; then
1717
exit 1
1818
fi
1919

20+
# Check if ORG is set
21+
if [ -z "${ORG}" ]; then
22+
echo "ORG is empty. Please set your organization and try again"
23+
exit 1
24+
fi
25+
26+
# Check if REPO is set
27+
if [ -z "${REPO}" ]; then
28+
echo "REPO is empty. Please set your repository and try again"
29+
exit 1
30+
fi
31+
32+
# Check if MONTH_START is set
33+
if [ -z "${MONTH_START}" ]; then
34+
echo "MONTH_START is empty. Please set your start date (YYYY-MM-DD) and try again"
35+
exit 1
36+
fi
37+
38+
# Check if MONTH_END is set
39+
if [ -z "${MONTH_END}" ]; then
40+
echo "MONTH_END is empty. Please set your end date (YYYY-MM-DD) and try again"
41+
exit 1
42+
fi
43+
2044
# Validate GITHUB_TOKEN by calling GitHub API
2145
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
2246

@@ -26,7 +50,7 @@ if [ "${RESPONSE}" -ne 200 ]; then
2650
fi
2751

2852
get_public_pagination () {
29-
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;)
53+
public_pages=$(curl -s -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;)
3054
echo "${public_pages:-1}"
3155
}
3256

@@ -36,9 +60,9 @@ limit_public_pagination () {
3660

3761
repo_issues () {
3862
for PAGE in $(limit_public_pagination); 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
40-
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")
41-
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")')
63+
for i in $(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "${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
64+
ISSUE_PAYLOAD=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/repos/${ORG}/${REPO}/issues/${i}" -H "Accept: application/vnd.github.mercy-preview+json")
65+
ISSUE_TIMELINE_PAYLOAD=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "${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")')
4266

4367
ISSUE_AUTHOR=$(echo "${ISSUE_PAYLOAD}" | jq -r .user.login)
4468
ISSUE_TITLE=$(echo "${ISSUE_PAYLOAD}" | jq -r .title | tr '"' "'")

‎github-repo-from-template/github-repo-from-template.sh‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
###
5-
## GLOBAL VARIABLES
5+
# GLOBAL VARIABLES
66
###
77
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
88
ORG=${ORG:-''}
@@ -11,15 +11,39 @@ TEMPLATE_REPO=${TEMPLATE_REPO:-''}
1111
REPO_NAME=${1:-''}
1212
CD_USERNAME=${CD_USERNAME:-''}
1313
CD_GITHUB_TOKEN=${CD_GITHUB_TOKEN:-''}
14-
REPO_ADMIN=${REPO_ADMIN:-'admins example'}
15-
REPO_WRITE=${REPO_WRITE:-'write example2'}
14+
REPO_ADMIN=${REPO_ADMIN:-''}
15+
REPO_WRITE=${REPO_WRITE:-''}
1616

1717
# Check if GITHUB_TOKEN is set
1818
if [ -z "${GITHUB_TOKEN}" ]; then
1919
echo "GITHUB_TOKEN is empty. Please set your token and try again"
2020
exit 1
2121
fi
2222

23+
# Check if ORG is set
24+
if [ -z "${ORG}" ]; then
25+
echo "ORG is empty. Please set your organization and try again"
26+
exit 1
27+
fi
28+
29+
# Check if TEMPLATE_REPO is set
30+
if [ -z "${TEMPLATE_REPO}" ]; then
31+
echo "TEMPLATE_REPO is empty. Please set your template repository and try again"
32+
exit 1
33+
fi
34+
35+
# Check if REPO_NAME is set
36+
if [ -z "${REPO_NAME}" ]; then
37+
echo "REPO_NAME is empty. Please provide repository name as first argument"
38+
exit 1
39+
fi
40+
41+
# Check if CD_USERNAME is set
42+
if [ -z "${CD_USERNAME}" ]; then
43+
echo "CD_USERNAME is empty. Please set your CD username and try again"
44+
exit 1
45+
fi
46+
2347
# Validate GITHUB_TOKEN by calling GitHub API
2448
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
2549

0 commit comments

Comments
 (0)