|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import { once } from 'node:events'; |
| 3 | +import { env } from 'node:process'; |
| 4 | + |
| 5 | +import { |
| 6 | + runAsync |
| 7 | +} from './run.js'; |
| 8 | +import Session from './session.js'; |
| 9 | +import { |
| 10 | + getEditor, isGhAvailable |
| 11 | +} from './utils.js'; |
| 12 | + |
| 13 | +import voteUsingGit from '@node-core/caritat/voteUsingGit'; |
| 14 | +import * as yaml from 'js-yaml'; |
| 15 | + |
| 16 | +function getHTTPRepoURL(repoURL, login) { |
| 17 | + const url = new URL(repoURL + '.git'); |
| 18 | + url.username = login; |
| 19 | + return url.toString(); |
| 20 | +} |
| 21 | + |
| 22 | +export default class VotingSession extends Session { |
| 23 | + constructor(cli, req, dir, { |
| 24 | + prid, abstain, ...argv |
| 25 | + } = {}) { |
| 26 | + super(cli, dir, prid, argv, false); |
| 27 | + this.req = req; |
| 28 | + this.abstain = abstain; |
| 29 | + this.closeVote = argv['decrypt-key-part']; |
| 30 | + this.postComment = argv['post-comment']; |
| 31 | + this.gpgSign = argv['gpg-sign']; |
| 32 | + } |
| 33 | + |
| 34 | + get argv() { |
| 35 | + const args = super.argv; |
| 36 | + args.decryptKeyPart = this.closeVote; |
| 37 | + return args; |
| 38 | + } |
| 39 | + |
| 40 | + async start(metadata) { |
| 41 | + const { repository, viewer } = await this.req.gql('VotePRInfo', |
| 42 | + { owner: this.owner, repo: this.repo, prid: this.prid }); |
| 43 | + if (repository.pullRequest.merged) { |
| 44 | + this.cli.warn('The pull request appears to have been merged already.'); |
| 45 | + } else if (repository.pullRequest.closed) { |
| 46 | + this.cli.warn('The pull request appears to have been closed already.'); |
| 47 | + } |
| 48 | + if (this.closeVote) return this.decryptKeyPart(repository.pullRequest); |
| 49 | + // @see https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#_committing |
| 50 | + const username = process.env.GIT_AUTHOR_NAME || (await runAsync( |
| 51 | + 'git', ['config', '--get', 'user.name'], { captureStdout: true })).trim(); |
| 52 | + const emailAddress = process.env.GIT_AUTHOR_EMAIL || (await runAsync( |
| 53 | + 'git', ['config', '--get', 'user.email'], { captureStdout: true })).trim(); |
| 54 | + const { headRef } = repository.pullRequest; |
| 55 | + await voteUsingGit({ |
| 56 | + GIT_BIN: 'git', |
| 57 | + abstain: this.abstain, |
| 58 | + EDITOR: await getEditor({ git: true }), |
| 59 | + handle: viewer.login, |
| 60 | + username, |
| 61 | + emailAddress, |
| 62 | + gpgSign: this.gpgSign, |
| 63 | + repoURL: viewer.publicKeys.totalCount |
| 64 | + ? headRef.repository.sshUrl |
| 65 | + : getHTTPRepoURL(headRef.repository.url, viewer.login), |
| 66 | + branch: headRef.name, |
| 67 | + subPath: headRef.name |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + async decryptKeyPart(prInfo) { |
| 72 | + const subPath = `${prInfo.headRef.name}/vote.yml`; |
| 73 | + this.cli.startSpinner('Downloading vote file from remote...'); |
| 74 | + const yamlString = await this.req.text( |
| 75 | + `https://api.github.com/repos/${this.owner}/${this.repo}/contents/${encodeURIComponent(subPath)}?ref=${prInfo.commits.nodes[0].commit.oid}`, { |
| 76 | + agent: this.req.proxyAgent, |
| 77 | + headers: { |
| 78 | + Authorization: `Basic ${this.req.credentials.github}`, |
| 79 | + 'User-Agent': 'node-core-utils', |
| 80 | + Accept: 'application/vnd.github.raw' |
| 81 | + } |
| 82 | + }); |
| 83 | + this.cli.stopSpinner('Download complete'); |
| 84 | + |
| 85 | + const { shares } = yaml.load(yamlString); |
| 86 | + const ac = new AbortController(); |
| 87 | + this.cli.startSpinner('Decrypt key part...'); |
| 88 | + const out = await Promise.any( |
| 89 | + shares.map(async(share) => { |
| 90 | + const cp = spawn(env.GPG_BIN || 'gpg', ['-d'], { |
| 91 | + stdio: ['pipe', 'pipe', 'inherit'], |
| 92 | + signal: ac.signal |
| 93 | + }); |
| 94 | + // @ts-ignore toArray exists |
| 95 | + const stdout = cp.stdout.toArray(); |
| 96 | + stdout.catch(Function.prototype); // ignore errors. |
| 97 | + cp.stdin.end(share); |
| 98 | + const [code] = await Promise.race([ |
| 99 | + once(cp, 'exit'), |
| 100 | + once(cp, 'error').then((er) => Promise.reject(er)) |
| 101 | + ]); |
| 102 | + if (code !== 0) throw new Error('failed', { cause: code }); |
| 103 | + return Buffer.concat(await stdout); |
| 104 | + }) |
| 105 | + ); |
| 106 | + ac.abort(); |
| 107 | + this.cli.stopSpinner('Found one key part.'); |
| 108 | + |
| 109 | + const keyPart = '-----BEGIN SHAMIR KEY PART-----\n' + |
| 110 | + out.toString('base64') + |
| 111 | + '\n-----END SHAMIR KEY PART-----'; |
| 112 | + this.cli.log('Your key part is:'); |
| 113 | + this.cli.log(keyPart); |
| 114 | + const body = 'I would like to close this vote, and for this effect, I\'m revealing my ' + |
| 115 | + `key part:\n\n${'```'}\n${keyPart}\n${'```'}\n`; |
| 116 | + if (this.postComment) { |
| 117 | + const { html_url } = await this.req.json(`https://api.github.com/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, { |
| 118 | + agent: this.req.proxyAgent, |
| 119 | + method: 'POST', |
| 120 | + headers: { |
| 121 | + Authorization: `Basic ${this.req.credentials.github}`, |
| 122 | + 'User-Agent': 'node-core-utils', |
| 123 | + Accept: 'application/vnd.github.antiope-preview+json' |
| 124 | + }, |
| 125 | + body: JSON.stringify({ body }) |
| 126 | + }); |
| 127 | + this.cli.log('Comment posted at:', html_url); |
| 128 | + } else if (isGhAvailable()) { |
| 129 | + this.cli.log('\nRun the following command to post the comment:\n'); |
| 130 | + this.cli.log( |
| 131 | + `gh pr comment ${this.prid} --repo ${this.owner}/${this.repo} ` + |
| 132 | + `--body-file - <<'EOF'\n${body}\nEOF` |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments