Skip to content

Commit 1c1811e

Browse files
committed
feat: ✨ post action to cleanup secret
1 parent ef42837 commit 1c1811e

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ outputs:
1616
runs:
1717
using: node12
1818
main: dist/index.js
19+
post: dist/index.js
1920
branding:
2021
icon: smile
2122
color: orange # gray-dark purple red orange green blue yellow black white

src/action.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import isBase64 from 'is-base64'
55
import { Util } from './util'
66

77
export namespace Action {
8-
export async function start() {
8+
let token: string
9+
10+
export async function run() {
911
try {
1012
const id = Number(core.getInput('APP_ID', { required: true }))
1113
const privateKeyInput = core.getInput('PRIVATE_KEY', { required: true })
@@ -18,7 +20,8 @@ export namespace Action {
1820
const {
1921
data: { id: installationId },
2022
} = await octokit.apps.getRepoInstallation(github.context.repo)
21-
const token = await app.getInstallationAccessToken({
23+
24+
token = await app.getInstallationAccessToken({
2225
installationId,
2326
})
2427

@@ -35,4 +38,16 @@ export namespace Action {
3538
core.setFailed(e.message)
3639
}
3740
}
41+
42+
export async function cleanup() {
43+
try {
44+
const secretName = core.getInput('SECRET_NAME')
45+
if (secretName) {
46+
await Util.deleteSecret(token, secretName)
47+
}
48+
} catch (e) {
49+
core.error(e)
50+
core.setFailed(e.message)
51+
}
52+
}
3853
}

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import { Action } from './action'
22

3-
Action.start()
3+
const isPost = !!process.env['STATE_isPost']
4+
5+
if (!isPost) {
6+
Action.run()
7+
} else {
8+
Action.cleanup()
9+
}

src/util.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import * as core from '@actions/core'
2-
import * as github from '@actions/github'
1+
import { context } from '@actions/github'
32
import { Octokit } from '@octokit/core'
43
import sodium from 'tweetsodium'
54

65
export namespace Util {
7-
export function getOctokit() {
8-
const token = core.getInput('GITHUB_TOKEN', { required: true })
9-
return github.getOctokit(token)
10-
}
11-
126
async function createSecret(octokit: Octokit, value: string) {
13-
const repo = github.context.repo
7+
const repo = context.repo
148
const res = await octokit.request(
159
'GET /repos/:owner/:repo/actions/secrets/public-key',
1610
repo,
@@ -37,23 +31,26 @@ export namespace Util {
3731
name: string,
3832
value: string,
3933
) {
40-
try {
41-
const octokit = new Octokit({ auth: token })
42-
const secret = await createSecret(octokit, value)
43-
core.info(`created secret: ${JSON.stringify(secret, null, 2)}`)
34+
const octokit = new Octokit({ auth: token })
35+
const secret = await createSecret(octokit, value)
36+
await octokit.request(
37+
'PUT /repos/:owner/:repo/actions/secrets/:secret_name',
38+
{
39+
...context.repo,
40+
secret_name: name,
41+
data: secret,
42+
},
43+
)
44+
}
4445

45-
await octokit.request(
46-
'PUT /repos/:owner/:repo/actions/secrets/:secret_name',
47-
{
48-
...github.context.repo,
49-
secret_name: name,
50-
data: secret,
51-
},
52-
)
53-
} catch (e) {
54-
core.error(e)
55-
core.error(JSON.stringify(e, null, 2))
56-
core.setFailed(e.message)
57-
}
46+
export async function deleteSecret(token: string, name: string) {
47+
const octokit = new Octokit({ auth: token })
48+
await octokit.request(
49+
'DELETE /repos/:owner/:repo/actions/secrets/:secret_name',
50+
{
51+
...context.repo,
52+
secret_name: name,
53+
},
54+
)
5855
}
5956
}

0 commit comments

Comments
 (0)