|
| 1 | +import * as TYPES from '../types/types.js' // eslint-disable-line |
| 2 | +import { request } from '@octokit/request' |
| 3 | +import chalk from 'chalk' |
| 4 | +import ora from 'ora' |
| 5 | +import _sodium from 'libsodium-wrappers' |
| 6 | + |
| 7 | +/** |
| 8 | +* @ignore |
| 9 | +* @typedef {TYPES.LOCALDATA} LOCALDATA {@link LOCALDATA} |
| 10 | +*/ |
| 11 | + |
| 12 | +/** |
| 13 | + * #### add repository secrets |
| 14 | + * @async |
| 15 | + * @memberof GITHUB |
| 16 | + * @param {LOCALDATA} data - data object |
| 17 | + * @returns undefined |
| 18 | + */ |
| 19 | +async function addRepositorySecrets (data) { |
| 20 | + const spinner = ora('Add repository secrets').start() |
| 21 | + try { |
| 22 | + const { hubspotPortalIdSecret, hubspotPersonalAccessKeySecret, publicKeyId } = await encryptSecrets(data) |
| 23 | + await request('PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}', { |
| 24 | + headers: { |
| 25 | + authorization: `token ${process.env.GITHUB_TOKEN}` |
| 26 | + }, |
| 27 | + owner: data.prompts?.repoFromTmpl?.newRepoOwner || '', |
| 28 | + repo: data.prompts?.repoFromTmpl?.newRepoName || '', |
| 29 | + secret_name: 'HUBSPOT_PORTAL_ID', |
| 30 | + encrypted_value: hubspotPortalIdSecret, |
| 31 | + key_id: publicKeyId |
| 32 | + }) |
| 33 | + await request('PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}', { |
| 34 | + headers: { |
| 35 | + authorization: `token ${process.env.GITHUB_TOKEN}` |
| 36 | + }, |
| 37 | + owner: data.prompts?.repoFromTmpl?.newRepoOwner || '', |
| 38 | + repo: data.prompts?.repoFromTmpl?.newRepoName || '', |
| 39 | + secret_name: 'HUBSPOT_PERSONAL_ACCESS_KEY', |
| 40 | + encrypted_value: hubspotPersonalAccessKeySecret, |
| 41 | + key_id: publicKeyId |
| 42 | + }) |
| 43 | + spinner.succeed() |
| 44 | + } catch (error) { |
| 45 | + spinner.fail() |
| 46 | + console.error(`${chalk.red('Error:')} ${error.message}`) |
| 47 | + if (process.env.RH_MODE === 'debug') { |
| 48 | + console.error(error) |
| 49 | + } |
| 50 | + process.exit(1) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * #### get a repository public key |
| 56 | + * @async |
| 57 | + * @private |
| 58 | + * @memberof GITHUB |
| 59 | + * @param {LOCALDATA} data - data object |
| 60 | + * @returns {Promise<{publicKey: string, publicKeyId: string}>} - public key |
| 61 | + */ |
| 62 | +async function getRepositoryPublicKey (data) { |
| 63 | + const spinner = ora('Get repository public key').start() |
| 64 | + try { |
| 65 | + const publicKeyResponce = await request('GET /repos/{owner}/{repo}/actions/secrets/public-key', { |
| 66 | + headers: { |
| 67 | + authorization: `token ${process.env.GITHUB_TOKEN}` |
| 68 | + }, |
| 69 | + owner: data.prompts?.repoFromTmpl?.newRepoOwner || '', |
| 70 | + repo: data.prompts?.repoFromTmpl?.newRepoName || '' |
| 71 | + }) |
| 72 | + const publicKey = publicKeyResponce.data.key |
| 73 | + const publicKeyId = publicKeyResponce.data.key_id |
| 74 | + spinner.succeed() |
| 75 | + return { publicKey, publicKeyId } |
| 76 | + } catch (error) { |
| 77 | + spinner.fail() |
| 78 | + console.error(`${chalk.red('Error:')} ${error.message}`) |
| 79 | + if (process.env.RH_MODE === 'debug') { |
| 80 | + console.error(error) |
| 81 | + } |
| 82 | + process.exit(1) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * #### Encrypt secrets for the REST API |
| 88 | + * @async |
| 89 | + * @private |
| 90 | + * @memberof GITHUB |
| 91 | + * @param {LOCALDATA} data - data object |
| 92 | + * @returns {Promise<{hubspotPortalIdSecret: string, hubspotPersonalAccessKeySecret: string, publicKeyId: string}>} - encrypted secrets and public key ID |
| 93 | + */ |
| 94 | +async function encryptSecrets (data) { |
| 95 | + const spinner = ora('Encrypting secret').start() |
| 96 | + try { |
| 97 | + const hubspotPortalIdSecretVal = data.prompts?.repoFromTmpl?.repoSecrets?.hubspotPortalId || '' |
| 98 | + const hubspotPersonalAccessKeySecretVal = data.prompts?.repoFromTmpl?.repoSecrets?.hubspotPersonalAccessKey || '' |
| 99 | + const publicKey = await getRepositoryPublicKey(data) |
| 100 | + await _sodium.ready |
| 101 | + const sodium = _sodium |
| 102 | + |
| 103 | + // Convert the secret and key to a Uint8Array. |
| 104 | + const binkey = sodium.from_base64(publicKey.publicKey, sodium.base64_variants.ORIGINAL) |
| 105 | + const binHubspotPortalIdSecret = sodium.from_string(hubspotPortalIdSecretVal) |
| 106 | + const binHubspotPersonalAccessKeySecret = sodium.from_string(hubspotPersonalAccessKeySecretVal) |
| 107 | + |
| 108 | + // Encrypt the secret using libsodium |
| 109 | + const encryptHubspotPortalIdSecret = sodium.crypto_box_seal(binHubspotPortalIdSecret, binkey) |
| 110 | + const encryptHubspotPersonalAccessKeySecret = sodium.crypto_box_seal(binHubspotPersonalAccessKeySecret, binkey) |
| 111 | + |
| 112 | + // Convert the encrypted Uint8Array to Base64 |
| 113 | + const hubspotPortalIdSecret = sodium.to_base64(encryptHubspotPortalIdSecret, sodium.base64_variants.ORIGINAL) |
| 114 | + const hubspotPersonalAccessKeySecret = sodium.to_base64(encryptHubspotPersonalAccessKeySecret, sodium.base64_variants.ORIGINAL) |
| 115 | + spinner.succeed() |
| 116 | + return { hubspotPortalIdSecret, hubspotPersonalAccessKeySecret, publicKeyId: publicKey.publicKeyId } |
| 117 | + } catch (error) { |
| 118 | + spinner.fail() |
| 119 | + console.error(`${chalk.red('Error:')} ${error.message}`) |
| 120 | + if (process.env.RH_MODE === 'debug') { |
| 121 | + console.error(error) |
| 122 | + } |
| 123 | + process.exit(1) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export { addRepositorySecrets } |
0 commit comments