package forge import ( "encoding/base64" "errors" "io" "regexp" "strings" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) // The refusals git's answer to a push can show. const ( // RefusalNonFastForward: the branch is not an ancestor of the commit // ("[rejected] ... (non-fast-forward)", "fetch first", or the hub's // own "[remote rejected] ... (non-fast-forward)"). RefusalNonFastForward = "non-fast-forward" // RefusalRefused: the hub refused the update itself: a hook, a // protected branch ("[remote rejected] ...", "pre-receive hook // declined"). RefusalRefused = "refused" ) // PushError is git's answer to a push, or to the read of the branch // around it, that failed: what git was doing (Op: "push" or // "ls-remote"), the refusal its answer shows (a push only; "" when it // shows none) and its words (the rejected ref lines and stderr), // redacted and bounded. It wraps the outcome (ErrPushUncertain, // ErrPushConflict), which still decides the step's reason. type PushError struct { Op, Refusal, Answer string Err error } func (e *PushError) Error() string { s := e.Err.Error() + ": git " + e.Op if e.Refusal != "" { s += " (" + e.Refusal + ")" } return s + ": " + e.Answer } func (e *PushError) Unwrap() error { return e.Err } // PushRefusal is the refusal a failed push's answer shows, or "". func PushRefusal(err error) string { var p *PushError if errors.As(err, &p) { return p.Refusal } return "" } // PushDetail is a failed push for the step report, a few lines: the // refusal and git's answer when git gave one, else the error's own // words; redacted and at most runnerproto.DetailLimit bytes either way. func PushDetail(err error) string { var p *PushError switch { case err == nil: return "" case !errors.As(err, &p): return "error: " + runnerproto.RedactDetail(err.Error(), runnerproto.DetailLimit-len("error: ")) } out := "" if p.Refusal != "" { out = "refusal: " + p.Refusal + "\n" } out += p.Op + ": " return out + runnerproto.RedactDetail(p.Answer, runnerproto.DetailLimit-len(out)) } // gitAnswer is a git command that failed and what it said. type gitAnswer struct { err error refusal, text string } func (g *gitAnswer) Error() string { return g.text } func (g *gitAnswer) Unwrap() error { return g.err } // pushError is outcome with git's answer when err carries one. func pushError(op string, err, outcome error) error { var g *gitAnswer if !errors.As(err, &g) { return outcome } p := &PushError{Op: op, Answer: g.text, Err: outcome} if op == "push" { p.Refusal = g.refusal } if p.Answer == "" { p.Answer = runnerproto.RedactDetail(g.err.Error(), runnerproto.DetailLimit) } return p } var ( pushNonFastForward = regexp.MustCompile(`(?i)\[(?:remote )?rejected\][^\n]*\((?:non-fast-forward|fetch first)\)`) pushRefused = regexp.MustCompile(`(?i)\[remote rejected\]|pre-receive hook declined|protected branch|\bis protected\b|\bGH006\b`) ) // pushRefusalOf is the refusal git's output shows: a non-fast-forward // before anything else, then the hub's own refusal; "" for neither (a // stale lease, a transport failure). func pushRefusalOf(out string) string { switch { case pushNonFastForward.MatchString(out): return RefusalNonFastForward case pushRefused.MatchString(out): return RefusalRefused } return "" } // pushAnswer is git's answer, safe to log and report: the rejected ref // lines of a porcelain push, then stderr without git's hints; the git // password gone (raw and as the Basic header git sends), then every // other credential shape, one line, bounded. func pushAnswer(stdout, stderr string, remote *workspace.Remote) string { var lines []string for _, l := range strings.Split(stdout, "\n") { if strings.HasPrefix(l, "!") { lines = append(lines, l) } } for _, l := range strings.Split(stderr, "\n") { if l = strings.TrimSpace(l); l != "" && !strings.HasPrefix(l, "hint:") { lines = append(lines, l) } } text := strings.Join(lines, "\n") if remote != nil && len(remote.GitPassword) >= 8 { text = strings.ReplaceAll(text, base64.StdEncoding.EncodeToString([]byte(remote.Username()+":"+remote.GitPassword)), "***") text = strings.ReplaceAll(text, remote.GitPassword, "***") } return runnerproto.RedactDetail(text, runnerproto.DetailLimit) } // capWriter keeps the first n bytes written and drops the rest. type capWriter struct { w io.Writer n int } func (c *capWriter) Write(p []byte) (int, error) { if k := min(len(p), c.n); k > 0 { if _, err := c.w.Write(p[:k]); err != nil { return 0, err } c.n -= k } return len(p), nil }