package thin import ( "amadan.net/oficina/equipo/internal/runner/forge" "context" "encoding/json" "os" "strings" "time" "amadan.net/oficina/equipo/internal/copydesk" "amadan.net/oficina/equipo/internal/wire" ) func (e *Engineer) classifyChecks(ctx context.Context, r stepRow, rep wire.StepReport) wire.StepReport { if rep.State != wire.StepDone || rep.Checks != wire.ChecksFailed { return rep } tree, _, err := e.judgeTree(ctx, r) if err != nil { return rep } defer os.RemoveAll(tree.Dir) defer os.RemoveAll(tree.GitDir) r.Copy = &wire.CopyDispatch{Compared: r.BaseSHA} w, err := e.prepareCopyContract(ctx, r, tree) if w.root != "" { defer os.RemoveAll(w.root) } if err != nil || w.contract == nil { return rep } 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} if rep.Hosted != nil { if e.CI == nil { return rep } cctx, cancel := context.WithTimeout(ctx, 30*time.Second) full, err := e.CI.CIStatus(cctx, r.Repo, r.Branch, forge.CILogsFailed) cancel() if err != nil || full.SHA != rep.Hosted.SHA || full.State != wire.HostedFailure || full.UpdatedAt != rep.Hosted.ReportedAt { return rep } in.Units = failedUnits(full.Steps) in.Log = copyFailedLogs(full.Steps) count := 0 for _, step := range full.Steps { if step.State != wire.HostedFailure { continue } n := 0 for _, shard := range step.Shards { if shard.State == wire.HostedFailure { n++ } } count += max(n, 1) } in.Truncated = count > wire.MaxHostedFailed } if raw, err := json.Marshal(in); err != nil || len(raw) > 2<<20 { in.Log = in.Log[:min(len(in.Log), 256<<10)] in.Truncated = true } var c copydesk.Classification if err := e.copyCommand(ctx, r, w, w.contract.Classify, in, &c); err != nil { return rep } script, err := e.copyRead(ctx, tree, r.BaseSHA, w.contract.Classify[1]) if err != nil { return rep } signature := copydesk.Classify(in, c, copydesk.Hash(string(script))) rep.Failure = &signature return rep } func copyFailedLogs(steps []forge.CIStep) string { var out strings.Builder for _, step := range steps { if step.State != wire.HostedFailure { continue } shards := false for _, sh := range step.Shards { if sh.State == wire.HostedFailure { shards = true out.WriteString("=== " + clipName(strings.Join(strings.Fields(step.Name+" "+sh.Name), " ")) + "\n" + sh.Log + "\n") } } if !shards { out.WriteString("=== " + clipName(strings.Join(strings.Fields(step.Name), " ")) + "\n" + step.Log + "\n") } } return out.String() }