BlueGit

timfa bluegit

Files

bluegit / internal / server / api_flow.go

api_flow.go

feat: beschrijf de API-objecten en geef ze een web_url
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 2f39b617 History
api_flow.go 19.3 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) apiCancelPipeline(w http.ResponseWriter, r *http.Request) {
398 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
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 if !s.apiOK(w, r, s.opCancelPipeline(r.Context(), apiUser(r), repo, id)) {
408 return
409 }
410 if p, err := s.Store.PipelineByID(r.Context(), repo.ID, id); err == nil {
411 writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))
412 return
413 }
414 writeJSON(w, http.StatusOK, map[string]any{
415 "id": id, "status": "canceled", "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(id, 10)),
416 })
417}
418
419func (s *Server) apiJobs(w http.ResponseWriter, r *http.Request) {
420 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
421 if !ok {
422 return
423 }
424 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
425 if err != nil {
426 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
427 return
428 }
429 p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)
430 if err != nil {
431 s.writeAPIErr(w, r, notFound(err))
432 return
433 }
434 jobs, err := s.Store.JobsByPipeline(r.Context(), p.ID)
435 if !s.apiOK(w, r, err) {
436 return
437 }
438 rows := make([]map[string]any, 0, len(jobs))
439 for _, j := range jobs {
440 rows = append(rows, jobJSON(j))
441 }
442 writeJSON(w, http.StatusOK, rows)
443}
444
445func (s *Server) apiJob(w http.ResponseWriter, r *http.Request) {
446 _, job, ok := s.apiFindJob(w, r)
447 if !ok {
448 return
449 }
450 writeJSON(w, http.StatusOK, jobJSON(job))
451}
452
453func (s *Server) apiJobLog(w http.ResponseWriter, r *http.Request) {
454 _, job, ok := s.apiFindJob(w, r)
455 if !ok {
456 return
457 }
458 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
459 w.WriteHeader(http.StatusOK)
460 _, _ = w.Write([]byte(job.Log))
461}
462
463func (s *Server) apiJobAction(w http.ResponseWriter, r *http.Request, action string) {
464 repo, p, job, ok := s.apiFindJobFull(w, r, access.RoleDeveloper)
465 if !ok {
466 return
467 }
468 updated, err := s.opJobAction(r.Context(), apiUser(r), repo, p.ID, job.ID, action)
469 if !s.apiOK(w, r, err) {
470 return
471 }
472 writeJSON(w, http.StatusOK, jobJSON(updated))
473}
474
475func (s *Server) apiPlayJob(w http.ResponseWriter, r *http.Request) { s.apiJobAction(w, r, "play") }
476func (s *Server) apiRetryJob(w http.ResponseWriter, r *http.Request) {
477 s.apiJobAction(w, r, "retry")
478}
479func (s *Server) apiCancelJob(w http.ResponseWriter, r *http.Request) {
480 s.apiJobAction(w, r, "cancel")
481}
482
483func (s *Server) apiFindJob(w http.ResponseWriter, r *http.Request) (store.Pipeline, store.Job, bool) {
484 _, p, job, ok := s.apiFindJobFull(w, r, access.RoleReadonly)
485 return p, job, ok
486}
487
488func (s *Server) apiFindJobFull(w http.ResponseWriter, r *http.Request, min access.Role) (store.Repo, store.Pipeline, store.Job, bool) {
489 repo, _, ok := s.apiRepo(w, r, min)
490 if !ok {
491 return store.Repo{}, store.Pipeline{}, store.Job{}, false
492 }
493 pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
494 if err != nil {
495 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
496 return store.Repo{}, store.Pipeline{}, store.Job{}, false
497 }
498 jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64)
499 if err != nil {
500 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
501 return store.Repo{}, store.Pipeline{}, store.Job{}, false
502 }
503 p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid)
504 if err != nil {
505 s.writeAPIErr(w, r, notFound(err))
506 return store.Repo{}, store.Pipeline{}, store.Job{}, false
507 }
508 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
509 for _, j := range jobs {
510 if j.ID == jid {
511 return repo, p, j, true
512 }
513 }
514 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
515 return store.Repo{}, store.Pipeline{}, store.Job{}, false
516}
517
518func (s *Server) apiArtifacts(w http.ResponseWriter, r *http.Request) {
519 _, job, ok := s.apiFindJob(w, r)
520 if !ok {
521 return
522 }
523 list, err := s.Store.ListArtifacts(r.Context(), job.ID)
524 if !s.apiOK(w, r, err) {
525 return
526 }
527 rows := make([]map[string]any, 0, len(list))
528 for _, a := range list {
529 rows = append(rows, map[string]any{"path": a.Path, "size": a.Size})
530 }
531 writeJSON(w, http.StatusOK, rows)
532}
533
534func (s *Server) apiArtifact(w http.ResponseWriter, r *http.Request) {
535 _, job, ok := s.apiFindJob(w, r)
536 if !ok {
537 return
538 }
539 want := r.PathValue("path")
540 if strings.Contains(want, "..") {
541 s.writeAPIErr(w, r, fail(400, i18n.E("err.sugFile")))
542 return
543 }
544 list, err := s.Store.ListArtifacts(r.Context(), job.ID)
545 if !s.apiOK(w, r, err) {
546 return
547 }
548 for _, a := range list {
549 if a.Path == want {
550 http.ServeFile(w, r, a.StoragePath)
551 return
552 }
553 }
554 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
555}
556
557func (s *Server) apiCIFile(w http.ResponseWriter, r *http.Request) {
558 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
559 if !ok {
560 return
561 }
562 if r.Method == http.MethodPut {
563 var body struct {
564 Branch string `json:"branch"`
565 Base string `json:"base"`
566 Message string `json:"message"`
567 Content string `json:"content"`
568 OpenMR bool `json:"open_mr"`
569 }
570 if err := readJSON(r, &body); err != nil {
571 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
572 return
573 }
574 sha, number, err := s.opSaveCI(r.Context(), apiUser(r), repo, body.Base, body.Branch, body.Message, body.Content, body.OpenMR)
575 if !s.apiOK(w, r, err) {
576 return
577 }
578 writeJSON(w, http.StatusOK, map[string]any{"id": sha, "merge_request": number})
579 return
580 }
581 body, err := s.opCIFile(r.Context(), repo, r.URL.Query().Get("ref"))
582 if !s.apiOK(w, r, err) {
583 return
584 }
585 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
586 w.WriteHeader(http.StatusOK)
587 _, _ = w.Write([]byte(body))
588}
589
590func (s *Server) apiLintCI(w http.ResponseWriter, r *http.Request) {
591 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
592 if !ok {
593 return
594 }
595 var body struct {
596 Body string `json:"body"`
597 Ref string `json:"ref"`
598 }
599 if err := readJSON(r, &body); err != nil {
600 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
601 return
602 }
603 out, err := s.opLintCI(r, repo, body.Body, body.Ref)
604 if !s.apiOK(w, r, err) {
605 return
606 }
607 writeJSON(w, http.StatusOK, out)
608}
609
610func (s *Server) apiPresets(w http.ResponseWriter, r *http.Request) {
611 set, err := images.Effective(s.Cfg.CI.ImageOverrides())
612 if err != nil {
613 s.writeAPIErr(w, r, fail(500, err))
614 return
615 }
616 rows := make([]map[string]any, 0, len(set))
617 for _, p := range set {
618 rows = append(rows, map[string]any{"name": "bluegit/" + p.Name, "from": p.From, "runtime": p.Runtime, "tools": p.Tools, "direct": p.Direct})
619 }
620 writeJSON(w, http.StatusOK, rows)
621}
622
623func (s *Server) apiReleases(w http.ResponseWriter, r *http.Request) {
624 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
625 if !ok {
626 return
627 }
628 list, err := s.Store.ListReleases(r.Context(), repo.ID)
629 if !s.apiOK(w, r, err) {
630 return
631 }
632 page, per := pageQuery(r)
633 rows := make([]map[string]any, 0, len(list))
634 for _, rel := range list {
635 assets := make([]map[string]any, 0, len(rel.Assets))
636 for _, a := range rel.Assets {
637 assets = append(assets, map[string]any{"name": a.Name, "size": a.Size})
638 }
639 rows = append(rows, map[string]any{
640 "id": rel.ID, "tag": rel.Tag, "commit": rel.CommitSHA, "name": rel.Name, "body": rel.Body,
641 "created_at": rfcTime(rel.CreatedAt), "assets": assets,
642 })
643 }
644 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
645}
646
647func (s *Server) apiReleaseAsset(w http.ResponseWriter, r *http.Request) {
648 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
649 if !ok {
650 return
651 }
652 rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag"))
653 if err != nil {
654 s.writeAPIErr(w, r, notFound(err))
655 return
656 }
657 name := r.PathValue("name")
658 for _, a := range rel.Assets {
659 if a.Name == name {
660 w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"")
661 http.ServeFile(w, r, a.StoragePath)
662 return
663 }
664 }
665 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
666}