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