Skip to content

Commit 52acaba

Browse files
author
Patrick Lewis
committed
Enhance scripts with standardized header blocks, usage instructions, and environment variable details
1 parent 7f661a9 commit 52acaba

18 files changed

Lines changed: 459 additions & 115 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,57 @@ Provides reusable helpers sourced by scripts:
2626

2727
### Script Anatomy (Standardized Pattern)
2828

29-
All scripts follow this structure:
29+
All scripts **must** begin with a `# ===` header block, followed immediately by
30+
`set -euo pipefail`. Fill in the script name, description, usage, every
31+
environment variable the script reads (Required/Optional), and every external
32+
command it depends on.
3033

3134
```bash
3235
#!/bin/bash
36+
# =============================================================================
37+
# github-<script-name>.sh
38+
#
39+
# <One-paragraph description of what this script does.>
40+
#
41+
# Usage:
42+
# export GITHUB_TOKEN=ghp_yourtoken
43+
# export ORG=my-org
44+
# ./github-<script-name>.sh [options]
45+
#
46+
# Options: (omit this section if there are no CLI flags)
47+
# --dry-run Preview changes without making them
48+
#
49+
# Environment variables:
50+
# GITHUB_TOKEN Required. PAT with <scope> scope
51+
# ORG Required. GitHub organization name
52+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
53+
#
54+
# Requirements:
55+
# - curl
56+
# - jq
57+
# =============================================================================
58+
3359
set -euo pipefail # ALWAYS include: exit on error, undefined vars, pipe failures
3460

61+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
62+
# shellcheck source=../../lib/github-common.sh
63+
source "${SCRIPT_DIR}/../../lib/github-common.sh"
64+
3565
### GLOBAL VARIABLES
3666
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
3767
ORG=${ORG:-''}
3868
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
39-
GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'}
4069
# Script-specific required vars...
4170

42-
# Source shared library (when available)
43-
# SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
44-
# source "${SCRIPT_DIR}/../../lib/github-common.sh"
45-
4671
# Token validation block
4772
# Helper functions for pagination/processing
4873
# Main execution logic
4974
```
5075

51-
**Critical patterns:**
76+
**Rules:**
77+
- The `# ===` fence is exactly 79 characters.
78+
- Do **not** add a second `###` description block inside the body — all descriptive content belongs in the top header.
79+
- `set -euo pipefail` must be the first executable line after the header.
5280

5381
## GitHub API Patterns
5482

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-add-enterprise-team-read-permissions.sh
4+
#
5+
# Assigns the built-in "All-repository read" organisation role to a specified
6+
# enterprise team in every organisation within a GitHub Enterprise account.
7+
# This grants read access to all current and future repositories without
8+
# requiring per-repository assignments.
9+
#
10+
# Usage:
11+
# export GITHUB_TOKEN=ghp_yourtoken
12+
# export ENTERPRISE=my-enterprise
13+
# export ENTERPRISE_TEAM_SLUG=platform-team
14+
# ./github-add-enterprise-team-read-permissions.sh
15+
#
16+
# Environment variables:
17+
# GITHUB_TOKEN Required. PAT with admin:enterprise scope
18+
# ENTERPRISE Required. GitHub Enterprise slug
19+
# ENTERPRISE_TEAM_SLUG Required. Enterprise team slug (without "ent:" prefix)
20+
# ALL_REPO_READ_ROLE_NAME Optional. Org role name (default: all_repo_read)
21+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
22+
#
23+
# Requirements:
24+
# - curl
25+
# - jq
26+
# =============================================================================
27+
228
set -euo pipefail
329

430
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
531
# shellcheck source=../../lib/github-common.sh
632
source "${SCRIPT_DIR}/../../lib/github-common.sh"
733

8-
###
9-
## GitHub Enterprise Team Org Role Assignment
10-
## Assigns the built-in "All-repository read" organization role to the
11-
## configured enterprise team in every org in the enterprise.
12-
##
13-
## This org-level role grants read access to all current AND future
14-
## repositories, so the assignment only needs to be made once per org.
15-
###
16-
1734
###
1835
## GLOBAL VARIABLES
1936
###

‎enterprise/github-dockerfile-discovery/github-dockerfile-discovery.sh‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-dockerfile-discovery.sh
4+
#
5+
# Searches all organisations in a GitHub Enterprise account for Dockerfiles,
6+
# extracts base image references from FROM instructions, and generates CSV
7+
# reports to identify common base images across the estate.
8+
#
9+
# Usage:
10+
# export GITHUB_TOKEN=ghp_yourtoken
11+
# export ENTERPRISE=my-enterprise
12+
# ./github-dockerfile-discovery.sh
13+
#
14+
# Environment variables:
15+
# GITHUB_TOKEN Required. PAT with read:org and repo scope
16+
# ENTERPRISE Required. GitHub Enterprise slug
17+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
18+
# REPORT_DIR Optional. Output directory (default: ./reports)
19+
# ORGS Optional. Comma-separated org list; skips enterprise lookup
20+
# ORG_FILTER Optional. ERE regex to keep only matching org names
21+
# ORG_EXCLUDE Optional. ERE regex to drop matching org names
22+
# SEARCH_SLEEP Optional. Seconds between code-search requests (default: 2)
23+
# CONTENT_SLEEP Optional. Seconds between content-fetch requests (default: 1)
24+
#
25+
# Requirements:
26+
# - curl
27+
# - jq
28+
# =============================================================================
29+
230
set -euo pipefail
331

