| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "html/template" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | |
| 16 | "gitlab.com/Timfa/bluegit/internal/access" |
| 17 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 18 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 19 | "gitlab.com/Timfa/bluegit/internal/highlight" |
| 20 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 21 | "gitlab.com/Timfa/bluegit/internal/store" |
| 22 | ) |
| 23 | |
| 24 | const ciFile = "blue-ci-cd.yml" |
| 25 | const ciMaxBytes = 200_000 |
| 26 | |
| 27 | // ciTemplate is het startbestand als blue-ci-cd.yml nog niet bestaat. |
| 28 | // Alles staat in commentaar, zodat opslaan geen job en geen mislukte pipeline maakt. |
| 29 | const ciTemplate = `# blue-ci-cd.yml |
| 30 | # Uncomment the job to run it. |
| 31 | # |
| 32 | # stages: |
| 33 | # - test |
| 34 | # |
| 35 | # unit: |
| 36 | # stage: test |
| 37 | # image: alpine:3.20 |
| 38 | # script: |
| 39 | # - echo hello |
| 40 | ` |
| 41 | |
| 42 | // CIEditor is de Pipeline Editor voor blue-ci-cd.yml. |
| 43 | type CIEditor struct { |
| 44 | Ref string |
| 45 | Body string |
| 46 | Rows []EditorLine |
| 47 | Issues []LintRow |
| 48 | Errors int |
| 49 | Warnings int |
| 50 | Status string |
| 51 | Class string |
| 52 | Missing bool |
| 53 | NeedsMR bool |
| 54 | Suggest string |
| 55 | } |
| 56 | |
| 57 | // EditorLine is één gekleurde regel, met een markering als de linter daar klaagt. |
| 58 | type EditorLine struct { |
| 59 | N int |
| 60 | HTML template.HTML |
| 61 | Bad bool |
| 62 | } |
| 63 | |
| 64 | // LintRow is één melding onder de editor. |
| 65 | type LintRow struct { |
| 66 | Line int `json:"line"` |
| 67 | Text string `json:"text"` |
| 68 | Warning bool `json:"warning"` |
| 69 | } |
| 70 | |
| 71 | func (s *Server) ciEditor(w http.ResponseWriter, r *http.Request) { |
| 72 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 73 | if !ok { |
| 74 | return |
| 75 | } |
| 76 | ref := strings.TrimSpace(r.URL.Query().Get("ref")) |
| 77 | if ref == "" { |
| 78 | ref = r.URL.Query().Get("rev") |
| 79 | } |
| 80 | ref = safeRev(ref, repo.DefaultBranch) |
| 81 | v := s.repoView(w, r, repo, role, "edit.title") |
| 82 | v.Wide = true |
| 83 | v.Page = "editor" |
| 84 | v.Rev = ref |
| 85 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 86 | s.branchList(r, &v, dir) |
| 87 | body, missing := s.ciFileAt(r, dir, ref) |
| 88 | if missing { |
| 89 | body = ciTemplate |
| 90 | } |
| 91 | model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref)) |
| 92 | model.Ref = ref |
| 93 | model.Missing = missing |
| 94 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 95 | model.NeedsMR = access.BranchProtected(patterns, ref) && !role.CanPushBranch(true) |
| 96 | if model.NeedsMR { |
| 97 | model.Suggest = s.suggestCIBranch(r, dir, patterns) |
| 98 | } |
| 99 | v.Editor = model |
| 100 | s.render(w, "editor", v) |
| 101 | } |
| 102 | |
| 103 | func (s *Server) lintCI(w http.ResponseWriter, r *http.Request) { |
| 104 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 105 | if !ok || !s.csrfOK(w, r) { |
| 106 | return |
| 107 | } |
| 108 | out, err := s.opLintCI(r, repo, r.FormValue("body"), r.FormValue("ref")) |
| 109 | if err != nil { |
| 110 | http.Error(w, s.trErr(r, err), statusOf(err)) |
| 111 | return |
| 112 | } |
| 113 | w.Header().Set("Content-Type", "application/json") |
| 114 | _ = json.NewEncoder(w).Encode(out) |
| 115 | } |
| 116 | |
| 117 | func (s *Server) saveCIEditor(w http.ResponseWriter, r *http.Request) { |
| 118 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 119 | if !ok || !s.csrfOK(w, r) { |
| 120 | return |
| 121 | } |
| 122 | branch := strings.TrimSpace(r.FormValue("branch")) |
| 123 | base := r.FormValue("base") |
| 124 | back := repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(branch) |
| 125 | openMR := r.FormValue("open_mr") == "1" |
| 126 | _, number, err := s.opSaveCI(r.Context(), u, repo, base, branch, r.FormValue("message"), r.FormValue("body"), openMR) |
| 127 | if err != nil { |
| 128 | if strings.TrimSpace(branch) == "" || !validBranchName(branch) { |
| 129 | back = repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(safeRev(base, repo.DefaultBranch)) |
| 130 | } |
| 131 | s.redirect(w, r, back, "", s.commitProblem(r, err)) |
| 132 | return |
| 133 | } |
| 134 | if openMR && number > 0 { |
| 135 | s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(number, 10), s.tr(r, "flash.mrOpened"), "") |
| 136 | return |
| 137 | } |
| 138 | s.redirect(w, r, back, s.tr(r, "edit.saved"), "") |
| 139 | } |
| 140 | |
| 141 | // openCIMergeRequest opent een merge request van branch naar target. |
| 142 | // Een open verzoek met dezelfde bronbranch wordt hergebruikt. |
| 143 | func (s *Server) openCIMergeRequest(r *http.Request, repo store.Repo, author store.User, dir, branch, target, title string) (int64, error) { |
| 144 | existing, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, branch) |
| 145 | if err != nil { |
| 146 | return 0, err |
| 147 | } |
| 148 | if len(existing) > 0 { |
| 149 | return existing[0].Number, nil |
| 150 | } |
| 151 | mr, err := s.Store.CreateMergeRequest(r.Context(), repo.ID, author.ID, title, "", branch, target) |
| 152 | if err != nil { |
| 153 | return 0, err |
| 154 | } |
| 155 | num := strconv.FormatInt(mr.Number, 10) |
| 156 | if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch); sha != "" { |
| 157 | s.startOneMergePipeline(r.Context(), repo, sha, author.Username, "", mr) |
| 158 | } |
| 159 | s.Dispatch(repo.ID, pushEvent(repo, "merge_request", target, "", author.Username, s.tr(r, "hook.mr", num, mr.Title), repoURL(repo)+"/-/merge-requests/"+num)) |
| 160 | s.recordContribution(author.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt) |
| 161 | return mr.Number, nil |
| 162 | } |
| 163 | |
| 164 | func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor { |
| 165 | _, issues := ci.Lint([]byte(body), read) |
| 166 | bad := map[int]bool{} |
| 167 | var rows []LintRow |
| 168 | errors, warnings := 0, 0 |
| 169 | for _, is := range issues { |
| 170 | line := is.Line |
| 171 | if line < 1 { |
| 172 | line = 1 |
| 173 | } |
| 174 | text := s.tr(r, is.Key, is.Args...) |
| 175 | rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning}) |
| 176 | if is.Warning { |
| 177 | warnings++ |
| 178 | continue |
| 179 | } |
| 180 | errors++ |
| 181 | bad[line] = true |
| 182 | } |
| 183 | painted := highlight.Lines(ciFile, body) |
| 184 | if len(painted) == 0 { |
| 185 | painted = []string{""} |
| 186 | } |
| 187 | if strings.HasSuffix(body, "\n") { |
| 188 | painted = append(painted, "") |
| 189 | } |
| 190 | lines := make([]EditorLine, len(painted)) |
| 191 | for i, htmlLine := range painted { |
| 192 | n := i + 1 |
| 193 | lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]} |
| 194 | } |
| 195 | status, class := s.tr(r, "edit.clean"), "passed" |
| 196 | if errors > 0 { |
| 197 | key := "edit.issues" |
| 198 | if errors == 1 { |
| 199 | key = "edit.issueOne" |
| 200 | } |
| 201 | status, class = s.tr(r, key, errors), "failed" |
| 202 | } else if warnings > 0 { |
| 203 | key := "edit.warnings" |
| 204 | if warnings == 1 { |
| 205 | key = "edit.warningOne" |
| 206 | } |
| 207 | status, class = s.tr(r, key, warnings), "warning" |
| 208 | } |
| 209 | if rows == nil { |
| 210 | rows = []LintRow{} |
| 211 | } |
| 212 | return &CIEditor{ |
| 213 | Body: body, Rows: lines, Issues: rows, |
| 214 | Errors: errors, Warnings: warnings, Status: status, Class: class, |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func (s *Server) ciFileAt(r *http.Request, dir, ref string) (string, bool) { |
| 219 | out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output() |
| 220 | if err != nil { |
| 221 | return "", true |
| 222 | } |
| 223 | if len(out) > ciMaxBytes { |
| 224 | return "", false |
| 225 | } |
| 226 | return string(out), false |
| 227 | } |
| 228 | |
| 229 | func (s *Server) suggestCIBranch(r *http.Request, dir string, patterns []string) string { |
| 230 | const base = "add-blue-ci-cd" |
| 231 | for n := 1; n < 100; n++ { |
| 232 | name := base |
| 233 | if n > 1 { |
| 234 | name = base + "-" + strconv.Itoa(n) |
| 235 | } |
| 236 | if access.BranchProtected(patterns, name) { |
| 237 | continue |
| 238 | } |
| 239 | if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) == "" { |
| 240 | return name |
| 241 | } |
| 242 | } |
| 243 | return base |
| 244 | } |
| 245 | |
| 246 | func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) { |
| 247 | return func(path string) ([]byte, error) { |
| 248 | if !validInclude(path) { |
| 249 | return nil, os.ErrInvalid |
| 250 | } |
| 251 | return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output() |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func (s *Server) ensureBranch(ctx context.Context, dir, branch, base, work string) error { |
| 256 | home := filepath.Join(work, "home") |
| 257 | if err := os.MkdirAll(home, 0o700); err != nil { |
| 258 | return gitrepo.Cause("edit.failWrite", err.Error()) |
| 259 | } |
| 260 | env := gitrepo.CommandEnv("bluegit", "bluegit@users.git.timfalken.com", home, home) |
| 261 | if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" { |
| 262 | return nil |
| 263 | } |
| 264 | if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) != "" { |
| 265 | cmd := exec.CommandContext(ctx, s.Git, "-C", dir, "branch", branch, base) |
| 266 | cmd.Env = env |
| 267 | if out, err := cmd.CombinedOutput(); err != nil { |
| 268 | return gitrepo.Cause("edit.failBranch", strings.TrimSpace(string(out))) |
| 269 | } |
| 270 | return nil |
| 271 | } |
| 272 | // Nog geen enkele commit. De gekozen branch wordt de eerste, zonder parent. |
| 273 | // Een beheerder, ook de eigenaar, mag dat ook op een beschermde branch. |
| 274 | if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "HEAD") == "" { |
| 275 | return nil |
| 276 | } |
| 277 | return i18n.E("edit.failBranch") |
| 278 | } |
| 279 | |
| 280 | func (s *Server) gitEnv(ctx context.Context, dir string, env []string, args ...string) string { |
| 281 | bin := s.Git |
| 282 | if bin == "" { |
| 283 | bin = "git" |
| 284 | } |
| 285 | cmd := exec.CommandContext(ctx, bin, append([]string{"-C", dir}, args...)...) |
| 286 | cmd.Env = env |
| 287 | out, err := cmd.Output() |
| 288 | if err != nil { |
| 289 | return "" |
| 290 | } |
| 291 | return strings.TrimSpace(string(out)) |
| 292 | } |
| 293 | |
| 294 | // scratchDir is een werkmap onder de state (/var/lib/bluegit/tmp of STATE_DIRECTORY). |
| 295 | // /tmp en de homedirectory van de service zijn dat niet: ProtectSystem en ProtectHome sluiten die. |
| 296 | func (s *Server) scratchDir(prefix string) (string, error) { |
| 297 | root := strings.TrimSpace(s.StateDir) |
| 298 | if root == "" { |
| 299 | root = strings.TrimSpace(os.Getenv("STATE_DIRECTORY")) |
| 300 | } |
| 301 | if root == "" { |
| 302 | root = strings.TrimSpace(s.StoragePath()) |
| 303 | } |
| 304 | if root == "" { |
| 305 | return "", i18n.E("edit.failWrite") |
| 306 | } |
| 307 | dir := filepath.Join(root, "tmp") |
| 308 | if err := os.MkdirAll(dir, 0o750); err != nil { |
| 309 | return "", gitrepo.Cause("edit.failWrite", err.Error()) |
| 310 | } |
| 311 | d, err := os.MkdirTemp(dir, prefix+"-") |
| 312 | if err != nil { |
| 313 | return "", gitrepo.Cause("edit.failWrite", err.Error()) |
| 314 | } |
| 315 | return d, nil |
| 316 | } |
| 317 | |
| 318 | // commitProblem logt de ruwe git-fout en geeft de vertaalde reden voor de pagina. |
| 319 | func (s *Server) commitProblem(r *http.Request, err error) string { |
| 320 | if err != nil && s.Log != nil { |
| 321 | s.Log.Error("git-commit", "err", err) |
| 322 | } |
| 323 | var ie i18n.Error |
| 324 | if errors.As(err, &ie) { |
| 325 | return s.trErr(r, err) |
| 326 | } |
| 327 | return s.tr(r, "edit.failGit") |
| 328 | } |
| 329 | |
| 330 | func validBranchName(s string) bool { |
| 331 | if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") { |
| 332 | return false |
| 333 | } |
| 334 | return !strings.ContainsAny(s, " \t\n~^:?*[\\") |
| 335 | } |
| 336 | |
| 337 | func validInclude(p string) bool { |
| 338 | if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") { |
| 339 | return false |
| 340 | } |
| 341 | return true |
| 342 | } |