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