Skip to content

Commit

Permalink
Update dependencies and support custom installation_id (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex authored Nov 6, 2021
1 parent 516ef60 commit b077f95
Show file tree
Hide file tree
Showing 13 changed files with 2,011 additions and 825 deletions.
52 changes: 0 additions & 52 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ on:
jobs:
publish:
name: Publish
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: dylanvann/publish-github-action@v1.1.49
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
14 changes: 8 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ on:
jobs:
test:
name: Test
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Install
run: yarn install --frozen-lockfile
- name: Deduplicate dependencies
run: yarn run yarn-deduplicate --fail
- name: Build
run: yarn run build
- name: ESLint
run: yarn run eslint
- name: Prettier
run: yarn run check-prettier
- name: Format
run: yarn run prettier --check
- name: Lint
run: yarn run xo
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2020 Thibault Derousseaux <tibdex@gmail.com>
Copyright (c) 2021 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ However, for organizations, GitHub Apps are [a more appropriate automation solut
```yml
jobs:
job:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
# Optional (defaults to ID of the repository's installation).
# installation_id: 1337
# Optional (defaults to the current repository).
# repository: owner/repo
# repository: "owner/repo"
- name: Use token
env:
TOKEN: ${{ steps.generate_token.outputs.token }}
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ inputs:
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).
outputs:
Expand Down
48 changes: 21 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
{
"name": "github-app-token",
"version": "1.4.0",
"version": "1.5.0",
"license": "MIT",
"main": "dist/index.js",
"files": [
"action.yml",
"dist"
],
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/index.ts --minify --v8-cache",
"check-prettier": "yarn run prettier --check",
"eslint": "eslint --ignore-path .gitignore --max-warnings 0 \"./**/*.{js,ts}\"",
"format-prettier": "yarn run prettier --write",
"prettier": "prettier --ignore-path .gitignore \"./**/*.{js,json,md,ts,yml}\""
"build": "ncc build src/index.ts --minify --target es2018 --v8-cache",
"prettier": "prettier --ignore-path .gitignore \"./**/*.{js,json,md,ts,yml}\"",
"xo": "xo"
},
"devDependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"@octokit/auth-app": "^2.10.5",
"@types/is-base64": "^1.1.0",
"@types/node": "^14.14.14",
"@typescript-eslint/eslint-plugin": "^4.10.0",
"@typescript-eslint/parser": "^4.10.0",
"@vercel/ncc": "^0.26.1",
"eslint": "^7.16.0",
"eslint-config-prettier": "^7.1.0",
"eslint-config-xo": "^0.33.1",
"eslint-config-xo-typescript": "^0.37.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": "^24.0.0",
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0",
"@octokit/auth-app": "^3.6.1",
"@octokit/request": "^5.6.2",
"@types/is-base64": "^1.1.1",
"@types/node": "^16.11.6",
"@vercel/ncc": "^0.31.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-sort-destructure-keys": "^1.4.0",
"eslint-plugin-typescript-sort-keys": "^2.0.0",
"is-base64": "^1.1.0",
"prettier": "^2.2.1",
"promise-retry": "^2.0.1",
"typescript": "^4.1.3"
"prettier": "^2.4.1",
"prettier-plugin-packagejson": "^2.2.13",
"typescript": "^4.4.4",
"xo": "^0.46.4",
"yarn-deduplicate": "^3.1.0"
}
}
File renamed without changes.
21 changes: 14 additions & 7 deletions src/fetch-installation-token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { env } from "process";
import { getOctokit } from "@actions/github";
import { createAppAuth } from "@octokit/auth-app";
import { request } from "@octokit/request";

export const fetchInstallationToken = async ({
appId,
installationId,
owner,
privateKey,
repo,
}: Readonly<{
appId: string;
installationId?: number;
owner: string;
privateKey: string;
repo: string;
Expand All @@ -19,14 +22,18 @@ export const fetchInstallationToken = async ({
request: request.defaults({
// 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.
baseUrl: process.env["GITHUB_API_URL"]
})
baseUrl: env.GITHUB_API_URL,
}),
});
const authApp = await app({ type: "app" });
const octokit = getOctokit(authApp.token);
const {
data: { id: installationId },
} = await octokit.apps.getRepoInstallation({ owner, repo });

if (installationId === undefined) {
const authApp = await app({ type: "app" });
const octokit = getOctokit(authApp.token);
({
data: { id: installationId },
} = await octokit.rest.apps.getRepoInstallation({ owner, repo }));
}

const installation = await app({ installationId, type: "installation" });
return installation.token;
};
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Buffer } from "buffer";
import { getInput, info, setFailed, setOutput, setSecret } from "@actions/core";
import { context } from "@actions/github";

import isBase64 from "is-base64";

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

const run = async () => {
Expand All @@ -13,13 +12,15 @@ const run = async () => {
? Buffer.from(privateKeyInput, "base64").toString("utf8")
: privateKeyInput;

const installationId = getInput("installation_id");
const repositoryInput = getInput("repository");
const [owner, repo] = repositoryInput
? repositoryInput.split("/")
: [context.repo.owner, context.repo.repo];

const installationToken = await fetchInstallationToken({
appId,
installationId: installationId ? Number(installationId) : undefined,
owner,
privateKey,
repo,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"target": "esnext",
"types": ["node"]
},
"include": [".*.js", "*.js", "src"]
"include": ["src"]
}
61 changes: 61 additions & 0 deletions xo.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";

module.exports = {
extends: ["prettier"],
overrides: [
{
files: "**/*.{ts,tsx}",
rules: {
// Covered by TypeScript.
"default-case": "off",
},
},
],
plugins: ["sort-destructure-keys", "typescript-sort-keys"],
rules: {
// Forbid function declarations.
"func-style": ["error", "expression", { allowArrowFunctions: true }],
// Already taken care of by TypeScript.
"import/namespace": "off",
// Named export are better for static analysis.
// See https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/
"import/no-default-export": "error",
"import/no-namespace": "error",
"import/order": [
"error",
{
alphabetize: {
caseInsensitive: true,
order: "asc",
},
"newlines-between": "never",
},
],
"no-console": "error",
"object-shorthand": [
"error",
"always",
{ avoidExplicitReturnArrows: true },
],
"sort-destructure-keys/sort-destructure-keys": [
"error",
{
caseSensitive: false,
},
],
"sort-imports": ["error", { ignoreDeclarationSort: true }],
"sort-keys": [
"error",
"asc",
{
caseSensitive: false,
minKeys: 2,
natural: true,
},
],
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error",
// Not supported by Node.js 12.
"unicorn/prefer-node-protocol": "off",
},
};
Loading

0 comments on commit b077f95

Please sign in to comment.