Skip to content

Commit 5a997fb

Browse files
feat(release): post an immutable report of every classification and write record version 2
Each run rewrote the batch body whole, so the only account of what a run found was gone at the next run, and a merged batch kept only its last one. Nothing said what a classification was made of either: not the heads it read, the policy, the tool or the flags. A run now makes a report: every original merged into main since the line branched, in exactly one category, with what the run read. Its ID hashes the classification and names no pull request, so a new batch's report is whole before GitHub numbers the pull request, and a run that finds the same as the last one makes the same report, which is posted once and keeps its first bytes. The body carries the latest report under the record; a comment the tool never edits keeps each one, so report reads any of them back, from merged batches too. The comment goes up before the push and the body edit on an open batch, and after the pull request a new batch opens with the report in its body; a later run posts a report the body kept but no comment did, byte for byte, before its own. Every write is preceded by a read of the pull request, and nothing is written once it is merged or closed. Both the comment and the body are rendered and measured before the first write, so an oversized one stops the run with nothing written. The record becomes version 2: what it was built from, with the run's --no-workflow-files, which --defer keeps rather than resets, and the report it goes with. Assisted-By: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 5945d2d commit 5a997fb

19 files changed

Lines changed: 1898 additions & 98 deletions

‎cmd/backport-batch/README.md‎

Lines changed: 14 additions & 7 deletions
Large diffs are not rendered by default.

‎cmd/backport-batch/assemble.go‎

Lines changed: 125 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ func (a *app) run(args []string) int {
158158
}
159159

160160
r := newReport(b)
161+
r.opts = runOptions{NoWorkflowFiles: *noWorkflows, Mode: modeNew}
161162
if carried != nil {
163+
r.opts.Mode = modeContinue
162164
if r.stale, err = keptOut(b, carried); err != nil {
163165
return a.fail(err)
164166
}
@@ -264,12 +266,25 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
264266
if r.shapes, err = summarize(b, head, r.onBranch); err != nil {
265267
return a.fail(err)
266268
}
267-
title := prTitle(b.line, date.Format(time.DateOnly))
269+
r.built = builtFrom{Main: b.mainHead, Line: b.lineHead, Newer: b.newerHead, Policy: b.policyAt, Batch: head, Tool: a.tool,
270+
Run: runURL(os.LookupEnv), At: a.now().UTC().Format(time.RFC3339), Options: r.opts}
271+
doc, err := buildReport(b, r)
272+
if err != nil {
273+
return a.fail(err)
274+
}
275+
p := publication{branch: branch, head: head, title: prTitle(b.line, date.Format(time.DateOnly)), report: storedReport{doc: doc, raw: reportJSON(doc)}}
276+
p.record = r.lineRecord(doc)
277+
// A record the next command would refuse is never written.
278+
if err := p.record.validate(b.line); err != nil {
279+
return a.fail(fmt.Errorf("the record this run made %w", err))
280+
}
281+
r.blocks = recordBlock(p.record) + "\n\n" + reportBlock(p.report.raw)
268282
template, err := lineTemplate(b.lineHead)
269283
if err != nil {
270284
return a.fail(err)
271285
}
272286
body, left := r.body(template)
287+
p.body = body
273288
fmt.Fprint(a.stdout, body)
274289
skipped := 0
275290
for _, items := range r.skipped {
@@ -283,8 +298,8 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
283298
for _, p := range r.waiting {
284299
fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
285300
}
286-
fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\n",
287-
branch, head, len(r.onBranch), len(r.deferred)+len(r.held), len(r.waiting), skipped, title)
301+
fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
302+
branch, head, len(r.onBranch), len(r.deferred)+len(r.held), len(r.waiting), skipped, p.title, doc.ID)
288303
if !push {
289304
return 0
290305
}
@@ -296,12 +311,19 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
296311
fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
297312
return 0
298313
}
299-
if err := a.publish(b, branch, head, title, body, rebuilt); err != nil {
314+
if err := a.publish(b, p, rebuilt); err != nil {
300315
return a.fail(err)
301316
}
302317
return 0
303318
}
304319

