BlueGit

timfa bluegit

Files

bluegit / internal / server / suggest.go

suggest.go

feat: voeg een issue-tracker per repository toe
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
f3222d5f History
suggest.go 2.9 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
16func buildNoteRows(notes []store.Note, lang i18n.Lang, repoPath string) []NoteRow {
17 rows := make([]NoteRow, len(notes))
18 index := map[int64]int{}
19 for i, n := range notes {
20 rows[i] = NoteRow{
21 ID: n.ID, Author: n.Author, Body: n.Body, Path: n.FilePath,
22 Start: n.LineStart, End: n.LineEnd, HasSuggestion: n.Suggestion != "",
23 When: i18n.FormatDate(lang, n.CreatedAt), At: n.CreatedAt, ParentID: n.ParentID,
24 HTML: template.HTML(markdown.Render(n.Body, "", "", repoPath)),
25 }
26 index[n.ID] = i
27 }
28 for i := range rows {
29 parent := rows[i].ParentID
30 if parent == 0 {
31 continue
32 }
33 pi, ok := index[parent]
34 if !ok || rows[pi].ParentID != 0 {
35 rows[i].ParentID = 0
36 continue
37 }
38 rows[pi].Replies = append(rows[pi].Replies, rows[i])
39 }
40 return rows
41}
42
43func arrangeNotes(notes []store.Note, files []gitrepo.DiffFile, lang i18n.Lang, repoPath string) ([]NoteRow, []DiffFileView) {
44 rows := buildNoteRows(notes, lang, repoPath)
45 placed := map[int64]bool{}
46 var views []DiffFileView
47 for _, f := range files {
48 view := DiffFileView{Path: f.Path}
49 for _, ln := range f.Lines {
50 row := DiffLineView{Kind: ln.Kind, Text: ln.Text, Old: ln.Old, New: ln.New}
51 if ln.New > 0 {
52 for _, note := range rows {
53 if note.ParentID != 0 {
54 continue
55 }
56 if note.Path == f.Path && note.End == ln.New {
57 row.Notes = append(row.Notes, note)
58 placed[note.ID] = true
59 }
60 }
61 }
62 view.Lines = append(view.Lines, row)
63 }
64 views = append(views, view)
65 }
66 var general []NoteRow
67 for _, note := range rows {
68 if note.ParentID != 0 {
69 continue
70 }
71 if note.Path == "" || !placed[note.ID] {
72 general = append(general, note)
73 }
74 }
75 return general, views
76}
77
78func repoPath(raw string) (string, bool) {
79 raw = strings.TrimSpace(raw)
80 if raw == "" {
81 return "", true
82 }
83 p := strings.ReplaceAll(raw, `\`, "/")
84 if strings.HasPrefix(p, "/") || strings.Contains(p, "..") || strings.ContainsAny(p, "\r\n") {
85 return "", false
86 }
87 return p, true
88}
89
90func (s *Server) applySuggestions(w http.ResponseWriter, r *http.Request) {
91 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
92 if !ok || !s.csrfOK(w, r) {
93 return
94 }
95 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
96 if err != nil {
97 http.NotFound(w, r)
98 return
99 }
100 back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number")
101 var ids []int64
102 for _, raw := range r.Form["id"] {
103 id, err := strconv.ParseInt(raw, 10, 64)
104 if err == nil {
105 ids = append(ids, id)
106 }
107 }
108 if !s.uiOK(w, r, back, s.opApplySuggestions(r.Context(), u, repo, n, r.FormValue("title"), r.FormValue("description"), ids)) {
109 return
110 }
111 s.redirect(w, r, back, s.tr(r, "flash.suggestions"), "")
112}