432
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -10,27 +38,6 @@ print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" >&2; }
1038
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" >&2; }
1139
print_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
1240

13-
###
14-
## GitHub Enterprise Dockerfile Discovery
15-
## Searches all organisations in a GitHub Enterprise account for Dockerfiles,
16-
## extracts base image references from FROM instructions, and generates CSV
17-
## reports to identify common base images across the estate.
18-
##
19-
## Usage:
20-
## export GITHUB_TOKEN=ghp_yourtoken
21-
## export ENTERPRISE=my-enterprise
22-
## ./github-dockerfile-discovery.sh
23-
##
24-
## Optional env vars:
25-
## API_URL_PREFIX - GitHub API base URL (default: https://api.github.com)
26-
## REPORT_DIR - Output directory (default: ./reports)
27-
## ORGS - Comma-separated org list; skips enterprise lookup
28-
## ORG_FILTER - ERE regex to keep only matching org names (e.g. '^my-enterprise-prefix')
29-
## ORG_EXCLUDE - ERE regex to drop matching org names
30-
## SEARCH_SLEEP - Seconds between code-search requests (default: 2)
31-
## CONTENT_SLEEP - Seconds between content-fetch requests (default: 1)
32-
###
33-
3441
###
3542
## GLOBAL VARIABLES
3643
###

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-get-consumed-licenses.sh
4+
#
5+
# Displays the total number of GitHub Enterprise license seats consumed and
6+
# purchased for the given enterprise account.
7+
#
8+
# Usage:
9+
# export GITHUB_TOKEN=ghp_yourtoken
10+
# export ENTERPRISE=my-enterprise
11+
# ./github-get-consumed-licenses.sh
12+
#
13+
# Environment variables:
14+
# GITHUB_TOKEN Required. PAT with manage_billing:enterprise scope
15+
# ENTERPRISE Required. GitHub Enterprise slug
16+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
17+
#
18+
# Requirements:
19+
# - curl
20+
# - jq
21+
# =============================================================================
22+
223
set -euo pipefail
324

425
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

‎enterprise/github-get-public-repos/github-get-public-repos.sh‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-get-public-repos.sh
4+
#
5+
# Lists all repositories with public visibility across every organisation in a
6+
# GitHub Enterprise account and writes a timestamped CSV report.
7+
#
8+
# Usage:
9+
# export GITHUB_TOKEN=ghp_yourtoken
10+
# export ENTERPRISE=my-enterprise
11+
# ./github-get-public-repos.sh
12+
#
13+
# Environment variables:
14+
# GITHUB_TOKEN Required. PAT with read:org and repo scope
15+
# ENTERPRISE Required. GitHub Enterprise slug
16+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
17+
# REPORT_DIR Optional. Output directory (default: ./reports)
18+
# ORGS Optional. Comma-separated org list; skips enterprise lookup
19+
# ORG_FILTER Optional. ERE regex to keep only matching org names
20+
# ORG_EXCLUDE Optional. ERE regex to drop matching org names
21+
#
22+
# Requirements:
23+
# - curl
24+
# - jq
25+
# =============================================================================
26+
227
set -euo pipefail
328

429
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -10,24 +35,6 @@ print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" >&2; }
1035
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" >&2; }
1136
print_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
1237

13-
###
14-
## GitHub Enterprise Public Repo Discovery
15-
## Lists all repositories with public visibility across every organisation
16-
## in a GitHub Cloud Enterprise account and writes a timestamped CSV report.
17-
##
18-
## Usage:
19-
## export GITHUB_TOKEN=ghp_yourtoken
20-
## export ENTERPRISE=my-enterprise
21-
## ./github-get-public-repos.sh
22-
##
23-
## Optional env vars:
24-
## API_URL_PREFIX - GitHub API base URL (default: https://api.github.com)
25-
## REPORT_DIR - Output directory (default: ./reports)
26-
## ORGS - Comma-separated org list; skips enterprise org lookup
27-
## ORG_FILTER - ERE regex to keep only matching org names (e.g. '^my-enterprise-prefix')
28-
## ORG_EXCLUDE - ERE regex to drop matching org names
29-
###
30-
3138
###
3239
## GLOBAL VARIABLES
3340
###

