diff --git a/catalog/hourly-status.md b/catalog/hourly-status.md new file mode 100644 index 0000000..3b28b71 --- /dev/null +++ b/catalog/hourly-status.md @@ -0,0 +1,31 @@ +--- +id: "hourly-status" +name: "Hourly status line, only when something changed" +trigger: {"cron": "15 * * * *", "timezone": "UTC"} +check: {"run": "hourly-status/check.sh", "timeout": 60} +thread: "same" +secrets: [] +priority: 2 +params: {"repo": "example-org/example-repo", "spec": 1, "status_repo": "example-org/example-tracker", "status_issue": 1} +--- +You manage spec {{spec}} in {{repo}}. This hour's check found that its +tickets (their state or labels) or the open pull requests that close them +(their head, draft state, reviews, CI or comments) changed since your last +status line; its output is below. + +1. Write one status line for spec {{spec}}, from the check's output and what + you know of the spec: tickets merged out of the total, which are in + review, which are being built, and what blocks any that wait. Start it + with `Status for spec {{spec}}:`. Say nothing the output and the spec's + own records do not show. +2. Post it as one comment on issue {{status_issue}} in {{status_repo}}. +3. Only once the comment is posted, run the `mv` line the check printed, so + the next quiet hour stays quiet. If the post failed, leave the file: the + next hour shows the same change again. + +Post nothing else and change nothing else. Public comments carry no thread +ids, private names or exact words of a private decision. + +This loop grants no secrets (`secrets: []`). It runs in the thread that +applied it, so its check and this run use that thread's own `gh` login to +read {{repo}} and to post on {{status_repo}}. diff --git a/catalog/hourly-status/check.sh b/catalog/hourly-status/check.sh new file mode 100644 index 0000000..d64c00b --- /dev/null +++ b/catalog/hourly-status/check.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# hourly-status check for spec {{spec}} in {{repo}}. +# +# Reads every one of the spec's tickets (all pages) with its labels, and for +# each ticket the open pull requests GitHub links as closing that exact +# issue, with their head, draft state, review decision, latest reviews, CI +# state and comment count. It prints that state only when it differs from +# the last status line the agent posted. Nothing changed: it prints nothing, +# so no agent starts. GitHub can't be read, or cut a list short: it fails the +# run (exit 2), never a silent quiet hour. +# +# The state it printed waits in .pending until the agent has posted the +# line and renamed it to .posted, so a run that fails before posting +# shows the same change again next hour. +set -eu +dir="$HOME/.wizloops/hourly-status" +key=$(printf '%s-%s' '{{repo}}' '{{spec}}' | tr -c 'A-Za-z0-9.\n-' '_') +mkdir -p "$dir" +repo='{{repo}}' + +query='query($owner: String!, $name: String!, $spec: Int!, $endCursor: String) { + repository(owner: $owner, name: $name) { issue(number: $spec) { + subIssues(first: 50, after: $endCursor) { + pageInfo { hasNextPage endCursor } + nodes { + number state repository { nameWithOwner } + labels(first: 50) { pageInfo { hasNextPage } nodes { name } } + closedByPullRequestsReferences(first: 20, includeClosedPrs: false) { + pageInfo { hasNextPage } + nodes { + number state isDraft headRefOid reviewDecision + repository { nameWithOwner } + comments { totalCount } + latestReviews(first: 50) { pageInfo { hasNextPage } nodes { state author { login } } } + commits(last: 1) { nodes { commit { statusCheckRollup { state } } } } + } + } + } + } + } } +}' +# One line per ticket and per open pull request that closes it; a line +# "cut-short" when GitHub has more labels, pull requests or reviews than +# were read. +filter='.data.repository.issue.subIssues.nodes[] | + (if .labels.pageInfo.hasNextPage or .closedByPullRequestsReferences.pageInfo.hasNextPage + or any(.closedByPullRequestsReferences.nodes[]; .latestReviews.pageInfo.hasNextPage) + then "cut-short #\(.number)" else empty end), + "#\(.number) \(.repository.nameWithOwner) \(.state) labels:\([.labels.nodes[].name] | sort | join(","))", + (.closedByPullRequestsReferences.nodes[] | select(.state == "OPEN") | + " #\(.number) at \(.headRefOid[0:7]) in \(.repository.nameWithOwner)" + + " \(if .isDraft then "draft" else "ready" end)" + + " decision:\(.reviewDecision // "none")" + + " reviews:\([.latestReviews.nodes[] | "\(.author.login // "ghost")=\(.state)"] | sort | join(","))" + + " checks:\(.commits.nodes[0].commit.statusCheckRollup.state // "none")" + + " comments:\(.comments.totalCount)")' + +state=$(gh api graphql --paginate -F owner="${repo%%/*}" -F name="${repo#*/}" \ + -F spec='{{spec}}' -f query="$query" --jq "$filter") || + { echo "hourly-status: cannot read spec {{spec}} in {{repo}}" >&2; exit 2; } +case "$state" in + cut-short*|*" +cut-short"*) echo "hourly-status: GitHub cut a list short for spec {{spec}} in {{repo}}" >&2; exit 2 ;; +esac + +if [ -f "$dir/$key.posted" ] && [ "$state" = "$(cat "$dir/$key.posted")" ]; then + rm -f "$dir/$key.pending" + exit 0 +fi +printf '%s\n' "$state" > "$dir/$key.pending" +echo "Spec {{spec}} in {{repo}} changed since the last status line." +echo "Once the line is posted: mv $dir/$key.pending $dir/$key.posted" +echo "Each ticket, then the open pull requests that close it:" +printf '%s\n' "$state" diff --git a/catalog/hourly-status/test.sh b/catalog/hourly-status/test.sh new file mode 100644 index 0000000..b488070 --- /dev/null +++ b/catalog/hourly-status/test.sh @@ -0,0 +1,163 @@ +#!/bin/sh +# Runs hourly-status's check against a stand-in `gh` that serves raw, +# synthetic GitHub JSON and applies the check's own --jq filter with jq, and +# checks the one promise the loop makes: it prints the spec's state when it +# changed since the last posted status line, and nothing otherwise, so a +# quiet hour starts no agent. +# +# Needs jq. Usage, from anywhere: sh catalog/hourly-status/test.sh +set -eu +here=$(cd "$(dirname "$0")" && pwd) +command -v jq >/dev/null || { echo "test: needs jq" >&2; exit 2; } +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$work/bin" "$work/home" "$work/data" + +# The check with its params replaced by the file's synthetic defaults, as an +# adapter replaces them before it applies the loop. +sed -e 's#[{][{]repo[}][}]#example-org/example-repo#g' \ + -e 's#[{][{]spec[}][}]#1#g' \ + "$here/check.sh" > "$work/check.sh" + +# A stand-in for `gh`. `api graphql` serves page1.json, and page2.json only +# with --paginate and an $endCursor query, as gh does; `issue view` and +# `pr list` serve the same world in their shapes (pr list honours --limit). +# Each answer goes through the caller's --jq, applied by jq. With +# data/fail it fails as gh does when GitHub can't be read. +cat > "$work/bin/gh" <<'EOF' +#!/bin/sh +[ -f "$FAKE/fail" ] && { echo "gh: could not connect" >&2; exit 1; } +cmd="$1 $2"; shift 2 +filter=. paginate=no limit=30 query= +while [ $# -gt 0 ]; do + case "$1" in + --jq) filter=$2; shift ;; + --limit) limit=$2; shift ;; + --paginate) paginate=yes ;; + -f|-F) case "$2" in query=*) query=${2#query=} ;; esac; shift ;; + esac + shift +done +pages="$FAKE/page1.json" +case "$query" in *'$endCursor'*) [ $paginate = yes ] && pages="$pages $FAKE/page2.json" ;; esac +case "$cmd" in + "api graphql") for p in $pages; do jq -r "$filter" "$p" || exit 1; done ;; + "issue view") jq -s '{subIssues: {nodes: [.[].data.repository.issue.subIssues.nodes[]]}}' \ + "$FAKE/page1.json" "$FAKE/page2.json" | jq -r "$filter" ;; + "pr list") jq ".[0:$limit]" "$FAKE/prs.json" | jq -r "$filter" ;; + *) echo "gh: unexpected: $cmd" >&2; exit 1 ;; +esac +EOF +chmod +x "$work/bin/gh" + +# The synthetic world. Spec 1 has tickets #2 and #3 on the first page of its +# sub-issues and #4 on the second. #12 closes #3; #30 closes #4 and is the +# 201st open pull request; #40 closes other-org/other-repo#2, not ticket #2. +t2=CLOSED t3=OPEN t3_labels='"ready-for-agent"' +h12=abc1234 draft12=false reviews12= checks12=PENDING comments12=1 +h30=ccc3000 h40=ddd4000 more_labels=false +pr() { # number repo head draft reviews checks comments + printf '{"number":%s,"state":"OPEN","isDraft":%s,"headRefOid":"%s000000000000000000000000000000000","reviewDecision":null,"repository":{"nameWithOwner":"%s"},"comments":{"totalCount":%s},"latestReviews":{"pageInfo":{"hasNextPage":false},"nodes":[%s]},"commits":{"nodes":[{"commit":{"statusCheckRollup":{"state":"%s"}}}]},"closingIssuesReferences":[]}' \ + "$1" "$4" "$3" "$2" "$7" "$5" "$6" +} +ticket() { # number state labels prs + printf '{"number":%s,"state":"%s","repository":{"nameWithOwner":"example-org/example-repo"},"labels":{"pageInfo":{"hasNextPage":%s},"nodes":[%s]},"closedByPullRequestsReferences":{"pageInfo":{"hasNextPage":false},"nodes":[%s]}}' \ + "$1" "$2" "$more_labels" "$(printf '%s' "$3" | sed 's/"\([^",]*\)"/{"name":"\1"}/g')" "$4" +} +closes() { # + printf '%s' "$1" | sed "s#\"closingIssuesReferences\":\[\]#\"closingIssuesReferences\":[{\"number\":$2,\"repository\":{\"nameWithOwner\":\"$3\"}}]#" +} +world() { + p12=$(pr 12 example-org/example-repo "$h12" "$draft12" "$reviews12" "$checks12" "$comments12") + p30=$(pr 30 example-org/example-repo "$h30" false "" SUCCESS 0) + p40=$(pr 40 example-org/example-repo "$h40" false "" SUCCESS 0) + printf '{"data":{"repository":{"issue":{"subIssues":{"pageInfo":{"hasNextPage":true,"endCursor":"Mg"},"nodes":[%s,%s]}}}}}\n' \ + "$(ticket 2 "$t2" "" "")" "$(ticket 3 "$t3" "$t3_labels" "$p12")" > "$work/data/page1.json" + printf '{"data":{"repository":{"issue":{"subIssues":{"pageInfo":{"hasNextPage":false,"endCursor":"Mw"},"nodes":[%s]}}}}}\n' \ + "$(ticket 4 OPEN "" "$p30")" > "$work/data/page2.json" + # pr list: #12 and #40 first, 198 unrelated, then #30 as the 201st. + { printf '[%s,%s' "$(closes "$p12" 3 example-org/example-repo)" "$(closes "$p40" 2 other-org/other-repo)" + i=100; while [ $i -lt 298 ]; do printf ',%s' "$(pr $i example-org/example-repo eee0000 false "" SUCCESS 0)"; i=$((i + 1)); done + printf ',%s]\n' "$(closes "$p30" 4 example-org/example-repo)" + } > "$work/data/prs.json" +} + +fails=0 +wake() { + world + set +e + out=$(FAKE="$work/data" HOME="$work/home" PATH="$work/bin:$PATH" sh "$work/check.sh" 2>"$work/err") + code=$? + set -e +} +quiet() { # : the wake printed nothing and ended 0 or 1 + wake + if [ -n "$out" ] || [ "$code" -gt 1 ]; then + echo "FAIL $1: expected a quiet wake, got exit $code and: $out"; fails=$((fails + 1)) + else echo "ok $1"; fi +} +due() { # : the wake printed the state + wake + case "$out" in + *"$2"*) if [ "$code" -le 1 ]; then echo "ok $1"; return; fi ;; + esac + echo "FAIL $1: expected output with '$2', got exit $code and: $out"; fails=$((fails + 1)) +} +shows() { # : the last wake's output shows it + case "$out" in *"$2"*) echo "ok $1" ;; *) echo "FAIL $1: no '$2' in: $out"; fails=$((fails + 1)) ;; esac +} +lacks() { # : the last wake's output does not show it + case "$out" in *"$2"*) echo "FAIL $1: '$2' shown in: $out"; fails=$((fails + 1)) ;; *) echo "ok $1" ;; esac +} +failed() { # : the wake failed the run (exit over 1) and printed nothing + wake + if [ -z "$out" ] && [ "$code" -gt 1 ]; then echo "ok $1" + else echo "FAIL $1: expected a failed run, got exit $code and: $out"; fails=$((fails + 1)); fi +} +posted() { # the agent posted the line and recorded it, as the prompt says + for f in "$work/home/.wizloops/hourly-status/"*.pending; do [ -e "$f" ] || continue; mv "$f" "${f%.pending}.posted"; done +} + +# catches: the first wake stays quiet, so a spec never gets its first line +due "first wake shows the state" "#12 at abc1234" +# catches: a pull request that closes another repository's #2 is counted as +# work on this spec's ticket #2 +lacks "another repository's #2 is not this spec's" "#40" +# catches: a fixed window over the repository's open pull requests, or only +# the first page of sub-issues, hides the spec's pull request #30 +shows "a ticket's pull request past any window is seen" "#30 at ccc3000" +# catches: the check prints on every wake (as an always-echo check does), so +# every hour starts an agent even when nothing changed +posted; quiet "nothing changed since the posted line" +# catches: a change is missed, so the status line goes stale +t3=CLOSED +due "a ticket closed" "#3 example-org/example-repo CLOSED" +# catches: the check marks a change as posted itself, so a run that fails +# before posting loses the change for good +due "not posted yet, still due" "#3 example-org/example-repo CLOSED" +posted; quiet "posted, quiet again" +# catches: a push to the spec's pull request is not seen as progress +t3=OPEN h12=abc9999 +due "a pull request moved" "#12 at abc9999" +posted +# catches: progress at the same head is missed: a draft marked ready, a +# review, a CI result, a verdict comment, a blocking label +draft12=true; due "same head: marked draft" "draft"; posted +draft12=false; due "same head: marked ready" "ready"; posted +reviews12='{"state":"CHANGES_REQUESTED","author":{"login":"example-reviewer"}}' +due "same head: a review asks for changes" "example-reviewer=CHANGES_REQUESTED"; posted +checks12=FAILURE; due "same head: CI failed" "checks:FAILURE"; posted +comments12=2; due "same head: a new comment (a verdict)" "comments:2"; posted +t3_labels='"ready-for-agent","needs-info"' +due "a ticket asks a question" "needs-info"; posted +quiet "all of that posted, quiet again" +# catches: another spec's or nobody's pull request wakes this spec's agent +h40=ddd9999; quiet "an unrelated pull request moved" +# catches: a list GitHub cut short is read as complete, hiding changes +more_labels=true; failed "a cut-short list fails the run"; more_labels=false +# catches: GitHub can't be read and the check exits 0 or 1 with no output, +# which reads as a quiet hour and hides the failure +touch "$work/data/fail"; failed "a failed read fails the run" + +[ "$fails" -eq 0 ] || { echo "$fails failed"; exit 1; } +echo "all passed"