| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | type issueWrite struct { |
| 16 | Title *string `json:"title"` |
| 17 | Description *string `json:"description"` |
| 18 | State *string `json:"state"` |
| 19 | Confidential *bool `json:"confidential"` |
| 20 | Labels *[]string `json:"labels"` |
| 21 | Assignees *[]string `json:"assignees"` |
| 22 | Milestone *string `json:"milestone"` |
| 23 | DueDate *string `json:"due_date"` |
| 24 | } |
| 25 | |
| 26 | func (s *Server) issuesAPI(w http.ResponseWriter, r *http.Request) { |
| 27 | write := r.Method == http.MethodPost |
| 28 | u, repo, role, ok := s.issueAuth(w, r, write) |
| 29 | if !ok { |
| 30 | return |
| 31 | } |
| 32 | if r.Method == http.MethodGet { |
| 33 | state := r.URL.Query().Get("state") |
| 34 | if state != "closed" && state != "all" { |
| 35 | state = "open" |
| 36 | } |
| 37 | list, err := s.Store.ListIssues(r.Context(), repo.ID, store.IssueFilter{ |
| 38 | State: state, Query: r.URL.Query().Get("q"), Label: r.URL.Query().Get("label"), |
| 39 | Assignee: r.URL.Query().Get("assignee"), ViewerID: u.ID, Triage: role.CanTriageIssue(), |
| 40 | }) |
| 41 | if err != nil { |
| 42 | s.fail(w, r, err) |
| 43 | return |
| 44 | } |
| 45 | out := make([]any, 0, len(list)) |
| 46 | for _, iss := range list { |
| 47 | out = append(out, s.issueJSON(repo, iss, nil)) |
| 48 | } |
| 49 | writeJSON(w, http.StatusOK, map[string]any{"issues": out}) |
| 50 | return |
| 51 | } |
| 52 | if !role.CanCreateIssue() { |
| 53 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 54 | return |
| 55 | } |
| 56 | var req issueWrite |
| 57 | if !s.readJSON(w, r, &req) { |
| 58 | return |
| 59 | } |
| 60 | if req.Title == nil || strings.TrimSpace(*req.Title) == "" { |
| 61 | http.Error(w, s.tr(r, "err.issueTitle"), http.StatusBadRequest) |
| 62 | return |
| 63 | } |
| 64 | in := store.NewIssue{Title: *req.Title} |
| 65 | if req.Description != nil { |
| 66 | in.Description = *req.Description |
| 67 | } |
| 68 | if req.Confidential != nil { |
| 69 | in.Confidential = *req.Confidential |
| 70 | } |
| 71 | if req.Labels != nil || req.Assignees != nil || req.Milestone != nil || req.DueDate != nil { |
| 72 | if !role.CanTriageIssue() { |
| 73 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 74 | return |
| 75 | } |
| 76 | if err := s.applyIssueWrite(r, repo, &in, req); err != nil { |
| 77 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 78 | return |
| 79 | } |
| 80 | } |
| 81 | iss, err := s.Store.CreateIssue(r.Context(), repo.ID, u.ID, in) |
| 82 | if err != nil { |
| 83 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 84 | return |
| 85 | } |
| 86 | s.issueHook(repo, iss.Number, u.Username, "opened", "") |
| 87 | notes, _ := s.Store.ListIssueNotes(r.Context(), iss.ID) |
| 88 | writeJSON(w, http.StatusCreated, s.issueJSON(repo, iss, notes)) |
| 89 | } |
| 90 | |
| 91 | func (s *Server) issueAPI(w http.ResponseWriter, r *http.Request) { |
| 92 | write := r.Method != http.MethodGet |
| 93 | u, repo, role, ok := s.issueAuth(w, r, write) |
| 94 | if !ok { |
| 95 | return |
| 96 | } |
| 97 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 98 | if err != nil || n < 1 { |
| 99 | http.NotFound(w, r) |
| 100 | return |
| 101 | } |
| 102 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 103 | if err != nil || !issueVisible(iss, u, role) { |
| 104 | http.NotFound(w, r) |
| 105 | return |
| 106 | } |
| 107 | if r.Method == http.MethodGet { |
| 108 | notes, err := s.Store.ListIssueNotes(r.Context(), iss.ID) |
| 109 | if err != nil { |
| 110 | s.fail(w, r, err) |
| 111 | return |
| 112 | } |
| 113 | writeJSON(w, http.StatusOK, s.issueJSON(repo, iss, notes)) |
| 114 | return |
| 115 | } |
| 116 | if r.Method == http.MethodDelete { |
| 117 | if !role.CanManageSettings() { |
| 118 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 119 | return |
| 120 | } |
| 121 | if err := s.Store.DeleteIssue(r.Context(), repo.ID, iss.ID); err != nil { |
| 122 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 123 | return |
| 124 | } |
| 125 | w.WriteHeader(http.StatusNoContent) |
| 126 | return |
| 127 | } |
| 128 | if u.ID != iss.AuthorID && !role.CanTriageIssue() { |
| 129 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 130 | return |
| 131 | } |
| 132 | var req issueWrite |
| 133 | if !s.readJSON(w, r, &req) { |
| 134 | return |
| 135 | } |
| 136 | action := "" |
| 137 | if req.Title != nil || req.Description != nil { |
| 138 | title, desc := iss.Title, iss.Description |
| 139 | if req.Title != nil { |
| 140 | title = *req.Title |
| 141 | } |
| 142 | if req.Description != nil { |
| 143 | desc = *req.Description |
| 144 | } |
| 145 | if err := s.Store.UpdateIssueText(r.Context(), iss.ID, title, desc); err != nil { |
| 146 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 147 | return |
| 148 | } |
| 149 | action = "updated" |
| 150 | } |
| 151 | if req.Labels != nil || req.Assignees != nil || req.Milestone != nil || req.DueDate != nil || req.Confidential != nil { |
| 152 | if !role.CanTriageIssue() { |
| 153 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 154 | return |
| 155 | } |
| 156 | if req.Labels != nil { |
| 157 | ids, err := s.resolveLabels(r.Context(), repo.ID, *req.Labels) |
| 158 | if err != nil { |
| 159 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 160 | return |
| 161 | } |
| 162 | if err := s.Store.SetIssueLabels(r.Context(), repo.ID, iss.ID, ids); err != nil { |
| 163 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 164 | return |
| 165 | } |
| 166 | } |
| 167 | if req.Assignees != nil { |
| 168 | ids, err := s.resolveAssignees(r.Context(), repo, *req.Assignees) |
| 169 | if err != nil { |
| 170 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 171 | return |
| 172 | } |
| 173 | if err := s.Store.SetIssueAssignees(r.Context(), repo.ID, iss.ID, ids); err != nil { |
| 174 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 175 | return |
| 176 | } |
| 177 | } |
| 178 | if req.Milestone != nil || req.DueDate != nil || req.Confidential != nil { |
| 179 | mile, due, conf := iss.MilestoneID, iss.DueOn, iss.Confidential |
| 180 | if req.Milestone != nil { |
| 181 | id, err := s.resolveMilestone(r.Context(), repo.ID, *req.Milestone) |
| 182 | if err != nil { |
| 183 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 184 | return |
| 185 | } |
| 186 | mile = id |
| 187 | } |
| 188 | if req.DueDate != nil { |
| 189 | due = *req.DueDate |
| 190 | } |
| 191 | if req.Confidential != nil { |
| 192 | conf = *req.Confidential |
| 193 | } |
| 194 | if err := s.Store.SetIssueMeta(r.Context(), repo.ID, iss.ID, mile, conf, due); err != nil { |
| 195 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 196 | return |
| 197 | } |
| 198 | } |
| 199 | if action == "" { |
| 200 | action = "updated" |
| 201 | } |
| 202 | } |
| 203 | if req.State != nil { |
| 204 | if *req.State != "open" && *req.State != "closed" { |
| 205 | http.Error(w, s.tr(r, "err.issueState"), http.StatusBadRequest) |
| 206 | return |
| 207 | } |
| 208 | changed, err := s.Store.SetIssueState(r.Context(), iss.ID, *req.State) |
| 209 | if err != nil { |
| 210 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 211 | return |
| 212 | } |
| 213 | if changed { |
| 214 | if *req.State == "closed" { |
| 215 | action = "closed" |
| 216 | } else { |
| 217 | action = "reopened" |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | fresh, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 222 | if err != nil { |
| 223 | s.fail(w, r, err) |
| 224 | return |
| 225 | } |
| 226 | if action != "" { |
| 227 | comment := "" |
| 228 | s.issueHook(repo, fresh.Number, u.Username, action, comment) |
| 229 | } |
| 230 | notes, _ := s.Store.ListIssueNotes(r.Context(), fresh.ID) |
| 231 | writeJSON(w, http.StatusOK, s.issueJSON(repo, fresh, notes)) |
| 232 | } |
| 233 | |
| 234 | func (s *Server) issueNoteAPI(w http.ResponseWriter, r *http.Request) { |
| 235 | u, repo, role, ok := s.issueAuth(w, r, true) |
| 236 | if !ok { |
| 237 | return |
| 238 | } |
| 239 | if !role.CanCreateIssue() { |
| 240 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 241 | return |
| 242 | } |
| 243 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 244 | if err != nil || n < 1 { |
| 245 | http.NotFound(w, r) |
| 246 | return |
| 247 | } |
| 248 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, n) |
| 249 | if err != nil || !issueVisible(iss, u, role) { |
| 250 | http.NotFound(w, r) |
| 251 | return |
| 252 | } |
| 253 | var req struct { |
| 254 | Body string `json:"body"` |
| 255 | } |
| 256 | if !s.readJSON(w, r, &req) { |
| 257 | return |
| 258 | } |
| 259 | note, err := s.Store.AddIssueNote(r.Context(), repo.ID, iss.ID, u.ID, req.Body) |
| 260 | if err != nil { |
| 261 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 262 | return |
| 263 | } |
| 264 | s.issueHook(repo, iss.Number, u.Username, "commented", note.Body) |
| 265 | writeJSON(w, http.StatusCreated, map[string]any{ |
| 266 | "id": note.ID, "author": note.Author, "body": note.Body, |
| 267 | "created_at": note.CreatedAt.UTC().Format(time.RFC3339), |
| 268 | }) |
| 269 | } |
| 270 | |
| 271 | func (s *Server) labelsAPI(w http.ResponseWriter, r *http.Request) { |
| 272 | u, repo, role, ok := s.issueAuth(w, r, r.Method == http.MethodPost) |
| 273 | if !ok { |
| 274 | return |
| 275 | } |
| 276 | if r.Method == http.MethodGet { |
| 277 | list, err := s.Store.ListLabels(r.Context(), repo.ID) |
| 278 | if err != nil { |
| 279 | s.fail(w, r, err) |
| 280 | return |
| 281 | } |
| 282 | out := make([]any, 0, len(list)) |
| 283 | for _, l := range list { |
| 284 | out = append(out, map[string]any{"id": l.ID, "name": l.Name, "color": l.Color}) |
| 285 | } |
| 286 | writeJSON(w, http.StatusOK, map[string]any{"labels": out}) |
| 287 | return |
| 288 | } |
| 289 | if u.ID == 0 || !role.CanManageSettings() { |
| 290 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 291 | return |
| 292 | } |
| 293 | var req struct { |
| 294 | Name string `json:"name"` |
| 295 | Color string `json:"color"` |
| 296 | } |
| 297 | if !s.readJSON(w, r, &req) { |
| 298 | return |
| 299 | } |
| 300 | lab, err := s.Store.UpsertLabel(r.Context(), repo.ID, req.Name, req.Color) |
| 301 | if err != nil { |
| 302 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 303 | return |
| 304 | } |
| 305 | writeJSON(w, http.StatusCreated, map[string]any{"id": lab.ID, "name": lab.Name, "color": lab.Color}) |
| 306 | } |
| 307 | |
| 308 | func (s *Server) milestonesAPI(w http.ResponseWriter, r *http.Request) { |
| 309 | u, repo, role, ok := s.issueAuth(w, r, r.Method == http.MethodPost) |
| 310 | if !ok { |
| 311 | return |
| 312 | } |
| 313 | if r.Method == http.MethodGet { |
| 314 | list, err := s.Store.ListMilestones(r.Context(), repo.ID) |
| 315 | if err != nil { |
| 316 | s.fail(w, r, err) |
| 317 | return |
| 318 | } |
| 319 | out := make([]any, 0, len(list)) |
| 320 | for _, m := range list { |
| 321 | out = append(out, map[string]any{"id": m.ID, "title": m.Title, "due_on": m.DueOn, "state": m.State}) |
| 322 | } |
| 323 | writeJSON(w, http.StatusOK, map[string]any{"milestones": out}) |
| 324 | return |
| 325 | } |
| 326 | if u.ID == 0 || !role.CanManageSettings() { |
| 327 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 328 | return |
| 329 | } |
| 330 | var req struct { |
| 331 | Title string `json:"title"` |
| 332 | DueOn string `json:"due_on"` |
| 333 | } |
| 334 | if !s.readJSON(w, r, &req) { |
| 335 | return |
| 336 | } |
| 337 | m, err := s.Store.UpsertMilestone(r.Context(), repo.ID, req.Title, req.DueOn) |
| 338 | if err != nil { |
| 339 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 340 | return |
| 341 | } |
| 342 | writeJSON(w, http.StatusCreated, map[string]any{"id": m.ID, "title": m.Title, "due_on": m.DueOn, "state": m.State}) |
| 343 | } |
| 344 | |
| 345 | func (s *Server) applyIssueWrite(r *http.Request, repo store.Repo, in *store.NewIssue, req issueWrite) error { |
| 346 | if req.Labels != nil { |
| 347 | ids, err := s.resolveLabels(r.Context(), repo.ID, *req.Labels) |
| 348 | if err != nil { |
| 349 | return err |
| 350 | } |
| 351 | in.LabelIDs = ids |
| 352 | } |
| 353 | if req.Assignees != nil { |
| 354 | ids, err := s.resolveAssignees(r.Context(), repo, *req.Assignees) |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | in.AssigneeIDs = ids |
| 359 | } |
| 360 | if req.Milestone != nil { |
| 361 | id, err := s.resolveMilestone(r.Context(), repo.ID, *req.Milestone) |
| 362 | if err != nil { |
| 363 | return err |
| 364 | } |
| 365 | in.MilestoneID = id |
| 366 | } |
| 367 | if req.DueDate != nil { |
| 368 | in.DueOn = *req.DueDate |
| 369 | } |
| 370 | return nil |
| 371 | } |
| 372 | |
| 373 | func (s *Server) issueAuth(w http.ResponseWriter, r *http.Request, write bool) (store.User, store.Repo, access.Role, bool) { |
| 374 | if s.Store == nil { |
| 375 | http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) |
| 376 | return store.User{}, store.Repo{}, "", false |
| 377 | } |
| 378 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 379 | if err != nil { |
| 380 | http.NotFound(w, r) |
| 381 | return store.User{}, store.Repo{}, "", false |
| 382 | } |
| 383 | token := bearer(r) |
| 384 | if token == "" { |
| 385 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 386 | return store.User{}, store.Repo{}, "", false |
| 387 | } |
| 388 | if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") { |
| 389 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 390 | return store.User{}, store.Repo{}, "", false |
| 391 | } |
| 392 | u, err := s.Store.UserByToken(r.Context(), token) |
| 393 | if err != nil { |
| 394 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 395 | return store.User{}, store.Repo{}, "", false |
| 396 | } |
| 397 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 398 | if err != nil { |
| 399 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 400 | return store.User{}, store.Repo{}, "", false |
| 401 | } |
| 402 | if !write && !role.CanRead() && repo.Visibility != "public" { |
| 403 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 404 | return store.User{}, store.Repo{}, "", false |
| 405 | } |
| 406 | return u, repo, role, true |
| 407 | } |
| 408 | |
| 409 | func (s *Server) readJSON(w http.ResponseWriter, r *http.Request, dst any) bool { |
| 410 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(dst); err != nil { |
| 411 | http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest) |
| 412 | return false |
| 413 | } |
| 414 | return true |
| 415 | } |
| 416 | |
| 417 | func (s *Server) issueJSON(repo store.Repo, iss store.Issue, notes []store.IssueNote) map[string]any { |
| 418 | obj := map[string]any{ |
| 419 | "id": iss.Number, "title": iss.Title, "description": iss.Description, |
| 420 | "status": iss.Status, "confidential": iss.Confidential, "author": iss.Author, |
| 421 | "assignees": nonNil(iss.Assignees), "labels": labelNames(iss.Labels), |
| 422 | "milestone": iss.Milestone, "due_date": iss.DueOn, |
| 423 | "created_at": iss.CreatedAt.UTC().Format(time.RFC3339), |
| 424 | "updated_at": iss.UpdatedAt.UTC().Format(time.RFC3339), |
| 425 | "web_url": strings.TrimRight(s.Cfg.BaseURL, "/") + issuePath(repo, iss.Number), |
| 426 | } |
| 427 | if !iss.ClosedAt.IsZero() { |
| 428 | obj["closed_at"] = iss.ClosedAt.UTC().Format(time.RFC3339) |
| 429 | } |
| 430 | if notes != nil { |
| 431 | arr := make([]any, 0, len(notes)) |
| 432 | for _, n := range notes { |
| 433 | arr = append(arr, map[string]any{ |
| 434 | "id": n.ID, "author": n.Author, "body": n.Body, |
| 435 | "created_at": n.CreatedAt.UTC().Format(time.RFC3339), |
| 436 | }) |
| 437 | } |
| 438 | obj["notes"] = arr |
| 439 | } |
| 440 | return obj |
| 441 | } |
| 442 | |
| 443 | func writeJSON(w http.ResponseWriter, status int, v any) { |
| 444 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 445 | w.Header().Set("Cache-Control", "no-store") |
| 446 | w.WriteHeader(status) |
| 447 | _ = json.NewEncoder(w).Encode(v) |
| 448 | } |
| 449 | |
| 450 | func nonNil(s []string) []string { |
| 451 | if s == nil { |
| 452 | return []string{} |
| 453 | } |
| 454 | return s |
| 455 | } |