Skip to content

Commit 2578707

Browse files
committed
Initial commit: GitHub App token utility
0 parents  commit 2578707

6 files changed

Lines changed: 577 additions & 0 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# GitHub App credentials
2+
GITHUB_APP_ID=
3+
GITHUB_INSTALLATION_ID=
4+
GITHUB_PRIVATE_KEY_PATH=./private-key.pem

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
.env*
3+
!.env.example
4+
*.pem

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# github-app-util
2+
3+
Generate GitHub App installation tokens for API access.
4+
5+
## Prerequisites
6+
7+
- Node.js 18+
8+
- A [GitHub App](https://docs.github.com/en/apps/creating-github-apps) with a private key and at least one installation
9+
10+
## Install
11+
12+
Install directly from GitHub:
13+
14+
```bash
15+
npm install -g git+https://github.com/AztecProtocol/github-app-util.git
16+
```
17+
18+
Or clone and install locally:
19+
20+
```bash
21+
git clone https://github.com/AztecProtocol/github-app-util.git
22+
cd github-app-util
23+
npm install
24+
```
25+
26+
## Setup
27+
28+
1. Copy the example env file:
29+
30+
```bash
31+
cp .env.example .env
32+
```
33+
34+
2. Fill in your credentials in `.env`:
35+
36+
```
37+
GITHUB_APP_ID=123456
38+
GITHUB_INSTALLATION_ID=789012
39+
GITHUB_PRIVATE_KEY_PATH=./private-key.pem
40+
```
41+
42+
3. Set `GITHUB_PRIVATE_KEY_PATH` to the location of your GitHub App private key (`.pem` file). Defaults to `./private-key.pem` if not set.
43+
44+
## Usage
45+
46+
If installed globally:
47+
48+
```bash
49+
github-app-util
50+
```
51+
52+
If cloned locally:
53+
54+
```bash
55+
node bin/get-token.js
56+
```
57+
58+
The tool outputs an installation token you can use for API calls.
59+
60+
To capture the token as an environment variable:
61+
62+
```bash
63+
export GITHUB_TOKEN=$(github-app-util 2>/dev/null)
64+
```
65+
66+
The `2>/dev/null` suppresses the TTY warning so only the token is captured.
67+

bin/get-token.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env node
2+
3+
import "dotenv/config";
4+
import { App } from "octokit";
5+
import fs from "fs";
6+
import { resolve } from "path";
7+
8+
const appId = process.env.GITHUB_APP_ID;
9+
const installationId = process.env.GITHUB_INSTALLATION_ID;
10+
const keyPath = process.env.GITHUB_PRIVATE_KEY_PATH || "./private-key.pem";
11+
12+
if (!appId || !installationId) {
13+
console.error("Missing GITHUB_APP_ID or GITHUB_INSTALLATION_ID in environment");
14+
process.exit(1);
15+
}
16+
17+
const privateKey = fs.readFileSync(resolve(keyPath), "utf8");
18+
19+
const app = new App({ appId, privateKey });
20+
21+
// This automatically handles JWT generation and token exchange
22+
const octokit = await app.getInstallationOctokit(installationId);
23+
24+
const { data } = await octokit.rest.apps.getAuthenticated();
25+
console.log(`Authenticated as: ${data.name}`);
26+
27+
// Output the token for use in other tools
28+
const auth = await octokit.auth({ type: "installation" });
29+
30+
if (process.stdout.isTTY) {
31+
console.warn("\n⚠ Token below — avoid logging this in CI or shared terminals.\n");
32+
}
33+
console.log(auth.token);

0 commit comments

Comments
 (0)