Skip to content

Commit 388a364

Browse files
committed
Refactor scripts to use shared utility functions for environment variable validation and token validation
- Introduced a new library `github-common.sh` containing common functions for validating environment variables and commands, as well as token validation. - Updated multiple scripts to source the new library and replace manual checks with calls to the new utility functions. - Removed redundant color code definitions and print functions from individual scripts, centralizing them in the shared library. - Ensured all scripts validate required environment variables and commands before execution, improving maintainability and readability.
1 parent 0ff5a95 commit 388a364

16 files changed

Lines changed: 237 additions & 523 deletions

File tree

‎github-add-enterprise-team-read-permissions/github-add-enterprise-team-read-permissions.sh‎

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=../lib/github-common.sh
6+
source "${SCRIPT_DIR}/../lib/github-common.sh"
7+
48
###
59
## GitHub Enterprise Team Org Role Assignment
610
## Assigns the built-in "All-repository read" organization role to the
@@ -22,54 +26,11 @@ ENTERPRISE_TEAM_SLUG=${ENTERPRISE_TEAM_SLUG:-''}
2226
# API name of the built-in org role that grants read on all repositories
2327
ALL_REPO_READ_ROLE_NAME=${ALL_REPO_READ_ROLE_NAME:-'all_repo_read'}
2428

25-
###
26-
## Color codes for output
27-
###
28-
RED='\033[0;31m'
29-
GREEN='\033[0;32m'
30-
YELLOW='\033[1;33m'
31-
BLUE='\033[0;34m'
32-
NC='\033[0m'
33-
34-
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
35-
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
36-
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
37-
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
38-
39-
# Check if GITHUB_TOKEN is set
40-
if [ -z "${GITHUB_TOKEN}" ]; then
41-
print_error "GITHUB_TOKEN is empty. Please set your token and try again"
42-
exit 1
43-
fi
44-
45-
# Check if ENTERPRISE is set
46-
if [ -z "${ENTERPRISE}" ]; then
47-
print_error "ENTERPRISE is empty. Please set the enterprise slug and try again"
48-
exit 1
49-
fi
50-
51-
# Check if ENTERPRISE_TEAM_SLUG is set
52-
if [ -z "${ENTERPRISE_TEAM_SLUG}" ]; then
53-
print_error "ENTERPRISE_TEAM_SLUG is empty. Please set the enterprise team slug and try again"
54-
exit 1
55-
fi
56-
57-
# Check if jq is installed
58-
if ! command -v jq &> /dev/null; then
59-
print_error "jq is required but not installed. Install it and try again."
60-
exit 1
61-
fi
62-
63-
print_status "Validating GitHub token..."
64-
65-
# Validate GITHUB_TOKEN by calling GitHub API
66-
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" "${API_URL_PREFIX}/user")
67-
68-
if [ "$RESPONSE" -ne 200 ]; then
69-
print_error "GITHUB_TOKEN is invalid or does not have required permissions."
70-
exit 1
71-
fi
72-
29+
require_env_var GITHUB_TOKEN "GitHub token"
30+
require_env_var ENTERPRISE "GitHub enterprise slug"
31+
require_env_var ENTERPRISE_TEAM_SLUG "Enterprise team slug"
32+
require_command jq
33+
validate_github_token
7334
print_success "GitHub token validated successfully"
7435
print_status "Enterprise: ${ENTERPRISE}"
7536
print_status "Enterprise team slug: ${ENTERPRISE_TEAM_SLUG}"

‎github-add-repo-collaborators-by-pattern/github-add-repo-collaborators-by-pattern.sh‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=../lib/github-common.sh
6+
source "${SCRIPT_DIR}/../lib/github-common.sh"
7+
48
###
59
## GLOBAL VARIABLES
610
###
@@ -31,23 +35,12 @@ limit_repo_pagination() {
3135
}
3236

3337
validate() {
34-
if [ -z "${GITHUB_TOKEN}" ] || [ -z "${ORG}" ] || [ -z "${COLLABORATORS}" ] || [ -z "${REPO_NAME_REGEX}" ]; then
35-
echo "Error: missing required variable(s)."
36-
usage
37-
exit 1
38-
fi
39-
40-
if ! command -v jq > /dev/null 2>&1; then
41-
echo "Error: jq is required but not installed."
42-
exit 1
43-
fi
44-
45-
local auth_status
46-
auth_status=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
47-
if [ "${auth_status}" -ne 200 ]; then
48-
echo "Error: invalid GITHUB_TOKEN or insufficient permissions (HTTP ${auth_status})."
49-
exit 1
50-
fi
38+
require_env_var GITHUB_TOKEN "GitHub token"
39+
require_env_var ORG "GitHub organization"
40+
require_env_var COLLABORATORS "Collaborators list"
41+
require_env_var REPO_NAME_REGEX "Repo name regex"
42+
require_command jq
43+
validate_github_token
5144
}
5245

