|
4 | 4 |
|
5 | 5 | 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. |
6 | 6 |
|
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. |
8 | 8 |
|
9 | 9 | ## Architecture Pattern |
10 | 10 |
|
@@ -41,6 +41,10 @@ GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'} |
41 | 41 | - Set `API_URL_PREFIX` and `GIT_URL_PREFIX` for GitHub Enterprise Server compatibility |
42 | 42 | - Include `sleep 5` between API calls to avoid rate limits (see `github-add-repo-admin.sh`) |
43 | 43 | - 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 |
44 | 48 |
|
45 | 49 | ## GitHub API Patterns |
46 | 50 |
|
|
65 | 69 | - Use `token` for standard API: `-H "Authorization: token ${GITHUB_TOKEN}"` |
66 | 70 | - Include API version when specified: `-H "X-GitHub-Api-Version: 2022-11-28"` |
67 | 71 |
|
| 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 | + |
68 | 87 | ## Script-Specific Behaviors |
69 | 88 |
|
70 | 89 | ### `github-add-repo-admin` |
|
76 | 95 | - `REPO_ADMIN` and `REPO_WRITE` are **space-separated lists** of team slugs |
77 | 96 | - Automatically accepts collaborator invite using `CD_GITHUB_TOKEN` (a separate token for the CD user) |
78 | 97 | - 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 |
79 | 100 |
|
80 | 101 | ### `github-import-repo` |
81 | 102 | - Performs **bare clone** + mirror push (full git history) |
82 | 103 | - Takes 2 positional args: `$1=SRC_REPO`, `$2=DEST_REPO` |
83 | 104 | - 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 |
84 | 108 |
|
85 | 109 | ### `github-monthly-issues-report` |
86 | 110 | - Generates HTML output to `output.txt` (not stdout) |
87 | 111 | - Creates temporary `test.json` file (cleaned up at end) |
88 | 112 | - 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 |
89 | 122 |
|
90 | 123 | ## Development Guidelines |
91 | 124 |
|
|
0 commit comments