Skip to content

Commit 5207c76

Browse files
committed
feat: export app name to env or save to secret
1 parent 88d3424 commit 5207c76

File tree

5 files changed

+79
-73
lines changed

5 files changed

+79
-73
lines changed

action.yml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@ inputs:
1111
fallback:
1212
description: The fallback token when bot token generate failed.
1313
required: false
14-
variable_name:
15-
description: The name of generated token in exported environment variable.
14+
app_login_name:
15+
description: The app login name exported to `env` or saved to `secrets`.
1616
required: false
17-
env_name:
18-
description: The name of generated token in exported environment variable.
17+
default: BOT_NAME
18+
app_token_name:
19+
description: The app token name exported to `env` or saved to `secrets`.
1920
required: false
20-
secret_name:
21-
description: The secret name created on current repository.
22-
required: false
23-
clean_secret:
24-
description: Should clean the secret or not when this action run completed.
21+
default: BOT_TOKEN
22+
secret:
23+
description: Specify true to save app token and app login name into the secrets of current repository.
2524
required: false
2625
default: false
26+
clean:
27+
description: Specify true to clean saved secrets when workflow run completed.
28+
required: false
29+
default: true
2730

2831
outputs:
29-
token:
30-
description: The token for the GitHub App on the current repository.
32+
bot_name:
33+
description: The name of the GitHub App on the current repository.
34+
bot_token:
35+
description: The token of the GitHub App on the current repository.
3136

3237
runs:
3338
using: node16

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-app-token",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"description": "Run GitHub Actions as a GitHub App by using the app's authentication token",
55
"main": "dist/index.js",
66
"files": [
@@ -43,15 +43,15 @@
4343
"@bubkoo/tsconfig": "^1.0.0",
4444
"@types/is-base64": "^1.1.0",
4545
"@types/libsodium-wrappers": "^0.7.10",
46-
"@types/node": "^18.11.7",
46+
"@types/node": "^18.11.9",
4747
"@vercel/ncc": "^0.34.0",
48-
"eslint": "^8.26.0",
49-
"husky": "^8.0.1",
48+
"eslint": "^8.28.0",
49+
"husky": "^8.0.2",
5050
"is-ci": "^3.0.1",
5151
"npm-run-all": "^4.1.5",
5252
"prettier": "^2.7.1",
5353
"pretty-quick": "^3.1.3",
5454
"rimraf": "^3.0.2",
55-
"typescript": "^4.8.4"
55+
"typescript": "^4.9.3"
5656
}
5757
}

