| 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/names" |
| 14 | "gitlab.com/Timfa/bluegit/internal/store" |
| 15 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 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 | http.NotFound(w, r) |
| 34 | return |
| 35 | } |
| 36 | token := bearer(r) |
| 37 | var job store.Job |
| 38 | var author int64 |
| 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 | http.Error(w, "job-token ongeldig", http.StatusUnauthorized) |
| 43 | return |
| 44 | } |
| 45 | } else if strings.HasPrefix(token, "bg_") { |
| 46 | u, err := s.Store.UserByToken(r.Context(), token) |
| 47 | if err != nil { |
| 48 | http.Error(w, "token ongeldig", http.StatusUnauthorized) |
| 49 | return |
| 50 | } |
| 51 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 52 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 53 | http.Error(w, "alleen een beheerder", http.StatusForbidden) |
| 54 | return |
| 55 | } |
| 56 | author = u.ID |
| 57 | } else { |
| 58 | http.Error(w, "token ontbreekt", http.StatusUnauthorized) |
| 59 | return |
| 60 | } |
| 61 | var req releaseRequest |
| 62 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&req); err != nil { |
| 63 | http.Error(w, "json", http.StatusBadRequest) |
| 64 | return |
| 65 | } |
| 66 | if err := names.ValidTag(req.Tag); err != nil { |
| 67 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 68 | return |
| 69 | } |
| 70 | if !safeSHA(req.Commit) { |
| 71 | http.Error(w, "commit ontbreekt", http.StatusBadRequest) |
| 72 | return |
| 73 | } |
| 74 | dir := gitrepo.Dir(s.Cfg.StoragePath, repo.OwnerPath, repo.Name) |
| 75 | if !gitrepo.HasCommit(r.Context(), s.Git, dir, req.Commit) { |
| 76 | http.Error(w, "commit niet in deze repository", http.StatusBadRequest) |
| 77 | return |
| 78 | } |
| 79 | if req.Name == "" { |
| 80 | req.Name = req.Tag |
| 81 | } |
| 82 | if job.ID == 0 && len(req.Assets) > 0 { |
| 83 | http.Error(w, "assets horen bij een job-token", http.StatusBadRequest) |
| 84 | return |
| 85 | } |
| 86 | var assets []store.ReleaseAsset |
| 87 | for _, a := range req.Assets { |
| 88 | if a.Name == "" { |
| 89 | a.Name = filepath.Base(a.Path) |
| 90 | } |
| 91 | if err := names.ValidRepoName(strings.ReplaceAll(a.Name, ".", "x")); err != nil && strings.Contains(a.Name, "..") { |
| 92 | http.Error(w, "assetnaam", http.StatusBadRequest) |
| 93 | return |
| 94 | } |
| 95 | if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" { |
| 96 | http.Error(w, "assetnaam", http.StatusBadRequest) |
| 97 | return |
| 98 | } |
| 99 | src, err := s.Store.ArtifactPath(r.Context(), job.PipelineID, a.Job, a.Path) |
| 100 | if err != nil { |
| 101 | http.Error(w, "artifact niet gevonden: "+a.Job+" "+a.Path, http.StatusBadRequest) |
| 102 | return |
| 103 | } |
| 104 | info, err := os.Stat(src) |
| 105 | if err != nil { |
| 106 | http.Error(w, "artifact ontbreekt op schijf", http.StatusBadRequest) |
| 107 | return |
| 108 | } |
| 109 | dst := filepath.Join(s.Cfg.StoragePath, "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name) |
| 110 | if err := copyRelease(src, dst); err != nil { |
| 111 | http.Error(w, "asset kopiëren", http.StatusInternalServerError) |
| 112 | return |
| 113 | } |
| 114 | assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: info.Size()}) |
| 115 | } |
| 116 | if err := gitrepo.Tag(r.Context(), s.Git, dir, req.Tag, req.Commit, req.Name); err != nil { |
| 117 | http.Error(w, "tag: "+err.Error(), http.StatusBadRequest) |
| 118 | return |
| 119 | } |
| 120 | id, err := s.Store.CreateRelease(r.Context(), repo.ID, req.Tag, req.Commit, req.Name, req.Body, author, job.ID, assets) |
| 121 | if err != nil { |
| 122 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 123 | return |
| 124 | } |
| 125 | s.Dispatch(repo.ID, webhook.Event{ |
| 126 | Type: webhook.EventRelease, Project: repo.FullPath(), Commit: req.Commit, |
| 127 | ReleaseTag: req.Tag, Summary: "release " + req.Tag + " van " + repo.FullPath() + " gepubliceerd", |
| 128 | URL: strings.TrimRight(s.Cfg.BaseURL, "/") + repoURL(repo) + "/-/releases", |
| 129 | }) |
| 130 | w.Header().Set("Content-Type", "application/json") |
| 131 | w.WriteHeader(http.StatusCreated) |
| 132 | _ = json.NewEncoder(w).Encode(map[string]any{"id": id, "tag": req.Tag, "commit": req.Commit}) |
| 133 | } |
| 134 | |
| 135 | func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) { |
| 136 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 137 | if !ok { |
| 138 | return |
| 139 | } |
| 140 | rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag")) |
| 141 | if err != nil { |
| 142 | http.NotFound(w, r) |
| 143 | return |
| 144 | } |
| 145 | name := r.PathValue("name") |
| 146 | for _, a := range rel.Assets { |
| 147 | if a.Name == name { |
| 148 | w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"") |
| 149 | http.ServeFile(w, r, a.StoragePath) |
| 150 | return |
| 151 | } |
| 152 | } |
| 153 | http.NotFound(w, r) |
| 154 | } |
| 155 | |
| 156 | func bearer(r *http.Request) string { |
| 157 | h := r.Header.Get("Authorization") |
| 158 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 159 | return strings.TrimSpace(h[7:]) |
| 160 | } |
| 161 | return "" |
| 162 | } |
| 163 | |
| 164 | func copyRelease(src, dst string) error { |
| 165 | if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | in, err := os.Open(src) |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | defer in.Close() |
| 173 | out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | defer out.Close() |
| 178 | _, err = io.Copy(out, in) |
| 179 | return err |
| 180 | } |