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