BlueGit

timfa bluegit

Files

bluegit / internal / server / editor.go

editor.go

fix: start een pipeline na commits vanuit de UI
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
f576a3e4 History
editor.go 11.7 KiB Download Raw
1package server
2
3import (
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
23const ciFile = "blue-ci-cd.yml"
24const 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.
28const 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.
42type 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.
57type EditorLine struct {
58 N int
59 HTML template.HTML
60 Bad bool
61}
62
63// LintRow is één melding onder de editor.
64type LintRow struct {
65 Line int `json:"line"`
66 Text string `json:"text"`
67 Warning bool `json:"warning"`
68}
69
70func (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
102func (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
135func (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 = s.postReceive(r.Context(), repo, u, dir, func() error {
180 return gitrepo.CommitOnBranch(r.Context(), s.Git, dir, work, branch, u.Username, email, message, "", map[string]string{ciFile: body}, role, patterns)
181 })
182 if err != nil {
183 s.redirect(w, r, back, "", s.commitProblem(r, err))
184 return
185 }
186 if openMR {
187 number, mrErr := s.openCIMergeRequest(r, repo, u, dir, branch, base, message)
188 if mrErr != nil {
189 if s.Log != nil {
190 s.Log.Error("ci-editor-mr", "err", mrErr)
191 }
192 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
193 return
194 }
195 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(number, 10), s.tr(r, "flash.mrOpened"), "")
196 return
197 }
198 s.redirect(w, r, back, s.tr(r, "edit.saved"), "")
199}
200
201// openCIMergeRequest opent een merge request van branch naar target.
202// Een open verzoek met dezelfde bronbranch wordt hergebruikt.
203func (s *Server) openCIMergeRequest(r *http.Request, repo store.Repo, author store.User, dir, branch, target, title string) (int64, error) {
204 existing, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, branch)
205 if err != nil {
206 return 0, err
207 }
208 if len(existing) > 0 {
209 return existing[0].Number, nil
210 }
211 mr, err := s.Store.CreateMergeRequest(r.Context(), repo.ID, author.ID, title, "", branch, target)
212 if err != nil {
213 return 0, err
214 }
215 num := strconv.FormatInt(mr.Number, 10)
216 if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch); sha != "" {
217 s.startOneMergePipeline(r.Context(), repo, sha, author.Username, "", mr)
218 }
219 s.Dispatch(repo.ID, pushEvent(repo, "merge_request", target, "", author.Username, s.tr(r, "hook.mr", num, mr.Title), repoURL(repo)+"/-/merge-requests/"+num))
220 s.recordContribution(author.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
221 return mr.Number, nil
222}
223
224func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor {
225 _, issues := ci.Lint([]byte(body), read)
226 bad := map[int]bool{}
227 var rows []LintRow
228 errors, warnings := 0, 0
229 for _, is := range issues {
230 line := is.Line
231 if line < 1 {
232 line = 1
233 }
234 text := s.tr(r, is.Key, is.Args...)
235 rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning})
236 if is.Warning {
237 warnings++
238 continue
239 }
240 errors++
241 bad[line] = true
242 }
243 painted := highlight.Lines(ciFile, body)
244 if len(painted) == 0 {
245 painted = []string{""}
246 }
247 if strings.HasSuffix(body, "\n") {
248 painted = append(painted, "")
249 }
250 lines := make([]EditorLine, len(painted))
251 for i, htmlLine := range painted {
252 n := i + 1
253 lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]}
254 }
255 status, class := s.tr(r, "edit.clean"), "passed"
256 if errors > 0 {
257 key := "edit.issues"
258 if errors == 1 {
259 key = "edit.issueOne"
260 }
261 status, class = s.tr(r, key, errors), "failed"
262 } else if warnings > 0 {
263 key := "edit.warnings"
264 if warnings == 1 {
265 key = "edit.warningOne"
266 }
267 status, class = s.tr(r, key, warnings), "warning"
268 }
269 if rows == nil {
270 rows = []LintRow{}
271 }
272 return &CIEditor{
273 Body: body, Rows: lines, Issues: rows,
274 Errors: errors, Warnings: warnings, Status: status, Class: class,
275 }
276}
277
278func (s *Server) ciFileAt(r *http.Request, dir, ref string) (string, bool) {
279 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output()
280 if err != nil {
281 return "", true
282 }
283 if len(out) > ciMaxBytes {
284 return "", false
285 }
286 return string(out), false
287}
288
289func (s *Server) suggestCIBranch(r *http.Request, dir string, patterns []string) string {
290 const base = "add-blue-ci-cd"
291 for n := 1; n < 100; n++ {
292 name := base
293 if n > 1 {
294 name = base + "-" + strconv.Itoa(n)
295 }
296 if access.BranchProtected(patterns, name) {
297 continue
298 }
299 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) == "" {
300 return name
301 }
302 }
303 return base
304}
305
306func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) {
307 return func(path string) ([]byte, error) {
308 if !validInclude(path) {
309 return nil, os.ErrInvalid
310 }
311 return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
312 }
313}
314
315func (s *Server) ensureBranch(r *http.Request, dir, branch, base, work string) error {
316 home := filepath.Join(work, "home")
317 if err := os.MkdirAll(home, 0o700); err != nil {
318 return gitrepo.Cause("edit.failWrite", err.Error())
319 }
320 env := gitrepo.CommandEnv("bluegit", "bluegit@users.git.timfalken.com", home, home)
321 if s.gitEnv(r, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" {
322 return nil
323 }
324 if s.gitEnv(r, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) != "" {
325 cmd := exec.CommandContext(r.Context(), s.Git, "-C", dir, "branch", branch, base)
326 cmd.Env = env
327 if out, err := cmd.CombinedOutput(); err != nil {
328 return gitrepo.Cause("edit.failBranch", strings.TrimSpace(string(out)))
329 }
330 return nil
331 }
332 // Nog geen enkele commit. De gekozen branch wordt de eerste, zonder parent.
333 // Een beheerder, ook de eigenaar, mag dat ook op een beschermde branch.
334 if s.gitEnv(r, dir, env, "rev-parse", "--verify", "--quiet", "HEAD") == "" {
335 return nil
336 }
337 return i18n.E("edit.failBranch")
338}
339
340func (s *Server) gitEnv(r *http.Request, dir string, env []string, args ...string) string {
341 bin := s.Git
342 if bin == "" {
343 bin = "git"
344 }
345 cmd := exec.CommandContext(r.Context(), bin, append([]string{"-C", dir}, args...)...)
346 cmd.Env = env
347 out, err := cmd.Output()
348 if err != nil {
349 return ""
350 }
351 return strings.TrimSpace(string(out))
352}
353
354// scratchDir is een werkmap onder de state (/var/lib/bluegit/tmp of STATE_DIRECTORY).
355// /tmp en de homedirectory van de service zijn dat niet: ProtectSystem en ProtectHome sluiten die.
356func (s *Server) scratchDir(prefix string) (string, error) {
357 root := strings.TrimSpace(s.StateDir)
358 if root == "" {
359 root = strings.TrimSpace(os.Getenv("STATE_DIRECTORY"))
360 }
361 if root == "" {
362 root = strings.TrimSpace(s.StoragePath())
363 }
364 if root == "" {
365 return "", i18n.E("edit.failWrite")
366 }
367 dir := filepath.Join(root, "tmp")
368 if err := os.MkdirAll(dir, 0o750); err != nil {
369 return "", gitrepo.Cause("edit.failWrite", err.Error())
370 }
371 d, err := os.MkdirTemp(dir, prefix+"-")
372 if err != nil {
373 return "", gitrepo.Cause("edit.failWrite", err.Error())
374 }
375 return d, nil
376}
377
378// commitProblem logt de ruwe git-fout en geeft de vertaalde reden voor de pagina.
379func (s *Server) commitProblem(r *http.Request, err error) string {
380 if err != nil && s.Log != nil {
381 s.Log.Error("git-commit", "err", err)
382 }
383 var ie i18n.Error
384 if errors.As(err, &ie) {
385 return s.trErr(r, err)
386 }
387 return s.tr(r, "edit.failGit")
388}
389
390func validBranchName(s string) bool {
391 if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") {
392 return false
393 }
394 return !strings.ContainsAny(s, " \t\n~^:?*[\\")
395}
396
397func validInclude(p string) bool {
398 if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") {
399 return false
400 }
401 return true
402}