5346
get_matching_repos() {

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

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=../lib/github-common.sh
6+
source "${SCRIPT_DIR}/../lib/github-common.sh"
7+
48
### GLOBAL VARIABLES
59
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
610
ORG=${ORG:-''}
@@ -14,33 +18,9 @@ REPO_PUSH=${REPO_PUSH:-''}
1418
REPO_TRIAGE=${REPO_TRIAGE:-''}
1519
REPO_PULL=${REPO_PULL:-''}
1620

17-
RED='\033[0;31m'
18-
GREEN='\033[0;32m'
19-
YELLOW='\033[1;33m'
20-
BLUE='\033[0;34m'
21-
NC='\033[0m'
22-
23-
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
24-
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
25-
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
26-
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
27-
28-
# Check if GITHUB_TOKEN is set
29-
if [ -z "${GITHUB_TOKEN}" ]; then
30-
print_error "GITHUB_TOKEN is empty. Please set your token and try again"
31-
exit 1
32-
fi
33-
34-
# Check if ORG is set
35-
if [ -z "${ORG}" ]; then
36-
print_error "ORG is empty. Please set your organization and try again"
37-
exit 1
38-
fi
39-
40-
if ! command -v jq > /dev/null 2>&1; then
41-
print_error "jq is not installed. Please install jq and try again"
42-
exit 1
43-
fi
21+
require_env_var GITHUB_TOKEN "GitHub token"
22+
require_env_var ORG "GitHub organization"
23+
require_command jq
4424

4525
# Check if at least one permission level is set
4626
if [ -z "${REPO_ADMIN}" ] && [ -z "${REPO_MAINTAIN}" ] && [ -z "${REPO_PUSH}" ] && [ -z "${REPO_TRIAGE}" ] && [ -z "${REPO_PULL}" ]; then
@@ -49,13 +29,7 @@ if [ -z "${REPO_ADMIN}" ] && [ -z "${REPO_MAINTAIN}" ] && [ -z "${REPO_PUSH}" ]
4929
exit 1
5030
fi
5131

52-
# Validate GITHUB_TOKEN by calling GitHub API
53-
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
54-
55-
if [ "${RESPONSE}" -ne 200 ]; then
56-
print_error "GITHUB_TOKEN is invalid or does not have required permissions."
57-
exit 1
58-
fi
32+
validate_github_token
5933

6034
print_status "Organization: ${ORG}"
6135

‎github-archive-old-repos/github-archive-old-repos.sh‎

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
set -euo pipefail
99

10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
# shellcheck source=../lib/github-common.sh
12+
source "${SCRIPT_DIR}/../lib/github-common.sh"
13+
1014
###
1115
## CONFIGURATION
1216
###
@@ -19,32 +23,9 @@ REPORT_DIR="$(dirname "$0")/reports"
1923
REPORT_FILE="${REPORT_DIR}/old_repos_${TIMESTAMP}.csv"
2024
TEMP_FILE=$(mktemp)
2125

22-
# Color codes for output
23-
RED='\033[0;31m'
24-
GREEN='\033[0;32m'
25-
YELLOW='\033[1;33m'
26-
BLUE='\033[0;34m'
27-
NC='\033[0m' # No Color
28-
2926
###
3027
## HELPER FUNCTIONS
3128
###
32-
print_status() {
33-
echo -e "${BLUE}[INFO]${NC} $1"
34-
}
35-
36-
print_success() {
37-
echo -e "${GREEN}[SUCCESS]${NC} $1"
38-
}
39-
40-
print_warning() {
41-
echo -e "${YELLOW}[WARNING]${NC} $1"
42-
}
43-
44-
print_error() {
45-
echo -e "${RED}[ERROR]${NC} $1"
46-
}
47-
4829
cleanup() {
4930
rm -f "$TEMP_FILE"
5031
}
@@ -56,35 +37,10 @@ trap cleanup EXIT
5637
###
5738
validate_environment() {
5839
print_status "Validating environment..."
59-
60-
# Check if GITHUB_TOKEN is set
61-
if [ -z "${GITHUB_TOKEN}" ]; then
62-
print_error "GITHUB_TOKEN is empty. Please set your token and try again"
63-
exit 1
64-
fi
65-
66-
if [ -z "${ORG}" ]; then
67-
print_error "ORG is empty. Please set ORG and try again"
68-
exit 1
69-
fi
70-
71-
# Check if jq is installed
72-
if ! command -v jq &> /dev/null; then
73-
print_error "jq is not installed. Please install jq for JSON parsing."
74-
print_status "Install on macOS: brew install jq"
75-
print_status "Install on Ubuntu/Debian: sudo apt-get install jq"
76-
exit 1
77-
fi
78-
79-
# Validate GITHUB_TOKEN by calling GitHub API
80-
print_status "Validating GitHub token..."
81-
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GITHUB_TOKEN" "${API_URL_PREFIX}/user")
82-
83-
if [ "$RESPONSE" -ne 200 ]; then
84-
print_error "GITHUB_TOKEN is invalid or does not have required permissions."
85-
exit 1
86-
fi
87-
40+
require_env_var GITHUB_TOKEN "GitHub token"
41+
require_env_var ORG "GitHub organization"
42+
require_command jq
43+
validate_github_token
8844
print_success "Environment validation complete"
8945
}
9046

‎github-auto-repo-creation/github-auto-repo-creation.sh‎

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=../lib/github-common.sh
6+
source "${SCRIPT_DIR}/../lib/github-common.sh"
7+
48
###
59
## GLOBAL VARIABLES - Set default values for the required environment variables
610
###
@@ -13,41 +17,12 @@ REPO_NAMES=${REPO_NAMES:-''}
1317
ADMIN_TEAMS=${ADMIN_TEAMS:-''}
1418
REPO_OWNERS=${REPO_OWNERS:-''}
1519

16-
if [ -z "${GITHUB_TOKEN}" ]
17-
then
18-
echo "GITHUB_TOKEN is empty. Please set your token and try again"
19-
exit 1
20-
fi
21-
22-
if [ -z "${ORG}" ]
23-
then
24-
echo "ORG is empty. Please set ORG and try again"
25-
exit 1
26-
fi
27-
28-
if [ -z "${REPO_NAMES}" ]
29-
then
30-
echo "REPO_NAMES is empty. Please set at least one repository name and try again"
31-
exit 1
32-
fi
33-
34-
if [ -z "${REPO_OWNERS}" ]
35-
then
36-
echo "REPO_OWNERS is empty. Please set at least one repository owner team and try again"
37-
exit 1
38-
fi
39-
40-
if ! command -v base64 > /dev/null 2>&1; then
41-
echo "base64 is not installed. Please install base64 and try again"
42-
exit 1
43-
fi
44-
45-
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_URL_PREFIX}/user")
46-
47-
if [ "${RESPONSE}" -ne 200 ]; then
48-
echo "Error: GITHUB_TOKEN is invalid or does not have required permissions."
49-
exit 1
50-
fi
20+
require_env_var GITHUB_TOKEN "GitHub token"
21+
require_env_var ORG "GitHub organization"
22+
require_env_var REPO_NAMES "Repo names list"
23+
require_env_var REPO_OWNERS "Repo owners list"
24+
require_command base64
25+
validate_github_token
5126

