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