Skip to content

Commit

Permalink
Use @octokit/auth-app (tibdex#15)
Browse files Browse the repository at this point in the history
Duplicate of tibdex#14 to make some adjustments.

Co-authored-by: adoami <[email protected]>
Co-authored-by: edulop <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2020
1 parent 0c895ba commit d196e95
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 306 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Publish
on:
push:
branches:
- master
- main

jobs:
publish:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test
on:
push:
branches-ignore:
- master
- main

jobs:
test:
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-app-token",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"files": [
"action.yml",
Expand All @@ -17,24 +17,25 @@
"devDependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"@octokit/app": "^4.3.0",
"@octokit/auth-app": "^2.10.5",
"@types/is-base64": "^1.1.0",
"@types/node": "^14.14.6",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"@vercel/ncc": "^0.24.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"@types/jest": "^26.0.18",
"@types/node": "^14.14.11",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"@vercel/ncc": "^0.25.1",
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-config-xo": "^0.33.1",
"eslint-config-xo-typescript": "^0.35.0",
"eslint-config-xo-typescript": "^0.36.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-sort-destructure-keys": "^1.3.5",
"eslint-plugin-typescript-sort-keys": "^1.5.0",
"eslint-plugin-unicorn": "^23.0.0",
"is-base64": "^1.1.0",
"prettier": "^2.1.2",
"prettier": "^2.2.1",
"promise-retry": "^2.0.1",
"typescript": "^4.0.5"
"typescript": "^4.1.2"
}
}
16 changes: 16 additions & 0 deletions src/fetch-installation-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { context, getOctokit } from "@actions/github";
import { createAppAuth } from "@octokit/auth-app";

export const fetchInstallationToken = async ({
appId,
privateKey,
}: Readonly<{ appId: string; privateKey: string }>): Promise<string> => {
const app = createAppAuth({ appId, privateKey });
const authApp = await app({ type: "app" });
const octokit = getOctokit(authApp.token);
const {
data: { id: installationId },
} = await octokit.apps.getRepoInstallation(context.repo);
const installation = await app({ installationId, type: "installation" });
return installation.token;
};
41 changes: 16 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import {
error as logError,
getInput,
info,
setFailed,
setOutput,
setSecret,
} from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { App } from "@octokit/app";
import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";

import isBase64 from "is-base64";

import { fetchInstallationToken } from "./fetch-installation-token";

const run = async () => {
try {
const id = Number(getInput("app_id", { required: true }));
const appId = getInput("app_id", { required: true });
const privateKeyInput = getInput("private_key", { required: true });
const privateKey = isBase64(privateKeyInput)
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;
const app = new App({ id, privateKey });
const jwt = app.getSignedJsonWebToken();
const octokit = getOctokit(jwt);
const {
data: { id: installationId },
} = await octokit.apps.getRepoInstallation(context.repo);
const token = await app.getInstallationAccessToken({
installationId,

const installationToken = await fetchInstallationToken({
appId,
privateKey,
});
setSecret(token);
setOutput("token", token);

setSecret(installationToken);
setOutput("token", installationToken);
info("Token generated successfully!");
} catch (error: unknown) {
if (typeof error !== "string" && !(error instanceof Error)) {
throw new TypeError(`Caught error of unexpected type: ${typeof error}`);
if (typeof error === "string" || error instanceof Error) {
setFailed(error);
} else {
setFailed(`Caught error of unexpected type: ${typeof error}`);
}

setFailed(error);
}
};

Expand Down
Loading

0 comments on commit d196e95

Please sign in to comment.