| 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/gitrepo" |
| 14 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 15 | "gitlab.com/Timfa/bluegit/internal/store" |
| 16 | ) |
| 17 | |
| 18 | type releaseRequest struct { |
| 19 | Tag string `json:"tag"` |
| 20 | Commit string `json:"commit"` |
| 21 | Name string `json:"name"` |
| 22 | Body string `json:"body"` |
| 23 | Assets []struct { |
| 24 | Job string `json:"job"` |
| 25 | Path string `json:"path"` |
| 26 | Name string `json:"name"` |
| 27 | } `json:"assets"` |
| 28 | } |
| 29 | |
| 30 | func (s *Server) createRelease(w http.ResponseWriter, r *http.Request) { |
| 31 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 32 | if err != nil { |
| 33 | s.writeAPIErr(w, r, fail(http.StatusNotFound, i18n.E("err.notFound"))) |
| 34 | return |
| 35 | } |
| 36 | token := tokenFrom(r) |
| 37 | var job store.Job |
| 38 | var author store.User |
| 39 | if strings.HasPrefix(token, "bgj_") { |
| 40 | job, err = s.Store.JobByToken(r.Context(), token) |
| 41 | if err != nil || job.RepoID != repo.ID || job.Status != "running" { |
| 42 | s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.jobToken"))) |
| 43 | return |
| 44 | } |
| 45 | } else { |
| 46 | author = apiUser(r) |
| 47 | if author.ID == 0 { |
| 48 | s.writeAPIErr(w, r, fail(http.StatusUnauthorized, i18n.E("err.apiAuth"))) |
| 49 | return |
| 50 | } |
| 51 | role, err := s.Store.EffectiveRole(r.Context(), author.ID, repo.ID) |
| 52 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 53 | s.writeAPIErr(w, r, fail(http.StatusForbidden, i18n.E("err.onlyAdmin"))) |
| 54 | return |
| 55 | } |
| 56 | } |
| 57 | var req releaseRequest |
| 58 | if err := readJSON(r, &req); err != nil { |
| 59 | s.writeAPIErr(w, r, fail(http.StatusBadRequest, i18n.E("err.json"))) |
| 60 | return |
| 61 | } |
| 62 | id, err := s.opPublishRelease(r.Context(), repo, author, job, req) |
| 63 | if !s.apiOK(w, r, err) { |
| 64 | return |
| 65 | } |
| 66 | writeJSON(w, http.StatusCreated, map[string]any{"id": id, "tag": req.Tag, "commit": req.Commit}) |
| 67 | } |
| 68 | |
| 69 | func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) { |
| 70 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 71 | if !ok { |
| 72 | return |
| 73 | } |
| 74 | rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag")) |
| 75 | if err != nil { |
| 76 | http.NotFound(w, r) |
| 77 | return |
| 78 | } |
| 79 | name := r.PathValue("name") |
| 80 | for _, a := range rel.Assets { |
| 81 | if a.Name == name { |
| 82 | w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"") |
| 83 | http.ServeFile(w, r, a.StoragePath) |
| 84 | return |
| 85 | } |
| 86 | } |
| 87 | http.NotFound(w, r) |
| 88 | } |
| 89 | |
| 90 | // jobLogAPI geeft de volledige console-output van een job, met geheimen gemaskeerd. |
| 91 | // Een persoonlijk token is verplicht, ook bij een publieke repo. Job-tokens tellen niet. |
| 92 | func (s *Server) jobLogAPI(w http.ResponseWriter, r *http.Request) { |
| 93 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 94 | if err != nil { |
| 95 | http.NotFound(w, r) |
| 96 | return |
| 97 | } |
| 98 | if !s.authorizeRepoRead(w, r, repo) { |
| 99 | return |
| 100 | } |
| 101 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 102 | if err != nil || id <= 0 { |
| 103 | http.NotFound(w, r) |
| 104 | return |
| 105 | } |
| 106 | job, err := s.Store.JobByID(r.Context(), repo.ID, id) |
| 107 | if err != nil { |
| 108 | http.NotFound(w, r) |
| 109 | return |
| 110 | } |
| 111 | text := ci.Mask(job.Log, s.maskSecrets(r.Context(), repo)) |
| 112 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 113 | w.Header().Set("Cache-Control", "no-store") |
| 114 | _, _ = w.Write([]byte(text)) |
| 115 | } |
| 116 | |
| 117 | // authorizeRepoRead eist een persoonlijk token. Op een publieke repo volstaat |
| 118 | // elk geldig token. Op een privérepo moet het token de repo mogen lezen. |
| 119 | func (s *Server) authorizeRepoRead(w http.ResponseWriter, r *http.Request, repo store.Repo) bool { |
| 120 | token := bearer(r) |
| 121 | if token == "" { |
| 122 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 123 | return false |
| 124 | } |
| 125 | if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") { |
| 126 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 127 | return false |
| 128 | } |
| 129 | u, err := s.Store.UserByToken(r.Context(), token) |
| 130 | if err != nil { |
| 131 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 132 | return false |
| 133 | } |
| 134 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 135 | if err != nil { |
| 136 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 137 | return false |
| 138 | } |
| 139 | if !role.CanRead() && repo.Visibility != "public" { |
| 140 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 141 | return false |
| 142 | } |
| 143 | return true |
| 144 | } |
| 145 | |
| 146 | // commitDiffAPI geeft de unified diff van één commit. |
| 147 | func (s *Server) commitDiffAPI(w http.ResponseWriter, r *http.Request) { |
| 148 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 149 | if err != nil { |
| 150 | http.NotFound(w, r) |
| 151 | return |
| 152 | } |
| 153 | if !s.authorizeRepoRead(w, r, repo) { |
| 154 | return |
| 155 | } |
| 156 | sha := r.PathValue("sha") |
| 157 | if !safeSHA(sha) || s.Git == "" || s.StoragePath() == "" { |
| 158 | http.NotFound(w, r) |
| 159 | return |
| 160 | } |
| 161 | text, err := gitrepo.CommitDiff(r.Context(), s.Git, gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name), sha) |
| 162 | if err != nil { |
| 163 | http.NotFound(w, r) |
| 164 | return |
| 165 | } |
| 166 | writePlain(w, text) |
| 167 | } |
| 168 | |
| 169 | // mrDiffAPI geeft de three-dot diff van een merge request. |
| 170 | func (s *Server) mrDiffAPI(w http.ResponseWriter, r *http.Request) { |
| 171 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 172 | if err != nil { |
| 173 | http.NotFound(w, r) |
| 174 | return |
| 175 | } |
| 176 | if !s.authorizeRepoRead(w, r, repo) { |
| 177 | return |
| 178 | } |
| 179 | number, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 180 | if err != nil || number <= 0 || s.Git == "" || s.StoragePath() == "" { |
| 181 | http.NotFound(w, r) |
| 182 | return |
| 183 | } |
| 184 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, number) |
| 185 | if err != nil || safeRev(mr.SourceBranch, "") != mr.SourceBranch || safeRev(mr.TargetBranch, "") != mr.TargetBranch { |
| 186 | http.NotFound(w, r) |
| 187 | return |
| 188 | } |
| 189 | text, err := gitrepo.CompareDiff(r.Context(), s.Git, gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name), mr.TargetBranch, mr.SourceBranch) |
| 190 | if err != nil { |
| 191 | http.NotFound(w, r) |
| 192 | return |
| 193 | } |
| 194 | writePlain(w, text) |
| 195 | } |
| 196 | |
| 197 | func writePlain(w http.ResponseWriter, text string) { |
| 198 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 199 | w.Header().Set("Cache-Control", "no-store") |
| 200 | _, _ = w.Write([]byte(text)) |
| 201 | } |
| 202 | |
| 203 | func bearer(r *http.Request) string { |
| 204 | h := r.Header.Get("Authorization") |
| 205 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 206 | return strings.TrimSpace(h[7:]) |
| 207 | } |
| 208 | return "" |
| 209 | } |
| 210 | |
| 211 | func copyRelease(src, dst string) error { |
| 212 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 213 | return err |
| 214 | } |
| 215 | in, err := os.Open(src) |
| 216 | if err != nil { |
| 217 | return err |
| 218 | } |
| 219 | defer in.Close() |
| 220 | out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 221 | if err != nil { |
| 222 | return err |
| 223 | } |
| 224 | defer out.Close() |
| 225 | _, err = io.Copy(out, in) |
| 226 | return err |
| 227 | } |