Skip to content

Commit 1af3528

Browse files
committed
fix: 🐛 post action
1 parent 1c1811e commit 1af3528

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ jobs:
1717
APP_ID: ${{ secrets.APP_ID }}
1818
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
1919
SECRET_NAME: APP_TOKEN
20-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

src/action.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
import * as core from '@actions/core'
2-
import * as github from '@actions/github'
3-
import { App } from '@octokit/app'
4-
import isBase64 from 'is-base64'
52
import { Util } from './util'
63

74
export namespace Action {
8-
let token: string
9-
105
export async function run() {
116
try {
12-
const id = Number(core.getInput('APP_ID', { required: true }))
13-
const privateKeyInput = core.getInput('PRIVATE_KEY', { required: true })
14-
const privateKey = isBase64(privateKeyInput)
15-
? Buffer.from(privateKeyInput, 'base64').toString('utf8')
16-
: privateKeyInput
17-
const app = new App({ id, privateKey })
18-
const jwt = app.getSignedJsonWebToken()
19-
const octokit = github.getOctokit(jwt)
20-
const {
21-
data: { id: installationId },
22-
} = await octokit.apps.getRepoInstallation(github.context.repo)
23-
24-
token = await app.getInstallationAccessToken({
25-
installationId,
26-
})
27-
28-
const secretName = core.getInput('SECRET_NAME')
29-
if (secretName) {
30-
await Util.createOrUpdateRepoSecret(token, secretName, token)
31-
}
32-
7+
const token = await Util.getAppToken()
8+
await Util.saveAppTokenToSecret(token)
339
core.setSecret(token)
3410
core.setOutput('token', token)
3511
core.info('Token generated successfully!')
@@ -41,10 +17,7 @@ export namespace Action {
4117

4218
export async function cleanup() {
4319
try {
44-
const secretName = core.getInput('SECRET_NAME')
45-
if (secretName) {
46-
await Util.deleteSecret(token, secretName)
47-
}
20+
await Util.removeAppTokenFromSecret()
4821
} catch (e) {
4922
core.error(e)
5023
core.setFailed(e.message)

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Action } from './action'
22

33
const isPost = !!process.env['STATE_isPost']
44

5+
console.log(JSON.stringify(process.env, null, 2))
6+
57
if (!isPost) {
68
Action.run()
79
} else {

src/util.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
1-
import { context } from '@actions/github'
1+
import { context, getOctokit } from '@actions/github'
22
import { Octokit } from '@octokit/core'
3+
import { getInput } from '@actions/core'
4+
import { App } from '@octokit/app'
5+
import isBase64 from 'is-base64'
36
import sodium from 'tweetsodium'
47

58
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) {
743
const repo = context.repo
844
const res = await octokit.request(
945
'GET /repos/:owner/:repo/actions/secrets/public-key',

0 commit comments

Comments
 (0)