BlueGit

timfa bluegit

Files

bluegit / internal / server / api_flow.go

api_flow.go

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