Skip to content

Commit 42a6622

Browse files
committed
feat(docs): add comprehensive Copilot instructions for GitHub API scripts
1 parent e880c4f commit 42a6622

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# GitHub API Scripts - AI Agent Instructions
2+
3+
## Project Overview
4+
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+
7+
**Key principle:** Scripts are independent, self-contained utilities—not a unified codebase. There's no shared library or common framework.
8+
9+
## Architecture Pattern
10+
11+
### Directory Structure
12+
```
13+
github-<script-name>/
14+
└── github-<script-name>.sh # Single executable script
15+
```
16+
17+
Each script follows this exact naming convention. Scripts are NOT imported or called by each other.
18+
19+
### Script Anatomy (Standardized Pattern)
20+
21+
All scripts follow this structure:
22+
23+
```bash
24+
#!/usr/bin/env /bin/bash
25+
set -euo pipefail # ALWAYS include: exit on error, undefined vars, pipe failures
26+
27+
### GLOBAL VARIABLES
28+
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
29+
ORG=${ORG:-''}
30+
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
31+
GIT_URL_PREFIX=${GIT_URL_PREFIX:-'https://github.com'}
32+
# Script-specific required vars...
33+
34+
# Token validation block (if applicable)
35+
# Helper functions for pagination/processing
36+
# Main execution logic
37+
```
38+
39+
**Critical patterns:**
40+
- Use `${VARIABLE:-''}` for all env vars to provide empty defaults
41+
- Set `API_URL_PREFIX` and `GIT_URL_PREFIX` for GitHub Enterprise Server compatibility
42+
- Include `sleep 5` between API calls to avoid rate limits (see `github-add-repo-admin.sh`)
43+
- Use pagination helpers for org-level operations (pattern in `github-add-repo-admin.sh` lines 20-28)
44+
45+
## GitHub API Patterns
46+
47+
### Pagination
48+
When iterating over all repos in an org, use this pattern:
49+
```bash
50+
get_repo_pagination () {
51+
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;)
52+
echo "${repo_pages:-1}"
53+
}
54+
55+
for PAGE in $(seq "$(get_repo_pagination)"); do
56+
# Process repos on this page with per_page=100
57+
done
58+
```
59+
60+
### Rate Limiting
61+
**Always include `sleep 5`** between repository-level operations to stay under GitHub's rate limits. See `github-add-repo-admin.sh` line 40.
62+
63+
### Authentication Headers
64+
- Use `Bearer` token for enterprise endpoints: `-H "Authorization: Bearer ${GITHUB_TOKEN}"`
65+
- Use `token` for standard API: `-H "Authorization: token ${GITHUB_TOKEN}"`
66+
- Include API version when specified: `-H "X-GitHub-Api-Version: 2022-11-28"`
67+
68+
## Script-Specific Behaviors
69+
70+
### `github-add-repo-admin`
71+
- Grants team admin permissions to ALL repos in an org
72+
- Uses pagination + 5-second delay between repos
73+
- Variable: `REPO_ADMIN` is a **team slug** (not team name)
74+
75+
### `github-repo-from-template`
76+
- `REPO_ADMIN` and `REPO_WRITE` are **space-separated lists** of team slugs
77+
- Automatically accepts collaborator invite using `CD_GITHUB_TOKEN` (a separate token for the CD user)
78+
- Permission mapping: `admin` = admin, `push` = write access
79+
80+
### `github-import-repo`
81+
- Performs **bare clone** + mirror push (full git history)
82+
- Takes 2 positional args: `$1=SRC_REPO`, `$2=DEST_REPO`
83+
- Cleans up local `.git` directory after mirroring
84+
85+
### `github-monthly-issues-report`
86+
- Generates HTML output to `output.txt` (not stdout)
87+
- Creates temporary `test.json` file (cleaned up at end)
88+
- Hardcoded label filter: `Linked%20[AC]` (URL-encoded)
89+
90+
## Development Guidelines
91+
92+
### Adding New Scripts
93+
1. Create directory: `github-<action-verb-object>/`
94+
2. Create script: `github-<action-verb-object>.sh` (match directory name)
95+
3. Start with the standard boilerplate (see Script Anatomy above)
96+
4. Document in README.md following existing format:
97+
- Use case description
98+
- Required variables table
99+
- Usage example with exports
100+
- Output format (if applicable)
101+
102+
### Testing Approach
103+
- **Always test on a test organization first** (mentioned in README tips)
104+
- Scripts have no built-in dry-run mode—handle with care
105+
- Use `2>&1 | tee execution.log` to capture output for auditing
106+
107+
### Variable Naming Conventions
108+
- `GITHUB_TOKEN` - main admin token
109+
- `ORG` - organization name
110+
- `ENTERPRISE` - enterprise account name
111+
- `REPO` - single repository name
112+
- `TEMPLATE_REPO` - source template repository
113+
- `CD_USERNAME` / `CD_GITHUB_TOKEN` - automation user credentials
114+
- `MONTH_START` / `MONTH_END` - ISO date format `YYYY-MM-DD`
115+
116+
### Dependencies
117+
- **bash 4+** (for modern features)
118+
- **curl** (API requests)
119+
- **jq** (JSON parsing - REQUIRED for all scripts)
120+
- **git** (only for `github-import-repo`)
121+
122+
No other dependencies. Keep scripts dependency-minimal.
123+
124+
## Common Pitfalls
125+
126+
1. **Team slugs vs names:** GitHub API uses team slugs (lowercase, hyphenated). Example: "Platform Team" → "platform-team"
127+
2. **Enterprise vs Org endpoints:** License consumption requires enterprise-level token scopes
128+
3. **Git URL authentication:** When using `git push --mirror`, ensure token has repo scope
129+
4. **Space-separated lists:** Some variables like `REPO_ADMIN` accept multiple values separated by spaces (loop over them)
130+
5. **URL encoding:** Labels in API calls must be URL-encoded (`Linked [AC]` → `Linked%20[AC]`)

0 commit comments

Comments
 (0)