5227
# Define the content of the CODEOWNERS file
5328
CODEOWNERS_CONTENT=$(cat << EOF

‎github-close-archived-repo-security-alerts/github-close-archived-repo-security-alerts.sh‎

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=../lib/github-common.sh
6+
source "${SCRIPT_DIR}/../lib/github-common.sh"
7+
48
###
59
## GitHub Close Security Alerts
610
## Dismisses or resolves all open security alerts across all repositories
@@ -41,20 +45,6 @@ SECRET_SCANNING_RESOLUTION=${SECRET_SCANNING_RESOLUTION:-'wont_fix'} # false_po
4145
ALERT_TYPE='all' # default, can be overridden by --type flag
4246
DRY_RUN=false
4347

44-
###
45-
## Color codes for output
46-
###
47-
RED='\033[0;31m'
48-
GREEN='\033[0;32m'
49-
YELLOW='\033[1;33m'
50-
BLUE='\033[0;34m'
51-
NC='\033[0m'
52-
53-
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
54-
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
55-
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
56-
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
57-
5848
###
5949
## ARGUMENT PARSING
6050
###
@@ -88,33 +78,10 @@ esac
8878
###
8979
## VALIDATION
9080
###
91-
if [ -z "${GITHUB_TOKEN}" ]; then
92-
print_error "GITHUB_TOKEN is empty. Please set your token and try again"
93-
exit 1
94-
fi
95-
96-
if [ -z "${ORG}" ]; then
97-
print_error "ORG is empty. Please set the organization name and try again"
98-
exit 1
99-
fi
100-
101-
if ! command -v jq &> /dev/null; then
102-
print_error "jq is not installed. Please install jq for JSON parsing."
103-
print_status "Install on macOS: brew install jq"
104-
print_status "Install on Ubuntu/Debian: sudo apt-get install jq"
105-
exit 1
106-
fi
107-
108-
print_status "Validating GitHub token..."
109-
TOKEN_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
110-
-H "Authorization: token ${GITHUB_TOKEN}" \
111-
"${API_URL_PREFIX}/user")
112-
113-
if [ "${TOKEN_STATUS}" -ne 200 ]; then
114-
print_error "GITHUB_TOKEN is invalid or does not have required permissions (HTTP ${TOKEN_STATUS})."
115-
exit 1
116-
fi
117-
81+
require_env_var GITHUB_TOKEN "GitHub token"
82+
require_env_var ORG "GitHub organization"
83+
require_command jq
84+
validate_github_token
11885
print_success "GitHub token validated"
11986
print_status "Organization : ${ORG}"
12087
print_status "Alert type : ${ALERT_TYPE}"

0 commit comments

Comments
 (0)