1- import { once } from "node:events" ;
2- import { spawn } from "node:child_process" ;
3- import { exit } from "node:process" ;
4- import { Buffer } from "node:buffer" ;
5-
61export const keyServerURL = "hkps://keys.openpgp.org" ;
72
83export function secretHolderThreshold ( argv ) {
@@ -41,46 +36,39 @@ export const prOptions = {
4136}
4237
4338export async function createVotePR ( argv ) {
44- const cp = spawn (
45- "gh" ,
46- [
47- "api" ,
48- `repos/${ argv [ "github-repo-name" ] } /pulls` ,
49- "-F" ,
50- "base=main" ,
51- "-F" ,
52- `head=${ argv . branch } ` ,
53- "-F" ,
54- `title=${ argv . subject } ` ,
55- "-F" ,
56- `body=${ argv [ "pr-intro" ] ?? "" } ,
39+ const response = await fetch ( `${ process . env . GITHUB_API_URL } /repos/${ argv [ "github-repo-name" ] } /pulls` , {
40+ method : 'POST' ,
41+ headers : {
42+ 'Authorization' : `Bearer ${ process . env . GH_TOKEN } ` ,
43+ 'Accept' : 'application/vnd.github+json' ,
44+ 'X-GitHub-Api-Version' : '2022-11-28' ,
45+ } ,
46+ body : JSON . stringify ( {
47+ base : 'main' ,
48+ head : argv . branch ,
49+ title : argv . subject ,
50+ body : `${ argv [ "pr-intro" ] ?? "" } ,
5751
5852To close the vote, a minimum of ${ secretHolderThreshold ( argv ) } key parts would need to be revealed.
5953
6054Vote instructions will follow.` ,
61- "--jq" ,
62- ".html_url" ,
63- ] ,
64- { stdio : [ "inherit" , "pipe" , "inherit" ] }
65- ) ;
66- // @ts -ignore toArray does exist!
67- const out = cp . stdout . toArray ( ) ;
68- const [ code ] = await once ( cp , "exit" ) ;
69- if ( code !== 0 ) exit ( code ) ;
70-
71- const prUrl = Buffer . concat ( await out )
72- . toString ( )
73- . trim ( ) ;
55+ } ) ,
56+ } ) ;
57+ if ( ! response . ok ) {
58+ throw new Error ( 'Failed to create PR: ' + response . statusText , { cause : response } )
59+ }
60+ const { html_url : prURL , url } = await response . json ( ) ;
7461
7562 {
76- const cp = spawn (
77- "gh" ,
78- [
79- "pr" ,
80- "edit" ,
81- prUrl ,
82- "--body" ,
83- `${ argv [ "pr-intro" ] ?? "" }
63+ const response = await fetch ( url , {
64+ method : 'PATCH' ,
65+ headers : {
66+ 'Authorization' : `Bearer ${ process . env . GH_TOKEN } ` ,
67+ 'Accept' : 'application/vnd.github+json' ,
68+ 'X-GitHub-Api-Version' : '2022-11-28' ,
69+ } ,
70+ body : JSON . stringify ( {
71+ body : `${ argv [ "pr-intro" ] ?? "" }
8472
8573Vote instructions:
8674
@@ -95,12 +83,12 @@ ${argv['secret-holder']?.length ?
9583run the following command: ${ "`" } git node vote ${ prUrl } --decrypt-key-part --post-comment${ "`" }
9684` : '' }
9785 ` ,
98- ] ,
99- { stdio : "inherit" } ,
100- ) ;
86+ } ) ,
87+ } ) ;
10188
102- const [ code ] = await once ( cp , "exit" ) ;
103- if ( code !== 0 ) exit ( code ) ;
89+ if ( ! response . ok ) {
90+ throw new Error ( 'Failed to edit PR: ' + response . statusText , { cause : response } )
91+ }
10492 }
10593
10694 console . log ( "PR created" , prUrl ) ;
0 commit comments