| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 13 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 14 | "gitlab.com/Timfa/bluegit/internal/store" |
| 15 | ) |
| 16 | |
| 17 | type releaseRequest struct { |
| 18 | Tag string `json:"tag"` |
| 19 | Commit string `json:"commit"` |
| 20 | Name string `json:"name"` |
| 21 | Body string `json:"body"` |
| 22 | Assets []struct { |
| 23 | Job string `json:"job"` |
| 24 | Path string `json:"path"` |
| 25 | Name string `json:"name"` |
| 26 | } `json:"assets"` |
| 27 | } |
| 28 | |
| 29 | func (s *Server) createRelease(w http.ResponseWriter, r *http.Request) { |
| 30 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 31 | if err != nil { |
| 32 | s.writeAPIErr(w, r, fail(http.StatusNotFound, i18n.E("err.notFound"))) |
| 33 | return |
| 34 | } |
| 35 | token := tokenFrom(r) |
| 36 | var job store.Job |
| 37 | var author store.User |
| 38 | if strings.HasPrefix(token, "bgj_") { |
| 39 | job, err = s.Store.JobByToken(r.Context(), token) |
| 40 | if err != nil || job.RepoID != repo.ID || job.Status != "running" { |
| 41 | s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.jobToken"))) |
| 42 | return |
| 43 | } |
| 44 | } else { |
| 45 | author = apiUser(r) |
| 46 | if author.ID == 0 { |
| 47 | s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.apiAuth"))) |
| 48 | return |
| 49 | } |
| 50 | role, err := s.Store.EffectiveRole(r.Context(), author.ID, repo.ID) |
| 51 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 52 | s.writeAPIErr(w, r, fail(http.StatusForbidden, i18n.E("err.onlyAdmin"))) |
| 53 | return |
| 54 | } |
| 55 | } |
| 56 | var req releaseRequest |
| 57 | if err := readJSON(r, &req); err != nil { |
| 58 | s.writeAPIErr(w, r, fail(http.StatusBadRequest, i18n.E("err.json"))) |
| 59 | return |
| 60 | } |
| 61 | id, err := s.opPublishRelease(r.Context(), repo, author, job, req) |
| 62 | if !s.apiOK(w, r, err) { |
| 63 | return |
| 64 | } |
| 65 | writeJSON(w, http.StatusCreated, map[string]any{"id": id, "tag": req.Tag, "commit": req.Commit}) |
| 66 | } |
| 67 | |
| 68 | func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) { |
| 69 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 70 | if !ok { |
| 71 | return |
| 72 | } |
| 73 | rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag")) |
| 74 | if err != nil { |
| 75 | http.NotFound(w, r) |
| 76 | return |
| 77 | } |
| 78 | name := r.PathValue("name") |
| 79 | for _, a := range rel.Assets { |
| 80 | if a.Name == name { |
| 81 | w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"") |
| 82 | http.ServeFile(w, r, a.StoragePath) |
| 83 | return |
| 84 | } |
| 85 | } |
| 86 | http.NotFound(w, r) |
| 87 | } |
| 88 | |
| 89 | // jobLogAPI geeft de volledige console-output van een job, met geheimen gemaskeerd. |
| 90 | // Een persoonlijk token is verplicht, ook bij een publieke repo. Job-tokens tellen niet. |
| 91 | func (s *Server) jobLogAPI(w http.ResponseWriter, r *http.Request) { |
| 92 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 93 | if err != nil { |
| 94 | http.NotFound(w, r) |
| 95 | return |
| 96 | } |
| 97 | token := bearer(r) |
| 98 | if token == "" { |
| 99 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 100 | return |
| 101 | } |
| 102 | if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") { |
| 103 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 104 | return |
| 105 | } |
| 106 | u, err := s.Store.UserByToken(r.Context(), token) |
| 107 | if err != nil { |
| 108 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 109 | return |
| 110 | } |
| 111 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 112 | if err != nil { |
| 113 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 114 | return |
| 115 | } |
| 116 | if !role.CanRead() && repo.Visibility != "public" { |
| 117 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 118 | return |
| 119 | } |
| 120 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 121 | if err != nil || id <= 0 { |
| 122 | http.NotFound(w, r) |
| 123 | return |
| 124 | } |
| 125 | job, err := s.Store.JobByID(r.Context(), repo.ID, id) |
| 126 | if err != nil { |
| 127 | http.NotFound(w, r) |
| 128 | return |
| 129 | } |
| 130 | text := ci.Mask(job.Log, s.maskSecrets(r.Context(), repo)) |
| 131 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 132 | w.Header().Set("Cache-Control", "no-store") |
| 133 | _, _ = w.Write([]byte(text)) |
| 134 | } |
| 135 | |
| 136 | func bearer(r *http.Request) string { |
| 137 | h := r.Header.Get("Authorization") |
| 138 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 139 | return strings.TrimSpace(h[7:]) |
| 140 | } |
| 141 | return "" |
| 142 | } |
| 143 | |
| 144 | func copyRelease(src, dst string) error { |
| 145 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 146 | return err |
| 147 | } |
| 148 | in, err := os.Open(src) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | defer in.Close() |
| 153 | out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | defer out.Close() |
| 158 | _, err = io.Copy(out, in) |
| 159 | return err |
| 160 | } |