oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "amadan.net/oficina/equipo/internal/wire/forgekey"
5 "context"
6 "fmt"
7 "slices"
8 "strings"
9 "time"
10 "unicode/utf8"
11
12 "amadan.net/oficina/equipo/internal/runner/forge"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// HostedCI is the hub's own CI as the primary reads it with the agent's
17// credential (release slice 1): whether the hub runs a repository's CI on
18// its machines, and its verdict on a branch's tip.
19type HostedCI interface {
20 CIHosted(ctx context.Context, repo string) (bool, error)
21 CIStatus(ctx context.Context, repo, branch, logs string) (forge.CIStatus, error)
22}
23
24// phaseHosted marks a checks attempt that waits for the hub's hosted CI:
25// it runs again after a restart instead of failing runner_restarted.
26const phaseHosted = "hosted"
27
28// Hosted failure logs remain diagnostic input for copy recovery. They never
29// certify base-run checks or select the primary checks executor.
30var hostedRead = 30 * time.Second
31var hostedWait = 60 * time.Minute
32
33func (e *Engineer) hostedChecks(ctx context.Context, r stepRow, rep wire.StepReport) wire.StepReport {
34 if e.CI == nil {
35 rep.State, rep.Termination, rep.Checks, rep.Reason = wire.StepDone, true, wire.ChecksCannotRun, "checks_unavailable"
36 return rep
37 }
38 cctx, cancel := context.WithTimeout(ctx, hostedRead)
39 st, err := e.CI.CIStatus(cctx, r.Repo, r.Branch, forge.CILogsNone)
40 cancel()
41 if ctx.Err() != nil {
42 rep.State, rep.Reason, rep.Termination = wire.StepCancelled, "cancelled", true
43 return rep
44 }
45 if err == nil && st.SHA == r.CandidateSHA && st.UpdatedAt > 0 {
46 if out, ok := e.hostedVerdict(ctx, r, rep, st); ok {
47 return out
48 }
49 }
50 if since := e.O.State.deferredSince(context.WithoutCancel(ctx), r); !since.IsZero() && time.Since(since) >= hostedWait {
51 rep.State, rep.Termination, rep.Checks, rep.Reason = wire.StepDone, true, wire.ChecksCannotRun, "checks_timeout"
52 rep.LogTail = fmt.Sprintf("Hosted CI gave no verdict on %s within %d minutes.", shortSHA(r.CandidateSHA), int(hostedWait/time.Minute))
53 return rep
54 }
55 rep.State = stepDeferred
56 return rep
57}
58
59// hostedVerdict is the report for a verdict the hub gave on the
60// candidate, from that one snapshot (its failing logs only from a second
61// read of the very same run). ok is false while there is no verdict.
62func (e *Engineer) hostedVerdict(ctx context.Context, r stepRow, rep wire.StepReport, st forge.CIStatus) (wire.StepReport, bool) {
63 done := func(outcome, reason string) (wire.StepReport, bool) {
64 rep.State, rep.Termination, rep.Checks, rep.Reason = wire.StepDone, true, outcome, reason
65 rep.Hosted = &wire.HostedResult{Repo: r.Repo, Branch: r.Branch, SHA: st.SHA, State: st.State, ReportedAt: st.UpdatedAt, Failed: failedUnits(st.Steps)}
66 return rep, true
67 }
68 where := "Hosted CI on " + forgekey.Display(r.Repo) + " at " + shortSHA(st.SHA)
69 switch st.State {
70 case wire.HostedSuccess:
71 code := 0
72 rep.Gate, rep.ExitCode, rep.Output = wire.GateHosted, &code, where+": passed."
73 rep.LogTail = rep.Output
74 return done(wire.ChecksPassed, "")
75 case wire.HostedFailure:
76 code := 1
77 var logs []wire.CIUnit
78 cctx, cancel := context.WithTimeout(ctx, hostedRead)
79 full, err := e.CI.CIStatus(cctx, r.Repo, r.Branch, forge.CILogsFailed)
80 cancel()
81 if err == nil && full.SHA == st.SHA && full.State == st.State && full.UpdatedAt == st.UpdatedAt {
82 logs = failedLogs(full.Steps)
83 }
84 head := where + ": failed.\n"
85 if names := failedUnits(st.Steps); len(names) > 0 {
86 head += "Failed: " + strings.Join(names, "; ") + "\n"
87 }
88 if s := strings.TrimSpace(st.Summary); s != "" {
89 head += s + "\n"
90 }
91 // Every failing unit's share of the output, its failing lines
92 // first: the fix round reads them all.
93 rep.Gate, rep.ExitCode, rep.Output = wire.GateHosted, &code, head+wire.JoinCIUnits(logs, wire.MaxChecksOutput-len(head))
94 rep.LogTail = lastLines(rep.Output, stepTailLines, wire.MaxLogTail)
95 return done(wire.ChecksFailed, "")
96 case "skipped":
97 rep.LogTail = where + ": no CI script."
98 return done(wire.ChecksNone, "")
99 case "lost", "stale":
100 rep.LogTail = where + ": the run did not finish (" + st.State + ")."
101 return done(wire.ChecksCannotRun, "checks_unavailable")
102 }
103 return rep, false
104}
105
106// failedUnits names what failed, finest first: a sharded step's failing
107// shards, else the step. At most wire.MaxHostedFailed names of
108// wire.MaxHostedName bytes.
109func failedUnits(steps []forge.CIStep) []string {
110 out := []string{}
111 add := func(name string) {
112 if len(out) < wire.MaxHostedFailed {
113 out = append(out, unitName(name))
114 }
115 }
116 for _, s := range steps {
117 if s.State != wire.HostedFailure {
118 continue
119 }
120 shards := slices.IndexFunc(s.Shards, func(sh forge.CIShard) bool { return sh.State == wire.HostedFailure }) >= 0
121 if !shards {
122 add(s.Name)
123 continue
124 }
125 for _, sh := range s.Shards {
126 if sh.State == wire.HostedFailure {
127 add(s.Name + " " + sh.Name)
128 }
129 }
130 }
131 return out
132}
133
134// failedLogs are the logs of the failing units the hub sent, each named
135// as failedUnits names a unit.
136func failedLogs(steps []forge.CIStep) []wire.CIUnit {
137 var out []wire.CIUnit
138 for _, s := range steps {
139 if s.Log != "" {
140 out = append(out, wire.CIUnit{Name: unitName(s.Name), Log: s.Log})
141 }
142 for _, sh := range s.Shards {
143 if sh.Log != "" {
144 out = append(out, wire.CIUnit{Name: unitName(s.Name + " " + sh.Name), Log: sh.Log})
145 }
146 }
147 }
148 return out
149}
150
151// unitName is a unit's name as the binding records it: one line, bounded.
152func unitName(name string) string { return clipName(strings.Join(strings.Fields(name), " ")) }
153
154func clipName(s string) string {
155 for len(s) > wire.MaxHostedName {
156 _, n := utf8.DecodeLastRuneInString(s)
157 s = s[:len(s)-n]
158 }
159 return s
160}
161
162// tailBytes is the end of s within limit bytes, starting on a line.
163func tailBytes(s string, limit int) string {
164 if limit <= 0 {
165 return ""
166 }
167 if len(s) <= limit {
168 return s
169 }
170 s = s[len(s)-limit:]
171 if i := strings.IndexByte(s, '\n'); i >= 0 {
172 s = s[i+1:]
173 }
174 return strings.ToValidUTF8(s, "")
175}
176
177func shortSHA(sha string) string { return sha[:min(len(sha), 7)] }
178