Skip to content

Commit e6fe2c8

Browse files
committed
chore: change file structure, remove use of gh from JS
Spawning a different process makes the process more fragile, for little benefit. Removing the dependency to `gh` follows the least surprise path
1 parent 4d53f63 commit e6fe2c8

File tree

7 files changed

+36
-48
lines changed

7 files changed

+36
-48
lines changed

.github/workflows/initiateNewVote.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ jobs:
129129
fi
130130
- name: Generate the vote init commit
131131
run: |
132-
./votes/initiateNewVote/generateNewVote.mjs \
132+
./votes/initiateNewVote/generateVoteInitCommit.mjs \
133133
--remote origin \
134134
--github-repo-name "$GITHUB_REPOSITORY" \
135135
--vote-repository-path . \

votes/initiateNewVote/decryptPrivateKeyAndCloseVote.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath } from "node:url";
55
import { parseArgs } from "node:util";
66

77
import countFromGit from "@node-core/caritat/countBallotsFromGit";
8-
import { findVoteSubPath } from "./getVoteSubpath.mjs";
8+
import { findVoteSubPath } from "./utils/getVoteSubpath.mjs";
99

1010
const { values: parsedArgs } = parseArgs({
1111
options: {

votes/initiateNewVote/generateNewVotePR.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import { parseArgs } from "node:util";
4-
import { prOptions, createVotePR } from "./_generateNewVotePR.mjs";
4+
import { prOptions, createVotePR } from "./utils/generateNewVotePR.mjs";
55

66
const { values: argv } = parseArgs({ options: prOptions });
77

File renamed without changes.

votes/initiateNewVote/getVoteStatus.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath } from "node:url";
66

77
import count from "@node-core/caritat/countParticipationFromGit";
88
import countFromGit from "@node-core/caritat/countBallotsFromGit";
9-
import { findVoteSubPath } from "./getVoteSubpath.mjs";
9+
import { findVoteSubPath } from "./utils/getVoteSubpath.mjs";
1010

1111
const { allowedVoters, shares } = JSON.parse(argv[2])
1212

votes/initiateNewVote/_generateNewVotePR.mjs renamed to votes/initiateNewVote/utils/generateNewVotePR.mjs

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
61
export const keyServerURL = "hkps://keys.openpgp.org";
72

83
export function secretHolderThreshold(argv) {
@@ -41,46 +36,39 @@ export const prOptions = {
4136
}
4237

4338
export 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
5852
To close the vote, a minimum of ${secretHolderThreshold(argv)} key parts would need to be revealed.
5953
6054
Vote 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
8573
Vote instructions:
8674
@@ -95,12 +83,12 @@ ${argv['secret-holder']?.length ?
9583
run 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);
File renamed without changes.

0 commit comments

Comments
 (0)