| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "regexp" |
| 8 | "sort" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | |
| 12 | "gitlab.com/Timfa/bluegit/internal/access" |
| 13 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 14 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 15 | "gitlab.com/Timfa/bluegit/internal/markdown" |
| 16 | "gitlab.com/Timfa/bluegit/internal/store" |
| 17 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 18 | ) |
| 19 | |
| 20 | // IssueBoard is de gegevensset voor de issuepagina's. |
| 21 | type IssueBoard struct { |
| 22 | List []IssueRow |
| 23 | One *IssueRow |
| 24 | Notes []IssueNoteRow |
| 25 | Labels []IssueChoice |
| 26 | Milestones []store.Milestone |
| 27 | People []IssueChoice |
| 28 | State string |
| 29 | Query string |
| 30 | Label string |
| 31 | Assignee string |
| 32 | Milestone string |
| 33 | Due string |
| 34 | FormTitle string |
| 35 | FormBody string |
| 36 | Confidential bool |
| 37 | CanCreate bool |
| 38 | CanTriage bool |
| 39 | CanEdit bool |
| 40 | CanClose bool |
| 41 | CanReopen bool |
| 42 | CanDelete bool |
| 43 | Relate int64 |
| 44 | Related []IssueRow |
| 45 | Tpls []string |
| 46 | Tpl string |
| 47 | } |
| 48 | |
| 49 | // IssueChoice is een label of persoon in een formulier. |
| 50 | type IssueChoice struct { |
| 51 | Name string |
| 52 | Color string |
| 53 | AvatarURL string |
| 54 | On bool |
| 55 | } |
| 56 | |
| 57 | // IssueRow is één issue in een lijst of op de detailpagina. |
| 58 | type IssueRow struct { |
| 59 | Number int64 |
| 60 | Title string |
| 61 | Status string |
| 62 | Author string |
| 63 | AuthorURL string |
| 64 | Confidential bool |
| 65 | Milestone string |
| 66 | Due string |
| 67 | When string |
| 68 | Assignees string |
| 69 | People []AccountLink |
| 70 | Labels []store.Label |
| 71 | HTML template.HTML |
| 72 | } |
| 73 | |
| 74 | // IssueNoteRow is een gerenderde reactie. |
| 75 | type IssueNoteRow struct { |
| 76 | Author string |
| 77 | AuthorURL string |
| 78 | When string |
| 79 | HTML template.HTML |
| 80 | } |
| 81 | |
| 82 | var ( |
| 83 | closeIssueRe = regexp.MustCompile(`(?i)(?:^|[\s:])((?:close[sd]?|fix(?:e[sd])?|resolve[sd]?|implement(?:s|ed)?))\s+#(\d+)\b`) |
| 84 | fenceRe = regexp.MustCompile("(?s)```.*?```") |
| 85 | inlineCodeRe = regexp.MustCompile("`[^`\n]*`") |
| 86 | ) |
| 87 | |
| 88 | func (s *Server) issuesPage(w http.ResponseWriter, r *http.Request) { |
| 89 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 90 | if !ok { |
| 91 | return |
| 92 | } |
| 93 | v := s.repoView(w, r, repo, role, "tab.issues") |
| 94 | v.Title = s.tr(r, "tab.issues") |
| 95 | v.Page = "issues" |
| 96 | v.Wide = true |
| 97 | b := s.issueBoard(r, repo, role, u) |
| 98 | b.Query = strings.TrimSpace(r.URL.Query().Get("q")) |
| 99 | b.Label = strings.TrimSpace(r.URL.Query().Get("label")) |
| 100 | b.Assignee = strings.TrimSpace(r.URL.Query().Get("assignee")) |
| 101 | list, err := s.Store.ListIssues(r.Context(), repo.ID, store.IssueFilter{ |
| 102 | State: "all", Label: b.Label, Assignee: b.Assignee, |
| 103 | ViewerID: u.ID, Triage: role.CanTriageIssue(), |
| 104 | }) |
| 105 | if err != nil { |
| 106 | s.fail(w, r, err) |
| 107 | return |
| 108 | } |
| 109 | v.Flow = s.flowIssues(r, repo, b, list) |
| 110 | b.State = v.Flow.State |
| 111 | for _, iss := range list { |
| 112 | if iss.Status != b.State && b.State != "all" { |
| 113 | continue |
| 114 | } |
| 115 | if !flowHit(b.Query, issueSearchBits(iss)...) { |
| 116 | continue |
| 117 | } |
| 118 | b.List = append(b.List, s.issueRow(r, repo, iss)) |
| 119 | } |
| 120 | v.Issues = b |
| 121 | s.render(w, "issues", v) |
| 122 | } |
| 123 | |
| 124 | func (s *Server) newIssue(w http.ResponseWriter, r *http.Request) { |
| 125 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 126 | if !ok { |
| 127 | return |
| 128 | } |
| 129 | if u.ID == 0 || !role.CanCreateIssue() { |
| 130 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 131 | return |
| 132 | } |
| 133 | v := s.repoView(w, r, repo, role, "issue.new") |
| 134 | v.Title = s.tr(r, "issue.new") |
| 135 | v.Page = "issuenew" |
| 136 | v.Wide = true |
| 137 | b := s.issueBoard(r, repo, role, u) |
| 138 | s.prefillIssue(r, repo, u, role, b) |
| 139 | s.fillTpl(r, repo, "issue", &b.FormBody, &b.Tpl, &b.Tpls) |
| 140 | v.Issues = b |
| 141 | s.render(w, "issuenew", v) |
| 142 | } |
| 143 | |
| 144 | func (s *Server) createIssue(w http.ResponseWriter, r *http.Request) { |
| 145 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 146 | if !ok || !s.csrfOK(w, r) { |
| 147 | return |
| 148 | } |
| 149 | back := repoURL(repo) + "/-/issues/new" |
| 150 | if u.ID == 0 || !role.CanCreateIssue() { |
| 151 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 152 | return |
| 153 | } |
| 154 | in := store.NewIssue{ |
| 155 | Title: r.FormValue("title"), |
| 156 | Description: r.FormValue("description"), |
| 157 | Confidential: r.FormValue("confidential") == "1", |
| 158 | } |
| 159 | base := quickBase{Confidential: in.Confidential} |
| 160 | if triagePosted(r) { |
| 161 | if !role.CanTriageIssue() { |
| 162 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 163 | return |
| 164 | } |
| 165 | var err error |
| 166 | in.LabelIDs, in.AssigneeIDs, in.MilestoneID, in.DueOn, err = s.triageInput(r, repo) |
| 167 | if err != nil { |
| 168 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 169 | return |
| 170 | } |
| 171 | base.Labels = r.Form["label"] |
| 172 | base.Assignees = r.Form["assignee"] |
| 173 | base.Milestone = r.FormValue("milestone") |
| 174 | base.Due = in.DueOn |
| 175 | } |
| 176 | q, err := s.foldQuick(r.Context(), repo, in.Description, role.CanTriageIssue(), true, base) |
| 177 | if err != nil { |
| 178 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 179 | return |
| 180 | } |
| 181 | in.Description = q.Body |
| 182 | if q.Labels { |
| 183 | in.LabelIDs = q.LabelIDs |
| 184 | } |
| 185 | if q.Assignees { |
| 186 | in.AssigneeIDs = q.AssigneeIDs |
| 187 | } |
| 188 | if q.Meta { |
| 189 | in.MilestoneID = q.MilestoneID |
| 190 | in.DueOn = q.Due |
| 191 | in.Confidential = q.Confidential |
| 192 | } |
| 193 | iss, err := s.Store.CreateIssue(r.Context(), repo.ID, u.ID, in) |
| 194 | if err != nil { |
| 195 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 196 | return |
| 197 | } |
| 198 | if rel, _ := strconv.ParseInt(r.FormValue("relate"), 10, 64); rel > 0 { |
| 199 | if other, err := s.Store.IssueByNumber(r.Context(), repo.ID, rel); err == nil && issueVisible(other, u, role) { |
| 200 | _ = s.Store.LinkIssues(r.Context(), repo.ID, iss.ID, other.ID) |
| 201 | } |
| 202 | } |
| 203 | s.issueHook(repo, iss.Number, u.Username, "opened", "") |
| 204 | s.redirect(w, r, issuePath(repo, iss.Number), s.tr(r, "flash.issueCreated"), "") |
| 205 | } |
| 206 | |
| 207 | func (s *Server) showIssue(w http.ResponseWriter, r *http.Request) { |
| 208 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 209 | if !ok { |
| 210 | return |
| 211 | } |
| 212 | iss, ok := s.loadVisibleIssue(w, r, repo, u, role) |
| 213 | if !ok { |
| 214 | return |
| 215 | } |
| 216 | v := s.repoView(w, r, repo, role, "#"+strconv.FormatInt(iss.Number, 10)) |
| 217 | v.Title = "#" + strconv.FormatInt(iss.Number, 10) + " " + iss.Title |
| 218 | v.Page = "issue" |
| 219 | v.Wide = true |
| 220 | b := s.issueBoard(r, repo, role, u) |
| 221 | row := s.issueRow(r, repo, iss) |
| 222 | canText := u.ID != 0 && (u.ID == iss.AuthorID || role.CanTriageIssue()) |
| 223 | row.HTML = template.HTML(markdown.RenderTasks(iss.Description, "", "", "/"+repo.FullPath(), s.taskOpt(r, v.CSRF, repo, "issue", strconv.FormatInt(iss.Number, 10), canText))) |
| 224 | b.One = &row |
| 225 | b.Confidential = iss.Confidential |
| 226 | b.Milestone = iss.Milestone |
| 227 | b.Due = iss.DueOn |
| 228 | b.FormTitle = iss.Title |
| 229 | b.FormBody = iss.Description |
| 230 | b.CanEdit = u.ID != 0 && (u.ID == iss.AuthorID || role.CanTriageIssue()) |
| 231 | b.CanClose = iss.Status == "open" && b.CanEdit |
| 232 | b.CanReopen = iss.Status == "closed" && b.CanEdit |
| 233 | b.Labels = markChoices(b.Labels, labelNames(iss.Labels)) |
| 234 | b.People = markChoices(b.People, iss.Assignees) |
| 235 | notes, _ := s.Store.ListIssueNotes(r.Context(), iss.ID) |
| 236 | for _, n := range notes { |
| 237 | noteURL := "" |
| 238 | if n.Author != "" { |
| 239 | noteURL = "/users/" + n.Author |
| 240 | } |
| 241 | b.Notes = append(b.Notes, IssueNoteRow{ |
| 242 | Author: n.Author, AuthorURL: noteURL, |
| 243 | When: i18n.FormatDate(s.langOf(r), n.CreatedAt), |
| 244 | HTML: template.HTML(markdown.RenderTasks(n.Body, "", "", "/"+repo.FullPath(), s.taskOpt(r, v.CSRF, repo, "issue-note", strconv.FormatInt(n.ID, 10), u.ID != 0 && u.ID == n.AuthorID))), |
| 245 | }) |
| 246 | } |
| 247 | related, _ := s.Store.RelatedIssues(r.Context(), repo.ID, iss.ID) |
| 248 | for _, other := range related { |
| 249 | if issueVisible(other, u, role) { |
| 250 | b.Related = append(b.Related, s.issueRow(r, repo, other)) |
| 251 | } |
| 252 | } |
| 253 | v.Issues = b |
| 254 | s.render(w, "issue", v) |
| 255 | } |
| 256 | |
| 257 | func (s *Server) postIssue(w http.ResponseWriter, r *http.Request) { |
| 258 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 259 | if !ok || !s.csrfOK(w, r) { |
| 260 | return |
| 261 | } |
| 262 | iss, ok := s.loadVisibleIssue(w, r, repo, u, role) |
| 263 | if !ok { |
| 264 | return |
| 265 | } |
| 266 | back := issuePath(repo, iss.Number) |
| 267 | if u.ID == 0 { |
| 268 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 269 | return |
| 270 | } |
| 271 | switch r.FormValue("action") { |
| 272 | case "note": |
| 273 | if !role.CanCreateIssue() { |
| 274 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 275 | return |
| 276 | } |
| 277 | q, err := s.foldQuick(r.Context(), repo, r.FormValue("body"), role.CanTriageIssue(), false, quickFromIssue(iss)) |
| 278 | if err != nil { |
| 279 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 280 | return |
| 281 | } |
| 282 | if err := s.persistQuick(r.Context(), repo, iss.ID, q); err != nil { |
| 283 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 284 | return |
| 285 | } |
| 286 | if strings.TrimSpace(q.Body) == "" { |
| 287 | if !q.touched() { |
| 288 | s.redirect(w, r, back, "", s.tr(r, "err.issueNote")) |
| 289 | return |
| 290 | } |
| 291 | s.issueHook(repo, iss.Number, u.Username, "updated", "") |
| 292 | s.redirect(w, r, back, s.tr(r, "flash.issueUpdated"), "") |
| 293 | return |
| 294 | } |
| 295 | if _, err := s.Store.AddIssueNote(r.Context(), repo.ID, iss.ID, u.ID, q.Body); err != nil { |
| 296 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 297 | return |
| 298 | } |
| 299 | s.issueHook(repo, iss.Number, u.Username, "commented", strings.TrimSpace(q.Body)) |
| 300 | s.redirect(w, r, back, s.tr(r, "flash.note"), "") |
| 301 | case "close", "reopen": |
| 302 | if u.ID != iss.AuthorID && !role.CanTriageIssue() { |
| 303 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 304 | return |
| 305 | } |
| 306 | status := "closed" |
| 307 | flash := "flash.issueClosed" |
| 308 | action := "closed" |
| 309 | if r.FormValue("action") == "reopen" { |
| 310 | status, flash, action = "open", "flash.issueReopened", "reopened" |
| 311 | } |
| 312 | changed, err := s.Store.SetIssueState(r.Context(), iss.ID, status) |
| 313 | if err != nil { |
| 314 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 315 | return |
| 316 | } |
| 317 | if changed { |
| 318 | s.issueHook(repo, iss.Number, u.Username, action, "") |
| 319 | } |
| 320 | s.redirect(w, r, back, s.tr(r, flash), "") |
| 321 | case "update": |
| 322 | if u.ID != iss.AuthorID && !role.CanTriageIssue() { |
| 323 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 324 | return |
| 325 | } |
| 326 | q, err := s.foldQuick(r.Context(), repo, r.FormValue("description"), role.CanTriageIssue(), false, quickFromIssue(iss)) |
| 327 | if err != nil { |
| 328 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 329 | return |
| 330 | } |
| 331 | if err := s.Store.UpdateIssueText(r.Context(), iss.ID, r.FormValue("title"), q.Body); err != nil { |
| 332 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 333 | return |
| 334 | } |
| 335 | if err := s.persistQuick(r.Context(), repo, iss.ID, q); err != nil { |
| 336 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 337 | return |
| 338 | } |
| 339 | s.issueHook(repo, iss.Number, u.Username, "updated", "") |
| 340 | s.redirect(w, r, back, s.tr(r, "flash.issueUpdated"), "") |
| 341 | case "meta": |
| 342 | if !role.CanTriageIssue() { |
| 343 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 344 | return |
| 345 | } |
| 346 | labels, assignees, milestoneID, due, err := s.triageInput(r, repo) |
| 347 | if err != nil { |
| 348 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 349 | return |
| 350 | } |
| 351 | if err := s.Store.SetIssueLabels(r.Context(), repo.ID, iss.ID, labels); err != nil { |
| 352 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 353 | return |
| 354 | } |
| 355 | if err := s.Store.SetIssueAssignees(r.Context(), repo.ID, iss.ID, assignees); err != nil { |
| 356 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 357 | return |
| 358 | } |
| 359 | if err := s.Store.SetIssueMeta(r.Context(), repo.ID, iss.ID, milestoneID, r.FormValue("confidential") == "1", due); err != nil { |
| 360 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 361 | return |
| 362 | } |
| 363 | s.issueHook(repo, iss.Number, u.Username, "updated", "") |
| 364 | s.redirect(w, r, back, s.tr(r, "flash.issueUpdated"), "") |
| 365 | case "delete": |
| 366 | if !role.CanManageSettings() { |
| 367 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 368 | return |
| 369 | } |
| 370 | if err := s.Store.DeleteIssue(r.Context(), repo.ID, iss.ID); err != nil { |
| 371 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 372 | return |
| 373 | } |
| 374 | s.redirect(w, r, repoURL(repo)+"/-/issues", s.tr(r, "flash.issueDeleted"), "") |
| 375 | default: |
| 376 | http.Error(w, s.tr(r, "err.badForm"), http.StatusBadRequest) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | func (s *Server) createIssueLabel(w http.ResponseWriter, r *http.Request) { |
| 381 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 382 | if !ok || !s.csrfOK(w, r) { |
| 383 | return |
| 384 | } |
| 385 | if u.ID == 0 || !role.CanManageSettings() { |
| 386 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 387 | return |
| 388 | } |
| 389 | back := repoURL(repo) + "/-/issues" |
| 390 | if _, err := s.Store.UpsertLabel(r.Context(), repo.ID, r.FormValue("name"), r.FormValue("color")); err != nil { |
| 391 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 392 | return |
| 393 | } |
| 394 | s.redirect(w, r, back, s.tr(r, "flash.label"), "") |
| 395 | } |
| 396 | |
| 397 | func (s *Server) createIssueMilestone(w http.ResponseWriter, r *http.Request) { |
| 398 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 399 | if !ok || !s.csrfOK(w, r) { |
| 400 | return |
| 401 | } |
| 402 | if u.ID == 0 || !role.CanManageSettings() { |
| 403 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 404 | return |
| 405 | } |
| 406 | back := repoURL(repo) + "/-/issues" |
| 407 | if _, err := s.Store.UpsertMilestone(r.Context(), repo.ID, r.FormValue("title"), r.FormValue("due_on")); err != nil { |
| 408 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 409 | return |
| 410 | } |
| 411 | s.redirect(w, r, back, s.tr(r, "flash.milestone"), "") |
| 412 | } |
| 413 | |
| 414 | func (s *Server) loadVisibleIssue(w http.ResponseWriter, r *http.Request, repo store.Repo, u store.User, role access.Role) (store.Issue, bool) { |
| 415 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 416 | if err != nil || n < 1 { |
| 417 | http.NotFound(w, r) |
| 418 | return store.Issue{}, false |
| 419 | } |
| 420 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 421 | if err != nil || !issueVisible(iss, u, role) { |
| 422 | if err == nil && iss.Confidential && isCrawler(r) { |
| 423 | s.writeLockedCard(w, r) |
| 424 | } else { |
| 425 | http.NotFound(w, r) |
| 426 | } |
| 427 | return store.Issue{}, false |
| 428 | } |
| 429 | return iss, true |
| 430 | } |
| 431 | |
| 432 | func (s *Server) issueBoard(r *http.Request, repo store.Repo, role access.Role, u store.User) *IssueBoard { |
| 433 | labels, _ := s.Store.ListLabels(r.Context(), repo.ID) |
| 434 | miles, _ := s.Store.ListMilestones(r.Context(), repo.ID) |
| 435 | b := &IssueBoard{ |
| 436 | Labels: []IssueChoice{}, |
| 437 | Milestones: miles, |
| 438 | People: []IssueChoice{}, |
| 439 | CanCreate: u.ID != 0 && role.CanCreateIssue(), |
| 440 | CanTriage: role.CanTriageIssue(), |
| 441 | CanDelete: role.CanManageSettings(), |
| 442 | } |
| 443 | if b.Milestones == nil { |
| 444 | b.Milestones = []store.Milestone{} |
| 445 | } |
| 446 | for _, l := range labels { |
| 447 | b.Labels = append(b.Labels, IssueChoice{Name: l.Name, Color: l.Color}) |
| 448 | } |
| 449 | for _, p := range s.issueCandidates(r.Context(), repo) { |
| 450 | b.People = append(b.People, IssueChoice{Name: p.Username, AvatarURL: p.AvatarURL}) |
| 451 | } |
| 452 | return b |
| 453 | } |
| 454 | |
| 455 | func (s *Server) issueRow(r *http.Request, repo store.Repo, iss store.Issue) IssueRow { |
| 456 | url := "" |
| 457 | if iss.Author != "" { |
| 458 | url = "/users/" + iss.Author |
| 459 | } |
| 460 | var people []AccountLink |
| 461 | for _, name := range iss.Assignees { |
| 462 | name = strings.TrimSpace(name) |
| 463 | if name == "" { |
| 464 | continue |
| 465 | } |
| 466 | link := AccountLink{Name: name} |
| 467 | if u, err := s.Store.UserByUsername(r.Context(), name); err == nil && u.ID != 0 { |
| 468 | if display := strings.TrimSpace(u.DisplayName); display != "" { |
| 469 | link.Name = display |
| 470 | } else { |
| 471 | link.Name = u.Username |
| 472 | } |
| 473 | link.URL = "/users/" + u.Username |
| 474 | } |
| 475 | people = append(people, link) |
| 476 | } |
| 477 | return IssueRow{ |
| 478 | Number: iss.Number, Title: iss.Title, Status: iss.Status, Author: iss.Author, AuthorURL: url, |
| 479 | Confidential: iss.Confidential, Milestone: iss.Milestone, Due: iss.DueOn, |
| 480 | When: i18n.FormatDate(s.langOf(r), iss.CreatedAt), Assignees: strings.Join(iss.Assignees, ", "), |
| 481 | People: people, |
| 482 | Labels: iss.Labels, HTML: template.HTML(markdown.Render(iss.Description, "", "", "/"+repo.FullPath())), |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | func (s *Server) prefillIssue(r *http.Request, repo store.Repo, u store.User, role access.Role, b *IssueBoard) { |
| 487 | if rel, err := strconv.ParseInt(r.URL.Query().Get("relate"), 10, 64); err == nil && rel > 0 { |
| 488 | if iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, rel); err == nil && issueVisible(iss, u, role) { |
| 489 | b.Relate = rel |
| 490 | } |
| 491 | } |
| 492 | if r.URL.Query().Get("from") != "mr" { |
| 493 | return |
| 494 | } |
| 495 | n, err := strconv.ParseInt(r.URL.Query().Get("mr"), 10, 64) |
| 496 | if err != nil || n < 1 { |
| 497 | return |
| 498 | } |
| 499 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 500 | if err != nil { |
| 501 | return |
| 502 | } |
| 503 | ref := "!" + strconv.FormatInt(n, 10) |
| 504 | b.FormBody = ref |
| 505 | noteID, _ := strconv.ParseInt(r.URL.Query().Get("note"), 10, 64) |
| 506 | if noteID == 0 { |
| 507 | return |
| 508 | } |
| 509 | notes, err := s.Store.ListNotes(r.Context(), mr.ID) |
| 510 | if err != nil { |
| 511 | return |
| 512 | } |
| 513 | for _, note := range notes { |
| 514 | if note.ID == noteID { |
| 515 | b.FormBody = strings.TrimSpace(note.Body) + "\n\n" + ref |
| 516 | return |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | func (s *Server) issueCandidates(ctx context.Context, repo store.Repo) []store.Member { |
| 522 | seen := map[string]store.Member{} |
| 523 | add := func(list []store.Member) { |
| 524 | for _, m := range list { |
| 525 | if m.Pending || m.UserID == 0 || m.Username == "" { |
| 526 | continue |
| 527 | } |
| 528 | seen[m.Username] = m |
| 529 | } |
| 530 | } |
| 531 | if repo.OwnerKind == "user" { |
| 532 | if u, err := s.Store.UserByID(ctx, repo.OwnerID); err == nil { |
| 533 | seen[u.Username] = store.Member{UserID: u.ID, Username: u.Username, AvatarURL: u.AvatarURL, Role: access.RoleMaintainer} |
| 534 | } |
| 535 | } |
| 536 | if members, err := s.Store.ListMembers(ctx, "repo", repo.ID); err == nil { |
| 537 | add(members) |
| 538 | } |
| 539 | if repo.OwnerKind == "group" { |
| 540 | if members, err := s.Store.ListMembers(ctx, "group", repo.OwnerID); err == nil { |
| 541 | add(members) |
| 542 | } |
| 543 | } |
| 544 | out := make([]store.Member, 0, len(seen)) |
| 545 | for _, m := range seen { |
| 546 | out = append(out, m) |
| 547 | } |
| 548 | sort.Slice(out, func(i, j int) bool { return out[i].Username < out[j].Username }) |
| 549 | return out |
| 550 | } |
| 551 | |
| 552 | func (s *Server) triageInput(r *http.Request, repo store.Repo) (labels, assignees []int64, milestoneID int64, due string, err error) { |
| 553 | labels, err = s.resolveLabels(r.Context(), repo.ID, r.Form["label"]) |
| 554 | if err != nil { |
| 555 | return nil, nil, 0, "", err |
| 556 | } |
| 557 | assignees, err = s.resolveAssignees(r.Context(), repo, r.Form["assignee"]) |
| 558 | if err != nil { |
| 559 | return nil, nil, 0, "", err |
| 560 | } |
| 561 | milestoneID, err = s.resolveMilestone(r.Context(), repo.ID, r.FormValue("milestone")) |
| 562 | if err != nil { |
| 563 | return nil, nil, 0, "", err |
| 564 | } |
| 565 | due = strings.TrimSpace(r.FormValue("due_on")) |
| 566 | if err := store.ValidDue(due); err != nil { |
| 567 | return nil, nil, 0, "", err |
| 568 | } |
| 569 | return labels, assignees, milestoneID, due, nil |
| 570 | } |
| 571 | |
| 572 | func (s *Server) resolveLabels(ctx context.Context, repoID int64, names []string) ([]int64, error) { |
| 573 | ids := []int64{} |
| 574 | seen := map[int64]bool{} |
| 575 | for _, name := range names { |
| 576 | name = strings.TrimSpace(name) |
| 577 | if name == "" { |
| 578 | continue |
| 579 | } |
| 580 | if err := store.ValidLabelName(name); err != nil { |
| 581 | return nil, err |
| 582 | } |
| 583 | lab, err := s.Store.LabelByName(ctx, repoID, name) |
| 584 | if err != nil { |
| 585 | return nil, i18n.E("err.labelMissing", name) |
| 586 | } |
| 587 | if seen[lab.ID] { |
| 588 | continue |
| 589 | } |
| 590 | seen[lab.ID] = true |
| 591 | ids = append(ids, lab.ID) |
| 592 | } |
| 593 | return ids, nil |
| 594 | } |
| 595 | |
| 596 | func (s *Server) resolveAssignees(ctx context.Context, repo store.Repo, names []string) ([]int64, error) { |
| 597 | cand := map[string]int64{} |
| 598 | for _, m := range s.issueCandidates(ctx, repo) { |
| 599 | cand[m.Username] = m.UserID |
| 600 | } |
| 601 | ids := []int64{} |
| 602 | seen := map[int64]bool{} |
| 603 | for _, name := range names { |
| 604 | name = strings.ToLower(strings.TrimSpace(strings.TrimPrefix(name, "@"))) |
| 605 | if name == "" { |
| 606 | continue |
| 607 | } |
| 608 | id, ok := cand[name] |
| 609 | if !ok { |
| 610 | return nil, i18n.E("err.assignee", name) |
| 611 | } |
| 612 | if seen[id] { |
| 613 | continue |
| 614 | } |
| 615 | seen[id] = true |
| 616 | ids = append(ids, id) |
| 617 | } |
| 618 | return ids, nil |
| 619 | } |
| 620 | |
| 621 | func (s *Server) resolveMilestone(ctx context.Context, repoID int64, title string) (int64, error) { |
| 622 | title = strings.TrimSpace(title) |
| 623 | if title == "" { |
| 624 | return 0, nil |
| 625 | } |
| 626 | m, err := s.Store.MilestoneByTitle(ctx, repoID, title) |
| 627 | if err != nil { |
| 628 | return 0, i18n.E("err.milestone") |
| 629 | } |
| 630 | return m.ID, nil |
| 631 | } |
| 632 | |
| 633 | func (s *Server) issueHook(repo store.Repo, number int64, actor, action, comment string) { |
| 634 | if s.Store == nil { |
| 635 | return |
| 636 | } |
| 637 | iss, err := s.Store.IssueByNumber(context.Background(), repo.ID, number) |
| 638 | if err != nil { |
| 639 | return |
| 640 | } |
| 641 | num := strconv.FormatInt(iss.Number, 10) |
| 642 | page := strings.TrimRight(s.Cfg.BaseURL, "/") + issuePath(repo, iss.Number) |
| 643 | key := "hook.issueOpened" |
| 644 | switch action { |
| 645 | case "updated": |
| 646 | key = "hook.issueUpdated" |
| 647 | case "closed": |
| 648 | key = "hook.issueClosed" |
| 649 | case "reopened": |
| 650 | key = "hook.issueReopened" |
| 651 | case "commented": |
| 652 | key = "hook.issueComment" |
| 653 | } |
| 654 | summary := i18n.T(i18n.EN, key, num) |
| 655 | if action != "commented" { |
| 656 | summary = i18n.T(i18n.EN, key, num, iss.Title) |
| 657 | } |
| 658 | s.Dispatch(repo.ID, webhook.Event{ |
| 659 | Type: webhook.EventIssue, Project: repo.FullPath(), Actor: actor, |
| 660 | Summary: summary, URL: page, Comment: comment, |
| 661 | Issue: &webhook.IssueInfo{ |
| 662 | ID: iss.Number, Title: iss.Title, Description: iss.Description, |
| 663 | Status: iss.Status, Confidential: iss.Confidential, Author: iss.Author, |
| 664 | Assignees: iss.Assignees, Labels: labelNames(iss.Labels), |
| 665 | Milestone: iss.Milestone, DueDate: iss.DueOn, Action: action, URL: page, |
| 666 | }, |
| 667 | }) |
| 668 | } |
| 669 | |
| 670 | func (s *Server) closeIssueNumbers(ctx context.Context, repo store.Repo, actor store.User, numbers []int64) { |
| 671 | if s.Store == nil { |
| 672 | return |
| 673 | } |
| 674 | for _, n := range numbers { |
| 675 | iss, err := s.Store.IssueByNumber(ctx, repo.ID, n) |
| 676 | if err != nil || iss.Status == "closed" { |
| 677 | continue |
| 678 | } |
| 679 | changed, err := s.Store.SetIssueState(ctx, iss.ID, "closed") |
| 680 | if err != nil || !changed { |
| 681 | continue |
| 682 | } |
| 683 | s.issueHook(repo, n, actor.Username, "closed", "") |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | func (s *Server) mergeCloseTexts(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 688 | texts := []string{mr.Description} |
| 689 | if s.StoragePath() == "" { |
| 690 | return texts |
| 691 | } |
| 692 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 693 | commits, err := gitrepo.RangeLog(r.Context(), s.Git, dir, mr.TargetBranch, mr.SourceBranch) |
| 694 | if err != nil { |
| 695 | return texts |
| 696 | } |
| 697 | for i, c := range commits { |
| 698 | if i >= 50 { |
| 699 | break |
| 700 | } |
| 701 | texts = append(texts, commitText(c)) |
| 702 | } |
| 703 | return texts |
| 704 | } |
| 705 | |
| 706 | func (s *Server) closeIssuesOnPush(repo store.Repo, user store.User, before, after string) { |
| 707 | if s.Store == nil || s.StoragePath() == "" { |
| 708 | return |
| 709 | } |
| 710 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 711 | commits, err := gitrepo.RangeLog(context.Background(), s.Git, dir, before, after) |
| 712 | if err != nil { |
| 713 | return |
| 714 | } |
| 715 | var texts []string |
| 716 | for i, c := range commits { |
| 717 | if i >= 50 { |
| 718 | break |
| 719 | } |
| 720 | texts = append(texts, commitText(c)) |
| 721 | } |
| 722 | s.closeIssueNumbers(context.Background(), repo, user, closingNumbers(texts...)) |
| 723 | } |
| 724 | |
| 725 | func closingNumbers(texts ...string) []int64 { |
| 726 | seen := map[int64]bool{} |
| 727 | var out []int64 |
| 728 | for _, text := range texts { |
| 729 | text = fenceRe.ReplaceAllString(text, " ") |
| 730 | text = inlineCodeRe.ReplaceAllString(text, " ") |
| 731 | for _, m := range closeIssueRe.FindAllStringSubmatch(text, -1) { |
| 732 | n, err := strconv.ParseInt(m[2], 10, 64) |
| 733 | if err != nil || n < 1 || seen[n] { |
| 734 | continue |
| 735 | } |
| 736 | seen[n] = true |
| 737 | out = append(out, n) |
| 738 | } |
| 739 | } |
| 740 | return out |
| 741 | } |
| 742 | |
| 743 | func issueVisible(iss store.Issue, u store.User, role access.Role) bool { |
| 744 | if !iss.Confidential { |
| 745 | return true |
| 746 | } |
| 747 | if role.CanTriageIssue() { |
| 748 | return true |
| 749 | } |
| 750 | return u.ID != 0 && u.ID == iss.AuthorID |
| 751 | } |
| 752 | |
| 753 | func triagePosted(r *http.Request) bool { |
| 754 | if r.Form == nil { |
| 755 | return false |
| 756 | } |
| 757 | _, label := r.Form["label"] |
| 758 | _, assignee := r.Form["assignee"] |
| 759 | _, milestone := r.Form["milestone"] |
| 760 | _, due := r.Form["due_on"] |
| 761 | return label || assignee || milestone || due |
| 762 | } |
| 763 | |
| 764 | func markChoices(list []IssueChoice, selected []string) []IssueChoice { |
| 765 | on := map[string]bool{} |
| 766 | for _, name := range selected { |
| 767 | on[name] = true |
| 768 | } |
| 769 | for i := range list { |
| 770 | list[i].On = on[list[i].Name] |
| 771 | } |
| 772 | return list |
| 773 | } |
| 774 | |
| 775 | func labelNames(labels []store.Label) []string { |
| 776 | out := []string{} |
| 777 | for _, l := range labels { |
| 778 | out = append(out, l.Name) |
| 779 | } |
| 780 | return out |
| 781 | } |
| 782 | |
| 783 | func issuePath(repo store.Repo, n int64) string { |
| 784 | return repoURL(repo) + "/-/issues/" + strconv.FormatInt(n, 10) |
| 785 | } |
| 786 | |
| 787 | func (s *Server) fail(w http.ResponseWriter, r *http.Request, err error) { |
| 788 | http.Error(w, s.trErr(r, err), http.StatusInternalServerError) |
| 789 | } |