Skip to content

Commit 5aa10f7

Browse files
committed
feat: enhance documentation and error handling across scripts for improved usability and validation
1 parent bb511bd commit 5aa10f7

7 files changed

Lines changed: 219 additions & 160 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This is a collection of **standalone bash automation scripts** for GitHub organization administration. Each script lives in its own directory with a single `.sh` file. Scripts use `curl` + `jq` to interact with the GitHub REST API v3.
66

7-
**Key principle:** Scripts are independent, self-contained utilities—not a unified codebase. There's no shared library or common framework.
7+
**Key principle:** Scripts are independent, self-contained utilities—not a unified codebase. There's no shared library or common framework. Each script is a complete, self-validating executable that manages its own error handling and input validation.
88

99
## Architecture Pattern
1010

@@ -41,6 +41,10 @@ GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'}
4141
- Set `API_URL_PREFIX` and `GIT_URL_PREFIX` for GitHub Enterprise Server compatibility
4242
- Include `sleep 5` between API calls to avoid rate limits (see `github-add-repo-admin.sh`)
4343
- Use pagination helpers for org-level operations (pattern in `github-add-repo-admin.sh` lines 20-28)
44+
- **ALWAYS validate required variables** with explicit `if [ -z "${VAR}" ]` checks before any API calls
45+
- **ALWAYS validate GITHUB_TOKEN** by calling `/user` endpoint and checking for 200 status code
46+
- Use `curl -s -o /dev/null -w "%{http_code}"` pattern to check HTTP status codes
47+
- Exit with status code 1 on validation failures with descriptive error messages
4448

4549
## GitHub API Patterns
4650

@@ -65,6 +69,21 @@ done
6569
- Use `token` for standard API: `-H "Authorization: token ${GITHUB_TOKEN}"`
6670
- Include API version when specified: `-H "X-GitHub-Api-Version: 2022-11-28"`
6771

72+
### Accept Headers (Media Types)
73+
Different endpoints require specific Accept headers:
74+
- Template generation: `application/vnd.github.baptiste-preview+json`
75+
- Timeline events: `application/vnd.github.mockingbird-preview+json`
76+
- Issue reactions: `application/vnd.github.mercy-preview+json`
77+
- Repository visibility: `application/vnd.github.nebula-preview+json`
78+
- Standard operations: `application/vnd.github.v3+json`
79+
80+
### Error Handling Pattern
81+
All scripts follow this validation sequence:
82+
1. Check all required env vars are non-empty (exit with descriptive message if missing)
83+
2. Validate token by calling API `/user` endpoint (exit if not 200 status)
84+
3. Validate additional tokens if used (e.g., `CD_GITHUB_TOKEN`)
85+
4. Proceed with main logic only after all validations pass
86+
6887
## Script-Specific Behaviors
6988

7089
### `github-add-repo-admin`
@@ -76,16 +95,30 @@ done
7695
- `REPO_ADMIN` and `REPO_WRITE` are **space-separated lists** of team slugs
7796
- Automatically accepts collaborator invite using `CD_GITHUB_TOKEN` (a separate token for the CD user)
7897
- Permission mapping: `admin` = admin, `push` = write access
98+
- Retrieves invite ID via `/user/repository_invitations` and accepts via PATCH request
99+
- Creates repo with `private: true` and `include_all_branches: true` by default
79100

80101
### `github-import-repo`
81102
- Performs **bare clone** + mirror push (full git history)
82103
- Takes 2 positional args: `$1=SRC_REPO`, `$2=DEST_REPO`
83104
- Cleans up local `.git` directory after mirroring
105+
- Creates new repo with `visibility: internal`
106+
- Grants admin permissions to `OWNER_USERNAME` on destination repo
107+
- Requires git to be installed and accessible in PATH
84108

85109
### `github-monthly-issues-report`
86110
- Generates HTML output to `output.txt` (not stdout)
87111
- Creates temporary `test.json` file (cleaned up at end)
88112
- Hardcoded label filter: `Linked%20[AC]` (URL-encoded)
113+
- Filters issues by creation date between `MONTH_START` and `MONTH_END`
114+
- Uses timeline API to track who applied labels
115+
- Aggregates statistics and sorts by count (descending)
116+
117+
### `github-get-consumed-licenses`
118+
- Uses **Bearer token** authentication (different from other scripts)
119+
- Requires enterprise-level token scope (`read:enterprise`)
120+
- Calls `/enterprises/{ENTERPRISE}/consumed-licenses` endpoint
121+
- Outputs two metrics: seats consumed and seats purchased
89122

90123
## Development Guidelines
91124

0 commit comments

Comments
 (0)