Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit d196e95

Browse files
tibdexalldoamiedulop
authored
Use @octokit/auth-app (#15)
Duplicate of #14 to make some adjustments. Co-authored-by: adoami <[email protected]> Co-authored-by: edulop <[email protected]>
1 parent 0c895ba commit d196e95

File tree

6 files changed

+401
-306
lines changed

6 files changed

+401
-306
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Publish
22
on:
33
push:
44
branches:
5-
- master
5+
- main
66

77
jobs:
88
publish:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Test
22
on:
33
push:
44
branches-ignore:
5-
- master
5+
- main
66

77
jobs:
88
test:

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-app-token",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"license": "MIT",
55
"files": [
66
"action.yml",
@@ -17,24 +17,25 @@
1717
"devDependencies": {
1818
"@actions/core": "^1.2.6",
1919
"@actions/github": "^4.0.0",
20-
"@octokit/app": "^4.3.0",
20+
"@octokit/auth-app": "^2.10.5",
2121
"@types/is-base64": "^1.1.0",
22-
"@types/node": "^14.14.6",
23-
"@typescript-eslint/eslint-plugin": "^4.6.0",
24-
"@typescript-eslint/parser": "^4.6.0",
25-
"@vercel/ncc": "^0.24.1",
26-
"eslint": "^7.12.1",
27-
"eslint-config-prettier": "^6.15.0",
22+
"@types/jest": "^26.0.18",
23+
"@types/node": "^14.14.11",
24+
"@typescript-eslint/eslint-plugin": "^4.9.1",
25+
"@typescript-eslint/parser": "^4.9.1",
26+
"@vercel/ncc": "^0.25.1",
27+
"eslint": "^7.15.0",
28+
"eslint-config-prettier": "^7.0.0",
2829
"eslint-config-xo": "^0.33.1",
29-
"eslint-config-xo-typescript": "^0.35.0",
30+
"eslint-config-xo-typescript": "^0.36.0",
3031
"eslint-import-resolver-typescript": "^2.3.0",
3132
"eslint-plugin-import": "^2.22.1",
3233
"eslint-plugin-sort-destructure-keys": "^1.3.5",
3334
"eslint-plugin-typescript-sort-keys": "^1.5.0",
3435
"eslint-plugin-unicorn": "^23.0.0",
3536
"is-base64": "^1.1.0",
36-
"prettier": "^2.1.2",
37+
"prettier": "^2.2.1",
3738
"promise-retry": "^2.0.1",
38-
"typescript": "^4.0.5"
39+
"typescript": "^4.1.2"
3940
}
4041
}

src/fetch-installation-token.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { context, getOctokit } from "@actions/github";
2+
import { createAppAuth } from "@octokit/auth-app";
3+
4+
export const fetchInstallationToken = async ({
5+
appId,
6+
privateKey,
7+
}: Readonly<{ appId: string; privateKey: string }>): Promise<string> => {
8+
const app = createAppAuth({ appId, privateKey });
9+
const authApp = await app({ type: "app" });
10+
const octokit = getOctokit(authApp.token);
11+
const {
12+
data: { id: installationId },
13+
} = await octokit.apps.getRepoInstallation(context.repo);
14+
const installation = await app({ installationId, type: "installation" });
15+
return installation.token;
16+
};

src/index.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
import {
2-
error as logError,
3-
getInput,
4-
info,
5-
setFailed,
6-
setOutput,
7-
setSecret,
8-
} from "@actions/core";
9-
import { context, getOctokit } from "@actions/github";
10-
import { App } from "@octokit/app";
1+
import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";
2+
113
import isBase64 from "is-base64";
124

5+
import { fetchInstallationToken } from "./fetch-installation-token";
6+
137
const run = async () => {
148
try {
15-
const id = Number(getInput("app_id", { required: true }));
9+
const appId = getInput("app_id", { required: true });
1610
const privateKeyInput = getInput("private_key", { required: true });
1711
const privateKey = isBase64(privateKeyInput)
1812
? Buffer.from(privateKeyInput, "base64").toString("utf8")
1913
: privateKeyInput;
20-
const app = new App({ id, privateKey });
21-
const jwt = app.getSignedJsonWebToken();
22-
const octokit = getOctokit(jwt);
23-
const {
24-
data: { id: installationId },
25-
} = await octokit.apps.getRepoInstallation(context.repo);
26-
const token = await app.getInstallationAccessToken({
27-
installationId,
14+
15+
const installationToken = await fetchInstallationToken({
16+
appId,
17+
privateKey,
2818
});
29-
setSecret(token);
30-
setOutput("token", token);
19+
20+
setSecret(installationToken);
21+
setOutput("token", installationToken);
3122
info("Token generated successfully!");
3223
} catch (error: unknown) {
33-
if (typeof error !== "string" && !(error instanceof Error)) {
34-
throw new TypeError(`Caught error of unexpected type: ${typeof error}`);
24+
if (typeof error === "string" || error instanceof Error) {
25+
setFailed(error);
26+
} else {
27+
setFailed(`Caught error of unexpected type: ${typeof error}`);
3528
}
36-
37-
setFailed(error);
3829
}
3930
};
4031

0 commit comments

Comments
 (0)