Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Apr 11, 2020
0 parents commit 0470095
Show file tree
Hide file tree
Showing 13 changed files with 2,295 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.eslintrc.js
/dist/
53 changes: 53 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

module.exports = {
// eslint-disable-next-line unicorn/prevent-abbreviations
env: {
es6: true,
},
extends: [
"plugin:unicorn/recommended",
"xo",
"xo-typescript",
"prettier",
"prettier/@typescript-eslint",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2018,
project: "tsconfig.json",
sourceType: "module",
},
plugins: ["sort-destructure-keys", "typescript-sort-keys", "unicorn"],
root: true,
rules: {
// @actions/github uses a lot of snake_case keys.
"@typescript-eslint/camelcase": "off",
// TypeScript is good at type inference and already requires types where they matter: exported symbols.
"@typescript-eslint/explicit-function-return-type": "off",
// We use sort-keys instead.
"@typescript-eslint/member-ordering": "off",
"arrow-body-style": "error",
// Forbid function declarations
"func-style": ["error", "expression", { allowArrowFunctions: true }],
"no-console": "error",
// TypeScript already takes care of that. See https://github.com/bradzacher/eslint-plugin-typescript/issues/110.
"no-undef": "off",
"object-shorthand": [
"error",
"always",
{ avoidExplicitReturnArrows: true },
],
"sort-destructure-keys/sort-destructure-keys": [
"error",
{ caseSensitive: false },
],
"sort-keys": [
"error",
"asc",
{ caseSensitive: false, minKeys: 2, natural: true },
],
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error",
},
};
15 changes: 15 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Publish
on:
push:
branches:
- master

jobs:
publish:
name: Publish
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- uses: dylanvann/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test
on:
push:
branches-ignore:
- master

jobs:
test:
name: Test
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: Install
run: yarn install --frozen-lockfile
- name: Build
run: yarn run build
- name: ESLint
run: yarn run eslint
- name: Prettier
run: yarn run check-prettier
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/node_modules
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2020 Thibault Derousseaux <[email protected]>

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# GitHub App Token

This [JavaScript GitHub Action](https://help.github.com/en/actions/building-actions/about-actions#javascript-actions) can be used to impersonate a GitHub App when `secrets.GITHUB_TOKEN`'s limitations are too restrictive and a personal access token is not suitable.

[`secrets.GITHUB_TOKEN`](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) has limitations such as [not being able to triggering a new workflow from another workflow](https://github.community/t5/GitHub-Actions/Triggering-a-new-workflow-from-another-workflow/td-p/31676). A workaround is to use a [personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) from a [personal user/bot account](https://help.github.com/en/github/getting-started-with-github/types-of-github-accounts#personal-user-accounts). However, for organizations, GitHub Apps are [a more appropriate automation solution](https://developer.github.com/apps/differences-between-apps/#machine-vs-bot-accounts).

# Example Workflow

```yml
jobs:
job:
runs-on: ubuntu-18.04
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- name: Use token
env:
TOKEN: ${{ steps.generate_token.outputs.token }}
run: |
echo "The generated token is masked: ${TOKEN}"
```
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: GitHub App token
author: Thibault Derousseaux <[email protected]>
description: Used to impersonate a GitHub App in a GitHub Action when secrets.GITHUB_TOKEN's limitations are too restrictive and a personal access token is not suitable.
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
outputs:
token:
description: An installation token for the GitHub App on the current repository.
runs:
using: node12
main: dist/index.js
branding:
icon: unlock
color: gray-dark
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "github-app-token",
"version": "1.0.0",
"license": "MIT",
"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}\""
},
"devDependencies": {
"@actions/core": "^1.2.3",
"@actions/github": "^2.1.1",
"@octokit/app": "^4.2.0",
"@types/is-base64": "^1.1.0",
"@types/node": "^10.0.3",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"@zeit/ncc": "^0.22.1",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-config-xo": "^0.29.1",
"eslint-config-xo-typescript": "^0.27.0",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-sort-destructure-keys": "^1.3.3",
"eslint-plugin-typescript-sort-keys": "^0.8.0",
"eslint-plugin-unicorn": "^18.0.1",
"is-base64": "^1.1.0",
"prettier": "^2.0.4",
"promise-retry": "^1.1.1",
"typescript": "^3.8.3"
}
}
5 changes: 5 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports = {
trailingComma: "all",
};
31 changes: 31 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getInput, setFailed, setOutput, setSecret } from "@actions/core";
import { context, GitHub } from "@actions/github";
import { App } from "@octokit/app";
import isBase64 from "is-base64";

const run = async () => {
try {
const id = Number(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 github = new GitHub(jwt);
const {
data: { id: installationId },
} = await github.apps.getRepoInstallation(context.repo);
const token = await app.getInstallationAccessToken({
installationId,
});
setSecret(token);
setOutput("token", token);
} catch (error) {
if (error instanceof Error) {
setFailed(error.message);
}
}
};

run();
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"noEmitOnError": true,
"strict": true,
"target": "esnext",
"types": ["node"]
},
"include": [".*.js", "*.js", "src"]
}
Loading

0 comments on commit 0470095

Please sign in to comment.