Skip to content

Commit 64e97b0

Browse files
committed
api-diff also takes a commit
1 parent 22bef0b commit 64e97b0

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

tools/deno/api-diff.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ Requirements:
2020
- GitHub CLI (gh)
2121
2222
Usage:
23-
./tools/deno/api-diff.ts [-f] [PR number]
23+
./tools/deno/api-diff.ts [-f] [PR number or commit SHA]
2424
./tools/deno/api-diff.ts -h
2525
2626
Flags:
27-
-f, --force Download spec and regen client even if dir already exists
28-
-h, --help Show this help message
27+
-f, --force Download spec and regen client even if dir already exists
28+
-h, --help Show this help message
2929
3030
Parameters:
31-
PR number <int>: If left out, interactive picker is shown
31+
PR number or commit SHA: If left out, interactive picker is shown
3232
`.trim()
3333

3434
function printHelpAndExit() {
@@ -62,21 +62,30 @@ async function pickPr() {
6262
if (!/^\d+$/.test(prNum)) {
6363
throw new Error(`Error picking PR. Expected number, got '${prNum}'`)
6464
}
65-
return parseInt(prNum, 10)
65+
return prNum
6666
}
6767

68-
async function getPrRange(prNum: number) {
69-
const query = `{
68+
async function getCommitRange(arg: string): Promise<{ base: string; head: string }> {
69+
if (!arg || /^\d+$/.test(arg)) {
70+
const prNum = arg || (await pickPr())
71+
const query = `{
7072
repository(owner: "oxidecomputer", name: "omicron") {
7173
pullRequest(number: ${prNum}) {
7274
baseRefOid
7375
headRefOid
7476
}
7577
}
7678
}`
77-
const pr = await $`gh api graphql -f query=${query}`.json()
78-
const { baseRefOid: base, headRefOid: head } = pr.data.repository.pullRequest
79-
return { base, head } as { base: string; head: string }
79+
const pr = await $`gh api graphql -f query=${query}`.json()
80+
const { baseRefOid: base, headRefOid: head } = pr.data.repository.pullRequest
81+
return { base, head }
82+
}
83+
84+
// otherwise assume it's a commit
85+
const parents =
86+
await $`gh api repos/oxidecomputer/omicron/commits/${arg} --jq '.parents'`.json()
87+
if (parents.length > 1) throw new Error(`Commit has multiple parents:`)
88+
return { base: parents[0].sha, head: arg }
8089
}
8190

8291
async function genForCommit(commit: string, force: boolean) {
@@ -104,19 +113,13 @@ if (!$.commandExistsSync('gh')) throw Error('Need gh (GitHub CLI)')
104113
const diffTool = $.commandExistsSync('difft') ? 'difft' : 'diff'
105114

106115
const args = flags.parse(Deno.args, {
107-
alias: { force: ['f'], h: 'help' },
116+
alias: { force: 'f', help: 'h' },
108117
boolean: ['force', 'help'],
109118
})
110119

111120
if (args.help) printHelpAndExit()
112121

113-
const prNum = args._[0] ? args._[0] : await pickPr()
114-
115-
if (typeof prNum !== 'number') {
116-
throw new Error(`PR number must be a number. Got '${prNum}' instead.`)
117-
}
118-
119-
const { base, head } = await getPrRange(prNum)
122+
const { base, head } = await getCommitRange(args._[0])
120123

121124
const tmpDirBase = await genForCommit(base, args.force)
122125
const tmpDirHead = await genForCommit(head, args.force)

0 commit comments

Comments
 (0)