Skip to content

Commit

Permalink
rewrite in bash and nu. nu has bonus prNum arg, documented for free
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Nov 28, 2023
1 parent c3d2f32 commit 1409198
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tools/api-diff.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /usr/bin/env nu

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# Copyright Oxide Computer Company

def genForCommit [commit] {
let tmpDir = $"/tmp/api-diff/($commit)"
rm -rf $tmpDir
mkdir $tmpDir
npm run --silent --prefix ../oxide.ts gen-from $commit $tmpDir
npx prettier --write --log-level error $tmpDir
}

if (which gh | is-empty) {
error make {msg: 'gh is not installed'}
}

def promptForPr [] {
let rowTemplate = '{{range .}}{{tablerow .number .title .author.name (timeago .updatedAt)}}{{end}}'
gh pr list -R oxidecomputer/omicron -L 100 --json number,title,updatedAt,author -t $rowTemplate |
fzf --height 25% --reverse |
cut -f1 -d ' '
}

def main [prNum?: int] {
let prNum = if ($prNum == null) {
promptForPr
} else {
$prNum
}

let query = $"query={
repository\(owner: \"oxidecomputer\", name: \"omicron\") {
pullRequest\(number: ($prNum)) {
baseRefOid
headRefOid
}
}
}"

let pr = (gh api graphql -f $query | from json | get data.repository.pullRequest)

let base = $pr | get baseRefOid
genForCommit $base

let head = $pr | get headRefOid
genForCommit $head

# git difftool is a trick to diff with whatever you have git set to use
git --no-pager difftool $"/tmp/api-diff/($base)/Api.ts" $"/tmp/api-diff/($head)/Api.ts"
}
51 changes: 51 additions & 0 deletions tools/api-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /usr/bin/env bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# Copyright Oxide Computer Company

function genForCommit() {
COMMIT=$1
TMP_DIR="/tmp/api-diff/$COMMIT"
rm -rf $TMP_DIR
mkdir -p $TMP_DIR
npm run --silent --prefix ../oxide.ts gen-from $COMMIT $TMP_DIR
npx prettier --write --log-level error $TMP_DIR
}

if ! command -v gh &> /dev/null; then
echo "Error: gh is not installed." >&2
exit 1
fi

PR_NUM=$(
gh pr list -R oxidecomputer/omicron --limit 100 --json number,title,updatedAt,author \
--template '{{range .}}{{tablerow .number .title .author.name (timeago .updatedAt)}}{{end}}' |
fzf --height 25% --reverse |
cut -f1 -d ' '
)

if ! [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then
echo "Error picking PR. Expected number, got $PR_NUM" >&2
exit 1
fi

PR_JSON=$(gh api graphql -f query="{
repository(owner: \"oxidecomputer\", name: \"omicron\") {
pullRequest(number: $PR_NUM) {
baseRefOid
headRefOid
}
}
}" | jq -r '.data.repository.pullRequest')

BASE=$(echo $PR_JSON | jq -r '.baseRefOid')
genForCommit $BASE

HEAD=$(echo $PR_JSON | jq -r '.headRefOid')
genForCommit $HEAD

# git difftool is a trick to diff with whatever you have git set to use
git --no-pager difftool "/tmp/api-diff/$BASE/Api.ts" "/tmp/api-diff/$HEAD/Api.ts" || true

0 comments on commit 1409198

Please sign in to comment.