watchpr: emit the baseline under --json

The baseline is the only notice that a watch started against an already
conflicted PR, and it was missing from the mode automation uses. It goes to
stderr as a JSON record, leaving stdout a single result record.
This commit is contained in:
2026-09-26 21:14:20 +10:00
parent 27e48ac45e
commit 15c527ee56
2 changed files with 79 additions and 9 deletions
+35 -9
View File
@@ -3,8 +3,13 @@
// new comment from someone other than the agent, its CI fails, or it loses
// mergeability after the baseline. Benign transitions (CI pending→success, the
// agent's own pushes and comments) are ignored, and so is any condition that was
// already true at the baseline -- which watchpr prints, so a run started against
// a conflicted or CI-red PR says so.
// already true at the baseline -- which watchpr prints on stderr (as a JSON
// record under --json), so a run started against a conflicted or CI-red PR says
// so. One case is never reported at all: a conflict introduced by the push
// immediately before the watch started, on a PR whose head and base then never
// move, stays silent for the whole run, because nothing in Gitea's payload
// separates it from a merge check still in flight. The baseline line is the
// only notice of it.
//
// watchpr owner/repo#12 owner/repo:15
// watchpr --once --json owner/repo#12
@@ -15,6 +20,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
@@ -131,13 +137,7 @@ func runWatch(c *agent.GiteaClient, refs []agent.PRRef, interval time.Duration,
defer ticker.Stop()
onBaseline := func(states []agent.PRState) {
if jsonMode {
return
}
fmt.Fprintf(os.Stderr, "watching %d PR(s) every %s; baseline established\n", len(refs), interval)
for _, st := range states {
fmt.Fprintln(os.Stderr, baselineLine(st))
}
emitBaselines(os.Stderr, states, interval, jsonMode)
}
onError := func(ref agent.PRRef, err error) {
fmt.Fprintf(os.Stderr, "warning: polling %s: %v\n", ref.String(), err)
@@ -151,6 +151,32 @@ func runWatch(c *agent.GiteaClient, refs []agent.PRRef, interval time.Duration,
return nil
}
// baselineRecord is the --json form of baselineLine. Both go to stderr, leaving
// the stdout contract a single result record: a caller automating watchpr is
// precisely the one who needs to be told the watch started against a PR that is
// already conflicted, since no alert will ever follow for it.
type baselineRecord struct {
Baseline bool `json:"baseline"`
Suppressed string `json:"suppressed,omitempty"`
State agent.PRState `json:"state"`
}
// emitBaselines writes the state each watch started from, in whichever form the
// caller asked for.
func emitBaselines(w io.Writer, states []agent.PRState, interval time.Duration, jsonMode bool) {
if jsonMode {
enc := json.NewEncoder(w)
for _, st := range states {
_ = enc.Encode(baselineRecord{Baseline: true, Suppressed: suppressedAtBaseline(st), State: st})
}
return
}
_, _ = fmt.Fprintf(w, "watching %d PR(s) every %s; baseline established\n", len(states), interval)
for _, st := range states {
_, _ = fmt.Fprintln(w, baselineLine(st))
}
}
// describeFailure names the cause of a terminal failure so a watcher that stops
// says why. An anonymous rejection, a permission boundary and a token that
// outlived its Vault lease are three different problems and only the last is