Skip to content

Commit

Permalink
Upgrade deps and cleanup (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Oct 16, 2022
1 parent fba8a67 commit da40f50
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 397 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Install
run: yarn install --frozen-lockfile
- name: Deduplicate dependencies
run: yarn run yarn-deduplicate --fail
run: yarn run yarn-deduplicate --fail --strategy fewer
- name: Build
run: yarn run build
- name: Format
Expand Down
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -24,18 +24,21 @@ jobs:
with:
app_id: ${{ secrets.APP_ID }}

# Optional (defaults to ID of the repository's installation).
# Optional.
# github_api_url: https://api.example.com

# Optional.
# installation_id: 1337

# Optional (defaults to all the Github App permissions).
# Optional.
# Using a YAML multiline string to avoid escaping the JSON quotes.
# permissions: >-
# {"members": "read"}

private_key: ${{ secrets.PRIVATE_KEY }}

# Optional (defaults to the current repository).
# repository: "owner/repo"
# Optional.
# repository: owner/repo

- name: Use token
env:
Expand Down
16 changes: 9 additions & 7 deletions action.yml
Expand Up @@ -5,17 +5,19 @@ inputs:
app_id:
description: ID of the GitHub App.
required: true
private_key:
description: Private key of the GitHub App (can be Base64 encoded).
required: true
installation_id:
description: The ID of the installation for which the token will be requested (defaults to the ID of the repository's installation).
repository:
description: The full name of the repository for which the token will be requested (defaults to the current repository).
github_api_url:
description: The API URL of the GitHub server.
default: ${{ github.api_url }}
permissions:
description: The JSON-stringified permissions granted to the token (defaults to all the GitHub app permissions, see https://docs.github.com/en/rest/apps/apps#create-an-installation-access-token-for-an-app).
github_api_url:
description: The API URL of the GitHub server, such as https://api.github.com. Defaults to the environment variable GITHUB_API_URL, see https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables.
private_key:
description: Private key of the GitHub App (can be Base64 encoded).
required: true
repository:
description: The full name of the repository for which the token will be requested.
default: ${{ github.repository }}
outputs:
token:
description: An installation token for the GitHub App on the requested repository.
Expand Down
16 changes: 8 additions & 8 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "github-app-token",
"version": "1.6.0",
"version": "1.7.0",
"license": "MIT",
"type": "module",
"files": [
Expand All @@ -15,9 +15,9 @@
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.0.3",
"@octokit/auth-app": "^4.0.4",
"@octokit/request": "^6.0.2",
"@actions/github": "^5.1.1",
"@octokit/auth-app": "^4.0.7",
"@octokit/request": "^6.2.2",
"ensure-error": "^4.0.0",
"is-base64": "^1.1.0"
},
Expand All @@ -30,10 +30,10 @@
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-sort-destructure-keys": "^1.4.0",
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"prettier": "^2.6.2",
"prettier-plugin-packagejson": "^2.2.18",
"typescript": "^4.7.4",
"xo": "^0.50.0",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.3.0",
"typescript": "^4.8.4",
"xo": "^0.52.4",
"yarn-deduplicate": "^5.0.0"
}
}
12 changes: 8 additions & 4 deletions src/fetch-installation-token.ts
Expand Up @@ -5,15 +5,15 @@ import ensureError from "ensure-error";

export const fetchInstallationToken = async ({
appId,
baseUrl,
githubApiUrl,
installationId,
owner,
permissions,
privateKey,
repo,
}: Readonly<{
appId: string;
baseUrl: URL;
githubApiUrl: URL;
installationId?: number;
owner: string;
permissions?: Record<string, string>;
Expand All @@ -24,12 +24,16 @@ export const fetchInstallationToken = async ({
appId,
privateKey,
request: request.defaults({
baseUrl: baseUrl.toString().replace(/\/+$/, ''),
baseUrl: githubApiUrl
.toString()
// Remove optional trailing `/`.
.replace(/\/$/, ""),
}),
});

const authApp = await app({ type: "app" });
const octokit = getOctokit(authApp.token);

if (installationId === undefined) {
try {
({
Expand All @@ -49,7 +53,7 @@ export const fetchInstallationToken = async ({
installation_id: installationId,
permissions,
});
return installation?.token;
return installation.token;
} catch (error: unknown) {
throw new Error("Could not create installation access token.", {
cause: ensureError(error),
Expand Down
18 changes: 5 additions & 13 deletions src/index.ts
@@ -1,7 +1,5 @@
import { Buffer } from "node:buffer";
import { env } from "node:process";
import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";
import { context } from "@actions/github";
import ensureError from "ensure-error";
import isBase64 from "is-base64";
import { fetchInstallationToken } from "./fetch-installation-token.js";
Expand All @@ -25,21 +23,15 @@ const run = async () => {
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

const repositoryInput = getInput("repository");
const [owner, repo] = repositoryInput
? repositoryInput.split("/")
: [context.repo.owner, context.repo.repo];
const repositoryInput = getInput("repository", { required: true });
const [owner, repo] = repositoryInput.split("/");

// GITHUB_API_URL is part of GitHub Actions' built-in environment variables.
// See https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables.
const githubUrlInput = getInput("github_api_url");
const baseUrl = githubUrlInput
? new URL(githubUrlInput)
: new URL(env.GITHUB_API_URL!);
const githubApiUrlInput = getInput("github_api_url", { required: true });
const githubApiUrl = new URL(githubApiUrlInput);

const installationToken = await fetchInstallationToken({
appId,
baseUrl,
githubApiUrl,
installationId,
owner,
permissions,
Expand Down

0 comments on commit da40f50

Please sign in to comment.