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