| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 13 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 14 | "gitlab.com/Timfa/bluegit/internal/names" |
| 15 | "gitlab.com/Timfa/bluegit/internal/store" |
| 16 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 17 | ) |
| 18 | |
| 19 | type releaseRequest struct { |
| 20 | Tag string `json:"tag"` |
| 21 | Commit string `json:"commit"` |
| 22 | Name string `json:"name"` |
| 23 | Body string `json:"body"` |
| 24 | Assets []struct { |
| 25 | Job string `json:"job"` |
| 26 | Path string `json:"path"` |
| 27 | Name string `json:"name"` |
| 28 | } `json:"assets"` |
| 29 | } |
| 30 | |
| 31 | func (s *Server) createRelease(w http.ResponseWriter, r *http.Request) { |
| 32 | if s.StoragePath() == "" { |
| 33 | http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) |
| 34 | return |
| 35 | } |
| 36 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 37 | if err != nil { |
| 38 | http.NotFound(w, r) |
| 39 | return |
| 40 | } |
| 41 | token := bearer(r) |
| 42 | var job store.Job |
| 43 | var author int64 |
| 44 | var actor store.User |
| 45 | if strings.HasPrefix(token, "bgj_") { |
| 46 | job, err = s.Store.JobByToken(r.Context(), token) |
| 47 | if err != nil || job.RepoID != repo.ID || job.Status != "running" { |
| 48 | http.Error(w, s.tr(r, "err.jobToken"), http.StatusUnauthorized) |
| 49 | return |
| 50 | } |
| 51 | } else if strings.HasPrefix(token, "bg_") { |
| 52 | u, err := s.Store.UserByToken(r.Context(), token) |
| 53 | if err != nil { |
| 54 | http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized) |
| 55 | return |
| 56 | } |
| 57 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 58 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 59 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 60 | return |
| 61 | } |
| 62 | author = u.ID |
| 63 | actor = u |
| 64 | } else { |
| 65 | http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized) |
| 66 | return |
| 67 | } |
| 68 | var req releaseRequest |
| 69 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&req); err != nil { |
| 70 | http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest) |
| 71 | return |
| 72 | } |
| 73 | if err := names.ValidTag(req.Tag); err != nil { |
| 74 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 75 | return |
| 76 | } |
| 77 | if !safeSHA(req.Commit) { |
| 78 | http.Error(w, s.tr(r, "err.commitAbsent"), http.StatusBadRequest) |
| 79 | return |
| 80 | } |
| 81 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 82 | if !gitrepo.HasCommit(r.Context(), s.Git, dir, req.Commit) { |
| 83 | http.Error(w, s.tr(r, "err.commitMissing"), http.StatusBadRequest) |
| 84 | return |
| 85 | } |
| 86 | if req.Name == "" { |
| 87 | req.Name = req.Tag |
| 88 | } |
| 89 | if job.ID == 0 && len(req.Assets) > 0 { |
| 90 | http.Error(w, s.tr(r, "err.assetsJob"), http.StatusBadRequest) |
| 91 | return |
| 92 | } |
| 93 | var assets []store.ReleaseAsset |
| 94 | for _, a := range req.Assets { |
| 95 | if a.Name == "" { |
| 96 | a.Name = filepath.Base(a.Path) |
| 97 | } |
| 98 | if err := names.ValidRepoName(strings.ReplaceAll(a.Name, ".", "x")); err != nil && strings.Contains(a.Name, "..") { |
| 99 | http.Error(w, s.tr(r, "err.assetName"), http.StatusBadRequest) |
| 100 | return |
| 101 | } |
| 102 | if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" { |
| 103 | http.Error(w, s.tr(r, "err.assetName"), http.StatusBadRequest) |
| 104 | return |
| 105 | } |
| 106 | src, err := s.Store.ArtifactPath(r.Context(), job.PipelineID, a.Job, a.Path) |
| 107 | if err != nil { |
| 108 | http.Error(w, s.tr(r, "err.artifactMissing", a.Job, a.Path), http.StatusBadRequest) |
| 109 | return |
| 110 | } |
| 111 | info, err := os.Stat(src) |
| 112 | if err != nil { |
| 113 | http.Error(w, s.tr(r, "err.assetDisk"), http.StatusBadRequest) |
| 114 | return |
| 115 | } |
| 116 | dst := filepath.Join(s.StoragePath(), "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name) |
| 117 | if err := copyRelease(src, dst); err != nil { |
| 118 | http.Error(w, s.tr(r, "err.assetCopy"), http.StatusInternalServerError) |
| 119 | return |
| 120 | } |
| 121 | assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: info.Size()}) |
| 122 | } |
| 123 | if err := s.postReceive(r.Context(), repo, actor, dir, func() error { |
| 124 | return gitrepo.Tag(r.Context(), s.Git, dir, req.Tag, req.Commit, req.Name) |
| 125 | }); err != nil { |
| 126 | http.Error(w, s.tr(r, "err.tagFail", err.Error()), http.StatusBadRequest) |
| 127 | return |
| 128 | } |
| 129 | id, err := s.Store.CreateRelease(r.Context(), repo.ID, req.Tag, req.Commit, req.Name, req.Body, author, job.ID, assets) |
| 130 | if err != nil { |
| 131 | http.Error(w, s.trErr(r, err), http.StatusBadRequest) |
| 132 | return |
| 133 | } |
| 134 | s.Dispatch(repo.ID, webhook.Event{ |
| 135 | Type: webhook.EventRelease, Project: repo.FullPath(), Commit: req.Commit, |
| 136 | ReleaseTag: req.Tag, Summary: i18n.T(i18n.EN, "hook.release", req.Tag, repo.FullPath()), |
| 137 | URL: strings.TrimRight(s.Cfg.BaseURL, "/") + repoURL(repo) + "/-/releases", |
| 138 | }) |
| 139 | w.Header().Set("Content-Type", "application/json") |
| 140 | w.WriteHeader(http.StatusCreated) |
| 141 | _ = json.NewEncoder(w).Encode(map[string]any{"id": id, "tag": req.Tag, "commit": req.Commit}) |
| 142 | } |
| 143 | |
| 144 | func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) { |
| 145 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 146 | if !ok { |
| 147 | return |
| 148 | } |
| 149 | rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag")) |
| 150 | if err != nil { |
| 151 | http.NotFound(w, r) |
| 152 | return |
| 153 | } |
| 154 | name := r.PathValue("name") |
| 155 | for _, a := range rel.Assets { |
| 156 | if a.Name == name { |
| 157 | w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"") |
| 158 | http.ServeFile(w, r, a.StoragePath) |
| 159 | return |
| 160 | } |
| 161 | } |
| 162 | http.NotFound(w, r) |
| 163 | } |
| 164 | |
| 165 | func bearer(r *http.Request) string { |
| 166 | h := r.Header.Get("Authorization") |
| 167 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 168 | return strings.TrimSpace(h[7:]) |
| 169 | } |
| 170 | return "" |
| 171 | } |
| 172 | |
| 173 | func copyRelease(src, dst string) error { |
| 174 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | in, err := os.Open(src) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | defer in.Close() |
| 182 | out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | defer out.Close() |
| 187 | _, err = io.Copy(out, in) |
| 188 | return err |
| 189 | } |