(flag: boolean)
| 20 | }; |
| 21 | |
| 22 | const toggleMerge = async (flag: boolean) => { |
| 23 | logger.log(`Setting 'ENABLE_MERGE' flag to '${flag}' for repository '${OWNER}/${REPOSITORY}'`); |
| 24 | |
| 25 | const octokit = initializeOctokit(); |
| 26 | |
| 27 | const goPubKey = await go(() => |
| 28 | octokit.request(`GET /repos/${OWNER}/${REPOSITORY}/actions/secrets/public-key`, { |
| 29 | owner: OWNER, |
| 30 | repo: REPOSITORY, |
| 31 | }) |
| 32 | ); |
| 33 | if (!goPubKey.success) { |
| 34 | throw new Error(`Can't obtain GitHub repository public key: ${goPubKey.error}`); |
| 35 | } |
| 36 | |
| 37 | const repositoryPublicKey = goPubKey.data.data.key as string; |
| 38 | const repositoryPublicKeyId = goPubKey.data.data.key_id as string; |
| 39 | |
| 40 | logger.log(`Repository public key: ${repositoryPublicKey} with ID ${repositoryPublicKeyId}`); |
| 41 | |
| 42 | const goSodium = await go(() => sodium.ready); |
| 43 | if (!goSodium.success) { |
| 44 | throw new Error(`Can't load the sodium encryption library: ${goSodium.error}`); |
| 45 | } |
| 46 | |
| 47 | // Convert Secret & Base64 key to Uint8Array. |
| 48 | const binKey = sodium.from_base64(repositoryPublicKey, sodium.base64_variants.ORIGINAL); |
| 49 | const binSecret = sodium.from_string(`${flag}`); |
| 50 | |
| 51 | // Encrypt the secret using LibSodium |
| 52 | const encSecret = sodium.crypto_box_seal(binSecret, binKey); |
| 53 | |
| 54 | // Convert encrypted Uint8Array to Base64 |
| 55 | const base64Secret = sodium.to_base64(encSecret, sodium.base64_variants.ORIGINAL); |
| 56 | |
| 57 | const goSecret = await go(() => |
| 58 | octokit.request(`PUT /repos/${OWNER}/${REPOSITORY}/actions/secrets/ENABLE_MERGE`, { |
| 59 | owner: OWNER, |
| 60 | repo: REPOSITORY, |
| 61 | secret_name: 'ENABLE_MERGE', |
| 62 | encrypted_value: base64Secret, |
| 63 | key_id: repositoryPublicKeyId, |
| 64 | }) |
| 65 | ); |
| 66 | if (!goSecret.success) { |
| 67 | throw new Error(`Can't update GitHub repository secret: ${goSecret.error}`); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | export const enableMerge = () => toggleMerge(true); |
| 72 | export const disableMerge = () => toggleMerge(false); |
no test coverage detected