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