forked from hmanzur/actions-set-secret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (48 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const Core = require('@actions/core')
const Api = require('./src/api')
/**
* Set secrets in GitHub repo
* This actions is participating in #ActionsHackathon 2020
*
* @param {Api} api - Api instance
* @param {string} secret_name - Secret key name
* @param {string} secret_value - Secret raw value
* @see https://developer.github.com/v3/actions/secrets/#create-or-update-an-organization-secret
* @see https://dev.to/devteam/announcing-the-github-actions-hackathon-on-dev-3ljn
* @see https://dev.to/habibmanzur/placeholder-title-5e62
*/
const boostrap = async (api, secret_name, secret_value) => {
try {
const {key_id, key} = await api.getPublicKey()
const data = await api.createSecret(key_id, key, secret_name, secret_value)
if (api.isOrg()) {
data.visibility = Core.getInput('visibility')
if (data.visibility === 'selected') {
data.selected_repository_ids = Core.getInput('selected_repository_ids')
}
}
const response = await api.setSecret(data, secret_name)
console.error(response.status, response.data)
if (response.status >= 400) {
Core.setFailed(response.data)
} else {
Core.setOutput('status', response.status)
Core.setOutput('data', response.data)
}
} catch (e) {
Core.setFailed(e.message)
console.error(e)
}
}
try {
// `who-to-greet` input defined in action metadata file
const name = Core.getInput('name')
const value = Core.getInput('value')
const repository = Core.getInput('repository')
const token = Core.getInput('token')
const org = Core.getInput('org')
const api = new Api(token, repository, !!org)
boostrap(api, name, value)
} catch (error) {
Core.setFailed(error.message)
}