| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "strings" |
| 11 | |
| 12 | "gitlab.com/Timfa/bluegit/internal/access" |
| 13 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 14 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 15 | "gitlab.com/Timfa/bluegit/internal/highlight" |
| 16 | ) |
| 17 | |
| 18 | const ciFile = "blue-ci-cd.yml" |
| 19 | const ciMaxBytes = 200_000 |
| 20 | |
| 21 | // CIEditor is de Pipeline Editor voor blue-ci-cd.yml. |
| 22 | type CIEditor struct { |
| 23 | Ref string |
| 24 | Body string |
| 25 | Rows []EditorLine |
| 26 | Issues []LintRow |
| 27 | Errors int |
| 28 | Warnings int |
| 29 | Status string |
| 30 | Class string |
| 31 | } |
| 32 | |
| 33 | // EditorLine is één gekleurde regel, met een markering als de linter daar klaagt. |
| 34 | type EditorLine struct { |
| 35 | N int |
| 36 | HTML template.HTML |
| 37 | Bad bool |
| 38 | } |
| 39 | |
| 40 | // LintRow is één melding onder de editor. |
| 41 | type LintRow struct { |
| 42 | Line int `json:"line"` |
| 43 | Text string `json:"text"` |
| 44 | Warning bool `json:"warning"` |
| 45 | } |
| 46 | |
| 47 | func (s *Server) ciEditor(w http.ResponseWriter, r *http.Request) { |
| 48 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 49 | if !ok { |
| 50 | return |
| 51 | } |
| 52 | ref := strings.TrimSpace(r.URL.Query().Get("ref")) |
| 53 | if ref == "" { |
| 54 | ref = r.URL.Query().Get("rev") |
| 55 | } |
| 56 | ref = safeRev(ref, repo.DefaultBranch) |
| 57 | v := s.repoView(w, r, repo, role, "edit.title") |
| 58 | v.Wide = true |
| 59 | v.Page = "editor" |
| 60 | v.Rev = ref |
| 61 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 62 | s.branchList(r, &v, dir) |
| 63 | body := s.ciFileAt(r, dir, ref) |
| 64 | model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref)) |
| 65 | model.Ref = ref |
| 66 | v.Editor = model |
| 67 | s.render(w, "editor", v) |
| 68 | } |
| 69 | |
| 70 | func (s *Server) lintCI(w http.ResponseWriter, r *http.Request) { |
| 71 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 72 | if !ok || !s.csrfOK(w, r) { |
| 73 | return |
| 74 | } |
| 75 | body := r.FormValue("body") |
| 76 | if len(body) > ciMaxBytes { |
| 77 | http.Error(w, s.tr(r, "edit.fail"), http.StatusBadRequest) |
| 78 | return |
| 79 | } |
| 80 | ref := safeRev(r.FormValue("ref"), repo.DefaultBranch) |
| 81 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 82 | model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref)) |
| 83 | lines := make([]string, len(model.Rows)) |
| 84 | var bad []int |
| 85 | for i, row := range model.Rows { |
| 86 | lines[i] = string(row.HTML) |
| 87 | if row.Bad { |
| 88 | bad = append(bad, row.N) |
| 89 | } |
| 90 | } |
| 91 | w.Header().Set("Content-Type", "application/json") |
| 92 | _ = json.NewEncoder(w).Encode(map[string]any{ |
| 93 | "status": model.Status, |
| 94 | "statusClass": model.Class, |
| 95 | "errors": model.Errors, |
| 96 | "warnings": model.Warnings, |
| 97 | "issues": model.Issues, |
| 98 | "lines": lines, |
| 99 | "bad": bad, |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | func (s *Server) saveCIEditor(w http.ResponseWriter, r *http.Request) { |
| 104 | u, repo, role, ok := s.guard(w, r, access.RoleDeveloper) |
| 105 | if !ok || !s.csrfOK(w, r) { |
| 106 | return |
| 107 | } |
| 108 | base := safeRev(r.FormValue("base"), repo.DefaultBranch) |
| 109 | branch := strings.TrimSpace(r.FormValue("branch")) |
| 110 | if !validBranchName(branch) { |
| 111 | s.redirect(w, r, repoURL(repo)+"/-/ci/editor?ref="+url.QueryEscape(base), "", s.tr(r, "edit.badBranch")) |
| 112 | return |
| 113 | } |
| 114 | back := repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(branch) |
| 115 | message := strings.TrimSpace(r.FormValue("message")) |
| 116 | if message == "" { |
| 117 | s.redirect(w, r, back, "", s.tr(r, "edit.needMessage")) |
| 118 | return |
| 119 | } |
| 120 | body := r.FormValue("body") |
| 121 | if len(body) > ciMaxBytes { |
| 122 | s.redirect(w, r, back, "", s.tr(r, "edit.fail")) |
| 123 | return |
| 124 | } |
| 125 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 126 | if access.BranchProtected(patterns, branch) && !role.CanPushBranch(true) { |
| 127 | s.redirect(w, r, back, "", s.tr(r, "edit.protected")) |
| 128 | return |
| 129 | } |
| 130 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 131 | if err := s.ensureBranch(r, dir, branch, base); err != nil { |
| 132 | s.redirect(w, r, back, "", s.tr(r, "edit.fail")) |
| 133 | return |
| 134 | } |
| 135 | work, err := os.MkdirTemp("", "bluegit-edit-") |
| 136 | if err != nil { |
| 137 | s.redirect(w, r, back, "", s.tr(r, "edit.fail")) |
| 138 | return |
| 139 | } |
| 140 | defer os.RemoveAll(work) |
| 141 | email := u.Username + "@users.git.timfalken.com" |
| 142 | err = gitrepo.CommitOnBranch(r.Context(), s.Git, dir, work, branch, u.Username, email, message, "", map[string]string{ciFile: body}, role, patterns) |
| 143 | if err != nil { |
| 144 | low := strings.ToLower(err.Error()) |
| 145 | if strings.Contains(low, "nothing to commit") || strings.Contains(low, "nothing added") { |
| 146 | s.redirect(w, r, back, "", s.tr(r, "edit.same")) |
| 147 | return |
| 148 | } |
| 149 | if s.Log != nil { |
| 150 | s.Log.Error("ci-editor", "err", err) |
| 151 | } |
| 152 | s.redirect(w, r, back, "", s.tr(r, "edit.fail")) |
| 153 | return |
| 154 | } |
| 155 | s.redirect(w, r, back, s.tr(r, "edit.saved"), "") |
| 156 | } |
| 157 | |
| 158 | func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor { |
| 159 | _, issues := ci.Lint([]byte(body), read) |
| 160 | bad := map[int]bool{} |
| 161 | var rows []LintRow |
| 162 | errors, warnings := 0, 0 |
| 163 | for _, is := range issues { |
| 164 | line := is.Line |
| 165 | if line < 1 { |
| 166 | line = 1 |
| 167 | } |
| 168 | text := s.tr(r, is.Key, is.Args...) |
| 169 | rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning}) |
| 170 | if is.Warning { |
| 171 | warnings++ |
| 172 | continue |
| 173 | } |
| 174 | errors++ |
| 175 | bad[line] = true |
| 176 | } |
| 177 | painted := highlight.Lines(ciFile, body) |
| 178 | if len(painted) == 0 { |
| 179 | painted = []string{""} |
| 180 | } |
| 181 | if strings.HasSuffix(body, "\n") { |
| 182 | painted = append(painted, "") |
| 183 | } |
| 184 | lines := make([]EditorLine, len(painted)) |
| 185 | for i, htmlLine := range painted { |
| 186 | n := i + 1 |
| 187 | lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]} |
| 188 | } |
| 189 | status, class := s.tr(r, "edit.clean"), "passed" |
| 190 | if errors > 0 { |
| 191 | key := "edit.issues" |
| 192 | if errors == 1 { |
| 193 | key = "edit.issueOne" |
| 194 | } |
| 195 | status, class = s.tr(r, key, errors), "failed" |
| 196 | } else if warnings > 0 { |
| 197 | key := "edit.warnings" |
| 198 | if warnings == 1 { |
| 199 | key = "edit.warningOne" |
| 200 | } |
| 201 | status, class = s.tr(r, key, warnings), "warning" |
| 202 | } |
| 203 | if rows == nil { |
| 204 | rows = []LintRow{} |
| 205 | } |
| 206 | return &CIEditor{ |
| 207 | Body: body, Rows: lines, Issues: rows, |
| 208 | Errors: errors, Warnings: warnings, Status: status, Class: class, |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func (s *Server) ciFileAt(r *http.Request, dir, ref string) string { |
| 213 | out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output() |
| 214 | if err != nil || len(out) > ciMaxBytes { |
| 215 | return "" |
| 216 | } |
| 217 | return string(out) |
| 218 | } |
| 219 | |
| 220 | func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) { |
| 221 | return func(path string) ([]byte, error) { |
| 222 | if !validInclude(path) { |
| 223 | return nil, os.ErrInvalid |
| 224 | } |
| 225 | return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output() |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func (s *Server) ensureBranch(r *http.Request, dir, branch, base string) error { |
| 230 | if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" { |
| 231 | return nil |
| 232 | } |
| 233 | if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) == "" { |
| 234 | return os.ErrNotExist |
| 235 | } |
| 236 | cmd := exec.CommandContext(r.Context(), s.Git, "-C", dir, "branch", branch, base) |
| 237 | if out, err := cmd.CombinedOutput(); err != nil { |
| 238 | if s.Log != nil { |
| 239 | s.Log.Error("ci-branch", "err", err, "out", string(out)) |
| 240 | } |
| 241 | return err |
| 242 | } |
| 243 | return nil |
| 244 | } |
| 245 | |
| 246 | func validBranchName(s string) bool { |
| 247 | if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") { |
| 248 | return false |
| 249 | } |
| 250 | return !strings.ContainsAny(s, " \t\n~^:?*[\\") |
| 251 | } |
| 252 | |
| 253 | func validInclude(p string) bool { |
| 254 | if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") { |
| 255 | return false |
| 256 | } |
| 257 | return true |
| 258 | } |