From f07b64811450f631792f39aa2053c0279df99ef3 Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Fri, 20 Nov 2026 10:19:26 +0200 Subject: [PATCH] feat: WARN: lines of the summary stay in the head of the question, and apply can add short lines to its report - A summary line that starts with "WARN: " moves from the
block to the head, after the watched-path warnings, as cleaned code. At most 10, then a count. They are not cut for the lists; each is at most 220 characters after the escape, so the watched paths keep room. - apply gets PULLGATE_REPORT_FILE. root reads it after apply (the same file it made, O_NOFOLLOW, 64 KiB) and adds its first 10 lines as code to the applied or failed message, then a count. Part of #42. Co-Authored-By: Claude Opus 5.5--- DESIGN.md | 36 ++++++- README.md | 6 +- THREAT-MODEL.md | 20 ++++ internal/gate/apply.go | 90 +++++++++++++--- internal/gate/env.go | 7 +- internal/gate/fixture_test.go | 7 +- internal/gate/report_test.go | 127 ++++++++++++++++++++++ internal/message/message.go | 39 +++++++ internal/message/question.go | 124 ++++++++++++++++++--- internal/message/warn_test.go | 195 ++++++++++++++++++++++++++++++++++ 10 files changed, 614 insertions(+), 37 deletions(-) create mode 100644 internal/gate/report_test.go create mode 100644 internal/message/warn_test.go diff --git a/DESIGN.md b/DESIGN.md index 8da7744..b109747 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -704,6 +704,16 @@ They run with an empty environment plus: - `PULLGATE_FAILED_SHA`: the held SHA, if there is one. The host may be part way to it, not at `PULLGATE_OLD_SHA`. - `PULLGATE_ROLLBACK`: `1` during a rollback, else empty. +- `PULLGATE_REPORT_FILE`: only for `apply`, and not in a rollback. The + path of an empty file (root's, mode 0600, in the per-process trees + directory) where `apply` may write short lines for the owner, for + example "skipped step x on purpose". After `apply` (also when it + failed), root reads the file: only a regular file, never a link + (`O_NOFOLLOW`), at most 64 KiB. Its first 10 lines are added to the + `applied` or `failed` message under "Report of apply:", each cleaned and + shown as code (section 12), at most 1500 characters in all; a last line + says how many lines were left out. A missing or empty file adds + nothing. `health` does not get the file. **In a rollback**, `PULLGATE_SHA` and `PULLGATE_TREE` are the last good SHA and its tree; `PULLGATE_OLD_SHA` and `PULLGATE_OLD_TREE` are the SHA that @@ -749,6 +759,8 @@ Apply this commit? First apply: there is no applied commit yet. All N files are listed. or: Since SHORT APPLIED SHA: ⚠ Watched path changed:path+⚠text of a WARN: line of the summary+⚠ N more warnings from the summary (only with more than 10) ⚠ Symlink out of the tree:path→target⚠ Submodule skipped:path@@ -783,12 +795,34 @@ cut to 200 characters, and then it is HTML-escaped. At most 50 commit subjects are read. Every message and every edit is sent with link previews off. +**Warnings from the summary.** The `[watch]` list catches changed paths; +only the owner's `summary` can find other risky changes, because it reads +the tree. A line of the `summary` output that starts with `WARN: ` (exactly +these 6 characters, at the start of the line) is taken out of the `` +block and shown in the head, after the watched-path warnings, as +`⚠text`: the text after the prefix, cleaned and cut to 200 +characters like any repo text. A CRLF line end is not part of the text, +and a `WARN: ` line with no text is dropped. At most 10 such lines are shown; more give +one line "⚠ N more warnings from the summary". A summary with only `WARN: ` +lines has no "Summary:" block. When `summary` failed, its output is not +used, so no warning comes from it. A summary without `WARN: ` lines gives +the same message as before. + The message must fit one Telegram message (4096 characters). pullgate cuts it to 3800 characters, counted on the escaped text in UTF-16 units: the head (host, instance, SHA, the warnings) always stays, and when lines are left out the last line says how many ("… 120 more lines left out"). The cut is on whole lines, so it never splits an HTML tag or entity. The `` block is -closed after a cut. +closed after a cut. The body is cut from its end: first the rest of the +summary, then the file list, then the commits. The "Full:" line stays last. +When the head alone is too long, the path warnings (watched paths, +symlinks, submodules) are cut first and counted ("⚠ … and N more +warnings"); the summary warnings stay. Each summary warning line is at most +220 characters after the escape (200 plain characters fit), so 10 of them +take at most about 2250 and the watched paths keep room. Only when the fixed +head lines and the summary warnings alone do not fit (a very long broken +reason) are the last summary warnings counted in the "more warnings from +the summary" line, so that the message still fits. ## 13. Modes, limits, status diff --git a/README.md b/README.md index 593cf27..f863fcd 100644 --- a/README.md +++ b/README.md @@ -284,8 +284,10 @@ known_hosts = "/etc/pullgate/NAME.known_hosts" [commands] check = ["/path/to/check"] # optional; runs as check_user; a failure means no question; # exit 3 means "nothing to apply on this host": no question, no message -summary = ["/path/to/summary"] # optional; runs as check_user; its output is in the question -apply = ["/path/to/apply"] # required; runs as root after Apply +summary = ["/path/to/summary"] # optional; runs as check_user; its output is in the question; + # a line that starts with "WARN: " is shown at the top of the question +apply = ["/path/to/apply"] # required; runs as root after Apply; the first 10 lines it + # writes to $PULLGATE_REPORT_FILE are added to the applied/failed message health = ["/path/to/health"] # optional; runs as root after apply check_user = "pullgate-check" diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index 27f8ad3..7ea3ab2 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -154,6 +154,26 @@ or during the review. reason is not cut away; root's tree paths in it become `NEW` and `OLD`. The head of the question (host, instance, SHA, the warnings) always comes first, before any text from the commit. +- **`WARN: ` lines of the summary in the head.** `summary` runs as + `check_user` on the commit's tree, so a hostile commit may make it print + `WARN: ` lines. They are shown only as cleaned code after `⚠ `, at most 10 + and a count, after the watched-path warnings. They never push out the + fixed head lines (host, instance, SHA, the broken and held lines). Each + is at most 220 characters after the escape, so all 10 take at most about + 2250 of the 3400 that the head may use: the watched-path warnings, root's + own, always keep room. Path warnings that still do not fit are counted + ("⚠ … and N more warnings"), as before. *Left:* a hostile summary can + print decoy `WARN: ` lines first and push the owner's real ones into the + "N more warnings from the summary" count; the owner's `summary` should + print its own warnings first. They change nothing that root checks: the tap is bound to + the code and the SHA in root's state. +- **The report file of `apply`.** `apply` runs as root and is the owner's + own command, but it may copy text from the tree. Root makes the file + itself, in its own trees directory, and reads it only when it is still + that same file (same device and inode, a regular file, opened with + `O_NOFOLLOW`), at most 64 KiB. So `apply` cannot make root show another + file by a symlink or a hard link. Its lines are cleaned and shown as code, at most + 10 lines and 1500 characters. A rollback and `health` get no report file. - **Hostile text shown as a link in a trusted message.** A commit picks its own file names, subjects and command output. Telegram turns text that looks like a domain or a URL (`README.md`, `login.example.com`), an diff --git a/internal/gate/apply.go b/internal/gate/apply.go index 03bf8c6..87700b1 100644 --- a/internal/gate/apply.go +++ b/internal/gate/apply.go @@ -7,6 +7,7 @@ import ( "io" "io/fs" "os" + "path/filepath" "strings" "syscall" @@ -230,7 +231,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( defer t.remove() wasBroken := st.Broken - err = e.tryApply(ctx, t, "apply", sha, old, failedSHA(st), false) + report, err := e.tryApply(ctx, t, "apply", sha, old, failedSHA(st), false) if stopped(ctx, err) { return "", err // in_progress stays: the next start pauses } @@ -247,10 +248,10 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( } e.Log.Info("applied", "sha", sha) if wasBroken != nil { - e.reply(q.ID, "✅ Applied "+code(sha)+". The fix forward worked: the instance is no longer broken.") + e.reply(q.ID, withReport("✅ Applied "+code(sha)+". The fix forward worked: the instance is no longer broken.", report)) return Applied, nil } - e.reply(q.ID, "✅ Applied "+code(sha)+".") + e.reply(q.ID, withReport("✅ Applied "+code(sha)+".", report)) return Applied, nil } @@ -268,7 +269,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( return "", err } e.Log.Error("the fix forward failed; the instance stays broken", "sha", sha) - e.reply(q.ID, "❌ The fix forward "+code(sha)+" failed ("+failure+"). No rollback was tried. The instance stays broken.\n"+e.waysOut()) + e.reply(q.ID, withReport("❌ The fix forward "+code(sha)+" failed ("+failure+"). No rollback was tried. The instance stays broken.\n"+e.waysOut(), report)) return StillBroken, nil } if e.Cfg.OnFailure == config.OnFailureRollback && old != "" { @@ -276,7 +277,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( if err := e.Dir.Save(st); err != nil { return "", err } - rerr := e.tryApply(ctx, t, "rb", old, sha, sha, true) + _, rerr := e.tryApply(ctx, t, "rb", old, sha, sha, true) if stopped(ctx, rerr) { return "", rerr } @@ -285,7 +286,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( if err := e.Dir.Save(st); err != nil { return "", err } - e.reply(q.ID, "❌ The apply of "+code(sha)+" failed ("+failure+"). Rolled back to "+code(old)+".") + e.reply(q.ID, withReport("❌ The apply of "+code(sha)+" failed ("+failure+"). Rolled back to "+code(old)+".", report)) return RolledBack, nil } // The broken state (DESIGN.md section 10). If this save fails, @@ -306,7 +307,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( return RollbackError, err } e.Log.Error("the apply and its rollback failed; the instance is broken", "sha", sha, "old", old) - e.reply(q.ID, "❌ The apply of "+code(sha)+" failed ("+failure+"), and the rollback to "+code(old)+" failed too ("+message.Reason(rerr.Error())+"). The host may be in a half state. The instance is now broken.\n"+e.waysOut()) + e.reply(q.ID, withReport("❌ The apply of "+code(sha)+" failed ("+failure+"), and the rollback to "+code(old)+" failed too ("+message.Reason(rerr.Error())+"). The host may be in a half state. The instance is now broken.\n"+e.waysOut(), report)) return RollbackError, nil } // on_failure = "pause", or nothing to roll back to: hold the SHA. @@ -315,7 +316,7 @@ func (e *Env) applySHA(ctx context.Context, st *state.State, q state.Question) ( if err := e.Dir.Save(st); err != nil { return "", err } - e.reply(q.ID, "❌ The apply of "+code(sha)+" failed ("+failure+"). It is held and not tried again. A newer commit will be asked, so you can fix it forward.") + e.reply(q.ID, withReport("❌ The apply of "+code(sha)+" failed ("+failure+"). It is held and not tried again. A newer commit will be asked, so you can fix it forward.", report)) return Held, nil } @@ -343,23 +344,82 @@ func stopped(ctx context.Context, err error) bool { return err != nil && (ctx.Err() != nil || errors.Is(err, ErrStopped)) } -// tryApply exports fresh trees and runs apply, then health. -func (e *Env) tryApply(ctx context.Context, t *trees, tag, sha, old, failed string, rollback bool) error { +// tryApply exports fresh trees and runs apply, then health. Outside a +// rollback, apply gets a report file; report is its text for the +// message (DESIGN.md section 11), "" when it is empty. +func (e *Env) tryApply(ctx context.Context, t *trees, tag, sha, old, failed string, rollback bool) (report string, err error) { p, err := e.prepare(ctx, t, tag, sha, old) if err != nil { - return fmt.Errorf("prepare: %w", err) + return "", fmt.Errorf("prepare: %w", err) } p.env.Failed = failed p.env.Rollback = rollback + if !rollback { + p.env.Report = filepath.Join(t.dir, tag+"-report") + f, err := os.OpenFile(p.env.Report, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return "", fmt.Errorf("prepare: make the report file: %w", err) + } + made, serr := f.Stat() + _ = f.Close() + if serr != nil { + return "", fmt.Errorf("prepare: the report file: %w", serr) + } + defer func() { report = e.readReport(p.env.Report, made) }() + } if err := e.runRoot(ctx, "apply", e.Cfg.Commands.Apply, p.env); err != nil { - return err + return "", err } if argv := e.Cfg.Commands.Health; len(argv) > 0 { - if err := e.runRoot(ctx, "health", argv, p.env); err != nil { - return err + h := p.env + h.Report = "" // the report file is for apply only + if err := e.runRoot(ctx, "health", argv, h); err != nil { + return "", err } } - return nil + return "", nil +} + +// maxReport is the most bytes of the report file that root reads. +const maxReport = 64 << 10 + +// readReport reads the report file of apply: the same file that root +// made (made), not a link, at most maxReport bytes. A file that cannot be read is logged and +// shown as a short note; it never stops the report of the apply. +func (e *Env) readReport(path string, made os.FileInfo) string { + f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0) //nolint:gosec // root made this file in its own trees directory + if errors.Is(err, fs.ErrNotExist) { + return "" // apply removed it: no report + } + if err != nil { + e.Log.Warn("read the report file of apply", "err", err) + return "(The report file of apply could not be read. See the journal.)" + } + defer func() { _ = f.Close() }() + // Only the file that root made: not a link, not another file put in + // its place (a hard link to another file has another inode). + if fi, err := f.Stat(); err != nil || !fi.Mode().IsRegular() || !os.SameFile(fi, made) { + e.Log.Warn("the report file of apply is not the file that root made", "path", path) + return "(The report file of apply is not the file that pullgate made, so it is not shown.)" + } + b, err := io.ReadAll(io.LimitReader(f, maxReport+1)) + if err != nil { + e.Log.Warn("read the report file of apply", "err", err) + return "(The report file of apply could not be read. See the journal.)" + } + truncated := len(b) > maxReport + if truncated { + b = b[:maxReport] + } + return message.Report(string(b), truncated) +} + +// withReport adds the report of apply to a message. +func withReport(text, report string) string { + if report == "" { + return text + } + return text + "\n" + report } // afterApply closes questions whose base is no longer the applied SHA, so diff --git a/internal/gate/env.go b/internal/gate/env.go index c77201e..b4b4a49 100644 --- a/internal/gate/env.go +++ b/internal/gate/env.go @@ -143,6 +143,7 @@ func (c *capBuf) Write(p []byte) (int, error) { // cmdEnv is the environment of a command (DESIGN.md section 11). type cmdEnv struct { SHA, OldSHA, Tree, OldTree, Changed, Failed string + Report string // PULLGATE_REPORT_FILE; only for apply Rollback bool } @@ -151,7 +152,7 @@ func (e *Env) environ(c cmdEnv) []string { if c.Rollback { rb = "1" } - return []string{ + env := []string{ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PULLGATE_INSTANCE=" + e.Cfg.Name, "PULLGATE_SHA=" + c.SHA, @@ -162,6 +163,10 @@ func (e *Env) environ(c cmdEnv) []string { "PULLGATE_FAILED_SHA=" + c.Failed, "PULLGATE_ROLLBACK=" + rb, } + if c.Report != "" { + env = append(env, "PULLGATE_REPORT_FILE="+c.Report) + } + return env } // command builds the exec.Cmd of argv. With sandbox it runs as check_user diff --git a/internal/gate/fixture_test.go b/internal/gate/fixture_test.go index 43da847..f31348a 100644 --- a/internal/gate/fixture_test.go +++ b/internal/gate/fixture_test.go @@ -52,11 +52,16 @@ func newFixture(t *testing.T, top, sections string) *fixture { c := f.ctrl f.script("apply.sh", `echo "apply $PULLGATE_SHA old=$PULLGATE_OLD_SHA rb=$PULLGATE_ROLLBACK failed=$PULLGATE_FAILED_SHA" >> `+c+`/log if [ "$PULLGATE_ROLLBACK" = 1 ] && [ -e `+c+`/fail-rollback ]; then exit 3; fi +echo "report=$PULLGATE_REPORT_FILE" >> `+c+`/env +if [ -n "$PULLGATE_REPORT_FILE" ] && [ -e "$PULLGATE_TREE/report" ]; then cat "$PULLGATE_TREE/report" > "$PULLGATE_REPORT_FILE"; fi +if [ -n "$PULLGATE_REPORT_FILE" ] && [ -e "$PULLGATE_TREE/report-link" ]; then rm -f "$PULLGATE_REPORT_FILE"; ln -s `+c+`/secret "$PULLGATE_REPORT_FILE"; fi +if [ -n "$PULLGATE_REPORT_FILE" ] && [ -e "$PULLGATE_TREE/report-hardlink" ]; then rm -f "$PULLGATE_REPORT_FILE"; ln `+c+`/secret "$PULLGATE_REPORT_FILE"; fi if [ -e "$PULLGATE_TREE/fail-apply" ]; then exit 2; fi tr '\0' '\n' < "$PULLGATE_CHANGED_FILES" > `+c+`/changed cat "$PULLGATE_TREE/value" > `+c+`/live `) - f.script("health.sh", `if [ -e "$PULLGATE_TREE/fail-health" ]; then exit 1; fi + f.script("health.sh", `echo "health report=$PULLGATE_REPORT_FILE" >> `+c+`/env +if [ -e "$PULLGATE_TREE/fail-health" ]; then exit 1; fi `) f.script("check.sh", `id -u > `+c+`/check-uid echo x >> `+c+`/check-count diff --git a/internal/gate/report_test.go b/internal/gate/report_test.go new file mode 100644 index 0000000..c3bbcdc --- /dev/null +++ b/internal/gate/report_test.go @@ -0,0 +1,127 @@ +package gate + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/code6x/pullgate/internal/spool" +) + +// Tests of code6x/pullgate#42: the report file of apply. + +func replies(ms []spool.Message) string { + var b strings.Builder + for _, m := range ms { + if m.Kind == spool.KindReply { + b.WriteString(m.Text + "\n") + } + } + return b.String() +} + +func (f *fixture) askAndApply(subject, value string, more map[string]string) (Result, string) { + f.t.Helper() + f.commit(subject, value, more) + if r := f.run(); r != Asked { + f.t.Fatalf("run: %s", r) + } + f.tap(f.ask().Code, "apply") + r := f.apply() + return r, replies(f.outbox()) +} + +func TestApplyReportTwoLines(t *testing.T) { + f := newFixture(t, ``, "") + r, text := f.askAndApply("first", "one", map[string]string{"report": "skipped step x on purpose\nall else done \n"}) + if r != Applied { + t.Fatalf("apply: %s", r) + } + for _, want := range []string{"✅ Applied", "Report of apply:\nskipped step x on purpose\nall else done <b>"} { + if !strings.Contains(text, want) { + t.Errorf("missing %q in\n%s", want, text) + } + } + if strings.Contains(text, "more lines") { + t.Error("no count for 2 lines") + } + env := f.read("env") + if !strings.Contains(env, "report="+f.env.Dir.Trees()) || !strings.Contains(env, "health report=\n") && !strings.HasSuffix(env, "health report=") { + t.Fatalf("apply gets the report file and health does not:\n%s", env) + } +} + +func TestApplyReportFifteenLinesShowTen(t *testing.T) { + f := newFixture(t, ``, "") + var lines []string + for i := 1; i <= 15; i++ { + lines = append(lines, fmt.Sprintf("line %d", i)) + } + _, text := f.askAndApply("first", "one", map[string]string{"report": strings.Join(lines, "\n") + "\n"}) + if n := strings.Count(text, "line "); n != 10 { + t.Fatalf("%d lines shown, want 10:\n%s", n, text) + } + if !strings.Contains(text, "line 10") || strings.Contains(text, "line 11") || !strings.Contains(text, "… 5 more lines in the report") { + t.Fatalf("the first 10 and a count:\n%s", text) + } +} + +func TestApplyReportInTheFailedMessage(t *testing.T) { + f := newFixture(t, ``, "") + r, text := f.askAndApply("first", "one", map[string]string{"report": "step 3 broke", "fail-apply": "1"}) + if r != Held { + t.Fatalf("apply: %s", r) + } + if !strings.Contains(text, "❌") || !strings.Contains(text, "step 3 broke") { + t.Fatalf("the failed message has the report:\n%s", text) + } +} + +func TestApplyWithoutReportIsUnchanged(t *testing.T) { + f := newFixture(t, ``, "") + _, text := f.askAndApply("first", "one", nil) + if strings.Contains(text, "Report") || !strings.Contains(text, "✅ Applied") { + t.Fatalf("no report, no report lines:\n%s", text) + } +} + +func TestApplyReportLinkIsNotFollowed(t *testing.T) { + f := newFixture(t, ``, "") + if err := os.WriteFile(filepath.Join(f.ctrl, "secret"), []byte("secret-value\n"), 0o600); err != nil { + t.Fatal(err) + } + r, text := f.askAndApply("first", "one", map[string]string{"report-link": "1"}) + if r != Applied { + t.Fatalf("apply: %s", r) + } + if strings.Contains(text, "secret-value") || !strings.Contains(text, "could not be read") { + t.Fatalf("a link as the report file is not read:\n%s", text) + } +} + +func TestApplyReportHardLinkIsNotRead(t *testing.T) { + f := newFixture(t, ``, "") + if err := os.WriteFile(filepath.Join(f.ctrl, "secret"), []byte("secret-value\n"), 0o600); err != nil { + t.Fatal(err) + } + r, text := f.askAndApply("first", "one", map[string]string{"report-hardlink": "1"}) + if r != Applied { + t.Fatalf("apply: %s", r) + } + if strings.Contains(text, "secret-value") || !strings.Contains(text, "not the file that pullgate made") { + t.Fatalf("a hard link as the report file is not read:\n%s", text) + } +} + +func TestApplyReportHostileText(t *testing.T) { + f := newFixture(t, ``, "") + _, text := f.askAndApply("first", "one", map[string]string{"report": "x\n\u202edm.EMDAER\n" + strings.Repeat("&", 5000) + "\n"}) + if strings.Contains(text, " 3000 { + t.Fatalf("the report is cut: %d", len([]rune(text))) + } +} diff --git a/internal/message/message.go b/internal/message/message.go index 7574ca7..6065ddf 100644 --- a/internal/message/message.go +++ b/internal/message/message.go @@ -228,3 +228,42 @@ func closedBefore(body []string, open, i int) bool { } return false } + +// ReportLines is the most lines of the apply report in a message. +const ReportLines = 10 + +// Report is the report file of apply in a message (DESIGN.md section +// 11): its first ReportLines lines, each cleaned and in, at most +// OutputMax characters in all; a last line counts the lines left out. +// truncated says that the file was longer than root read. It is "" when +// the report has no lines. +func Report(text string, truncated bool) string { + text = strings.TrimRight(text, "\n") + if strings.TrimSpace(text) == "" { + return "" + } + lines := strings.Split(text, "\n") + out := []string{"Report of apply:"} + used := 0 + shown := 0 + for _, l := range lines { + if shown >= ReportLines { + break + } + c := Code(strings.TrimRight(l, "\r")) + if used+Len(c)+1 > OutputMax && shown > 0 { + break + } + out = append(out, c) + used += Len(c) + 1 + shown++ + } + if left := len(lines) - shown; left > 0 || truncated { + more := fmt.Sprintf("… %d more lines in the report", left) + if truncated { + more = "… more lines in the report (only its start was read)" + } + out = append(out, more) + } + return strings.Join(out, "\n") +} diff --git a/internal/message/question.go b/internal/message/question.go index 01657de..62754e8 100644 --- a/internal/message/question.go +++ b/internal/message/question.go @@ -58,20 +58,22 @@ func (q *Question) Text() string { } else { head = append(head, "Since "+SHA(q.Applied)+":") } + var watched, other []string for _, f := range Watched(q.Watch, q.Files) { - head = append(head, "⚠ Watched path changed: "+Code(f)) + watched = append(watched, "⚠ Watched path changed: "+Code(f)) } for _, s := range q.Symlinks { - head = append(head, "⚠ Symlink out of the tree: "+Code(s.Path)+" → "+Code(s.Target)) + other = append(other, "⚠ Symlink out of the tree: "+Code(s.Path)+" → "+Code(s.Target)) } for _, s := range q.Submodules { - head = append(head, "⚠ Submodule skipped: "+Code(s)) + other = append(other, "⚠ Submodule skipped: "+Code(s)) } - // The head holds text from the commit too (watched paths); keep it - // within the limit on its own. - if Len(strings.Join(head, "\n")) > Limit-200 { - head = cutHead(head) + var warns []string + summary := q.Summary + if q.SummaryFailed == "" { + warns, summary = SplitWarnings(q.Summary) } + head = buildHead(head, watched, warns, other) var body []string body = append(body, "", fmt.Sprintf("Commits (%d):", q.TotalCommits)) for _, s := range q.Subjects { @@ -88,10 +90,10 @@ func (q *Question) Text() string { switch { case q.SummaryFailed != "": body = append(body, "⚠ The summary command failed: "+Code(q.SummaryFailed)) - case strings.TrimSpace(q.Summary) != "": + case strings.TrimSpace(summary) != "": body = append(body, "Summary:") pre = len(body) - lines := strings.Split(strings.TrimRight(q.Summary, "\n"), "\n") + lines := strings.Split(strings.TrimRight(summary, "\n"), "\n") body = append(body, ""+esc(lines[0])) for _, l := range lines[1:] { body = append(body, esc(l)) @@ -103,21 +105,109 @@ func (q *Question) Text() string { return Fit(head, body, "Full: "+Code(q.SHA), pre) } -// cutHead keeps the fixed lines and as many warnings as fit. -func cutHead(head []string) []string { - var out []string - used := 0 +// WarnPrefix starts a line of the summary output that is shown in the +// head of the question, not in the summary block (DESIGN.md section 12). +const WarnPrefix = "WARN: " + +// MaxWarnings is the most summary warnings shown; more give one line +// that counts them. +const MaxWarnings = 10 + +// maxWarnLen is the most UTF-16 units of one summary warning line, after +// the escape. 200 plain characters fit; text that the escape makes longer +// is cut. So 10 of them take at most about 2250 units, and the watched +// paths, root's own warnings, always keep room in the head. +const maxWarnLen = 220 + +// SplitWarnings takes the lines that start with WarnPrefix out of the +// summary output. It returns their text (without the prefix) and the +// rest of the output. +func SplitWarnings(summary string) (warns []string, rest string) { + if !strings.Contains(summary, WarnPrefix) { + return nil, summary + } + var keep []string + for _, l := range strings.Split(summary, "\n") { + if t, ok := strings.CutPrefix(l, WarnPrefix); ok { + // A warning with no text is dropped; a CRLF line ending is + // not part of the text. + if t = strings.TrimRight(t, "\r"); strings.TrimSpace(t) != "" { + warns = append(warns, t) + } + continue + } + keep = append(keep, l) + } + return warns, strings.Join(keep, "\n") +} + +// warnLine is one summary warning in the head: cleaned, as code, and cut +// so that its escaped text is at most maxWarnLen. +func warnLine(s string) string { + l := "⚠ " + Code(s) + r := []rune(s) + for n := len(r); Len(l) > maxWarnLen && n > 0; { + n = n * 3 / 4 + l = "⚠ " + Code(string(r[:n])+"…") + } + return l +} + +// buildHead adds the warnings to the fixed lines of the head. The +// summary warnings come after the watched paths and are never cut for +// the lists or for other warnings. Only when the fixed lines and the +// summary warnings alone do not fit are the last summary warnings counted +// instead. Path warnings are kept while they fit, and the rest counted. +func buildHead(fixed, watched, warns, other []string) []string { + var sum []string + more := 0 + for i, w := range warns { + if i >= MaxWarnings { + more = len(warns) - MaxWarnings + break + } + sum = append(sum, warnLine(w)) + } + moreLine := func() []string { + if more == 0 { + return nil + } + return []string{fmt.Sprintf("⚠ %d more warnings from the summary", more)} + } + all := append(append(append(append(append([]string{}, fixed...), watched...), sum...), moreLine()...), other...) + if Len(strings.Join(all, "\n")) <= Limit-200 { + return all + } + size := func(ls []string) int { + n := 0 + for _, l := range ls { + n += Len(l) + 1 + } + return n + } + used := size(fixed) + // The summary warnings get the whole room of the head (Limit-200); + // the path warnings only what is left within Limit-400. + for len(sum) > 0 && used+size(sum)+size(moreLine()) > Limit-200 { + sum = sum[:len(sum)-1] + more++ + } + used += size(sum) + size(moreLine()) left := 0 - for _, l := range head { - if strings.HasPrefix(l, "⚠ Watched") || strings.HasPrefix(l, "⚠ Symlink") || strings.HasPrefix(l, "⚠ Submodule") { + keep := func(ls []string) []string { + var out []string + for _, l := range ls { if used+Len(l)+1 > Limit-400 { left++ continue } + out = append(out, l) + used += Len(l) + 1 } - out = append(out, l) - used += Len(l) + 1 + return out } + kw, ko := keep(watched), keep(other) + out := append(append(append(append(append([]string{}, fixed...), kw...), sum...), moreLine()...), ko...) if left > 0 { out = append(out, fmt.Sprintf("⚠ … and %d more warnings", left)) } diff --git a/internal/message/warn_test.go b/internal/message/warn_test.go new file mode 100644 index 0000000..08c69d4 --- /dev/null +++ b/internal/message/warn_test.go @@ -0,0 +1,195 @@ +package message + +import ( + "fmt" + "strings" + "testing" +) + +// Tests of code6x/pullgate#42: WARN: lines of the summary in the head. + +func headOf(text string) string { + i := strings.Index(text, "\nCommits (") + if i < 0 { + return text + } + return text[:i] +} + +func TestSummaryWarningsStayInTheHeadOfALongQuestion(t *testing.T) { + q := baseQuestion() + for i := 0; i < 500; i++ { + q.Files = append(q.Files, fmt.Sprintf("dir/file-%04d.txt", i)) + } + q.TotalCommits = 1 + q.Subjects = []string{"many files"} + q.Summary = "line one\nWARN: sudoers changed\nline two\nWARN: login group changed\n" + strings.Repeat("more summary\n", 200) + text := q.Text() + if Len(text) > Limit { + t.Fatalf("too long: %d", Len(text)) + } + head := headOf(text) + for _, w := range []string{"⚠sudoers changed", "⚠login group changed"} { + if !strings.Contains(head, w) { + t.Errorf("missing %q in the head:\n%s", w, head) + } + } + if strings.Count(text, "sudoers changed") != 1 || strings.Contains(text, "WARN:") { + t.Error("a warning line is taken out of the summary block") + } + if !strings.Contains(text, "more lines left out") || strings.Contains(text, "dir/file-0499.txt") { + t.Error("the file list must be cut") + } + if !strings.HasSuffix(text, "Full:"+q.SHA+"") { + t.Error("the Full: line stays last") + } +} + +func TestSummaryWarningsComeAfterWatchedPaths(t *testing.T) { + q := baseQuestion() + q.Files = []string{"policy/a.toml"} + q.Symlinks = []Symlink{{Path: "link", Target: "/etc"}} + q.Summary = "WARN: x\n" + text := q.Text() + w, s, l := strings.Index(text, "⚠ Watched"), strings.Index(text, "⚠x"), strings.Index(text, "⚠ Symlink") + if w < 0 || s < w || l < s { + t.Fatalf("order: watched, summary warnings, symlinks:\n%s", text) + } + if strings.Contains(text, "Summary:") { + t.Error("a summary of only warnings has no summary block") + } +} + +func TestSummaryWarningIsOnlyCleanedCode(t *testing.T) { + q := baseQuestion() + q.Files = []string{"a"} + for _, h := range hostile { + q.Summary += WarnPrefix + h + "\n" + } + text := q.Text() + out := outsideCode(text) + for _, m := range markers { + if strings.Contains(out, m) { + t.Errorf("%q is outside:\n%s", m, out) + } + } + if strings.ContainsRune(text, '\u202e') || strings.Contains(text, "<a href="https://example.com">Apply</a>") { + t.Errorf("the HTML warning is shown escaped, as code:\n%s", headOf(text)) + } + if !strings.Contains(headOf(text), "⚠?dm.EMDAER") { + t.Error("the right-to-left mark becomes ?") + } +} + +func TestTwelveSummaryWarningsShowTenAndACount(t *testing.T) { + q := baseQuestion() + q.Files = []string{"a"} + for i := 1; i <= 12; i++ { + q.Summary += fmt.Sprintf("WARN: warning %d\n", i) + } + text := q.Text() + if n := strings.Count(text, "⚠warning "); n != 10 { + t.Fatalf("%d warnings shown, want 10:\n%s", n, text) + } + if !strings.Contains(text, "⚠warning 10") || strings.Contains(text, "warning 11") { + t.Error("the first 10 are shown") + } + if !strings.Contains(headOf(text), "⚠ 2 more warnings from the summary") { + t.Errorf("missing the count:\n%s", text) + } +} + +func TestLongSummaryWarningsAlwaysFit(t *testing.T) { + q := baseQuestion() + q.Broken = strings.Repeat("&", 900) + for i := 0; i < 500; i++ { + q.Files = append(q.Files, fmt.Sprintf("policy/%03d-%s", i, strings.Repeat("y", 150))) + } + for i := 0; i < 12; i++ { + q.Summary += WarnPrefix + strings.Repeat("&\"<", 100) + "\n" + } + text := q.Text() + if Len(text) > Limit { + t.Fatalf("too long: %d", Len(text)) + } + if strings.Count(text, "") != strings.Count(text, "") { + t.Fatal("atag is not closed") + } + if !strings.Contains(text, "more warnings from the summary") || !strings.Contains(text, "Full:"+q.SHA) { + t.Fatalf("the count and the Full: line stay:\n%s", text) + } +} + +func TestSummaryWithoutWarningsIsUnchanged(t *testing.T) { + // The text of 0.2.0 for the same question, built by hand. + q := baseQuestion() + q.Files = []string{"policy/a.toml", "b"} + q.TotalCommits = 1 + q.Subjects = []string{"one"} + q.Summary = "value 1\n WARN: not at the start\nwarn: lower case\n" + want := "pullgate on example-host, instance config-repo\n" + + "Apply this commit?\naaaaaaaaaaaa\nSincebbbbbbbbbbbb:\n" + + "⚠ Watched path changed:policy/a.toml\n" + + "\nCommits (1):\n•one\nChanged files (2):\n•policy/a.toml\n•b\n" + + "Summary:\nvalue 1\n WARN: not at the start\nwarn: lower case\n\n" + + "Full:" + q.SHA + "" + if got := q.Text(); got != want { + t.Fatalf("got\n%s\nwant\n%s", got, want) + } + // Many path warnings and no summary warnings: as in 0.2.0. + q = baseQuestion() + for i := 0; i < 500; i++ { + q.Files = append(q.Files, fmt.Sprintf("policy/%03d-%s", i, strings.Repeat("y", 150))) + } + if text := q.Text(); strings.Contains(text, "from the summary") || !strings.Contains(text, "⚠ … and") { + t.Fatal("path warnings are cut as before") + } +} + +func TestSummaryWarningCRLFAndEmpty(t *testing.T) { + q := baseQuestion() + q.Files = []string{"a"} + q.Summary = "WARN: crlf line\r\nWARN: \nWARN: \nnormal\n" + text := q.Text() + if !strings.Contains(text, "⚠crlf line") { + t.Fatalf("a CRLF ending is not part of the warning:\n%s", text) + } + if strings.Contains(text, "⚠") || strings.Contains(text, "⚠") || strings.Contains(text, "WARN:") { + t.Fatalf("an empty warning is dropped:\n%s", text) + } +} + +func TestTenLongSummaryWarningsAllStayWithAShortHead(t *testing.T) { + q := baseQuestion() + for i := 0; i < 500; i++ { + q.Files = append(q.Files, fmt.Sprintf("policy/%03d-%s", i, strings.Repeat("y", 150))) + } + for i := 0; i < 10; i++ { + q.Summary += WarnPrefix + fmt.Sprintf("%d", i) + strings.Repeat("&", 199) + "\n" + } + text := q.Text() + if Len(text) > Limit { + t.Fatalf("too long: %d", Len(text)) + } + if strings.Count(text, "⚠") != 10 || strings.Contains(text, "from the summary") { + t.Fatalf("all 10 long warnings stay:\n%s", text) + } +} + +func TestWatchedPathStaysWithTenLongSummaryWarnings(t *testing.T) { + q := baseQuestion() + q.Files = []string{"policy/sudoers"} + for i := 0; i < 10; i++ { + q.Summary += WarnPrefix + strings.Repeat("<", 200) + "\n" + } + text := q.Text() + if !strings.Contains(headOf(text), "⚠ Watched path changed:policy/sudoers") { + t.Fatalf("the watched path must stay:\n%s", headOf(text)) + } + if strings.Count(text, "⚠<") != 10 || Len(text) > Limit { + t.Fatal("all 10 summary warnings stay, cut, and the message fits") + } +}