This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
secrets.js
95 lines (77 loc) · 2.9 KB
/
secrets.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const core = require('@actions/core');
const github = require('@actions/github');
const styles = require('ansi-styles');
const sodium = require('tweetsodium');
/**
* Set a secret for an organization.
*
* @param {string} pat Personal access token to use
* @param {string} secret_name Name of the secret to save
* @param {string} key Fastlane session data
*/
async function setOrgSecret(pat, secret_name, key) {
const { context, getOctokit } = github;
const octokit = getOctokit(pat);
core.info(`${styles.cyanBright.open}===> Getting organization public key...`);
const { data: { key_id, key: publicKey } } = await octokit.actions.getOrgPublicKey({
org: context.repo.owner,
});
const secretBytes = Buffer.from(key);
const keyBytes = Buffer.from(publicKey, 'base64');
const encryptedBytes = sodium.seal(secretBytes, keyBytes);
const encrypted_value = Buffer.from(encryptedBytes).toString('base64');
await octokit.actions.createOrUpdateOrgSecret({
org: context.repo.owner,
secret_name,
encrypted_value,
key_id,
visibility: 'all',
});
core.info(`${styles.green.open}===> Created org secret!`);
}
/**
* Set a secret on the repository running our Action.
*
* @param {string} secret_name Name of the secret we're saving
* @param {string} key The Fastlane session
*/
async function setRepositorySecret(pat, secret_name, key) {
const octokit = github.getOctokit(pat);
const { context } = github;
core.info(`${styles.cyanBright.open}===> Getting repository public key...`);
const { data: publicKey } = await octokit.actions.getRepoPublicKey(context.repo);
const secretBytes = Buffer.from(key);
const keyBytes = Buffer.from(publicKey.key, 'base64');
const encryptedBytes = sodium.seal(secretBytes, keyBytes);
const encrypted_value = Buffer.from(encryptedBytes).toString('base64');
await octokit.actions.createOrUpdateRepoSecret({
...context.repo,
secret_name,
encrypted_value,
key_id: publicKey.key_id,
});
core.info(`${styles.green.open}===> Created repository secret!`);
}
/**
* Set the Github Secret as per the Action inputs
*
* @param {string} key The Fastlane session
*/
module.exports = async function(key) {
//
console.log("We have a key that starts with: ", key.substring(0, 10));
console.log("We have a key that ends with: ", key.substring(key.length - 10));
const secret = core.getInput('github_pat');
if (!secret) {
core.warning(`${styles.yellow.open} WARNING: No Github Token provided. Skipping setting your secret...`);
return;
}
const repoSecretName = core.getInput('repo_secret_name');
if (repoSecretName) {
setRepositorySecret(secret, repoSecretName, key);
}
const orgSecretName = core.getInput('org_secret_name');
if (orgSecretName) {
setOrgSecret(secret, orgSecretName, key);
}
}