BlueGit

timfa bluegit

Files

bluegit / internal / server / flow_list.go

flow_list.go

feat: maak de merge-requestlijst gelijk aan GitLab
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 8d4888a1 History
flow_list.go 10.0 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "net/url"
6 "sort"
7 "strconv"
8 "strings"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/access"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/store"
14)
15
16// FlowBoard is de lijst van merge requests of issues, in de opbouw van GitLab.
17type FlowBoard struct {
18 Base string
19 Query string
20 Sort string
21 Dir string
22 State string
23 FlipHref string
24 CanNew bool
25 NewHref string
26 NewKey string
27 EmptyKey string
28 Filters bool
29 Label string
30 Assignee string
31 Labels []IssueChoice
32 People []IssueChoice
33 Tabs []FlowTab
34 Rows []FlowRow
35}
36
37// FlowTab is één statusfilter met een teller.
38type FlowTab struct {
39 Href string
40 Key string
41 Count int
42 On bool
43}
44
45// FlowRow is één regel in die lijst.
46type FlowRow struct {
47 Href string
48 Title string
49 Ref string
50 State string
51 StateLabel string
52 Created string
53 Updated string
54 Author string
55 AuthorURL string
56 Target string
57 Comments int
58 PipeStatus string
59 PipeLabel string
60 PipeHref string
61 Draft bool
62 Confidential bool
63 Labels []store.Label
64 Extra string
65}
66
67type flowPerson struct {
68 Name string
69 URL string
70}
71
72func (s *Server) flowMRs(r *http.Request, repo store.Repo, role access.Role, all []store.MergeRequest) *FlowBoard {
73 q := strings.TrimSpace(r.URL.Query().Get("q"))
74 sortKey := flowSortKey(r.URL.Query().Get("sort"))
75 dir := flowDirKey(r.URL.Query().Get("dir"))
76 state := flowStateKey(r.URL.Query().Get("state"), true)
77 base := "/" + repo.FullPath() + "/-/merge-requests"
78 people := map[string]flowPerson{}
79
80 var matched []store.MergeRequest
81 openN, mergedN, closedN := 0, 0, 0
82 for _, mr := range all {
83 name, _ := s.flowAuthor(r, mr.Author, people)
84 if !flowHit(q, mr.Title, mr.Description, strconv.FormatInt(mr.Number, 10), "!"+strconv.FormatInt(mr.Number, 10), mr.Author, name, mr.SourceBranch, mr.TargetBranch) {
85 continue
86 }
87 matched = append(matched, mr)
88 switch mr.Status {
89 case "merged":
90 mergedN++
91 case "closed":
92 closedN++
93 default:
94 openN++
95 }
96 }
97 var shown []store.MergeRequest
98 for _, mr := range matched {
99 if state == "all" || mr.Status == state || (state == "open" && mr.Status != "merged" && mr.Status != "closed") {
100 shown = append(shown, mr)
101 }
102 }
103 sortFlow(shown, sortKey, dir, func(i int) (time.Time, time.Time, int64) {
104 return shown[i].CreatedAt, shown[i].UpdatedAt, shown[i].Number
105 })
106
107 rows := make([]FlowRow, 0, len(shown))
108 for _, mr := range shown {
109 name, href := s.flowAuthor(r, mr.Author, people)
110 row := FlowRow{
111 Href: base + "/" + strconv.FormatInt(mr.Number, 10),
112 Title: mr.Title,
113 Ref: "!" + strconv.FormatInt(mr.Number, 10),
114 State: flowBadge(mr.Status),
115 StateLabel: s.flowStateLabel(r, mr.Status),
116 Created: s.ago(r, mr.CreatedAt),
117 Updated: s.ago(r, mr.UpdatedAt),
118 Author: name,
119 AuthorURL: href,
120 Draft: mr.Draft,
121 }
122 if row.State == "open" {
123 row.Target = mr.TargetBranch
124 if notes, err := s.Store.ListNotes(r.Context(), mr.ID); err == nil {
125 row.Comments = len(notes)
126 }
127 s.attachFlowPipe(r, repo, mr, &row)
128 }
129 rows = append(rows, row)
130 }
131 board := &FlowBoard{
132 Base: base, Query: q, Sort: sortKey, Dir: dir, State: state,
133 FlipHref: flowHref(base, state, q, sortKey, flipDir(dir), "", ""),
134 CanNew: role.CanWriteCode(),
135 NewHref: base + "/new",
136 NewKey: "list.newMR",
137 EmptyKey: "mr.none",
138 Rows: rows,
139 }
140 board.Tabs = []FlowTab{
141 {Href: flowHref(base, "open", q, sortKey, dir, "", ""), Key: "issue.open", Count: openN, On: state == "open"},
142 {Href: flowHref(base, "merged", q, sortKey, dir, "", ""), Key: "list.merged", Count: mergedN, On: state == "merged"},
143 {Href: flowHref(base, "closed", q, sortKey, dir, "", ""), Key: "issue.closed", Count: closedN, On: state == "closed"},
144 {Href: flowHref(base, "all", q, sortKey, dir, "", ""), Key: "issue.all", Count: openN + mergedN + closedN, On: state == "all"},
145 }
146 return board
147}
148
149func (s *Server) flowIssues(r *http.Request, repo store.Repo, b *IssueBoard, all []store.Issue) *FlowBoard {
150 q := b.Query
151 sortKey := flowSortKey(r.URL.Query().Get("sort"))
152 dir := flowDirKey(r.URL.Query().Get("dir"))
153 state := flowStateKey(r.URL.Query().Get("state"), false)
154 base := "/" + repo.FullPath() + "/-/issues"
155 people := map[string]flowPerson{}
156
157 var matched []store.Issue
158 openN, closedN := 0, 0
159 for _, iss := range all {
160 name, _ := s.flowAuthor(r, iss.Author, people)
161 bits := append(issueSearchBits(iss), name)
162 if !flowHit(q, bits...) {
163 continue
164 }
165 matched = append(matched, iss)
166 if iss.Status == "closed" {
167 closedN++
168 } else {
169 openN++
170 }
171 }
172 var shown []store.Issue
173 for _, iss := range matched {
174 if state == "all" || iss.Status == state || (state == "open" && iss.Status != "closed") {
175 shown = append(shown, iss)
176 }
177 }
178 sortFlow(shown, sortKey, dir, func(i int) (time.Time, time.Time, int64) {
179 return shown[i].CreatedAt, shown[i].UpdatedAt, shown[i].Number
180 })
181
182 rows := make([]FlowRow, 0, len(shown))
183 for _, iss := range shown {
184 name, href := s.flowAuthor(r, iss.Author, people)
185 extra := strings.Join(iss.Assignees, ", ")
186 if iss.Milestone != "" {
187 if extra != "" {
188 extra += " · "
189 }
190 extra += iss.Milestone
191 }
192 row := FlowRow{
193 Href: base + "/" + strconv.FormatInt(iss.Number, 10),
194 Title: iss.Title,
195 Ref: "#" + strconv.FormatInt(iss.Number, 10),
196 State: flowBadge(iss.Status),
197 StateLabel: s.flowStateLabel(r, iss.Status),
198 Created: s.ago(r, iss.CreatedAt),
199 Updated: s.ago(r, iss.UpdatedAt),
200 Author: name,
201 AuthorURL: href,
202 Confidential: iss.Confidential,
203 Labels: iss.Labels,
204 Extra: extra,
205 }
206 if row.State == "open" {
207 if notes, err := s.Store.ListIssueNotes(r.Context(), iss.ID); err == nil {
208 row.Comments = len(notes)
209 }
210 }
211 rows = append(rows, row)
212 }
213 return &FlowBoard{
214 Base: base, Query: q, Sort: sortKey, Dir: dir, State: state,
215 FlipHref: flowHref(base, state, q, sortKey, flipDir(dir), b.Label, b.Assignee),
216 CanNew: b.CanCreate,
217 NewHref: base + "/new",
218 NewKey: "issue.new",
219 EmptyKey: "issue.none",
220 Filters: true,
221 Label: b.Label,
222 Assignee: b.Assignee,
223 Labels: b.Labels,
224 People: b.People,
225 Rows: rows,
226 Tabs: []FlowTab{
227 {Href: flowHref(base, "open", q, sortKey, dir, b.Label, b.Assignee), Key: "issue.open", Count: openN, On: state == "open"},
228 {Href: flowHref(base, "closed", q, sortKey, dir, b.Label, b.Assignee), Key: "issue.closed", Count: closedN, On: state == "closed"},
229 {Href: flowHref(base, "all", q, sortKey, dir, b.Label, b.Assignee), Key: "issue.all", Count: openN + closedN, On: state == "all"},
230 },
231 }
232}
233
234func (s *Server) attachFlowPipe(r *http.Request, repo store.Repo, mr store.MergeRequest, row *FlowRow) {
235 p, err := s.Store.LatestMergePipeline(r.Context(), repo.ID, mr.ID)
236 if err != nil {
237 return
238 }
239 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
240 st := displayStatus(p.Status, jobs)
241 row.PipeStatus = st
242 row.PipeLabel = s.tr(r, "pipe."+st)
243 row.PipeHref = "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
244}
245
246func (s *Server) flowAuthor(r *http.Request, username string, cache map[string]flowPerson) (string, string) {
247 username = strings.TrimSpace(username)
248 if username == "" {
249 return "", ""
250 }
251 if p, ok := cache[username]; ok {
252 return p.Name, p.URL
253 }
254 p := flowPerson{Name: username}
255 if u, err := s.Store.UserByUsername(r.Context(), username); err == nil && u.ID != 0 {
256 if name := strings.TrimSpace(u.DisplayName); name != "" {
257 p.Name = name
258 } else {
259 p.Name = u.Username
260 }
261 p.URL = "/users/" + u.Username
262 }
263 cache[username] = p
264 return p.Name, p.URL
265}
266
267func (s *Server) flowStateLabel(r *http.Request, status string) string {
268 switch status {
269 case "merged":
270 return s.tr(r, "list.merged")
271 case "closed":
272 return s.tr(r, "issue.closed")
273 default:
274 return s.tr(r, "issue.open")
275 }
276}
277
278func (s *Server) ago(r *http.Request, t time.Time) string {
279 if t.IsZero() {
280 return ""
281 }
282 return i18n.Ago(s.langOf(r), t.Unix())
283}
284
285func issueSearchBits(iss store.Issue) []string {
286 bits := []string{iss.Title, iss.Description, strconv.FormatInt(iss.Number, 10), "#" + strconv.FormatInt(iss.Number, 10), iss.Author, iss.Milestone}
287 bits = append(bits, iss.Assignees...)
288 for _, l := range iss.Labels {
289 bits = append(bits, l.Name)
290 }
291 return bits
292}
293
294func flowHit(q string, parts ...string) bool {
295 q = strings.ToLower(strings.TrimSpace(q))
296 if q == "" {
297 return true
298 }
299 q = strings.TrimPrefix(q, "!")
300 q = strings.TrimPrefix(q, "#")
301 for _, p := range parts {
302 if strings.Contains(strings.ToLower(p), q) {
303 return true
304 }
305 }
306 return false
307}
308
309func flowSortKey(raw string) string {
310 if raw == "updated" {
311 return "updated"
312 }
313 return "created"
314}
315
316func flowDirKey(raw string) string {
317 if raw == "asc" {
318 return "asc"
319 }
320 return "desc"
321}
322
323func flipDir(dir string) string {
324 if dir == "asc" {
325 return "desc"
326 }
327 return "asc"
328}
329
330func flowStateKey(raw string, merged bool) string {
331 switch raw {
332 case "closed", "all":
333 return raw
334 case "merged":
335 if merged {
336 return "merged"
337 }
338 }
339 return "open"
340}
341
342func flowBadge(status string) string {
343 switch status {
344 case "merged", "closed":
345 return status
346 default:
347 return "open"
348 }
349}
350
351func flowHref(base, state, q, sortKey, dir, label, assignee string) string {
352 v := url.Values{}
353 v.Set("state", state)
354 if strings.TrimSpace(q) != "" {
355 v.Set("q", q)
356 }
357 if sortKey == "updated" {
358 v.Set("sort", "updated")
359 }
360 if dir == "asc" {
361 v.Set("dir", "asc")
362 }
363 if label != "" {
364 v.Set("label", label)
365 }
366 if assignee != "" {
367 v.Set("assignee", assignee)
368 }
369 return base + "?" + v.Encode()
370}
371
372func sortFlow[T any](rows []T, sortKey, dir string, at func(i int) (created, updated time.Time, number int64)) {
373 sort.SliceStable(rows, func(i, j int) bool {
374 ac, au, an := at(i)
375 bc, bu, bn := at(j)
376 a, b := ac, bc
377 if sortKey == "updated" {
378 a, b = au, bu
379 }
380 if dir == "asc" {
381 if a.Equal(b) {
382 return an < bn
383 }
384 return a.Before(b)
385 }
386 if a.Equal(b) {
387 return an > bn
388 }
389 return a.After(b)
390 })
391}