BlueGit

timfa bluegit

Files

bluegit / internal / server / editor.go

editor.go

editor.go 9.7 KiB Download Raw
1package server
2
3import (
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
24const ciFile = "blue-ci-cd.yml"
25const 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.
29const 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.
43type 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.
58type EditorLine struct {
59 N int
60 HTML template.HTML
61 Bad bool
62}
63
64// LintRow is één melding onder de editor.
65type LintRow struct {
66 Line int `json:"line"`
67 Text string `json:"text"`
68 Warning bool `json:"warning"`
69}
70
71func (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, repo)
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
103func (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
117func (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.
143func (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 if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch); sha != "" {
156 s.startOneMergePipeline(r.Context(), repo, sha, author.Username, "", mr)
157 }
158 s.mrHook(repo, mr, author.Username, "opened", "")
159 s.recordContribution(author.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
160 return mr.Number, nil
161}
162
163func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor {
164 _, issues := ci.Lint([]byte(body), read)
165 bad := map[int]bool{}
166 var rows []LintRow
167 errors, warnings := 0, 0
168 for _, is := range issues {
169 line := is.Line
170 if line < 1 {
171 line = 1
172 }
173 text := s.tr(r, is.Key, is.Args...)
174 rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning})
175 if is.Warning {
176 warnings++
177 continue
178 }
179 errors++
180 bad[line] = true
181 }
182 painted := highlight.Lines(ciFile, body)
183 if len(painted) == 0 {
184 painted = []string{""}
185 }
186 if strings.HasSuffix(body, "\n") {
187 painted = append(painted, "")
188 }
189 lines := make([]EditorLine, len(painted))
190 for i, htmlLine := range painted {
191 n := i + 1
192 lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]}
193 }
194 status, class := s.tr(r, "edit.clean"), "passed"
195 if errors > 0 {
196 key := "edit.issues"
197 if errors == 1 {
198 key = "edit.issueOne"
199 }
200 status, class = s.tr(r, key, errors), "failed"
201 } else if warnings > 0 {
202 key := "edit.warnings"
203 if warnings == 1 {
204 key = "edit.warningOne"
205 }
206 status, class = s.tr(r, key, warnings), "warning"
207 }
208 if rows == nil {
209 rows = []LintRow{}
210 }
211 return &CIEditor{
212 Body: body, Rows: lines, Issues: rows,
213 Errors: errors, Warnings: warnings, Status: status, Class: class,
214 }
215}
216
217func (s *Server) ciFileAt(r *http.Request, dir, ref string) (string, bool) {
218 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output()
219 if err != nil {
220 return "", true
221 }
222 if len(out) > ciMaxBytes {
223 return "", false
224 }
225 return string(out), false
226}
227
228func (s *Server) suggestCIBranch(r *http.Request, dir string, patterns []string) string {
229 const base = "add-blue-ci-cd"
230 for n := 1; n < 100; n++ {
231 name := base
232 if n > 1 {
233 name = base + "-" + strconv.Itoa(n)
234 }
235 if access.BranchProtected(patterns, name) {
236 continue
237 }
238 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) == "" {
239 return name
240 }
241 }
242 return base
243}
244
245func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) {
246 return func(path string) ([]byte, error) {
247 if !validInclude(path) {
248 return nil, os.ErrInvalid
249 }
250 return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
251 }
252}
253
254func (s *Server) ensureBranch(ctx context.Context, dir, branch, base, work string) error {
255 home := filepath.Join(work, "home")
256 if err := os.MkdirAll(home, 0o700); err != nil {
257 return gitrepo.Cause("edit.failWrite", err.Error())
258 }
259 env := gitrepo.CommandEnv("bluegit", "bluegit@users.git.timfalken.com", home, home)
260 if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" {
261 return nil
262 }
263 if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) != "" {
264 cmd := exec.CommandContext(ctx, s.Git, "-C", dir, "branch", branch, base)
265 cmd.Env = env
266 if out, err := cmd.CombinedOutput(); err != nil {
267 return gitrepo.Cause("edit.failBranch", strings.TrimSpace(string(out)))
268 }
269 return nil
270 }
271 // Nog geen enkele commit. De gekozen branch wordt de eerste, zonder parent.
272 // Een beheerder, ook de eigenaar, mag dat ook op een beschermde branch.
273 if s.gitEnv(ctx, dir, env, "rev-parse", "--verify", "--quiet", "HEAD") == "" {
274 return nil
275 }
276 return i18n.E("edit.failBranch")
277}
278
279func (s *Server) gitEnv(ctx context.Context, dir string, env []string, args ...string) string {
280 bin := s.Git
281 if bin == "" {
282 bin = "git"
283 }
284 cmd := exec.CommandContext(ctx, bin, append([]string{"-C", dir}, args...)...)
285 cmd.Env = env
286 out, err := cmd.Output()
287 if err != nil {
288 return ""
289 }
290 return strings.TrimSpace(string(out))
291}
292
293// scratchDir is een werkmap onder de state (/var/lib/bluegit/tmp of STATE_DIRECTORY).
294// /tmp en de homedirectory van de service zijn dat niet: ProtectSystem en ProtectHome sluiten die.
295func (s *Server) scratchDir(prefix string) (string, error) {
296 root := strings.TrimSpace(s.StateDir)
297 if root == "" {
298 root = strings.TrimSpace(os.Getenv("STATE_DIRECTORY"))
299 }
300 if root == "" {
301 root = strings.TrimSpace(s.StoragePath())
302 }
303 if root == "" {
304 return "", i18n.E("edit.failWrite")
305 }
306 dir := filepath.Join(root, "tmp")
307 if err := os.MkdirAll(dir, 0o750); err != nil {
308 return "", gitrepo.Cause("edit.failWrite", err.Error())
309 }
310 d, err := os.MkdirTemp(dir, prefix+"-")
311 if err != nil {
312 return "", gitrepo.Cause("edit.failWrite", err.Error())
313 }
314 return d, nil
315}
316
317// commitProblem logt de ruwe git-fout en geeft de vertaalde reden voor de pagina.
318func (s *Server) commitProblem(r *http.Request, err error) string {
319 if err != nil && s.Log != nil {
320 s.Log.Error("git-commit", "err", err)
321 }
322 var ie i18n.Error
323 if errors.As(err, &ie) {
324 return s.trErr(r, err)
325 }
326 return s.tr(r, "edit.failGit")
327}
328
329func validBranchName(s string) bool {
330 if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") {
331 return false
332 }
333 return !strings.ContainsAny(s, " \t\n~^:?*[\\")
334}
335
336func validInclude(p string) bool {
337 if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") {
338 return false
339 }
340 return true
341}