BlueGit

timfa bluegit

Files

bluegit / internal / server / suggest.go

suggest.go

feat: suggesties op merge requests
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 3902d232 History
suggest.go 4.5 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "os"
6 "strconv"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/access"
10 "gitlab.com/Timfa/bluegit/internal/gitrepo"
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13)
14
15func arrangeNotes(notes []store.Note, files []gitrepo.DiffFile, lang i18n.Lang) ([]NoteRow, []DiffFileView) {
16 rows := make([]NoteRow, len(notes))
17 for i, n := range notes {
18 rows[i] = NoteRow{
19 ID: n.ID, Author: n.Author, Body: n.Body, Path: n.FilePath,
20 Start: n.LineStart, End: n.LineEnd, HasSuggestion: n.Suggestion != "",
21 When: i18n.FormatDate(lang, n.CreatedAt),
22 }
23 }
24 placed := map[int64]bool{}
25 var views []DiffFileView
26 for _, f := range files {
27 view := DiffFileView{Path: f.Path}
28 for _, ln := range f.Lines {
29 row := DiffLineView{Kind: ln.Kind, Text: ln.Text, Old: ln.Old, New: ln.New}
30 if ln.New > 0 {
31 for _, note := range rows {
32 if note.Path == f.Path && note.End == ln.New {
33 row.Notes = append(row.Notes, note)
34 placed[note.ID] = true
35 }
36 }
37 }
38 view.Lines = append(view.Lines, row)
39 }
40 views = append(views, view)
41 }
42 var general []NoteRow
43 for _, note := range rows {
44 if note.Path == "" || !placed[note.ID] {
45 general = append(general, note)
46 }
47 }
48 return general, views
49}
50
51func repoPath(raw string) (string, bool) {
52 raw = strings.TrimSpace(raw)
53 if raw == "" {
54 return "", true
55 }
56 p := strings.ReplaceAll(raw, `\`, "/")
57 if strings.HasPrefix(p, "/") || strings.Contains(p, "..") || strings.ContainsAny(p, "\r\n") {
58 return "", false
59 }
60 return p, true
61}
62
63func (s *Server) applySuggestions(w http.ResponseWriter, r *http.Request) {
64 u, repo, role, ok := s.guard(w, r, access.RoleDeveloper)
65 if !ok || !s.csrfOK(w, r) {
66 return
67 }
68 mr, err := s.mrFromRequest(r, repo.ID)
69 if err != nil || mr.Status != "open" {
70 http.NotFound(w, r)
71 return
72 }
73 back := repoURL(repo) + "/-/merge-requests/" + strconv.FormatInt(mr.Number, 10)
74 patterns, err := s.Store.ProtectedPatterns(r.Context(), repo.ID)
75 if err != nil {
76 s.redirect(w, r, back, "", s.trErr(r, err))
77 return
78 }
79 if !role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch)) {
80 s.redirect(w, r, back, "", s.tr(r, "err.sugBranch", mr.SourceBranch))
81 return
82 }
83 if s.StoragePath() == "" {
84 s.redirect(w, r, back, "", s.tr(r, "err.noStorage"))
85 return
86 }
87 title := strings.TrimSpace(r.FormValue("title"))
88 if title == "" || strings.ContainsAny(title, "\r\n") {
89 s.redirect(w, r, back, "", s.tr(r, "err.sugTitle"))
90 return
91 }
92 notes, err := s.Store.ListNotes(r.Context(), mr.ID)
93 if err != nil {
94 s.redirect(w, r, back, "", s.trErr(r, err))
95 return
96 }
97 byID := map[int64]store.Note{}
98 for _, n := range notes {
99 byID[n.ID] = n
100 }
101 var edits []gitrepo.SuggestionEdit
102 seen := map[int64]bool{}
103 for _, raw := range r.Form["id"] {
104 id, err := strconv.ParseInt(raw, 10, 64)
105 if err != nil || seen[id] {
106 continue
107 }
108 seen[id] = true
109 n, ok := byID[id]
110 if !ok || n.Suggestion == "" || n.FilePath == "" {
111 s.redirect(w, r, back, "", s.tr(r, "err.sugGone"))
112 return
113 }
114 edits = append(edits, gitrepo.SuggestionEdit{
115 Path: n.FilePath, Start: n.LineStart, End: n.LineEnd,
116 Base: n.BaseText, Replacement: n.Suggestion,
117 })
118 }
119 if len(edits) == 0 {
120 s.redirect(w, r, back, "", s.tr(r, "err.sugEmpty"))
121 return
122 }
123 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
124 files := map[string]string{}
125 for _, e := range edits {
126 if _, ok := files[e.Path]; ok {
127 continue
128 }
129 body, err := gitrepo.BlobText(r.Context(), s.Git, dir, mr.SourceBranch, e.Path)
130 if err != nil {
131 s.redirect(w, r, back, "", s.tr(r, "err.sugConflict", e.Path, e.Start, e.End))
132 return
133 }
134 files[e.Path] = body
135 }
136 updated, err := gitrepo.ApplySuggestions(files, edits)
137 if err != nil {
138 s.redirect(w, r, back, "", s.trErr(r, err))
139 return
140 }
141 branch := safeRev(mr.SourceBranch, "")
142 if branch == "" {
143 s.redirect(w, r, back, "", s.tr(r, "err.sugBranch", mr.SourceBranch))
144 return
145 }
146 before, _ := gitrepo.Refs(r.Context(), s.Git, dir)
147 work := s.StoragePath() + "/tmp/suggest-" + strconv.FormatInt(mr.ID, 10)
148 email := u.Username + "@users.git.timfalken.com"
149 if err := gitrepo.CommitOnBranch(r.Context(), s.Git, dir, work, branch, u.Username, email, title, r.FormValue("description"), updated, role, patterns); err != nil {
150 _ = os.RemoveAll(work)
151 s.redirect(w, r, back, "", s.trErr(r, err))
152 return
153 }
154 _ = os.RemoveAll(work)
155 after, _ := gitrepo.Refs(r.Context(), s.Git, dir)
156 s.AfterPush(repo, u, before, after)
157 s.redirect(w, r, back, s.tr(r, "flash.suggestions"), "")
158}