| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | // The refusals git's answer to a push can show. |
| 15 | const ( |
| 16 | // RefusalNonFastForward: the branch is not an ancestor of the commit |
| 17 | // ("[rejected] ... (non-fast-forward)", "fetch first", or the hub's |
| 18 | // own "[remote rejected] ... (non-fast-forward)"). |
| 19 | RefusalNonFastForward = "non-fast-forward" |
| 20 | // RefusalRefused: the hub refused the update itself: a hook, a |
| 21 | // protected branch ("[remote rejected] ...", "pre-receive hook |
| 22 | // declined"). |
| 23 | RefusalRefused = "refused" |
| 24 | ) |
| 25 | |
| 26 | // PushError is git's answer to a push, or to the read of the branch |
| 27 | // around it, that failed: what git was doing (Op: "push" or |
| 28 | // "ls-remote"), the refusal its answer shows (a push only; "" when it |
| 29 | // shows none) and its words (the rejected ref lines and stderr), |
| 30 | // redacted and bounded. It wraps the outcome (ErrPushUncertain, |
| 31 | // ErrPushConflict), which still decides the step's reason. |
| 32 | type PushError struct { |
| 33 | Op, Refusal, Answer string |
| 34 | Err error |
| 35 | } |
| 36 | |
| 37 | func (e *PushError) Error() string { |
| 38 | s := e.Err.Error() + ": git " + e.Op |
| 39 | if e.Refusal != "" { |
| 40 | s += " (" + e.Refusal + ")" |
| 41 | } |
| 42 | return s + ": " + e.Answer |
| 43 | } |
| 44 | |
| 45 | func (e *PushError) Unwrap() error { return e.Err } |
| 46 | |
| 47 | // PushRefusal is the refusal a failed push's answer shows, or "". |
| 48 | func PushRefusal(err error) string { |
| 49 | var p *PushError |
| 50 | if errors.As(err, &p) { |
| 51 | return p.Refusal |
| 52 | } |
| 53 | return "" |
| 54 | } |
| 55 | |
| 56 | // PushDetail is a failed push for the step report, a few lines: the |
| 57 | // refusal and git's answer when git gave one, else the error's own |
| 58 | // words; redacted and at most runnerproto.DetailLimit bytes either way. |
| 59 | func PushDetail(err error) string { |
| 60 | var p *PushError |
| 61 | switch { |
| 62 | case err == nil: |
| 63 | return "" |
| 64 | case !errors.As(err, &p): |
| 65 | return "error: " + runnerproto.RedactDetail(err.Error(), runnerproto.DetailLimit-len("error: ")) |
| 66 | } |
| 67 | out := "" |
| 68 | if p.Refusal != "" { |
| 69 | out = "refusal: " + p.Refusal + "\n" |
| 70 | } |
| 71 | out += p.Op + ": " |
| 72 | return out + runnerproto.RedactDetail(p.Answer, runnerproto.DetailLimit-len(out)) |
| 73 | } |
| 74 | |
| 75 | // gitAnswer is a git command that failed and what it said. |
| 76 | type gitAnswer struct { |
| 77 | err error |
| 78 | refusal, text string |
| 79 | } |
| 80 | |
| 81 | func (g *gitAnswer) Error() string { return g.text } |
| 82 | func (g *gitAnswer) Unwrap() error { return g.err } |
| 83 | |
| 84 | // pushError is outcome with git's answer when err carries one. |
| 85 | func pushError(op string, err, outcome error) error { |
| 86 | var g *gitAnswer |
| 87 | if !errors.As(err, &g) { |
| 88 | return outcome |
| 89 | } |
| 90 | p := &PushError{Op: op, Answer: g.text, Err: outcome} |
| 91 | if op == "push" { |
| 92 | p.Refusal = g.refusal |
| 93 | } |
| 94 | if p.Answer == "" { |
| 95 | p.Answer = runnerproto.RedactDetail(g.err.Error(), runnerproto.DetailLimit) |
| 96 | } |
| 97 | return p |
| 98 | } |
| 99 | |
| 100 | var ( |
| 101 | pushNonFastForward = regexp.MustCompile(`(?i)\[(?:remote )?rejected\][^\n]*\((?:non-fast-forward|fetch first)\)`) |
| 102 | pushRefused = regexp.MustCompile(`(?i)\[remote rejected\]|pre-receive hook declined|protected branch|\bis protected\b|\bGH006\b`) |
| 103 | ) |
| 104 | |
| 105 | // pushRefusalOf is the refusal git's output shows: a non-fast-forward |
| 106 | // before anything else, then the hub's own refusal; "" for neither (a |
| 107 | // stale lease, a transport failure). |
| 108 | func pushRefusalOf(out string) string { |
| 109 | switch { |
| 110 | case pushNonFastForward.MatchString(out): |
| 111 | return RefusalNonFastForward |
| 112 | case pushRefused.MatchString(out): |
| 113 | return RefusalRefused |
| 114 | } |
| 115 | return "" |
| 116 | } |
| 117 | |
| 118 | // pushAnswer is git's answer, safe to log and report: the rejected ref |
| 119 | // lines of a porcelain push, then stderr without git's hints; the git |
| 120 | // password gone (raw and as the Basic header git sends), then every |
| 121 | // other credential shape, one line, bounded. |
| 122 | func pushAnswer(stdout, stderr string, remote *workspace.Remote) string { |
| 123 | var lines []string |
| 124 | for _, l := range strings.Split(stdout, "\n") { |
| 125 | if strings.HasPrefix(l, "!") { |
| 126 | lines = append(lines, l) |
| 127 | } |
| 128 | } |
| 129 | for _, l := range strings.Split(stderr, "\n") { |
| 130 | if l = strings.TrimSpace(l); l != "" && !strings.HasPrefix(l, "hint:") { |
| 131 | lines = append(lines, l) |
| 132 | } |
| 133 | } |
| 134 | text := strings.Join(lines, "\n") |
| 135 | if remote != nil && len(remote.GitPassword) >= 8 { |
| 136 | text = strings.ReplaceAll(text, base64.StdEncoding.EncodeToString([]byte(remote.Username()+":"+remote.GitPassword)), "***") |
| 137 | text = strings.ReplaceAll(text, remote.GitPassword, "***") |
| 138 | } |
| 139 | return runnerproto.RedactDetail(text, runnerproto.DetailLimit) |
| 140 | } |
| 141 | |
| 142 | // capWriter keeps the first n bytes written and drops the rest. |
| 143 | type capWriter struct { |
| 144 | w io.Writer |
| 145 | n int |
| 146 | } |
| 147 | |
| 148 | func (c *capWriter) Write(p []byte) (int, error) { |
| 149 | if k := min(len(p), c.n); k > 0 { |
| 150 | if _, err := c.w.Write(p[:k]); err != nil { |
| 151 | return 0, err |
| 152 | } |
| 153 | c.n -= k |
| 154 | } |
| 155 | return len(p), nil |
| 156 | } |
| 157 | |