BlueGit

timfa bluegit

Files

bluegit / internal / server / events.go

events.go

feat: merge-request-pipelines en merge na een geslaagde run
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 24b281c4 History
events.go 6.8 KiB Download Raw
1package server
2
3import (
4 "bytes"
5 "context"
6 "io"
7 "net/http"
8 "net/url"
9 "os/exec"
10 "strings"
11 "time"
12
13 "gitlab.com/Timfa/bluegit/internal/access"
14 "gitlab.com/Timfa/bluegit/internal/ci"
15 "gitlab.com/Timfa/bluegit/internal/gitrepo"
16 "gitlab.com/Timfa/bluegit/internal/i18n"
17 "gitlab.com/Timfa/bluegit/internal/ssrf"
18 "gitlab.com/Timfa/bluegit/internal/store"
19 "gitlab.com/Timfa/bluegit/internal/webhook"
20)
21
22func (s *Server) AfterPush(repo store.Repo, user store.User, before, after map[string]string) {
23 if after == nil {
24 return
25 }
26 actor := user.Username
27 for ref, sha := range after {
28 if before[ref] == sha {
29 continue
30 }
31 if isZero(sha) {
32 continue
33 }
34 if strings.HasPrefix(ref, "refs/tags/") {
35 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
36 continue
37 }
38 branch, ok := gitrepo.BranchOfRef(ref)
39 if !ok {
40 continue
41 }
42 s.recordPushContributions(repo, user, map[string]string{ref: before[ref]}, map[string]string{ref: sha})
43 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
44 s.startMergeRequestPipelines(context.Background(), repo, sha, branch, actor, before[ref])
45 web := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath()
46 s.Dispatch(repo.ID, webhook.Event{
47 Type: webhook.EventPush,
48 Project: repo.FullPath(),
49 Branch: branch,
50 Commit: sha,
51 Summary: i18n.T(i18n.EN, "hook.push", actor, branch, repo.FullPath()),
52 URL: web,
53 Actor: actor,
54 })
55 }
56}
57
58func isZero(sha string) bool {
59 return strings.Trim(sha, "0") == ""
60}
61
62func (s *Server) startPipeline(ctx context.Context, repo store.Repo, sha, ref, actor, source, before string, mr *ci.MRInfo) int64 {
63 if !s.Cfg.CI.Enabled || s.StoragePath() == "" {
64 return 0
65 }
66 title, jobs, broken := s.planPipeline(ctx, repo, sha, ref, source, before, mr)
67 if broken != nil {
68 id, err := s.Store.RecordConfigFailure(ctx, repo.ID, sha, ref, broken.Error())
69 if err != nil && s.Log != nil {
70 s.Log.Error("pipeline", "err", err)
71 }
72 if mr != nil && id != 0 {
73 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
74 }
75 return id
76 }
77 if len(jobs) == 0 {
78 return 0
79 }
80 id, err := s.Store.CreatePipeline(ctx, repo.ID, sha, ref, source, actor, title, jobs)
81 if err != nil {
82 if s.Log != nil {
83 s.Log.Error("pipeline", "err", err)
84 }
85 return 0
86 }
87 if mr != nil {
88 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
89 }
90 return id
91}
92
93func gitOne(ctx context.Context, gitBin, dir string, args ...string) string {
94 out, err := exec.CommandContext(ctx, gitBin, append([]string{"-C", dir}, args...)...).Output()
95 if err != nil {
96 return ""
97 }
98 return strings.TrimSpace(string(out))
99}
100
101func gitLines(ctx context.Context, gitBin, dir string, args ...string) []string {
102 out := gitOne(ctx, gitBin, dir, args...)
103 if out == "" {
104 return []string{}
105 }
106 return strings.Split(out, "\n")
107}
108
109func changedFiles(ctx context.Context, gitBin, dir, sha, before string, all []string) []string {
110 if before == "" || strings.Trim(before, "0") == "" {
111 parent := gitOne(ctx, gitBin, dir, "rev-parse", "--verify", sha+"^")
112 if parent == "" {
113 return all
114 }
115 before = parent
116 }
117 out, err := exec.CommandContext(ctx, gitBin, "-C", dir, "diff", "--name-only", before, sha).Output()
118 if err != nil {
119 return nil
120 }
121 text := strings.TrimSpace(string(out))
122 if text == "" {
123 return []string{}
124 }
125 return strings.Split(text, "\n")
126}
127
128func pushEvent(repo store.Repo, kind, branch, commit, actor, summary, path string) webhook.Event {
129 return webhook.Event{
130 Type: kind, Project: repo.FullPath(), Branch: branch, Commit: commit,
131 Actor: actor, Summary: summary, URL: path,
132 }
133}
134
135func (s *Server) Dispatch(repoID int64, ev webhook.Event) {
136 ctx := context.Background()
137 repo, err := s.Store.RepoByID(ctx, repoID)
138 if err != nil {
139 return
140 }
141 if ev.Project == "" {
142 ev.Project = repo.FullPath()
143 }
144 if !strings.HasPrefix(ev.URL, "http") {
145 ev.URL = strings.TrimRight(s.Cfg.BaseURL, "/") + ev.URL
146 }
147 if repo.OwnerKind == "group" {
148 if g, err := s.Store.GroupByID(ctx, repo.OwnerID); err == nil {
149 ev.Group = g.Path
150 }
151 }
152 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
153 protected := access.BranchProtected(patterns, ev.Branch)
154 s.deliver(ctx, "repo", repo.ID, ev, protected)
155 if repo.OwnerKind == "group" {
156 s.deliver(ctx, "group", repo.OwnerID, ev, protected)
157 }
158 if ev.Type == webhook.EventPipelineFailure {
159 s.queueFailureMail(repo, ev)
160 }
161}
162
163func (s *Server) deliver(ctx context.Context, kind string, scopeID int64, ev webhook.Event, refProtected bool) {
164 hooks, err := s.Store.ListWebhooks(ctx, kind, scopeID)
165 if err != nil {
166 return
167 }
168 secrets, err := s.Store.SecretValues(ctx, kind, scopeID, refProtected)
169 if err != nil {
170 s.Log.Error("geheimen voor webhook", "err", err)
171 return
172 }
173 client := ssrf.NewClient(10 * time.Second)
174 for _, h := range hooks {
175 if !h.Active {
176 continue
177 }
178 ok, err := webhook.Allows(webhook.Hook{Preset: h.Preset, Events: h.Events, BranchFilter: h.BranchFilter}, ev)
179 if err != nil {
180 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
181 continue
182 }
183 if !ok {
184 continue
185 }
186 rawURL, err := webhook.Expand(h.URLTemplate, ev, secrets)
187 if err != nil {
188 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
189 continue
190 }
191 target, err := urlParse(rawURL)
192 if err != nil {
193 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
194 continue
195 }
196 if err := ssrf.ValidateURL(target); err != nil {
197 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
198 continue
199 }
200 headers, err := webhook.RenderHeaders(h.Preset, h.Headers, ev, secrets)
201 if err != nil {
202 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
203 continue
204 }
205 body, err := webhook.RenderBody(h.BodyTemplate, ev, secrets)
206 if err != nil {
207 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
208 continue
209 }
210 method := h.Method
211 if method == "" {
212 method = http.MethodPost
213 }
214 req, err := http.NewRequestWithContext(ctx, method, rawURL, bytes.NewReader(body))
215 if err != nil {
216 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
217 continue
218 }
219 for k, v := range headers {
220 if strings.ContainsAny(k, "\r\n") || strings.ContainsAny(v, "\r\n") {
221 continue
222 }
223 req.Header.Set(k, v)
224 }
225 res, err := client.Do(req)
226 if err != nil {
227 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, 0, err.Error())
228 continue
229 }
230 _, _ = io.Copy(io.Discard, io.LimitReader(res.Body, 1<<20))
231 res.Body.Close()
232 _ = s.Store.AddDelivery(ctx, h.ID, ev.Type, res.StatusCode, "")
233 }
234}
235
236func urlParse(raw string) (*url.URL, error) {
237 u, err := url.Parse(raw)
238 if err != nil {
239 return nil, i18n.E("err.whURL")
240 }
241 return u, nil
242}
243
244func gitSHA(r *http.Request, gitBin, dir, rev string) (string, error) {
245 out, err := exec.CommandContext(r.Context(), gitBin, "-C", dir, "rev-parse", "--verify", rev+"^{commit}").Output()
246 if err != nil {
247 return "", err
248 }
249 return strings.TrimSpace(string(out)), nil
250}