| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/copydesk" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | func (e *Engineer) classifyChecks(ctx context.Context, r stepRow, rep wire.StepReport) wire.StepReport { |
| 16 | if rep.State != wire.StepDone || rep.Checks != wire.ChecksFailed { |
| 17 | return rep |
| 18 | } |
| 19 | tree, _, err := e.judgeTree(ctx, r) |
| 20 | if err != nil { |
| 21 | return rep |
| 22 | } |
| 23 | defer os.RemoveAll(tree.Dir) |
| 24 | defer os.RemoveAll(tree.GitDir) |
| 25 | r.Copy = &wire.CopyDispatch{Compared: r.BaseSHA} |
| 26 | w, err := e.prepareCopyContract(ctx, r, tree) |
| 27 | if w.root != "" { |
| 28 | defer os.RemoveAll(w.root) |
| 29 | } |
| 30 | if err != nil || w.contract == nil { |
| 31 | return rep |
| 32 | } |
| 33 | in := copydesk.Input{Version: 1, Units: []string{rep.Gate}, Log: rep.Output, Truncated: len(strings.Split(strings.TrimRight(rep.Output, "\n"), "\n")) >= wire.MaxChecksLines || len(rep.Output) >= wire.MaxChecksOutput} |
| 34 | if rep.Hosted != nil { |
| 35 | if e.CI == nil { |
| 36 | return rep |
| 37 | } |
| 38 | cctx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 39 | full, err := e.CI.CIStatus(cctx, r.Repo, r.Branch, forge.CILogsFailed) |
| 40 | cancel() |
| 41 | if err != nil || full.SHA != rep.Hosted.SHA || full.State != wire.HostedFailure || full.UpdatedAt != rep.Hosted.ReportedAt { |
| 42 | return rep |
| 43 | } |
| 44 | in.Units = failedUnits(full.Steps) |
| 45 | in.Log = copyFailedLogs(full.Steps) |
| 46 | count := 0 |
| 47 | for _, step := range full.Steps { |
| 48 | if step.State != wire.HostedFailure { |
| 49 | continue |
| 50 | } |
| 51 | n := 0 |
| 52 | for _, shard := range step.Shards { |
| 53 | if shard.State == wire.HostedFailure { |
| 54 | n++ |
| 55 | } |
| 56 | } |
| 57 | count += max(n, 1) |
| 58 | } |
| 59 | in.Truncated = count > wire.MaxHostedFailed |
| 60 | } |
| 61 | if raw, err := json.Marshal(in); err != nil || len(raw) > 2<<20 { |
| 62 | in.Log = in.Log[:min(len(in.Log), 256<<10)] |
| 63 | in.Truncated = true |
| 64 | } |
| 65 | var c copydesk.Classification |
| 66 | if err := e.copyCommand(ctx, r, w, w.contract.Classify, in, &c); err != nil { |
| 67 | return rep |
| 68 | } |
| 69 | script, err := e.copyRead(ctx, tree, r.BaseSHA, w.contract.Classify[1]) |
| 70 | if err != nil { |
| 71 | return rep |
| 72 | } |
| 73 | signature := copydesk.Classify(in, c, copydesk.Hash(string(script))) |
| 74 | rep.Failure = &signature |
| 75 | return rep |
| 76 | } |
| 77 | |
| 78 | func copyFailedLogs(steps []forge.CIStep) string { |
| 79 | var out strings.Builder |
| 80 | for _, step := range steps { |
| 81 | if step.State != wire.HostedFailure { |
| 82 | continue |
| 83 | } |
| 84 | shards := false |
| 85 | for _, sh := range step.Shards { |
| 86 | if sh.State == wire.HostedFailure { |
| 87 | shards = true |
| 88 | out.WriteString("=== " + clipName(strings.Join(strings.Fields(step.Name+" "+sh.Name), " ")) + "\n" + sh.Log + "\n") |
| 89 | } |
| 90 | } |
| 91 | if !shards { |
| 92 | out.WriteString("=== " + clipName(strings.Join(strings.Fields(step.Name), " ")) + "\n" + step.Log + "\n") |
| 93 | } |
| 94 | } |
| 95 | return out.String() |
| 96 | } |
| 97 | |