| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/access" |
| 11 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 12 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 13 | "gitlab.com/Timfa/bluegit/internal/names" |
| 14 | "gitlab.com/Timfa/bluegit/internal/store" |
| 15 | ) |
| 16 | |
| 17 | func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) { |
| 18 | role, err := s.repoRole(ctx, user, repo) |
| 19 | if err != nil { |
| 20 | return 0, err |
| 21 | } |
| 22 | if !role.CanWriteCode() { |
| 23 | return 0, fail(403, i18n.E("err.forbidden")) |
| 24 | } |
| 25 | ref = safeRev(ref, repo.DefaultBranch) |
| 26 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 27 | sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", ref+"^{commit}") |
| 28 | if sha == "" { |
| 29 | return 0, fail(400, i18n.E("err.branchMissing")) |
| 30 | } |
| 31 | if source == "" { |
| 32 | source = "api" |
| 33 | } |
| 34 | id := s.startPipeline(ctx, repo, sha, "refs/heads/"+ref, user.Username, source, "", nil) |
| 35 | return id, nil |
| 36 | } |
| 37 | |
| 38 | func (s *Server) opJobAction(ctx context.Context, user store.User, repo store.Repo, pipeID, jobID int64, action string) (store.Job, error) { |
| 39 | role, err := s.repoRole(ctx, user, repo) |
| 40 | if err != nil { |
| 41 | return store.Job{}, err |
| 42 | } |
| 43 | if !role.CanWriteCode() { |
| 44 | return store.Job{}, fail(403, i18n.E("err.forbidden")) |
| 45 | } |
| 46 | p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID) |
| 47 | if err != nil { |
| 48 | return store.Job{}, notFound(err) |
| 49 | } |
| 50 | jobs, _ := s.Store.JobsByPipeline(ctx, p.ID) |
| 51 | var job store.Job |
| 52 | found := false |
| 53 | for _, j := range jobs { |
| 54 | if j.ID == jobID { |
| 55 | job = j |
| 56 | found = true |
| 57 | break |
| 58 | } |
| 59 | } |
| 60 | if !found { |
| 61 | return store.Job{}, fail(404, i18n.E("err.notFound")) |
| 62 | } |
| 63 | switch action { |
| 64 | case "play": |
| 65 | ok, err := s.Store.QueueJob(ctx, job.ID) |
| 66 | if err != nil { |
| 67 | return store.Job{}, fail(400, err) |
| 68 | } |
| 69 | if !ok { |
| 70 | return store.Job{}, fail(409, i18n.E("err.jobState")) |
| 71 | } |
| 72 | case "retry": |
| 73 | ok, err := s.Store.RetryJob(ctx, job.ID) |
| 74 | if err != nil { |
| 75 | return store.Job{}, fail(400, err) |
| 76 | } |
| 77 | if !ok { |
| 78 | return store.Job{}, fail(409, i18n.E("err.jobState")) |
| 79 | } |
| 80 | case "cancel": |
| 81 | ok, err := s.Store.CancelJob(ctx, job.ID) |
| 82 | if err != nil { |
| 83 | return store.Job{}, fail(400, err) |
| 84 | } |
| 85 | if !ok { |
| 86 | return store.Job{}, fail(409, i18n.E("err.jobState")) |
| 87 | } |
| 88 | if s.Runner != nil { |
| 89 | s.Runner.Interrupt([]int64{job.ID}) |
| 90 | } |
| 91 | _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID) |
| 92 | default: |
| 93 | return store.Job{}, fail(400, i18n.E("err.jobState")) |
| 94 | } |
| 95 | jobs, _ = s.Store.JobsByPipeline(ctx, p.ID) |
| 96 | for _, j := range jobs { |
| 97 | if j.ID == jobID { |
| 98 | return j, nil |
| 99 | } |
| 100 | } |
| 101 | return job, nil |
| 102 | } |
| 103 | |
| 104 | func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error { |
| 105 | role, err := s.repoRole(ctx, user, repo) |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | if !role.CanWriteCode() { |
| 110 | return fail(403, i18n.E("err.forbidden")) |
| 111 | } |
| 112 | p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID) |
| 113 | if err != nil { |
| 114 | return notFound(err) |
| 115 | } |
| 116 | jobs, _ := s.Store.JobsByPipeline(ctx, p.ID) |
| 117 | var running []int64 |
| 118 | for _, j := range jobs { |
| 119 | if j.Status == "queued" || j.Status == "running" || j.Status == "manual" || j.Status == "pending" || j.Status == "created" { |
| 120 | ok, _ := s.Store.CancelJob(ctx, j.ID) |
| 121 | if ok && (j.Status == "running" || j.Status == "queued") { |
| 122 | running = append(running, j.ID) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | if s.Runner != nil { |
| 127 | s.Runner.Interrupt(running) |
| 128 | } |
| 129 | _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID) |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | func (s *Server) opLintCI(r *http.Request, repo store.Repo, body, ref string) (map[string]any, error) { |
| 134 | if len(body) > ciMaxBytes { |
| 135 | return nil, fail(400, i18n.E("edit.fail")) |
| 136 | } |
| 137 | ref = safeRev(ref, repo.DefaultBranch) |
| 138 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 139 | model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref)) |
| 140 | lines := make([]string, len(model.Rows)) |
| 141 | var bad []int |
| 142 | for i, row := range model.Rows { |
| 143 | lines[i] = string(row.HTML) |
| 144 | if row.Bad { |
| 145 | bad = append(bad, row.N) |
| 146 | } |
| 147 | } |
| 148 | return map[string]any{ |
| 149 | "status": model.Status, "statusClass": model.Class, "errors": model.Errors, |
| 150 | "warnings": model.Warnings, "issues": model.Issues, "lines": lines, "bad": bad, |
| 151 | }, nil |
| 152 | } |
| 153 | |
| 154 | func (s *Server) opSaveCI(ctx context.Context, user store.User, repo store.Repo, base, branch, message, body string, openMR bool) (string, int64, error) { |
| 155 | role, err := s.repoRole(ctx, user, repo) |
| 156 | if err != nil { |
| 157 | return "", 0, err |
| 158 | } |
| 159 | base = safeRev(base, repo.DefaultBranch) |
| 160 | if !validBranchName(branch) { |
| 161 | return "", 0, fail(400, i18n.E("edit.badBranch")) |
| 162 | } |
| 163 | if len(body) > ciMaxBytes { |
| 164 | return "", 0, fail(400, i18n.E("edit.fail")) |
| 165 | } |
| 166 | patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID) |
| 167 | if openMR && (branch == base || access.BranchProtected(patterns, branch)) { |
| 168 | return "", 0, fail(400, i18n.E("edit.badBranch")) |
| 169 | } |
| 170 | sha, err := s.opWriteFiles(ctx, user, repo, role, branch, base, message, "", map[string]string{ciFile: body}, nil) |
| 171 | if err != nil { |
| 172 | return "", 0, err |
| 173 | } |
| 174 | if !openMR { |
| 175 | return sha, 0, nil |
| 176 | } |
| 177 | existing, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch) |
| 178 | if err != nil { |
| 179 | return sha, 0, fail(400, err) |
| 180 | } |
| 181 | if len(existing) > 0 { |
| 182 | return sha, existing[0].Number, nil |
| 183 | } |
| 184 | mr, err := s.opCreateMR(ctx, user, repo, message, "", branch, base, false) |
| 185 | if err != nil { |
| 186 | return sha, 0, err |
| 187 | } |
| 188 | return sha, mr.Number, nil |
| 189 | } |
| 190 | |
| 191 | func (s *Server) opPublishRelease(ctx context.Context, repo store.Repo, author store.User, job store.Job, req releaseRequest) (int64, error) { |
| 192 | if s.StoragePath() == "" { |
| 193 | return 0, fail(503, i18n.E("err.noStorage")) |
| 194 | } |
| 195 | if err := names.ValidTag(req.Tag); err != nil { |
| 196 | return 0, fail(400, err) |
| 197 | } |
| 198 | if !safeSHA(req.Commit) { |
| 199 | return 0, fail(400, i18n.E("err.commitAbsent")) |
| 200 | } |
| 201 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 202 | if !gitrepo.HasCommit(ctx, s.Git, dir, req.Commit) { |
| 203 | return 0, fail(400, i18n.E("err.commitMissing")) |
| 204 | } |
| 205 | if req.Name == "" { |
| 206 | req.Name = req.Tag |
| 207 | } |
| 208 | if job.ID == 0 && len(req.Assets) > 0 { |
| 209 | return 0, fail(400, i18n.E("err.assetsJob")) |
| 210 | } |
| 211 | var assets []store.ReleaseAsset |
| 212 | for _, a := range req.Assets { |
| 213 | if a.Name == "" { |
| 214 | a.Name = filepath.Base(a.Path) |
| 215 | } |
| 216 | if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" { |
| 217 | return 0, fail(400, i18n.E("err.assetName")) |
| 218 | } |
| 219 | src, err := s.Store.ArtifactPath(ctx, job.PipelineID, a.Job, a.Path) |
| 220 | if err != nil { |
| 221 | return 0, fail(400, i18n.E("err.artifactMissing", a.Job, a.Path)) |
| 222 | } |
| 223 | info, err := os.Stat(src) |
| 224 | if err != nil { |
| 225 | return 0, fail(400, i18n.E("err.assetDisk")) |
| 226 | } |
| 227 | dst := filepath.Join(s.StoragePath(), "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name) |
| 228 | if err := copyRelease(src, dst); err != nil { |
| 229 | return 0, fail(500, i18n.E("err.assetCopy")) |
| 230 | } |
| 231 | assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: info.Size()}) |
| 232 | } |
| 233 | if err := s.postReceive(ctx, repo, author, dir, func() error { |
| 234 | return gitrepo.Tag(ctx, s.Git, dir, req.Tag, req.Commit, req.Name) |
| 235 | }); err != nil { |
| 236 | return 0, fail(400, i18n.E("err.tagFail", err.Error())) |
| 237 | } |
| 238 | authorID := author.ID |
| 239 | id, err := s.Store.CreateRelease(ctx, repo.ID, req.Tag, req.Commit, req.Name, req.Body, authorID, job.ID, assets) |
| 240 | if err != nil { |
| 241 | return 0, fail(400, err) |
| 242 | } |
| 243 | s.Dispatch(repo.ID, s.releaseEvent(repo, req.Tag, req.Name, req.Commit, assets)) |
| 244 | return id, nil |
| 245 | } |
| 246 | |
| 247 | func (s *Server) opCIFile(ctx context.Context, repo store.Repo, ref string) (string, error) { |
| 248 | ref = safeRev(ref, repo.DefaultBranch) |
| 249 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 250 | body := gitOne(ctx, s.Git, dir, "show", ref+":"+ciFile) |
| 251 | return body, nil |
| 252 | } |