320+
// lineRecord is the record a run leaves for the line: what later commands
321+
// act on, with the full classification left to the report it names.
322+
func (r *report) lineRecord(doc reportDoc) lineRecord {
323+
built := doc.Built
324+
return lineRecord{Version: 2, Line: doc.Line, Built: &built, Report: doc.ID, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: []droppedEntry{}, Stale: slices.Clone(doc.Overlays.Stale)}
325+
}
326+
305327
// branchDate is the date a batch branch was opened under.
306328
func branchDate(branch string) (time.Time, error) {
307329
m := batchBranchRE.FindStringSubmatch(branch)
@@ -446,51 +468,64 @@ func lineTemplate(lineHead string) (string, error) {
446468
return git("show", "--end-of-options", lineHead+":"+template)
447469
}
448470

471+
// publication is what a run publishes: the branch at head, and the body that
472+
// carries the record and the report.
473+
type publication struct {
474+
branch, head, title, body string
475+
report storedReport
476+
record lineRecord
477+
}
478+
449479
// publish pushes the batch branch, then opens the draft batch pull request or
450480
// updates the open one. A run pushes only a fast-forward of what the remote
451481
// has. A rebuild replaces the branch, and only the head it was rebuilt from:
452482
// the lease is that head, as ls-remote reads it now, so a commit pushed since
453483
// is never dropped.
454-
func (a *app) publish(b *batch, branch, head, title, body string, rebuilt bool) error {
455-
if b.pr != nil && b.pr.Head.Ref != branch {
484+
//
485+
// The report goes up as a comment before the push and the body that names
486+
// it, so no write leaves a body naming a report nobody can read; a new batch
487+
// carries the report in the body it is opened with, and the comment follows.
488+
// Everything is rendered and measured before the first write.
489+
func (a *app) publish(b *batch, p publication, rebuilt bool) error {
490+
if b.pr != nil && b.pr.Head.Ref != p.branch {
456491
return fmt.Errorf("the open batch pull request #%d is on %s; continue it with --onto %s", b.pr.Number, b.pr.Head.Ref, b.prHead)
457492
}
458493
// A body without the block was written by hand, and the tool's would
459494
// replace it whole, title included; such a batch is the maintainer's.
460-
if b.pr != nil && !strings.Contains("\n"+strings.ReplaceAll(b.pr.Body, "\r\n", "\n")+"\n", "\n"+deferredFence+"\n") {
495+
if b.pr != nil && !b.src.open {
461496
return fmt.Errorf("batch pull request #%d has no %s block, so backport-batch did not write it; merge or close it, and the next run opens a batch of its own", b.pr.Number, deferredFence)
462497
}
463-
if head == b.lineHead {
498+
if p.head == b.lineHead {
464499
if rebuilt {
465-
return fmt.Errorf("without what is deferred, %s carries nothing beyond %s; close batch pull request #%d instead", branch, b.line.branch(), b.pr.Number)
500+
return fmt.Errorf("without what is deferred, %s carries nothing beyond %s; close batch pull request #%d instead", p.branch, b.line.branch(), b.pr.Number)
466501
}
467-
return fmt.Errorf("%s carries nothing beyond %s; nothing to push", branch, b.line.branch())
502+
return fmt.Errorf("%s carries nothing beyond %s; nothing to push", p.branch, b.line.branch())
468503
}
469504
heads, err := remoteBatchHeads(b.line)
470505
if err != nil {
471506
return err
472507
}
473-
pushed, onRemote := heads[branch]
508+
pushed, onRemote := heads[p.branch]
474509
lease := ""
475510
switch {
476511
case rebuilt:
477512
if !onRemote || pushed != b.prHead {
478-
return fmt.Errorf("%s on %s is at %.12s, not at %.12s, which the rebuild started from; run again", branch, remote, pushed, b.prHead)
513+
return fmt.Errorf("%s on %s is at %.12s, not at %.12s, which the rebuild started from; run again", p.branch, remote, pushed, b.prHead)
479514
}
480515
lease = pushed
481-
case onRemote && pushed != head:
516+
case onRemote && pushed != p.head:
482517
have, err := lookup(pushed)
483518
if err != nil {
484519
return err
485520
}
486521
descends := false
487522
if have != "" {
488-
if descends, err = isAncestor(pushed, head); err != nil {
523+
if descends, err = isAncestor(pushed, p.head); err != nil {
489524
return err
490525
}
491526
}
492527
if !descends {
493-
refusal := fmt.Sprintf("%s on %s is at %.12s, which %.12s does not descend from; refusing a non-fast-forward push", branch, remote, pushed, head)
528+
refusal := fmt.Sprintf("%s on %s is at %.12s, which %.12s does not descend from; refusing a non-fast-forward push", p.branch, remote, pushed, p.head)
494529
// A run that stopped between its push and opening the pull request
495530
// leaves the branch, and the next run that day builds it anew.
496531
if b.pr == nil {
@@ -499,38 +534,100 @@ func (a *app) publish(b *batch, branch, head, title, body string, rebuilt bool)
499534
return errors.New(refusal)
500535
}
501536
}
502-
if pushed != head {
503-
if _, err := git(pushArgs(branch, head, lease)...); err != nil {
537+
if err := p.fits(); err != nil {
538+
return err
539+
}
540+
if b.pr != nil {
541+
if err := a.keepReports(b, &p); err != nil {
504542
return err
505543
}
506544
}
507-
f, err := os.CreateTemp("", "backport-batch-body-*.md")
508-
if err != nil {
509-
return err
510-
}
511-
defer os.Remove(f.Name())
512-
if _, err := f.WriteString(body); err != nil {
513-
f.Close()
514-
return err
545+
if pushed != p.head {
546+
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
547+
return err
548+
}
515549
}
516-
if err := f.Close(); err != nil {
550+
name, err := writeTemp("backport-batch-body-*.md", p.body)
551+
if err != nil {
517552
return err
518553
}
554+
defer os.Remove(name)
519555
if b.pr == nil {
520-
url, err := a.gh.createPull(b.line.branch(), branch, title, f.Name())
556+
n, url, err := a.gh.createPull(b.line.branch(), p.branch, p.title, name)
521557
if err != nil {
522558
return err
523559
}
524560
fmt.Fprintf(a.stderr, "opened draft %s\n", url)
561+
if err := a.postReport(n, p.report); err != nil {
562+
return fmt.Errorf("posting report %.12s on #%d: %w; the body keeps it, and the next run --push posts it", p.report.doc.ID, n, err)
563+
}
525564
return nil
526565
}
527-
if err := a.gh.editPull(b.pr.Number, title, f.Name()); err != nil {
566+
if err := a.stillOpen(b.pr.Number); err != nil {
567+
return err
568+
}
569+
if err := a.gh.editPull(b.pr.Number, p.title, name); err != nil {
528570
return err
529571
}
530572
fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
531573
return nil
532574
}
533575

576+
// fits says why GitHub would not take the comment or the body.
577+
func (p *publication) fits() error {
578+
if _, err := reportComment(p.report); err != nil {
579+
return err
580+
}
581+
if len(p.body) > bodyLimit {
582+
return fmt.Errorf("the body is %d bytes with only what it cannot leave out, over GitHub's %d; nothing is published", len(p.body), bodyLimit)
583+
}
584+
return nil
585+
}
586+
587+
// keepReports makes sure the open batch's comments keep both the report its
588+
// body carries now and the one this run made, in that order, each once.
589+
// When this run's report is one already kept, the body carries the kept
590+
// bytes, which differ only in when and in which job it was made.
591+
func (a *app) keepReports(b *batch, p *publication) error {
592+
pr := b.pr
593+
posted, bad, err := findReports(a.gh, pr.Number, b.line)
594+
if err != nil {
595+
return err
596+
}
597+
for _, why := range bad {
598+
a.warn("batch pull request #%d: %s; it does not count as a report", pr.Number, why)
599+
}
600+
earlier, err := bodyReport(pr.Body, b.line)
601+
if err != nil {
602+
return fmt.Errorf("batch pull request #%d: %w", pr.Number, err)
603+
}
604+
kept, ok := posted[p.report.doc.ID]
605+
if !ok && earlier != nil && earlier.doc.ID == p.report.doc.ID {
606+
kept, ok = *earlier, true
607+
}
608+
if ok {
609+
p.body = strings.Replace(p.body, reportBlock(p.report.raw), reportBlock(kept.raw), 1)
610+
p.report = kept
611+
if err := p.fits(); err != nil {
612+
return err
613+
}
614+
}
615+
if earlier != nil {
616+
if _, ok := posted[earlier.doc.ID]; !ok {
617+
if err := a.postReport(pr.Number, *earlier); err != nil {
618+
return fmt.Errorf("posting the earlier report %.12s on #%d: %w", earlier.doc.ID, pr.Number, err)
619+
}
620+
posted[earlier.doc.ID] = *earlier
621+
}
622+
}
623+
if _, ok := posted[p.report.doc.ID]; !ok {
624+
if err := a.postReport(pr.Number, p.report); err != nil {
625+
return fmt.Errorf("posting report %.12s on #%d: %w", p.report.doc.ID, pr.Number, err)
626+
}
627+
}
628+
return nil
629+
}
630+
534631
// pullURL is where a pull request is on GitHub.
535632
func pullURL(pr *pullRequest) string {
536633
if pullURLRE.MatchString(pr.HTMLURL) {

‎cmd/backport-batch/body.go‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,11 @@ type deferredRecord struct {
190190
Deferred []deferredEntry `json:"deferred"`
191191
}
192192

193-
// deferredBlock is the machine-readable list of what the batch defers. One
194-
// line of JSON: encoding/json escapes newlines and <, > and &, so nothing in a
195-
// file name or a reason can end the fence or open HTML. It leaves bidi and
196-
// zero-width characters as they are, so file names go through clean first,
197-
// as the reason does.
193+
// deferredBlock is a version 1 record, which batches carried before records
194+
// had a version. encoding/json leaves bidi and zero-width characters as they
195+
// are, so file names go through clean first, as the reason does.
198196
func deferredBlock(l line, conflicts, held, waiting []pick) string {
199-
rec := deferredRecord{Line: l.branch(), Deferred: []deferredEntry{}}
200-
for _, p := range conflicts {
201-
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
202-
rec.Deferred = append(rec.Deferred, deferredEntry{Original: p.v.o.number, Commit: p.failed, Files: files, More: len(p.files) - len(files) + p.more})
203-
}
204-
for _, p := range held {
205-
rec.Deferred = append(rec.Deferred, deferredEntry{Original: p.v.o.number, Files: []string{}, Reason: capped(oneLine(clean(p.reason, false)), titleLimit)})
206-
}
207-
for _, p := range waiting {
208-
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
209-
rec.Deferred = append(rec.Deferred, deferredEntry{Original: p.v.o.number, Files: files, More: len(p.files) - len(files) + p.more, Workflows: true})
210-
}
211-
data, _ := json.Marshal(rec)
197+
data, _ := json.Marshal(deferredRecord{Line: l.branch(), Deferred: deferredEntries(conflicts, held, waiting)})
212198
return deferredFence + "\n" + string(data) + "\n```"
213199
}
214200

@@ -254,6 +240,14 @@ type report struct {
254240
start time.Time
255241
// shapes says, for an original on the branch, what it brought.
256242
shapes map[*verdict]string
243+
// missing are originals the policy takes that the run neither tried nor
244+
// found anywhere.
245+
missing []*verdict
246+
// opts is how the run was asked to classify, and built what it read.
247+
opts runOptions
248+
built builtFrom
249+
// blocks are the record and the report the body ends with.
250+
blocks string
257251
}
258252

259253
// ruleName is how output names what kept an original out: the policy's
@@ -368,7 +362,7 @@ func (r *report) what(details map[int]bool) string {
368362
b.WriteString("\n</details>\n")
369363
}
370364
}
371-
b.WriteString("\n" + deferredBlock(r.line, r.deferred, r.held, r.waiting) + "\n")
365+
b.WriteString("\n" + r.blocks + "\n")
372366
return b.String()
373367
}
374368

‎cmd/backport-batch/body_test.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestBodyFollowsTheTemplate(t *testing.T) {
286286
l, _ := parseLine("release-1.6")
287287
template := "<!-- tips -->\n\n## What this PR does\n\n\n### Screenshots\n\n<!-- shots -->\n\n### Downstream repositories\n\n- [ ] No downstream repository is affected by this change\n\n### Release note\n\n```release-note\n\n```\n"
288288
o := &original{number: 7, title: "fix(x): y", body: "```release-note\nfix(x): note\n```"}
289-
r := &report{line: l, onBranch: []*verdict{{o: o}}, skipped: map[int][]*verdict{}}
289+
r := &report{line: l, onBranch: []*verdict{{o: o}}, skipped: map[int][]*verdict{}, blocks: deferredBlock(l, nil, nil, nil)}
290290
body, _ := r.body(template)
291291
for _, want := range []string{"## What this PR does\n\nBackports to", "- [#7](https://github.com/cozystack/cozystack/pull/7) `fix(x): y`", "### Downstream repositories\n\n- [ ] No downstream", "```release-note\nfix(x): note\n```", deferredFence} {
292292
if !strings.Contains(body, want) {
@@ -305,7 +305,7 @@ func TestBodyFollowsTheTemplate(t *testing.T) {
305305
for i := range 40 {
306306
many = append(many, &verdict{o: &original{number: i + 1, title: "fix(x): y", body: "```release-note\n" + long + "\n```"}})
307307
}
308-
r = &report{line: l, onBranch: many, skipped: map[int][]*verdict{ruleType: many}}
308+
r = &report{line: l, onBranch: many, skipped: map[int][]*verdict{ruleType: many}, blocks: deferredBlock(l, nil, nil, nil)}
309309
if body, _ := r.body(template); len(body) > bodyLimit || !strings.Contains(body, "truncated") || !strings.Contains(body, deferredFence) {
310310
t.Errorf("a body over the limit is %d bytes (limit %d), truncated=%v", len(body), bodyLimit, strings.Contains(body, "truncated"))
311311
}
@@ -322,7 +322,7 @@ func TestBodyFollowsTheTemplate(t *testing.T) {
322322
for i := range 80 {
323323
conflict.files = append(conflict.files, fmt.Sprintf("chart/f%02d.yaml", i))
324324
}
325-
r = &report{line: l, onBranch: many[:1], deferred: []pick{conflict}, skipped: map[int][]*verdict{ruleType: lots, ruleOnLine: few}}
325+
r = &report{line: l, onBranch: many[:1], deferred: []pick{conflict}, skipped: map[int][]*verdict{ruleType: lots, ruleOnLine: few}, blocks: deferredBlock(l, []pick{conflict}, nil, nil)}
326326
body, left := r.body(template)
327327
if !slices.Equal(left, []int{ruleType}) {
328328
t.Errorf("left out the lists of rules %v, want only rule 2", left)
@@ -366,3 +366,17 @@ func TestBodyLinksTaskPagesThatExist(t *testing.T) {
366366
}
367367
}
368368
}
369+
370+
// A body that what it has to carry pushes over GitHub's limit is refused
371+
// before anything is written, the replay of an earlier report included,
372+
// although the report alone would fit a comment.
373+
func TestBodyRefusesMandatoryContentOverTheLimit(t *testing.T) {
374+
f, gh, head := bigWaits(t, 4)
375+
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push")
376+
if code != 2 || !strings.Contains(stderr, "with only what it cannot leave out, over GitHub's 65536") {
377+
t.Errorf("run --push = %d, want a refusal of the body:\n%s", code, stderr)
378+
}
379+
if f.remoteHead(batchBranch1) != head || len(gh.issueComments) != 0 || len(gh.edited) != 0 {
380+
t.Errorf("the refused run moved the batch to %.12s, wrote %d comments and edited %q", f.remoteHead(batchBranch1), len(gh.issueComments), gh.edited)
381+
}
382+
}

‎cmd/backport-batch/defer.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
114114
return a.fail(err)
115115
}
116116
r := newReport(b)
117+
// Taking an original out changes nothing about how the batch is
118+
// assembled, so the record keeps the flag the batch was assembled with.
119+
r.opts = runOptions{NoWorkflowFiles: b.src.savedNoWorkflowFiles(), Mode: modeDefer}
117120
if r.stale, err = keptOut(b, picks); err != nil {
118121
return a.fail(err)
119122
}
@@ -128,6 +131,10 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
128131
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: e.Files, more: e.More})
129132
case len(e.Files) > 0:
130133
r.deferred = append(r.deferred, pick{v: v, status: "conflict", files: e.Files, more: e.More, failed: e.Commit})
134+
default:
135+
// Merged into main since the batch was assembled, or recorded
136+
// nowhere: a rebuild adds no original, the next run tries it.
137+
r.missing = append(r.missing, v)
131138
}
132139
}
133140
return a.finish(b, r, branch, date, push, true)

‎cmd/backport-batch/defer_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ func TestDeferLeasesTheHeadItRebuilt(t *testing.T) {
261261

262262
pr := f.batchPR(90, "release-1.1", batchBranch1, gh.prs[0].Body)
263263
a := &app{gh: gh}
264-
b := &batch{line: l, lineHead: lineHead, pr: &pr, prHead: head}
265-
err := a.publish(b, batchBranch1, rebuilt, "title", "body", true)
264+
b := &batch{line: l, lineHead: lineHead, pr: &pr, prHead: head, src: recordSource{carrier: &pr, open: true, code: &pr}}
265+
err := a.publish(b, publication{branch: batchBranch1, head: rebuilt, title: "title", body: "body"}, true)
266266
if err == nil || !strings.Contains(err.Error(), "which the rebuild started from") {
267267
t.Errorf("publish over a head pushed since = %v, want a refusal", err)
268268
}

0 commit comments

Comments
 (0)