|
1 |
| -import { context } from '@actions/github' |
| 1 | +import { context, getOctokit } from '@actions/github' |
2 | 2 | import { Octokit } from '@octokit/core'
|
| 3 | +import { getInput } from '@actions/core' |
| 4 | +import { App } from '@octokit/app' |
| 5 | +import isBase64 from 'is-base64' |
3 | 6 | import sodium from 'tweetsodium'
|
4 | 7 |
|
5 | 8 | export namespace Util {
|
6 |
| - async function createSecret(octokit: Octokit, value: string) { |
| 9 | + export async function getAppToken() { |
| 10 | + const id = Number(getInput('APP_ID', { required: true })) |
| 11 | + const privateKeyInput = getInput('PRIVATE_KEY', { required: true }) |
| 12 | + const privateKey = isBase64(privateKeyInput) |
| 13 | + ? Buffer.from(privateKeyInput, 'base64').toString('utf8') |
| 14 | + : privateKeyInput |
| 15 | + const app = new App({ id, privateKey }) |
| 16 | + const jwt = app.getSignedJsonWebToken() |
| 17 | + const octokit = getOctokit(jwt) |
| 18 | + const { |
| 19 | + data: { id: installationId }, |
| 20 | + } = await octokit.apps.getRepoInstallation(context.repo) |
| 21 | + |
| 22 | + return app.getInstallationAccessToken({ |
| 23 | + installationId, |
| 24 | + }) |
| 25 | + } |
| 26 | + |
| 27 | + export async function saveAppTokenToSecret(token: string) { |
| 28 | + const secretName = getInput('SECRET_NAME') |
| 29 | + if (secretName) { |
| 30 | + return createOrUpdateRepoSecret(token, secretName, token) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + export async function removeAppTokenFromSecret() { |
| 35 | + const secretName = getInput('SECRET_NAME') |
| 36 | + if (secretName) { |
| 37 | + const token = await getAppToken() |
| 38 | + return Util.deleteSecret(token, secretName) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + export async function createSecret(octokit: Octokit, value: string) { |
7 | 43 | const repo = context.repo
|
8 | 44 | const res = await octokit.request(
|
9 | 45 | 'GET /repos/:owner/:repo/actions/secrets/public-key',
|
|
0 commit comments