|
1 | | -Github API Scripts |
2 | | -================== |
| 1 | +# GitHub API Scripts |
3 | 2 |
|
4 | | -This is a collection of bash scripts using curl I created to automate things with the [Github API](https://developer.github.com/v3). |
| 3 | +[](LICENSE) |
| 4 | + |
| 5 | +[](https://docs.github.com/en/rest) |
5 | 6 |
|
6 | | -* [Installation](#installation) |
| 7 | +⭐ If you like this project, star it on GitHub — it helps a lot! |
7 | 8 |
|
8 | | -Installation |
9 | | ------------- |
| 9 | +[Overview](#overview) • [Prerequisites](#prerequisites) • [Getting Started](#getting-started) • [Scripts](#scripts) • [Usage](#usage) |
10 | 10 |
|
11 | | -Requirements: |
| 11 | +A collection of bash scripts that automate common GitHub administrative tasks using the [GitHub REST API](https://docs.github.com/en/rest). These lightweight, dependency-minimal scripts help you manage repositories, teams, issues, and licenses across your GitHub organization. |
12 | 12 |
|
13 | | -* bash (recommend version 4+) |
14 | | -* [curl](https://curl.haxx.se) |
15 | | -* [jq](https://stedolan.github.io/jq) |
| 13 | +## Overview |
16 | 14 |
|
17 | | -Clone the repo, download a source zip or copy the shell scripts as needed. |
| 15 | +This toolkit provides ready-to-use scripts for GitHub organization administrators and developers who need to automate repetitive tasks. All scripts use `curl` to interact with the GitHub API and `jq` for JSON processing, making them portable and easy to customize. |
| 16 | + |
| 17 | +**Key capabilities:** |
| 18 | +- Bulk repository management and administration |
| 19 | +- Team permission assignment |
| 20 | +- Repository creation and migration |
| 21 | +- License consumption reporting |
| 22 | +- Issue tracking and reporting |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- **bash** (version 4+ recommended) |
| 27 | +- **[curl](https://curl.se)** - for API requests |
| 28 | +- **[jq](https://stedolan.github.io/jq)** - for JSON parsing |
| 29 | +- **[git](https://git-scm.com)** - for repository operations (required by some scripts) |
| 30 | +- **GitHub Personal Access Token** with appropriate scopes (see [Authentication](#authentication)) |
| 31 | + |
| 32 | +## Getting Started |
| 33 | + |
| 34 | +### Installation |
| 35 | + |
| 36 | +Clone the repository: |
| 37 | + |
| 38 | +```bash |
| 39 | +git clone https://github.com/locus313/github-api-scripts.git |
| 40 | +cd github-api-scripts |
| 41 | +``` |
| 42 | + |
| 43 | +Alternatively, download individual scripts as needed or grab the source archive. |
| 44 | + |
| 45 | +### Authentication |
| 46 | + |
| 47 | +All scripts require a GitHub Personal Access Token. Create one at [github.com/settings/tokens](https://github.com/settings/tokens) with the following scopes: |
| 48 | + |
| 49 | +- `repo` - Full control of repositories |
| 50 | +- `admin:org` - Organization administration (for team management) |
| 51 | +- `read:enterprise` - Read enterprise data (for license consumption) |
| 52 | + |
| 53 | +Set your token as an environment variable: |
| 54 | + |
| 55 | +```bash |
| 56 | +export GITHUB_TOKEN="your_github_token_here" |
| 57 | +``` |
| 58 | + |
| 59 | +> [!TIP] |
| 60 | +> Add this to your `~/.bashrc` or `~/.zshrc` to persist across sessions. |
| 61 | +
|
| 62 | +### Configuration |
| 63 | + |
| 64 | +Most scripts use environment variables for configuration. Common variables include: |
| 65 | + |
| 66 | +| Variable | Description | Required | |
| 67 | +|----------|-------------|----------| |
| 68 | +| `GITHUB_TOKEN` | Your GitHub personal access token | Yes | |
| 69 | +| `ORG` | GitHub organization name | Yes (for org scripts) | |
| 70 | +| `API_URL_PREFIX` | GitHub API base URL | No (defaults to `https://api.github.com`) | |
| 71 | +| `GIT_URL_PREFIX` | GitHub base URL | No (defaults to `https://github.com`) | |
| 72 | + |
| 73 | +## Scripts |
| 74 | + |
| 75 | +### `github-add-repo-admin` |
| 76 | + |
| 77 | +Grants admin permissions to a specified team across all repositories in an organization. |
| 78 | + |
| 79 | +**Use case:** Ensuring an internal admin team has administrator access to all repositories. |
| 80 | + |
| 81 | +**Required variables:** |
| 82 | +- `GITHUB_TOKEN` - Your access token |
| 83 | +- `ORG` - Organization name |
| 84 | +- `REPO_ADMIN` - Team slug to grant admin permissions |
| 85 | + |
| 86 | +**Usage:** |
| 87 | +```bash |
| 88 | +cd github-add-repo-admin |
| 89 | +export GITHUB_TOKEN="your_token" |
| 90 | +export ORG="your-org" |
| 91 | +export REPO_ADMIN="admins" |
| 92 | +./github-add-repo-admin.sh |
| 93 | +``` |
| 94 | + |
| 95 | +### `github-get-consumed-licenses` |
| 96 | + |
| 97 | +Retrieves license consumption information for a GitHub Enterprise account. |
| 98 | + |
| 99 | +**Use case:** Auditing seat usage and tracking license costs. |
| 100 | + |
| 101 | +**Required variables:** |
| 102 | +- `GITHUB_TOKEN` - Your access token (with `read:enterprise` scope) |
| 103 | +- `ENTERPRISE` - Enterprise account name |
| 104 | + |
| 105 | +**Usage:** |
| 106 | +```bash |
| 107 | +cd github-get-consumed-licenses |
| 108 | +export GITHUB_TOKEN="your_token" |
| 109 | +export ENTERPRISE="your-enterprise" |
| 110 | +./github-get-consumed-licenses.sh |
| 111 | +``` |
| 112 | + |
| 113 | +**Output:** |
| 114 | +``` |
| 115 | +Total seats consumed: 150 |
| 116 | +Total seats purchased: 200 |
| 117 | +``` |
| 118 | + |
| 119 | +### `github-import-repo` |
| 120 | + |
| 121 | +Creates a new repository and mirrors all content from an existing repository. |
| 122 | + |
| 123 | +**Use case:** Migrating or duplicating repositories within the same organization. |
| 124 | + |
| 125 | +**Required variables:** |
| 126 | +- `GITHUB_TOKEN` - Your access token |
| 127 | +- `ORG` - Organization name |
| 128 | +- `OWNER_USERNAME` - Username to grant admin permissions on the new repository |
| 129 | + |
| 130 | +**Usage:** |
| 131 | +```bash |
| 132 | +cd github-import-repo |
| 133 | +export GITHUB_TOKEN="your_token" |
| 134 | +export ORG="your-org" |
| 135 | +export OWNER_USERNAME="admin-user" |
| 136 | +./github-import-repo.sh source-repo destination-repo |
| 137 | +``` |
| 138 | + |
| 139 | +> [!WARNING] |
| 140 | +> This script performs a bare clone and mirror push. Ensure you have sufficient disk space and network bandwidth for large repositories. |
| 141 | +
|
| 142 | +### `github-monthly-issues-report` |
| 143 | + |
| 144 | +Generates a monthly report of issues with specific labels, including author and contributor statistics. |
| 145 | + |
| 146 | +**Use case:** Tracking community contributions and support activities. |
| 147 | + |
| 148 | +**Required variables:** |
| 149 | +- `GITHUB_TOKEN` - Your access token |
| 150 | +- `ORG` - Organization name |
| 151 | +- `REPO` - Repository name |
| 152 | +- `MONTH_START` - Start date (format: `YYYY-MM-DD`) |
| 153 | +- `MONTH_END` - End date (format: `YYYY-MM-DD`) |
| 154 | + |
| 155 | +**Usage:** |
| 156 | +```bash |
| 157 | +cd github-monthly-issues-report |
| 158 | +export GITHUB_TOKEN="your_token" |
| 159 | +export ORG="your-org" |
| 160 | +export REPO="your-repo" |
| 161 | +export MONTH_START="2025-10-01" |
| 162 | +export MONTH_END="2025-10-31" |
| 163 | +./github-monthly-issues-report.sh |
| 164 | +``` |
| 165 | + |
| 166 | +**Output:** Creates `output.txt` with HTML-formatted author and contributor statistics. |
| 167 | + |
| 168 | +### `github-repo-from-template` |
| 169 | + |
| 170 | +Creates a new repository from a template and configures team permissions and collaborator access. |
| 171 | + |
| 172 | +**Use case:** Standardizing new project setup with predefined structure and permissions. |
| 173 | + |
| 174 | +**Required variables:** |
| 175 | +- `GITHUB_TOKEN` - Your access token |
| 176 | +- `ORG` - Organization name |
| 177 | +- `TEMPLATE_REPO` - Template repository name |
| 178 | +- `REPO_ADMIN` - Space-separated list of admin teams |
| 179 | +- `REPO_WRITE` - Space-separated list of write teams |
| 180 | +- `CD_USERNAME` - Username for CD/automation user |
| 181 | +- `CD_GITHUB_TOKEN` - Token for CD user (to accept invitation) |
| 182 | + |
| 183 | +**Usage:** |
| 184 | +```bash |
| 185 | +cd github-repo-from-template |
| 186 | +export GITHUB_TOKEN="your_token" |
| 187 | +export ORG="your-org" |
| 188 | +export TEMPLATE_REPO="template-repo" |
| 189 | +export REPO_ADMIN="admins platform-team" |
| 190 | +export REPO_WRITE="developers" |
| 191 | +export CD_USERNAME="cd-user" |
| 192 | +export CD_GITHUB_TOKEN="cd_token" |
| 193 | +./github-repo-from-template.sh new-project-name |
| 194 | +``` |
| 195 | + |
| 196 | +## Usage |
| 197 | + |
| 198 | +### Basic Workflow |
| 199 | + |
| 200 | +1. **Navigate to the script directory:** |
| 201 | + ```bash |
| 202 | + cd github-<script-name> |
| 203 | + ``` |
| 204 | + |
| 205 | +2. **Set required environment variables:** |
| 206 | + ```bash |
| 207 | + export GITHUB_TOKEN="your_token" |
| 208 | + export ORG="your-org" |
| 209 | + # ... other variables as needed |
| 210 | + ``` |
| 211 | + |
| 212 | +3. **Run the script:** |
| 213 | + ```bash |
| 214 | + ./github-<script-name>.sh [arguments] |
| 215 | + ``` |
| 216 | + |
| 217 | +### Tips |
| 218 | + |
| 219 | +- **Rate limiting:** Scripts include delays to avoid hitting GitHub's rate limits. For large organizations, expect longer execution times. |
| 220 | +- **Dry run:** Consider testing scripts on a test organization first to verify behavior. |
| 221 | +- **Logging:** Redirect output to a file for audit trails: `./script.sh 2>&1 | tee execution.log` |
| 222 | +- **Customization:** All scripts are designed to be easily modified for your specific needs. |
| 223 | + |
| 224 | +### Advanced Usage |
| 225 | + |
| 226 | +**Custom GitHub Enterprise Server:** |
| 227 | + |
| 228 | +```bash |
| 229 | +export API_URL_PREFIX="https://github.company.com/api/v3" |
| 230 | +export GIT_URL_PREFIX="https://github.company.com" |
| 231 | +``` |
| 232 | + |
| 233 | +**Using with GitHub Enterprise Cloud:** |
| 234 | + |
| 235 | +```bash |
| 236 | +export API_URL_PREFIX="https://api.github.com" |
| 237 | +export GIT_URL_PREFIX="https://github.com" |
| 238 | +``` |
| 239 | + |
| 240 | +## Contributing |
| 241 | + |
| 242 | +Contributions are welcome! If you have a useful GitHub automation script or improvements to existing ones: |
| 243 | + |
| 244 | +1. Fork the repository |
| 245 | +2. Create a feature branch |
| 246 | +3. Add your script with documentation |
| 247 | +4. Submit a pull request |
| 248 | + |
| 249 | +## Resources |
| 250 | + |
| 251 | +- [GitHub REST API Documentation](https://docs.github.com/en/rest) |
| 252 | +- [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) |
| 253 | +- [jq Manual](https://stedolan.github.io/jq/manual/) |
| 254 | +- [curl Documentation](https://curl.se/docs/) |
0 commit comments