BlueGit

timfa bluegit

Files

bluegit / internal / server / api.go

api.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
api.go 2.8 KiB Download Raw
1package server
2
3import (
4 "io"
5 "net/http"
6 "os"
7 "path/filepath"
8 "strings"
9
10 "gitlab.com/Timfa/bluegit/internal/access"
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13)
14
15type releaseRequest struct {
16 Tag string `json:"tag"`
17 Commit string `json:"commit"`
18 Name string `json:"name"`
19 Body string `json:"body"`
20 Assets []struct {
21 Job string `json:"job"`
22 Path string `json:"path"`
23 Name string `json:"name"`
24 } `json:"assets"`
25}
26
27func (s *Server) createRelease(w http.ResponseWriter, r *http.Request) {
28 repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo"))
29 if err != nil {
30 s.writeAPIErr(w, r, fail(http.StatusNotFound, i18n.E("err.notFound")))
31 return
32 }
33 token := tokenFrom(r)
34 var job store.Job
35 var author store.User
36 if strings.HasPrefix(token, "bgj_") {
37 job, err = s.Store.JobByToken(r.Context(), token)
38 if err != nil || job.RepoID != repo.ID || job.Status != "running" {
39 s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.jobToken")))
40 return
41 }
42 } else {
43 author = apiUser(r)
44 if author.ID == 0 {
45 s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.apiAuth")))
46 return
47 }
48 role, err := s.Store.EffectiveRole(r.Context(), author.ID, repo.ID)
49 if err != nil || !role.AtLeast(access.RoleMaintainer) {
50 s.writeAPIErr(w, r, fail(http.StatusForbidden, i18n.E("err.onlyAdmin")))
51 return
52 }
53 }
54 var req releaseRequest
55 if err := readJSON(r, &req); err != nil {
56 s.writeAPIErr(w, r, fail(http.StatusBadRequest, i18n.E("err.json")))
57 return
58 }
59 id, err := s.opPublishRelease(r.Context(), repo, author, job, req)
60 if !s.apiOK(w, r, err) {
61 return
62 }
63 writeJSON(w, http.StatusCreated, map[string]any{"id": id, "tag": req.Tag, "commit": req.Commit})
64}
65
66func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) {
67 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
68 if !ok {
69 return
70 }
71 rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag"))
72 if err != nil {
73 http.NotFound(w, r)
74 return
75 }
76 name := r.PathValue("name")
77 for _, a := range rel.Assets {
78 if a.Name == name {
79 w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"")
80 http.ServeFile(w, r, a.StoragePath)
81 return
82 }
83 }
84 http.NotFound(w, r)
85}
86
87func bearer(r *http.Request) string {
88 h := r.Header.Get("Authorization")
89 if strings.HasPrefix(strings.ToLower(h), "bearer ") {
90 return strings.TrimSpace(h[7:])
91 }
92 return ""
93}
94
95func copyRelease(src, dst string) error {
96 if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
97 return err
98 }
99 in, err := os.Open(src)
100 if err != nil {
101 return err
102 }
103 defer in.Close()
104 out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
105 if err != nil {
106 return err
107 }
108 defer out.Close()
109 _, err = io.Copy(out, in)
110 return err
111}