| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 10 | "gitlab.com/Timfa/bluegit/internal/images" |
| 11 | "gitlab.com/Timfa/bluegit/internal/store" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) pageURL(repo store.Repo, suffix string) string { |
| 15 | if !strings.HasPrefix(suffix, "/") { |
| 16 | suffix = "/" + suffix |
| 17 | } |
| 18 | return strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + suffix |
| 19 | } |
| 20 | |
| 21 | func (s *Server) mrJSON(repo store.Repo, mr store.MergeRequest) map[string]any { |
| 22 | return map[string]any{ |
| 23 | "id": mr.ID, "iid": mr.Number, "title": mr.Title, "description": mr.Description, |
| 24 | "source_branch": mr.SourceBranch, "target_branch": mr.TargetBranch, |
| 25 | "author": mr.Author, "state": mr.Status, "draft": mr.Draft, "labels": mr.Labels, |
| 26 | "remove_source_branch": mr.RemoveSourceBranch, "squash": mr.Squash, |
| 27 | "squash_commit_message": mr.SquashMessage, |
| 28 | "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(mr.Number, 10)), |
| 29 | "created_at": rfcTime(mr.CreatedAt), "updated_at": rfcTime(mr.UpdatedAt), |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func (s *Server) apiMRs(w http.ResponseWriter, r *http.Request) { |
| 34 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 35 | if !ok { |
| 36 | return |
| 37 | } |
| 38 | if r.Method == http.MethodPost { |
| 39 | var body struct { |
| 40 | Title string `json:"title"` |
| 41 | Description string `json:"description"` |
| 42 | SourceBranch string `json:"source_branch"` |
| 43 | TargetBranch string `json:"target_branch"` |
| 44 | Draft bool `json:"draft"` |
| 45 | Remove *bool `json:"remove_source_branch"` |
| 46 | Squash *bool `json:"squash"` |
| 47 | Message *string `json:"squash_commit_message"` |
| 48 | } |
| 49 | if err := readJSON(r, &body); err != nil { |
| 50 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 51 | return |
| 52 | } |
| 53 | mr, err := s.opCreateMR(r.Context(), apiUser(r), repo, body.Title, body.Description, body.SourceBranch, body.TargetBranch, body.Draft) |
| 54 | if !s.apiOK(w, r, err) { |
| 55 | return |
| 56 | } |
| 57 | remove, squash, message := true, false, "" |
| 58 | if body.Remove != nil { |
| 59 | remove = *body.Remove |
| 60 | } |
| 61 | if body.Squash != nil { |
| 62 | squash = *body.Squash |
| 63 | } |
| 64 | if body.Message != nil { |
| 65 | message = *body.Message |
| 66 | } |
| 67 | if err := s.Store.SetMergeOptions(r.Context(), mr.ID, remove, squash, message); !s.apiOK(w, r, err) { |
| 68 | return |
| 69 | } |
| 70 | mr, err = s.Store.MergeRequestByNumber(r.Context(), repo.ID, mr.Number) |
| 71 | if !s.apiOK(w, r, err) { |
| 72 | return |
| 73 | } |
| 74 | writeJSON(w, http.StatusCreated, s.mrJSON(repo, mr)) |
| 75 | return |
| 76 | } |
| 77 | list, err := s.Store.ListMergeRequests(r.Context(), repo.ID) |
| 78 | if !s.apiOK(w, r, err) { |
| 79 | return |
| 80 | } |
| 81 | // Optionele filters: state (open, merged, closed of all), source_branch, target_branch. |
| 82 | q := r.URL.Query() |
| 83 | wantState := strings.TrimSpace(q.Get("state")) |
| 84 | wantSource := strings.TrimSpace(q.Get("source_branch")) |
| 85 | wantTarget := strings.TrimSpace(q.Get("target_branch")) |
| 86 | page, per := pageQuery(r) |
| 87 | rows := make([]map[string]any, 0, len(list)) |
| 88 | for _, mr := range list { |
| 89 | if wantState != "" && wantState != "all" && mr.Status != wantState { |
| 90 | continue |
| 91 | } |
| 92 | if wantSource != "" && mr.SourceBranch != wantSource { |
| 93 | continue |
| 94 | } |
| 95 | if wantTarget != "" && mr.TargetBranch != wantTarget { |
| 96 | continue |
| 97 | } |
| 98 | rows = append(rows, s.mrJSON(repo, mr)) |
| 99 | } |
| 100 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 101 | } |
| 102 | |
| 103 | func (s *Server) apiMR(w http.ResponseWriter, r *http.Request) { |
| 104 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 105 | if !ok { |
| 106 | return |
| 107 | } |
| 108 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 109 | if err != nil { |
| 110 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 111 | return |
| 112 | } |
| 113 | if r.Method == http.MethodPut { |
| 114 | var body struct { |
| 115 | Title string `json:"title"` |
| 116 | Description string `json:"description"` |
| 117 | Labels string `json:"labels"` |
| 118 | Remove *bool `json:"remove_source_branch"` |
| 119 | Squash *bool `json:"squash"` |
| 120 | Message *string `json:"squash_commit_message"` |
| 121 | } |
| 122 | if err := readJSON(r, &body); err != nil { |
| 123 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 124 | return |
| 125 | } |
| 126 | mr, err := s.opUpdateMR(r.Context(), apiUser(r), repo, n, body.Title, body.Description, body.Labels) |
| 127 | if !s.apiOK(w, r, err) { |
| 128 | return |
| 129 | } |
| 130 | if body.Remove != nil || body.Squash != nil || body.Message != nil { |
| 131 | remove, squash, message := mr.RemoveSourceBranch, mr.Squash, mr.SquashMessage |
| 132 | if body.Remove != nil { |
| 133 | remove = *body.Remove |
| 134 | } |
| 135 | if body.Squash != nil { |
| 136 | squash = *body.Squash |
| 137 | } |
| 138 | if body.Message != nil { |
| 139 | message = *body.Message |
| 140 | } |
| 141 | if err := s.Store.SetMergeOptions(r.Context(), mr.ID, remove, squash, message); !s.apiOK(w, r, err) { |
| 142 | return |
| 143 | } |
| 144 | mr, err = s.Store.MergeRequestByNumber(r.Context(), repo.ID, mr.Number) |
| 145 | if !s.apiOK(w, r, err) { |
| 146 | return |
| 147 | } |
| 148 | } |
| 149 | writeJSON(w, http.StatusOK, s.mrJSON(repo, mr)) |
| 150 | return |
| 151 | } |
| 152 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 153 | if err != nil { |
| 154 | s.writeAPIErr(w, r, notFound(err)) |
| 155 | return |
| 156 | } |
| 157 | writeJSON(w, http.StatusOK, s.mrJSON(repo, mr)) |
| 158 | } |
| 159 | |
| 160 | func (s *Server) apiMRNotes(w http.ResponseWriter, r *http.Request) { |
| 161 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 162 | if !ok { |
| 163 | return |
| 164 | } |
| 165 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 166 | if err != nil { |
| 167 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 168 | return |
| 169 | } |
| 170 | if r.Method == http.MethodGet { |
| 171 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 172 | if err != nil { |
| 173 | s.writeAPIErr(w, r, notFound(err)) |
| 174 | return |
| 175 | } |
| 176 | notes, err := s.Store.ListNotes(r.Context(), mr.ID) |
| 177 | if !s.apiOK(w, r, err) { |
| 178 | return |
| 179 | } |
| 180 | rows := make([]map[string]any, 0, len(notes)) |
| 181 | for _, note := range notes { |
| 182 | rows = append(rows, map[string]any{ |
| 183 | "id": note.ID, "body": note.Body, "author": note.Author, |
| 184 | "file_path": note.FilePath, "line_start": note.LineStart, "line_end": note.LineEnd, |
| 185 | "suggestion": note.Suggestion, "parent_id": note.ParentID, "created_at": rfcTime(note.CreatedAt), |
| 186 | }) |
| 187 | } |
| 188 | page, per := pageQuery(r) |
| 189 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 190 | return |
| 191 | } |
| 192 | var body struct { |
| 193 | Body string `json:"body"` |
| 194 | Path string `json:"path"` |
| 195 | LineStart int `json:"line_start"` |
| 196 | LineEnd int `json:"line_end"` |
| 197 | ParentID int64 `json:"parent_id"` |
| 198 | } |
| 199 | if err := readJSON(r, &body); err != nil { |
| 200 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 201 | return |
| 202 | } |
| 203 | note, err := s.opAddNote(r, apiUser(r), repo, n, body.Body, body.Path, body.LineStart, body.LineEnd, body.ParentID) |
| 204 | if !s.apiOK(w, r, err) { |
| 205 | return |
| 206 | } |
| 207 | writeJSON(w, http.StatusCreated, map[string]any{"id": note.ID}) |
| 208 | } |
| 209 | |
| 210 | func (s *Server) apiReview(w http.ResponseWriter, r *http.Request) { |
| 211 | repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper) |
| 212 | if !ok { |
| 213 | return |
| 214 | } |
| 215 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 216 | if err != nil { |
| 217 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 218 | return |
| 219 | } |
| 220 | var body struct { |
| 221 | Status string `json:"status"` |
| 222 | } |
| 223 | if err := readJSON(r, &body); err != nil { |
| 224 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 225 | return |
| 226 | } |
| 227 | if !s.apiOK(w, r, s.opReview(r.Context(), apiUser(r), repo, n, body.Status)) { |
| 228 | return |
| 229 | } |
| 230 | writeJSON(w, http.StatusOK, map[string]any{"status": body.Status}) |
| 231 | } |
| 232 | |
| 233 | func (s *Server) apiDismissReview(w http.ResponseWriter, r *http.Request) { |
| 234 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 235 | if !ok { |
| 236 | return |
| 237 | } |
| 238 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 239 | if err != nil { |
| 240 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 241 | return |
| 242 | } |
| 243 | var body struct { |
| 244 | UserID int64 `json:"user_id"` |
| 245 | } |
| 246 | if err := readJSON(r, &body); err != nil { |
| 247 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 248 | return |
| 249 | } |
| 250 | if !s.apiOK(w, r, s.opDismissReview(r.Context(), apiUser(r), repo, n, body.UserID)) { |
| 251 | return |
| 252 | } |
| 253 | writeJSON(w, http.StatusOK, map[string]any{"status": "dismissed"}) |
| 254 | } |
| 255 | |
| 256 | func (s *Server) apiSuggestions(w http.ResponseWriter, r *http.Request) { |
| 257 | repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper) |
| 258 | if !ok { |
| 259 | return |
| 260 | } |
| 261 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 262 | if err != nil { |
| 263 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 264 | return |
| 265 | } |
| 266 | var body struct { |
| 267 | Title string `json:"title"` |
| 268 | Description string `json:"description"` |
| 269 | IDs []int64 `json:"ids"` |
| 270 | } |
| 271 | if err := readJSON(r, &body); err != nil { |
| 272 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 273 | return |
| 274 | } |
| 275 | if !s.apiOK(w, r, s.opApplySuggestions(r.Context(), apiUser(r), repo, n, body.Title, body.Description, body.IDs)) { |
| 276 | return |
| 277 | } |
| 278 | writeJSON(w, http.StatusOK, map[string]any{"applied": len(body.IDs)}) |
| 279 | } |
| 280 | |
| 281 | func (s *Server) apiMerge(w http.ResponseWriter, r *http.Request) { |
| 282 | repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper) |
| 283 | if !ok { |
| 284 | return |
| 285 | } |
| 286 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 287 | if err != nil { |
| 288 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 289 | return |
| 290 | } |
| 291 | var body struct { |
| 292 | Remove *bool `json:"remove_source_branch"` |
| 293 | Squash *bool `json:"squash"` |
| 294 | Message *string `json:"squash_commit_message"` |
| 295 | } |
| 296 | if err := readJSON(r, &body); err != nil { |
| 297 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 298 | return |
| 299 | } |
| 300 | out, err := s.opMergeMR(r, apiUser(r), repo, n, mergeChoice{Remove: body.Remove, Squash: body.Squash, Message: body.Message}) |
| 301 | if !s.apiOK(w, r, err) { |
| 302 | return |
| 303 | } |
| 304 | writeJSON(w, http.StatusOK, map[string]any{ |
| 305 | "state": "merged", "source_branch_removed": out.Removed, "source_branch_skip": out.Skip, |
| 306 | "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(n, 10)), |
| 307 | }) |
| 308 | } |
| 309 | |
| 310 | func (s *Server) apiCloseMR(w http.ResponseWriter, r *http.Request) { |
| 311 | s.apiSetMR(w, r, false) |
| 312 | } |
| 313 | |
| 314 | func (s *Server) apiReopenMR(w http.ResponseWriter, r *http.Request) { |
| 315 | s.apiSetMR(w, r, true) |
| 316 | } |
| 317 | |
| 318 | func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) { |
| 319 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 320 | if !ok { |
| 321 | return |
| 322 | } |
| 323 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 324 | if err != nil { |
| 325 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 326 | return |
| 327 | } |
| 328 | if !s.apiOK(w, r, s.opCloseMR(r.Context(), apiUser(r), repo, n, reopen)) { |
| 329 | return |
| 330 | } |
| 331 | state := "closed" |
| 332 | if reopen { |
| 333 | state = "open" |
| 334 | } |
| 335 | writeJSON(w, http.StatusOK, map[string]any{ |
| 336 | "state": state, "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(n, 10)), |
| 337 | }) |
| 338 | } |
| 339 | |
| 340 | func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any { |
| 341 | return map[string]any{ |
| 342 | "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source, |
| 343 | "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt), |
| 344 | "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)), |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | func jobJSON(j store.Job) map[string]any { |
| 349 | return map[string]any{ |
| 350 | "id": j.ID, "name": j.Name, "stage": j.Stage, "status": j.Status, "when": j.When, |
| 351 | "allow_failure": j.AllowFailure, "started_at": rfcTime(j.StartedAt), "finished_at": rfcTime(j.FinishedAt), |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | func (s *Server) apiPipelines(w http.ResponseWriter, r *http.Request) { |
| 356 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 357 | if !ok { |
| 358 | return |
| 359 | } |
| 360 | if r.Method == http.MethodPost { |
| 361 | var body struct { |
| 362 | Ref string `json:"ref"` |
| 363 | } |
| 364 | if err := readJSON(r, &body); err != nil { |
| 365 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 366 | return |
| 367 | } |
| 368 | id, err := s.opRunPipeline(r.Context(), apiUser(r), repo, body.Ref, "api") |
| 369 | if !s.apiOK(w, r, err) { |
| 370 | return |
| 371 | } |
| 372 | if p, err := s.Store.PipelineByID(r.Context(), repo.ID, id); err == nil { |
| 373 | writeJSON(w, http.StatusCreated, s.pipeJSON(repo, p)) |
| 374 | return |
| 375 | } |
| 376 | writeJSON(w, http.StatusCreated, map[string]any{ |
| 377 | "id": id, "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(id, 10)), |
| 378 | }) |
| 379 | return |
| 380 | } |
| 381 | f, err := s.pipelineFilter(r, repo) |
| 382 | if !s.apiOK(w, r, err) { |
| 383 | return |
| 384 | } |
| 385 | list, err := s.Store.FindPipelines(r.Context(), repo.ID, f) |
| 386 | if !s.apiOK(w, r, err) { |
| 387 | return |
| 388 | } |
| 389 | page, per := pageQuery(r) |
| 390 | rows := make([]map[string]any, 0, len(list)) |
| 391 | for _, p := range list { |
| 392 | rows = append(rows, s.pipeJSON(repo, p)) |
| 393 | } |
| 394 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 395 | } |
| 396 | |
| 397 | func (s *Server) apiPipeline(w http.ResponseWriter, r *http.Request) { |
| 398 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 399 | if !ok { |
| 400 | return |
| 401 | } |
| 402 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 403 | if err != nil { |
| 404 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 405 | return |
| 406 | } |
| 407 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, id) |
| 408 | if err != nil { |
| 409 | s.writeAPIErr(w, r, notFound(err)) |
| 410 | return |
| 411 | } |
| 412 | writeJSON(w, http.StatusOK, s.pipeJSON(repo, p)) |
| 413 | } |
| 414 | |
| 415 | func (s *Server) apiRetryPipeline(w http.ResponseWriter, r *http.Request) { |
| 416 | repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper) |
| 417 | if !ok { |
| 418 | return |
| 419 | } |
| 420 | pipeID, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 421 | if err != nil { |
| 422 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 423 | return |
| 424 | } |
| 425 | var body struct { |
| 426 | Mode string `json:"mode"` |
| 427 | } |
| 428 | if err := readJSON(r, &body); err != nil { |
| 429 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 430 | return |
| 431 | } |
| 432 | id, err := s.opRetryPipeline(r.Context(), apiUser(r), repo, pipeID, body.Mode) |
| 433 | if !s.apiOK(w, r, err) { |
| 434 | return |
| 435 | } |
| 436 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, id) |
| 437 | if err != nil { |
| 438 | s.writeAPIErr(w, r, notFound(err)) |
| 439 | return |
| 440 | } |
| 441 | code := http.StatusOK |
| 442 | if body.Mode == "all" { |
| 443 | code = http.StatusCreated |
| 444 | } |
| 445 | writeJSON(w, code, s.pipeJSON(repo, p)) |
| 446 | } |
| 447 | |
| 448 | func (s *Server) apiCancelPipeline(w http.ResponseWriter, r *http.Request) { |
| 449 | repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper) |
| 450 | if !ok { |
| 451 | return |
| 452 | } |
| 453 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 454 | if err != nil { |
| 455 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 456 | return |
| 457 | } |
| 458 | if !s.apiOK(w, r, s.opCancelPipeline(r.Context(), apiUser(r), repo, id)) { |
| 459 | return |
| 460 | } |
| 461 | if p, err := s.Store.PipelineByID(r.Context(), repo.ID, id); err == nil { |
| 462 | writeJSON(w, http.StatusOK, s.pipeJSON(repo, p)) |
| 463 | return |
| 464 | } |
| 465 | writeJSON(w, http.StatusOK, map[string]any{ |
| 466 | "id": id, "status": "canceled", "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(id, 10)), |
| 467 | }) |
| 468 | } |
| 469 | |
| 470 | func (s *Server) apiJobs(w http.ResponseWriter, r *http.Request) { |
| 471 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 472 | if !ok { |
| 473 | return |
| 474 | } |
| 475 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 476 | if err != nil { |
| 477 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 478 | return |
| 479 | } |
| 480 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, id) |
| 481 | if err != nil { |
| 482 | s.writeAPIErr(w, r, notFound(err)) |
| 483 | return |
| 484 | } |
| 485 | jobs, err := s.Store.JobsByPipeline(r.Context(), p.ID) |
| 486 | if !s.apiOK(w, r, err) { |
| 487 | return |
| 488 | } |
| 489 | rows := make([]map[string]any, 0, len(jobs)) |
| 490 | for _, j := range jobs { |
| 491 | rows = append(rows, jobJSON(j)) |
| 492 | } |
| 493 | writeJSON(w, http.StatusOK, rows) |
| 494 | } |
| 495 | |
| 496 | func (s *Server) apiJob(w http.ResponseWriter, r *http.Request) { |
| 497 | _, job, ok := s.apiFindJob(w, r) |
| 498 | if !ok { |
| 499 | return |
| 500 | } |
| 501 | writeJSON(w, http.StatusOK, jobJSON(job)) |
| 502 | } |
| 503 | |
| 504 | func (s *Server) apiJobLog(w http.ResponseWriter, r *http.Request) { |
| 505 | _, job, ok := s.apiFindJob(w, r) |
| 506 | if !ok { |
| 507 | return |
| 508 | } |
| 509 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 510 | w.WriteHeader(http.StatusOK) |
| 511 | _, _ = w.Write([]byte(job.Log)) |
| 512 | } |
| 513 | |
| 514 | func (s *Server) apiJobAction(w http.ResponseWriter, r *http.Request, action string) { |
| 515 | repo, p, job, ok := s.apiFindJobFull(w, r, access.RoleDeveloper) |
| 516 | if !ok { |
| 517 | return |
| 518 | } |
| 519 | updated, err := s.opJobAction(r.Context(), apiUser(r), repo, p.ID, job.ID, action) |
| 520 | if !s.apiOK(w, r, err) { |
| 521 | return |
| 522 | } |
| 523 | writeJSON(w, http.StatusOK, jobJSON(updated)) |
| 524 | } |
| 525 | |
| 526 | func (s *Server) apiPlayJob(w http.ResponseWriter, r *http.Request) { s.apiJobAction(w, r, "play") } |
| 527 | func (s *Server) apiRetryJob(w http.ResponseWriter, r *http.Request) { |
| 528 | s.apiJobAction(w, r, "retry") |
| 529 | } |
| 530 | func (s *Server) apiCancelJob(w http.ResponseWriter, r *http.Request) { |
| 531 | s.apiJobAction(w, r, "cancel") |
| 532 | } |
| 533 | |
| 534 | func (s *Server) apiFindJob(w http.ResponseWriter, r *http.Request) (store.Pipeline, store.Job, bool) { |
| 535 | _, p, job, ok := s.apiFindJobFull(w, r, access.RoleReadonly) |
| 536 | return p, job, ok |
| 537 | } |
| 538 | |
| 539 | func (s *Server) apiFindJobFull(w http.ResponseWriter, r *http.Request, min access.Role) (store.Repo, store.Pipeline, store.Job, bool) { |
| 540 | repo, _, ok := s.apiRepo(w, r, min) |
| 541 | if !ok { |
| 542 | return store.Repo{}, store.Pipeline{}, store.Job{}, false |
| 543 | } |
| 544 | pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 545 | if err != nil { |
| 546 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 547 | return store.Repo{}, store.Pipeline{}, store.Job{}, false |
| 548 | } |
| 549 | jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64) |
| 550 | if err != nil { |
| 551 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 552 | return store.Repo{}, store.Pipeline{}, store.Job{}, false |
| 553 | } |
| 554 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid) |
| 555 | if err != nil { |
| 556 | s.writeAPIErr(w, r, notFound(err)) |
| 557 | return store.Repo{}, store.Pipeline{}, store.Job{}, false |
| 558 | } |
| 559 | jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) |
| 560 | for _, j := range jobs { |
| 561 | if j.ID == jid { |
| 562 | return repo, p, j, true |
| 563 | } |
| 564 | } |
| 565 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 566 | return store.Repo{}, store.Pipeline{}, store.Job{}, false |
| 567 | } |
| 568 | |
| 569 | func (s *Server) apiArtifacts(w http.ResponseWriter, r *http.Request) { |
| 570 | _, job, ok := s.apiFindJob(w, r) |
| 571 | if !ok { |
| 572 | return |
| 573 | } |
| 574 | list, err := s.Store.ListArtifacts(r.Context(), job.ID) |
| 575 | if !s.apiOK(w, r, err) { |
| 576 | return |
| 577 | } |
| 578 | rows := make([]map[string]any, 0, len(list)) |
| 579 | for _, a := range list { |
| 580 | rows = append(rows, map[string]any{"path": a.Path, "size": a.Size}) |
| 581 | } |
| 582 | writeJSON(w, http.StatusOK, rows) |
| 583 | } |
| 584 | |
| 585 | func (s *Server) apiArtifact(w http.ResponseWriter, r *http.Request) { |
| 586 | _, job, ok := s.apiFindJob(w, r) |
| 587 | if !ok { |
| 588 | return |
| 589 | } |
| 590 | want := r.PathValue("path") |
| 591 | if strings.Contains(want, "..") { |
| 592 | s.writeAPIErr(w, r, fail(400, i18n.E("err.sugFile"))) |
| 593 | return |
| 594 | } |
| 595 | list, err := s.Store.ListArtifacts(r.Context(), job.ID) |
| 596 | if !s.apiOK(w, r, err) { |
| 597 | return |
| 598 | } |
| 599 | for _, a := range list { |
| 600 | if a.Path == want { |
| 601 | http.ServeFile(w, r, a.StoragePath) |
| 602 | return |
| 603 | } |
| 604 | } |
| 605 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 606 | } |
| 607 | |
| 608 | func (s *Server) apiCIFile(w http.ResponseWriter, r *http.Request) { |
| 609 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 610 | if !ok { |
| 611 | return |
| 612 | } |
| 613 | if r.Method == http.MethodPut { |
| 614 | var body struct { |
| 615 | Branch string `json:"branch"` |
| 616 | Base string `json:"base"` |
| 617 | Message string `json:"message"` |
| 618 | Content string `json:"content"` |
| 619 | OpenMR bool `json:"open_mr"` |
| 620 | } |
| 621 | if err := readJSON(r, &body); err != nil { |
| 622 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 623 | return |
| 624 | } |
| 625 | sha, number, err := s.opSaveCI(r.Context(), apiUser(r), repo, body.Base, body.Branch, body.Message, body.Content, body.OpenMR) |
| 626 | if !s.apiOK(w, r, err) { |
| 627 | return |
| 628 | } |
| 629 | writeJSON(w, http.StatusOK, map[string]any{"id": sha, "merge_request": number}) |
| 630 | return |
| 631 | } |
| 632 | body, err := s.opCIFile(r.Context(), repo, r.URL.Query().Get("ref")) |
| 633 | if !s.apiOK(w, r, err) { |
| 634 | return |
| 635 | } |
| 636 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 637 | w.WriteHeader(http.StatusOK) |
| 638 | _, _ = w.Write([]byte(body)) |
| 639 | } |
| 640 | |
| 641 | func (s *Server) apiLintCI(w http.ResponseWriter, r *http.Request) { |
| 642 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 643 | if !ok { |
| 644 | return |
| 645 | } |
| 646 | var body struct { |
| 647 | Body string `json:"body"` |
| 648 | Ref string `json:"ref"` |
| 649 | } |
| 650 | if err := readJSON(r, &body); err != nil { |
| 651 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 652 | return |
| 653 | } |
| 654 | out, err := s.opLintCI(r, repo, body.Body, body.Ref) |
| 655 | if !s.apiOK(w, r, err) { |
| 656 | return |
| 657 | } |
| 658 | writeJSON(w, http.StatusOK, out) |
| 659 | } |
| 660 | |
| 661 | func (s *Server) apiPresets(w http.ResponseWriter, r *http.Request) { |
| 662 | set, err := images.Effective(s.Cfg.CI.ImageOverrides()) |
| 663 | if err != nil { |
| 664 | s.writeAPIErr(w, r, fail(500, err)) |
| 665 | return |
| 666 | } |
| 667 | rows := make([]map[string]any, 0, len(set)) |
| 668 | for _, p := range set { |
| 669 | rows = append(rows, map[string]any{"name": "bluegit/" + p.Name, "from": p.From, "runtime": p.Runtime, "tools": p.Tools, "direct": p.Direct}) |
| 670 | } |
| 671 | writeJSON(w, http.StatusOK, rows) |
| 672 | } |
| 673 | |
| 674 | func (s *Server) apiReleases(w http.ResponseWriter, r *http.Request) { |
| 675 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 676 | if !ok { |
| 677 | return |
| 678 | } |
| 679 | list, err := s.Store.ListReleases(r.Context(), repo.ID) |
| 680 | if !s.apiOK(w, r, err) { |
| 681 | return |
| 682 | } |
| 683 | page, per := pageQuery(r) |
| 684 | rows := make([]map[string]any, 0, len(list)) |
| 685 | for _, rel := range list { |
| 686 | assets := make([]map[string]any, 0, len(rel.Assets)) |
| 687 | for _, a := range rel.Assets { |
| 688 | assets = append(assets, map[string]any{"name": a.Name, "size": a.Size}) |
| 689 | } |
| 690 | rows = append(rows, map[string]any{ |
| 691 | "id": rel.ID, "tag": rel.Tag, "commit": rel.CommitSHA, "name": rel.Name, "body": rel.Body, |
| 692 | "created_at": rfcTime(rel.CreatedAt), "assets": assets, |
| 693 | }) |
| 694 | } |
| 695 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 696 | } |
| 697 | |
| 698 | func (s *Server) apiReleaseAsset(w http.ResponseWriter, r *http.Request) { |
| 699 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 700 | if !ok { |
| 701 | return |
| 702 | } |
| 703 | rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag")) |
| 704 | if err != nil { |
| 705 | s.writeAPIErr(w, r, notFound(err)) |
| 706 | return |
| 707 | } |
| 708 | name := r.PathValue("name") |
| 709 | for _, a := range rel.Assets { |
| 710 | if a.Name == name { |
| 711 | w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"") |
| 712 | http.ServeFile(w, r, a.StoragePath) |
| 713 | return |
| 714 | } |
| 715 | } |
| 716 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 717 | } |
| 718 | |
| 719 | // pipelineFilter leest sha, ref en mr uit de query. |
| 720 | // ref mag een branchnaam zijn (refs/heads/ en refs/tags/ worden geprobeerd) |
| 721 | // of een volledige ref. mr is het MR-nummer: pipelines van die MR plus |
| 722 | // pushes naar de bronbranch. |
| 723 | func (s *Server) pipelineFilter(r *http.Request, repo store.Repo) (store.PipelineFilter, error) { |
| 724 | q := r.URL.Query() |
| 725 | var f store.PipelineFilter |
| 726 | if sha := strings.TrimSpace(q.Get("sha")); sha != "" { |
| 727 | if len(sha) < 7 || len(sha) > 40 || strings.Trim(strings.ToLower(sha), "0123456789abcdef") != "" { |
| 728 | return f, fail(400, i18n.E("err.badSHA")) |
| 729 | } |
| 730 | f.SHA = sha |
| 731 | } |
| 732 | if ref := strings.TrimSpace(q.Get("ref")); ref != "" { |
| 733 | if strings.HasPrefix(ref, "refs/") { |
| 734 | f.Refs = []string{ref} |
| 735 | } else { |
| 736 | f.Refs = []string{"refs/heads/" + ref, "refs/tags/" + ref} |
| 737 | } |
| 738 | } |
| 739 | if raw := strings.TrimSpace(q.Get("mr")); raw != "" { |
| 740 | n, err := strconv.ParseInt(raw, 10, 64) |
| 741 | if err != nil || n < 1 { |
| 742 | return f, fail(400, i18n.E("err.notFound")) |
| 743 | } |
| 744 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 745 | if err != nil { |
| 746 | return f, notFound(err) |
| 747 | } |
| 748 | if len(f.Refs) > 0 { |
| 749 | return f, fail(400, i18n.E("err.pipeFilter")) |
| 750 | } |
| 751 | f.MRID = mr.ID |
| 752 | f.Refs = []string{"refs/heads/" + mr.SourceBranch} |
| 753 | } |
| 754 | return f, nil |
| 755 | } |