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