| 12
 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
 
 | import { Octokit } from '@octokit/core'import sodium from 'libsodium-wrappers'
 
 
 
 const octokit = new Octokit({
 auth: 'ghp_xxxxxxxxxxxxxxxxxx',
 request: {
 timeout: 10 * 1000,
 },
 })
 
 type GetARepositoryPublicKeyRequest = {
 owner: string
 repo: string
 }
 
 
 
 
 
 async function getARepositoryPublicKey(data: GetARepositoryPublicKeyRequest) {
 return (await octokit.request('GET /repos/{owner}/{repo}/actions/secrets/public-key', data)).data
 }
 
 type CreateOrUpdateARepositorySecretRequest = {
 owner: string
 repo: string
 
 
 
 secret_name: string
 
 
 
 secret_value: string
 }
 
 
 
 
 async function createOrUpdateARepositorySecret(data: CreateOrUpdateARepositorySecretRequest) {
 const { secret_value, owner, repo, ...other } = data
 
 const { key, key_id } = await getARepositoryPublicKey({ owner, repo })
 
 const binkey = sodium.from_base64(key, sodium.base64_variants.ORIGINAL)
 const binsec = sodium.from_string(secret_value)
 
 
 const encBytes = sodium.crypto_box_seal(binsec, binkey)
 
 
 const encrypted_value = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL)
 
 const newData = {
 ...other,
 owner,
 repo,
 encrypted_value,
 key_id,
 }
 
 return (await octokit.request('PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}', newData)).data
 }
 
 async function start() {
 await createOrUpdateARepositorySecret({
 owner: 'OWNER',
 repo: 'REPO',
 secret_name: 'SECRET_NAME',
 secret_value: 'xxxxxxxxxxxxxx',
 })
 }
 
 start()
 
 |