BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_ci.go

ops_ci.go

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