Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade deps and add test #64

Merged
merged 1 commit into from Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions .github/workflows/test.yml
Expand Up @@ -10,13 +10,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install
run: yarn install --frozen-lockfile
- name: Deduplicate dependencies
run: yarn run yarn-deduplicate --fail --strategy fewer
- name: Build
run: yarn run build
- name: Format
run: yarn run prettier --check
- name: Lint
run: yarn run xo
- run: yarn install --frozen-lockfile
- run: yarn run yarn-deduplicate --fail --strategy fewer
- run: yarn run build
- run: yarn run prettier --check
- run: yarn run xo
- id: generate_token
uses: ./
with:
app_id: ${{ vars.TEST_GITHUB_APP_ID }}
private_key: ${{ secrets.TEST_GITHUB_APP_PRIVATE_KEY }}
- run: node --eval "assert('${{ steps.generate_token.outputs.token }}'.length > 0);"
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2022 Thibault Derousseaux <tibdex@gmail.com>
Copyright (c) 2023 Thibault Derousseaux <tibdex@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -46,3 +46,5 @@ jobs:
run: |
echo "The generated token is masked: ${TOKEN}"
```

[Another use case for this action can (or could) be found in GitHub's own docs](https://web.archive.org/web/20230115194214/https://docs.github.com/en/issues/planning-and-tracking-with-projects/automating-your-project/automating-projects-using-actions#example-workflow-authenticating-with-a-github-app).
26 changes: 13 additions & 13 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "github-app-token",
"version": "1.7.0",
"version": "1.8.0",
"license": "MIT",
"type": "module",
"files": [
Expand All @@ -9,31 +9,31 @@
],
"scripts": {
"prebuild": "tsc --build",
"build": "ncc build src/index.ts --minify --target es2021 --v8-cache",
"build": "ncc build src/index.ts --minify --target es2021 --v8-cache",
"prettier": "prettier --ignore-path .gitignore \"./**/*.{cjs,js,json,md,ts,yml}\"",
"xo": "xo"
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@octokit/auth-app": "^4.0.7",
"@octokit/request": "^6.2.2",
"@octokit/auth-app": "^4.0.9",
"@octokit/request": "^6.2.3",
"ensure-error": "^4.0.0",
"is-base64": "^1.1.0"
},
"devDependencies": {
"@types/error-cause": "^1.0.1",
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
"@types/is-base64": "^1.1.1",
"@types/node": "^16.11.26",
"@vercel/ncc": "^0.34.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"@vercel/ncc": "^0.36.1",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-sort-destructure-keys": "^1.4.0",
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.3.0",
"typescript": "^4.8.4",
"xo": "^0.52.4",
"yarn-deduplicate": "^5.0.0"
"prettier": "^2.8.3",
"prettier-plugin-packagejson": "^2.4.0",
"typescript": "^4.9.4",
"xo": "^0.53.1",
"yarn-deduplicate": "^6.0.1"
}
}
4 changes: 4 additions & 0 deletions prettier.config.cjs
@@ -1,5 +1,9 @@
"use strict";

module.exports = {
importOrder: ["^node:(.*)$", "<THIRD_PARTY_MODULES>", "^[./]"],
importOrderGroupNamespaceSpecifiers: true,
importOrderSeparation: true,
importOrderSortSpecifiers: true,
trailingComma: "all",
};
5 changes: 2 additions & 3 deletions src/fetch-installation-token.ts
@@ -1,7 +1,6 @@
import { getOctokit } from "@actions/github";
import { createAppAuth } from "@octokit/auth-app";
import { request } from "@octokit/request";
import ensureError from "ensure-error";

export const fetchInstallationToken = async ({
appId,
Expand Down Expand Up @@ -42,7 +41,7 @@ export const fetchInstallationToken = async ({
} catch (error: unknown) {
throw new Error(
"Could not get repo installation. Is the app installed on this repo?",
{ cause: ensureError(error) },
{ cause: error },
);
}
}
Expand All @@ -56,7 +55,7 @@ export const fetchInstallationToken = async ({
return installation.token;
} catch (error: unknown) {
throw new Error("Could not create installation access token.", {
cause: ensureError(error),
cause: error,
});
}
};
88 changes: 43 additions & 45 deletions src/index.ts
@@ -1,51 +1,49 @@
import { Buffer } from "node:buffer";

import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";
import ensureError from "ensure-error";
import isBase64 from "is-base64";

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

const run = async () => {
try {
const appId = getInput("app_id", { required: true });

const installationIdInput = getInput("installation_id");
const installationId = installationIdInput
? Number(installationIdInput)
: undefined;

const permissionsInput = getInput("permissions");
const permissions = permissionsInput
? (JSON.parse(permissionsInput) as Record<string, string>)
: undefined;

const privateKeyInput = getInput("private_key", { required: true });
const privateKey = isBase64(privateKeyInput)
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

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

const githubApiUrlInput = getInput("github_api_url", { required: true });
const githubApiUrl = new URL(githubApiUrlInput);

const installationToken = await fetchInstallationToken({
appId,
githubApiUrl,
installationId,
owner,
permissions,
privateKey,
repo,
});

setSecret(installationToken);
setOutput("token", installationToken);
info("Token generated successfully!");
} catch (_error: unknown) {
const error = ensureError(_error);
setFailed(error);
}
};

void run();
try {
const appId = getInput("app_id", { required: true });

const installationIdInput = getInput("installation_id");
const installationId = installationIdInput
? Number(installationIdInput)
: undefined;

const permissionsInput = getInput("permissions");
const permissions = permissionsInput
? (JSON.parse(permissionsInput) as Record<string, string>)
: undefined;

const privateKeyInput = getInput("private_key", { required: true });
const privateKey = isBase64(privateKeyInput)
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

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

const githubApiUrlInput = getInput("github_api_url", { required: true });
const githubApiUrl = new URL(githubApiUrlInput);

const installationToken = await fetchInstallationToken({
appId,
githubApiUrl,
installationId,
owner,
permissions,
privateKey,
repo,
});

setSecret(installationToken);
setOutput("token", installationToken);
info("Token generated successfully!");
} catch (_error: unknown) {
const error = ensureError(_error);
setFailed(error);
}
8 changes: 4 additions & 4 deletions tsconfig.json
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"module": "nodenext",
"moduleResolution": "nodenext",
"module": "Node16",
"moduleResolution": "Node16",
"noEmit": true,
"strict": true,
"target": "es2021",
"types": ["node", "error-cause/auto"]
"target": "ES2022",
"types": ["node"]
},
"include": ["src"]
}
11 changes: 0 additions & 11 deletions xo.config.cjs
Expand Up @@ -31,16 +31,6 @@ module.exports = {
},
],
"import/no-namespace": "error",
"import/order": [
"error",
{
alphabetize: {
caseInsensitive: true,
order: "asc",
},
"newlines-between": "never",
},
],
"no-console": "error",
"object-shorthand": [
"error",
Expand All @@ -53,7 +43,6 @@ module.exports = {
caseSensitive: false,
},
],
"sort-imports": ["error", { ignoreDeclarationSort: true }],
"sort-keys": [
"error",
"asc",
Expand Down