BlueGit

timfa bluegit

Files

bluegit / internal / server / suggest.go

suggest.go

feat: lees .bluegit-sjablonen en klikbare takenlijsten
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
79722e46 History
suggest.go 3.2 KiB Download Raw
1package server
2
3import (
4 "html/template"
5 "net/http"
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/markdown"
13 "gitlab.com/Timfa/bluegit/internal/store"
14)
15
16type noteRender struct {
17 RepoPath string
18 Post string
19 CSRF string
20 Count string
21 Toggle string
22 ViewerID int64
23}
24
25func buildNoteRows(notes []store.Note, lang i18n.Lang, opt noteRender) []NoteRow {
26 rows := make([]NoteRow, len(notes))
27 index := map[int64]int{}
28 for i, n := range notes {
29 rows[i] = NoteRow{
30 ID: n.ID, Author: n.Author, Body: n.Body, Path: n.FilePath,
31 Start: n.LineStart, End: n.LineEnd, HasSuggestion: n.Suggestion != "",
32 When: i18n.FormatDate(lang, n.CreatedAt), At: n.CreatedAt, ParentID: n.ParentID,
33 HTML: template.HTML(markdown.RenderTasks(n.Body, "", "", opt.RepoPath, markdown.Tasks{
34 Post: opt.Post, CSRF: opt.CSRF, Kind: "mr-note", ID: strconv.FormatInt(n.ID, 10),
35 CanToggle: opt.ViewerID != 0 && opt.ViewerID == n.AuthorID, Count: opt.Count, Toggle: opt.Toggle,
36 })),
37 }
38 index[n.ID] = i
39 }
40 for i := range rows {
41 parent := rows[i].ParentID
42 if parent == 0 {
43 continue
44 }
45 pi, ok := index[parent]
46 if !ok || rows[pi].ParentID != 0 {
47 rows[i].ParentID = 0
48 continue
49 }
50 rows[pi].Replies = append(rows[pi].Replies, rows[i])
51 }
52 return rows
53}
54
55func arrangeNotes(notes []store.Note, files []gitrepo.DiffFile, lang i18n.Lang, opt noteRender) ([]NoteRow, []DiffFileView) {
56 rows := buildNoteRows(notes, lang, opt)
57 placed := map[int64]bool{}
58 var views []DiffFileView
59 for _, f := range files {
60 view := DiffFileView{Path: f.Path}
61 for _, ln := range f.Lines {
62 row := DiffLineView{Kind: ln.Kind, Text: ln.Text, Old: ln.Old, New: ln.New}
63 if ln.New > 0 {
64 for _, note := range rows {
65 if note.ParentID != 0 {
66 continue
67 }
68 if note.Path == f.Path && note.End == ln.New {
69 row.Notes = append(row.Notes, note)
70 placed[note.ID] = true
71 }
72 }
73 }
74 view.Lines = append(view.Lines, row)
75 }
76 views = append(views, view)
77 }
78 var general []NoteRow
79 for _, note := range rows {
80 if note.ParentID != 0 {
81 continue
82 }
83 if note.Path == "" || !placed[note.ID] {
84 general = append(general, note)
85 }
86 }
87 return general, views
88}
89
90func repoPath(raw string) (string, bool) {
91 raw = strings.TrimSpace(raw)
92 if raw == "" {
93 return "", true
94 }
95 p := strings.ReplaceAll(raw, `\`, "/")
96 if strings.HasPrefix(p, "/") || strings.Contains(p, "..") || strings.ContainsAny(p, "\r\n") {
97 return "", false
98 }
99 return p, true
100}
101
102func (s *Server) applySuggestions(w http.ResponseWriter, r *http.Request) {
103 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
104 if !ok || !s.csrfOK(w, r) {
105 return
106 }
107 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
108 if err != nil {
109 http.NotFound(w, r)
110 return
111 }
112 back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number")
113 var ids []int64
114 for _, raw := range r.Form["id"] {
115 id, err := strconv.ParseInt(raw, 10, 64)
116 if err == nil {
117 ids = append(ids, id)
118 }
119 }
120 if !s.uiOK(w, r, back, s.opApplySuggestions(r.Context(), u, repo, n, r.FormValue("title"), r.FormValue("description"), ids)) {
121 return
122 }
123 s.redirect(w, r, back, s.tr(r, "flash.suggestions"), "")
124}