From e4419a98aebf7c6186a0d0f1c3810482fef3a77f Mon Sep 17 00:00:00 2001 From: David Crespo Date: Sat, 25 Nov 2023 23:23:25 -0600 Subject: [PATCH] rewrite in bash and nu. nu has bonus prNum arg, documented for free --- tools/api-diff.nu | 57 +++++++++++++++++++++++++++++++++++++++++++++++ tools/api-diff.sh | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100755 tools/api-diff.nu create mode 100755 tools/api-diff.sh diff --git a/tools/api-diff.nu b/tools/api-diff.nu new file mode 100755 index 0000000000..b6f8b0ce62 --- /dev/null +++ b/tools/api-diff.nu @@ -0,0 +1,57 @@ +#! /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'} +} + +let rowTemplate = '{{range .}}{{tablerow .number .title .author.name (timeago .updatedAt)}}{{end}}' + +def promptForPr [] { + 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) { + gh pr list -R oxidecomputer/omicron -L 100 --json number,title,updatedAt,author -t $rowTemplate | + fzf --height 25% --reverse | + cut -f1 -d ' ' + } 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" +} diff --git a/tools/api-diff.sh b/tools/api-diff.sh new file mode 100755 index 0000000000..24584b5158 --- /dev/null +++ b/tools/api-diff.sh @@ -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