‎lib/github-common.sh‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#!/bin/bash
2-
# lib/github-common.sh — Shared utility functions for GitHub API scripts.
2+
# =============================================================================
3+
# github-common.sh
34
#
4-
# Source this from any script:
5+
# Shared utility functions for GitHub API scripts. Source this library from
6+
# any script; do not execute it directly.
7+
#
8+
# Usage (from a script two directory levels deep):
59
# SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
610
# # shellcheck source=../../lib/github-common.sh
711
# source "${SCRIPT_DIR}/../../lib/github-common.sh"
12+
#
13+
# Provides:
14+
# print_status / print_success / print_warning / print_error
15+
# require_env_var <VAR> [description] — exit if variable is unset/empty
16+
# require_command <cmd> — exit if command not found
17+
# validate_github_token [bearer] — verify token via /user endpoint
18+
# validate_token <VAR_NAME> — verify a secondary token variable
19+
# =============================================================================
820

921
###
1022
## COLOR CODES

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-add-repo-collaborators-by-pattern.sh
4+
#
5+
# Adds one or more individual collaborators to all repositories in an
6+
# organisation whose names match a given regex pattern.
7+
#
8+
# Usage:
9+
# export GITHUB_TOKEN=ghp_yourtoken
10+
# export ORG=my-org
11+
# export COLLABORATORS=alice,bob
12+
# export REPO_NAME_REGEX='^service-'
13+
# ./github-add-repo-collaborators-by-pattern.sh
14+
#
15+
# Environment variables:
16+
# GITHUB_TOKEN Required. PAT with repo and admin:org scope
17+
# ORG Required. GitHub organization name
18+
# COLLABORATORS Required. Comma-separated GitHub usernames
19+
# REPO_NAME_REGEX Required. ERE regex to match repository names
20+
# PERMISSION Optional. Permission level: pull|triage|push|maintain|admin (default: push)
21+
# REPO_EXCLUDE_REGEX Optional. ERE regex to exclude matching repository names
22+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
23+
#
24+
# Requirements:
25+
# - curl
26+
# - jq
27+
# =============================================================================
28+
229
set -euo pipefail
330

431
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
#!/bin/bash
2+
# =============================================================================
3+
# github-add-repo-permissions.sh
4+
#
5+
# Grants team-level permissions across all repositories in a GitHub
6+
# organisation. Supports all five permission levels: admin, maintain, push,
7+
# triage, and pull. At least one permission level must be specified.
8+
#
9+
# Usage:
10+
# export GITHUB_TOKEN=ghp_yourtoken
11+
# export ORG=my-org
12+
# export REPO_PUSH="platform-team ci-team"
13+
# ./github-add-repo-permissions.sh
14+
#
15+
# Environment variables:
16+
# GITHUB_TOKEN Required. PAT with admin:org scope
17+
# ORG Required. GitHub organization name
18+
# REPO_ADMIN Optional. Space-separated team slugs to grant admin access
19+
# REPO_MAINTAIN Optional. Space-separated team slugs to grant maintain access
20+
# REPO_PUSH Optional. Space-separated team slugs to grant push access
21+
# REPO_TRIAGE Optional. Space-separated team slugs to grant triage access
22+
# REPO_PULL Optional. Space-separated team slugs to grant pull access
23+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
24+
#
25+
# Note: At least one of REPO_ADMIN, REPO_MAINTAIN, REPO_PUSH, REPO_TRIAGE,
26+
# or REPO_PULL must be set.
27+
#
28+
# Requirements:
29+
# - curl
30+
# - jq
31+
# =============================================================================
32+
233
set -euo pipefail
334

435
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/bin/bash
2-
3-
# GitHub Archive Old Repositories
4-
# Identifies and archives repositories not updated in the last 5 years
5-
# Author: Patrick Lewis (locus313)
6-
# Date: October 15, 2025
2+
# =============================================================================
3+
# github-archive-old-repos.sh
4+
#
5+
# Identifies and archives repositories that have not been updated within a
6+
# configurable number of years. Generates a timestamped CSV report of all
7+
# archived repositories.
8+
#
9+
# Usage:
10+
# export GITHUB_TOKEN=ghp_yourtoken
11+
# export ORG=my-org
12+
# ./github-archive-old-repos.sh
13+
#
14+
# Environment variables:
15+
# GITHUB_TOKEN Required. PAT with repo scope
16+
# ORG Required. GitHub organization name
17+
# YEARS_THRESHOLD Optional. Age threshold in years (default: 5)
18+
# API_URL_PREFIX Optional. GitHub API base URL (default: https://api.github.com)
19+
#
20+
# Requirements:
21+
# - curl
22+
# - jq
23+
# =============================================================================
724

825
set -euo pipefail
926

0 commit comments

Comments
 (0)