src/action.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import * as util from './util'
44
export async function run() {
55
try {
66
const token = await util.getAppToken()
7-
core.info('Token generated!')
7+
const loginName = util.getAppLoginName()
8+
const tokenName = util.getAppTokenName()
89

9-
const secretName = core.getInput('secret_name')
10-
if (secretName) {
11-
await util.saveTokenToSecret(secretName, token)
12-
core.info(`Save token in secret "${secretName}"`)
10+
const saveToSecret = core.getBooleanInput('secret')
11+
if (saveToSecret) {
12+
await util.createSecret(token, loginName, token)
13+
await util.createSecret(token, tokenName, token)
14+
core.info(`Secrets "${loginName}" and "${tokenName}" was created`)
1315
}
1416

1517
core.setSecret(token)
16-
core.setOutput('token', token)
1718

18-
const envName = core.getInput('env_name') || core.getInput('variable_name')
19-
if (envName) {
20-
core.exportVariable(envName, token)
21-
}
19+
core.setOutput('bot_name', token)
20+
core.setOutput('bot_token', token)
21+
22+
core.exportVariable(loginName, token)
23+
core.exportVariable(tokenName, token)
2224
} catch (e) {
2325
core.error(e)
2426
core.setFailed(e.message)
@@ -27,10 +29,15 @@ export async function run() {
2729

2830
export async function cleanup() {
2931
try {
30-
const secretName = core.getInput('secret_name')
31-
if (secretName) {
32-
await util.removeTokenFromSecret(secretName)
33-
core.info(`Token in secret "${secretName}" was cleaned`)
32+
const clean = core.getBooleanInput('clean')
33+
const saveToSecret = core.getBooleanInput('secret')
34+
if (saveToSecret && clean) {
35+
const token = await util.getAppToken()
36+
const loginName = util.getAppLoginName()
37+
const tokenName = util.getAppTokenName()
38+
await util.deleteSecret(token, loginName)
39+
await util.deleteSecret(token, tokenName)
40+
core.info(`Secrets "${loginName}" and "${tokenName}" was removed`)
3441
}
3542
} catch (e) {
3643
core.error(e)

src/util.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import sodium from 'libsodium-wrappers'
55
import { Octokit } from '@octokit/core'
66
import { createAppAuth } from '@octokit/auth-app'
77

8+
export function getAppLoginName() {
9+
return core.getInput('app_login_name') || 'BOT_NAME'
10+
}
11+
12+
export function getAppTokenName() {
13+
return core.getInput('app_token_name') || 'BOT_TOKEN'
14+
}
15+
816
export async function getAppToken() {
917
const fallback = core.getInput('fallback')
1018
const required = fallback == null
@@ -29,22 +37,23 @@ export async function getAppToken() {
2937

3038
// 2. Get installationId of the app
3139
const octokit = github.getOctokit(jwt)
32-
const {
33-
data: { id: installationId },
34-
} = await octokit.rest.apps.getRepoInstallation({
40+
const install = await octokit.rest.apps.getRepoInstallation({
3541
...github.context.repo,
3642
})
3743

3844
// 3. Retrieve installation access token
3945
const { token } = await auth({
40-
installationId,
4146
type: 'installation',
47+
installationId: install.data.id,
4248
})
4349

50+
// eslint-disable-next-line no-console
51+
console.log(install.data)
52+
4453
return token
4554
}
4655

47-
async function createSecret(octokit: Octokit, value: string) {
56+
async function makeSecret(octokit: Octokit, value: string) {
4857
const { repo } = github.context
4958
const res = await octokit.request(
5059
'GET /repos/:owner/:repo/actions/secrets/public-key',
@@ -72,45 +81,30 @@ async function createSecret(octokit: Octokit, value: string) {
7281
}
7382
}
7483

75-
async function createOrUpdateRepoSecret(
84+
export async function createSecret(
7685
token: string,
77-
name: string,
78-
value: string,
86+
secretName: string,
87+
secretValue: string,
7988
) {
8089
const octokit = new Octokit({ auth: token })
81-
const secret = await createSecret(octokit, value)
90+
const secret = await makeSecret(octokit, secretValue)
8291
await octokit.request(
8392
'PUT /repos/:owner/:repo/actions/secrets/:secret_name',
8493
{
8594
...github.context.repo,
86-
secret_name: name,
95+
secret_name: secretName,
8796
data: secret,
8897
},
8998
)
9099
}
91100

92-
async function deleteSecret(token: string, name: string) {
101+
export async function deleteSecret(token: string, secretName: string) {
93102
const octokit = new Octokit({ auth: token })
94103
await octokit.request(
95104
'DELETE /repos/:owner/:repo/actions/secrets/:secret_name',
96105
{
97106
...github.context.repo,
98-
secret_name: name,
107+
secret_name: secretName,
99108
},
100109
)
101110
}
102-
103-
export async function saveTokenToSecret(secretName: string, token: string) {
104-
if (secretName) {
105-
return createOrUpdateRepoSecret(token, secretName, token)
106-
}
107-
return null
108-
}
109-
110-
export async function removeTokenFromSecret(secretName: string) {
111-
if (secretName) {
112-
const token = await getAppToken()
113-
return deleteSecret(token, secretName)
114-
}
115-
return null
116-
}

yarn.lock

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,10 @@
719719
resolved "https://registry.npmjs.org/@types/node/-/node-16.9.2.tgz"
720720
integrity sha512-ZHty/hKoOLZvSz6BtP1g7tc7nUeJhoCf3flLjh8ZEv1vFKBWHXcnMbJMyN/pftSljNyy0kNW/UqI3DccnBnZ8w==
721721

722-
"@types/node@^18.11.7":
723-
version "18.11.7"
724-
resolved "https://registry.npmmirror.com/@types/node/-/node-18.11.7.tgz#8ccef136f240770c1379d50100796a6952f01f94"
725-
integrity sha512-LhFTglglr63mNXUSRYD8A+ZAIu5sFqNJ4Y2fPuY7UlrySJH87rRRlhtVmMHplmfk5WkoJGmDjE9oiTfyX94CpQ==
722+
"@types/node@^18.11.9":
723+
version "18.11.9"
724+
resolved "https://registry.npmmirror.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4"
725+
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==
726726

727727
"@types/normalize-package-data@^2.4.0":
728728
version "2.4.1"
@@ -1508,10 +1508,10 @@ eslint-visitor-keys@^3.3.0:
15081508
resolved "https://registry.npmmirror.com/eslint-visitor-keys/-/eslint-visitor-keys-3.3.0.tgz#f6480fa6b1f30efe2d1968aa8ac745b862469826"
15091509
integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA==
15101510

1511-
eslint@^8.26.0:
1512-
version "8.26.0"
1513-
resolved "https://registry.npmmirror.com/eslint/-/eslint-8.26.0.tgz#2bcc8836e6c424c4ac26a5674a70d44d84f2181d"
1514-
integrity sha512-kzJkpaw1Bfwheq4VXUezFriD1GxszX6dUekM7Z3aC2o4hju+tsR/XyTC3RcoSD7jmy9VkPU3+N6YjVU2e96Oyg==
1511+
eslint@^8.28.0:
1512+
version "8.28.0"
1513+
resolved "https://registry.npmmirror.com/eslint/-/eslint-8.28.0.tgz#81a680732634677cc890134bcdd9fdfea8e63d6e"
1514+
integrity sha512-S27Di+EVyMxcHiwDrFzk8dJYAaD+/5SoWKxL1ri/71CRHsnJnRDPNt2Kzj24+MT9FDupf4aqqyqPrvI8MvQ4VQ==
15151515
dependencies:
15161516
"@eslint/eslintrc" "^1.3.3"
15171517
"@humanwhocodes/config-array" "^0.11.6"
@@ -1876,10 +1876,10 @@ human-signals@^1.1.1:
18761876
resolved "https://registry.npmjs.org/human-signals/-/human-signals-1.1.1.tgz"
18771877
integrity sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==
18781878

1879-
husky@^8.0.1:
1880-
version "8.0.1"
1881-
resolved "https://registry.npmmirror.com/husky/-/husky-8.0.1.tgz#511cb3e57de3e3190514ae49ed50f6bc3f50b3e9"
1882-
integrity sha512-xs7/chUH/CKdOCs7Zy0Aev9e/dKOMZf3K1Az1nar3tzlv0jfqnYtu235bstsWTmXOR0EfINrPa97yy4Lz6RiKw==
1879+
husky@^8.0.2:
1880+
version "8.0.2"
1881+
resolved "https://registry.npmmirror.com/husky/-/husky-8.0.2.tgz#5816a60db02650f1f22c8b69b928fd6bcd77a236"
1882+
integrity sha512-Tkv80jtvbnkK3mYWxPZePGFpQ/tT3HNSs/sasF9P2YfkMezDl3ON37YN6jUUI4eTg5LcyVynlb6r4eyvOmspvg==
18831883

18841884
ignore@^5.0.5, ignore@^5.1.4:
18851885
version "5.1.8"
@@ -3147,10 +3147,10 @@ type-fest@^0.8.1:
31473147
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz"
31483148
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==
31493149

3150-
typescript@^4.8.4:
3151-
version "4.8.4"
3152-
resolved "https://registry.npmmirror.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
3153-
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==
3150+
typescript@^4.9.3:
3151+
version "4.9.3"
3152+
resolved "https://registry.npmmirror.com/typescript/-/typescript-4.9.3.tgz#3aea307c1746b8c384435d8ac36b8a2e580d85db"
3153+
integrity sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==
31543154

31553155
unbox-primitive@^1.0.1:
31563156
version "1.0.1"

0 commit comments

Comments
 (0)