BlueGit

timfa bluegit

feat: merge-request-pipelines en merge na een geslaagde run

Jobs draaien in een merge-request-pipeline via rules op
CI_PIPELINE_SOURCE of only: merge_requests. De MR toont die pipeline.
Mergen wacht tot de nieuwste run klaar is; allow_failure mag mislukken.

Co-authored-by: tim <tim@timfalken.com>

timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa

Authored 2026-10-22 · 3 weeks ago

Hash
24b281c4d5c18f178602b4a9112cfb3c23e80183
Parent
6df82547
Pipeline
Failed
16 files changed +626 −73
docs/CI.md +3 −1
@@ -62,7 +62,9 @@ $NAAM !~ /reguliere-expressie/
62 62
63 63 `changes` kijkt naar de diff van de push. Is die diff niet te bepalen, dan geldt de voorwaarde. `exists` eist dat elk pad in de commit staat.
64 64
65 Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT_SHORT_SHA`, `CI_COMMIT_BRANCH`, `CI_COMMIT_TAG`, `CI_COMMIT_REF`, `CI_COMMIT_REF_NAME`, `CI_COMMIT_REF_SLUG`, `CI_COMMIT_TITLE`, `CI_DEFAULT_BRANCH`, `CI_PIPELINE_ID`, `CI_PIPELINE_SOURCE`, `CI_PIPELINE_URL`, `CI_PROJECT_PATH`, `CI_PROJECT_NAME`, `CI_PROJECT_NAMESPACE`, `CI_PROJECT_URL`, `CI_JOB_ID`, `CI_JOB_NAME`, `CI_JOB_STAGE`, `CI_JOB_URL`, `CI_JOB_TOKEN`, `CI_API_URL`, `CI_CONFIG_PATH`, `CI_SERVER_NAME`. Op een tag is `CI_COMMIT_BRANCH` leeg. `CI_PIPELINE_SOURCE` is `push` of `web`.
65 Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT_SHORT_SHA`, `CI_COMMIT_BRANCH`, `CI_COMMIT_TAG`, `CI_COMMIT_REF`, `CI_COMMIT_REF_NAME`, `CI_COMMIT_REF_SLUG`, `CI_COMMIT_TITLE`, `CI_DEFAULT_BRANCH`, `CI_PIPELINE_ID`, `CI_PIPELINE_SOURCE`, `CI_PIPELINE_URL`, `CI_PROJECT_PATH`, `CI_PROJECT_NAME`, `CI_PROJECT_NAMESPACE`, `CI_PROJECT_URL`, `CI_JOB_ID`, `CI_JOB_NAME`, `CI_JOB_STAGE`, `CI_JOB_URL`, `CI_JOB_TOKEN`, `CI_API_URL`, `CI_CONFIG_PATH`, `CI_SERVER_NAME`. Op een tag is `CI_COMMIT_BRANCH` leeg. `CI_PIPELINE_SOURCE` is `push`, `web` of `merge_request_event`.
66
67 Een merge-request-pipeline start bij het openen van de merge request en bij elke push naar de bronbranch. `CI_PIPELINE_SOURCE` is dan `merge_request_event`. Daarbij horen `CI_MERGE_REQUEST_ID`, `CI_MERGE_REQUEST_IID`, `CI_MERGE_REQUEST_TITLE`, `CI_MERGE_REQUEST_SOURCE_BRANCH_NAME`, `CI_MERGE_REQUEST_SOURCE_BRANCH_SHA`, `CI_MERGE_REQUEST_TARGET_BRANCH_NAME`, `CI_MERGE_REQUEST_TARGET_BRANCH_SHA`, `CI_MERGE_REQUEST_PROJECT_PATH` en `CI_MERGE_REQUEST_PROJECT_URL`. De pipeline draait op de commit van de bronbranch. Jobs zonder `rules` en zonder `only: merge_requests` doen daar niet aan mee. `only: merge_requests` en `except: merge_requests` werken wel. Zodra zo'n bestand jobs voor de merge request kiest, mag er pas gemerged worden als de nieuwste merge-request-pipeline geslaagd is. Een job met `allow_failure` mag mislukken, maar moet wel af zijn. Een job die nog loopt, in de wachtrij staat of op manual staat, houdt de merge tegen.
66 68
67 69 ## Bewust niet
68 70
@@ -62,7 +62,9 @@ $NAAM !~ /reguliere-expressie/
62
62
63`changes` kijkt naar de diff van de push. Is die diff niet te bepalen, dan geldt de voorwaarde. `exists` eist dat elk pad in de commit staat.
63`changes` kijkt naar de diff van de push. Is die diff niet te bepalen, dan geldt de voorwaarde. `exists` eist dat elk pad in de commit staat.
64
64
65Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT_SHORT_SHA`, `CI_COMMIT_BRANCH`, `CI_COMMIT_TAG`, `CI_COMMIT_REF`, `CI_COMMIT_REF_NAME`, `CI_COMMIT_REF_SLUG`, `CI_COMMIT_TITLE`, `CI_DEFAULT_BRANCH`, `CI_PIPELINE_ID`, `CI_PIPELINE_SOURCE`, `CI_PIPELINE_URL`, `CI_PROJECT_PATH`, `CI_PROJECT_NAME`, `CI_PROJECT_NAMESPACE`, `CI_PROJECT_URL`, `CI_JOB_ID`, `CI_JOB_NAME`, `CI_JOB_STAGE`, `CI_JOB_URL`, `CI_JOB_TOKEN`, `CI_API_URL`, `CI_CONFIG_PATH`, `CI_SERVER_NAME`. Op een tag is `CI_COMMIT_BRANCH` leeg. `CI_PIPELINE_SOURCE` is `push` of `web`.
65Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT_SHORT_SHA`, `CI_COMMIT_BRANCH`, `CI_COMMIT_TAG`, `CI_COMMIT_REF`, `CI_COMMIT_REF_NAME`, `CI_COMMIT_REF_SLUG`, `CI_COMMIT_TITLE`, `CI_DEFAULT_BRANCH`, `CI_PIPELINE_ID`, `CI_PIPELINE_SOURCE`, `CI_PIPELINE_URL`, `CI_PROJECT_PATH`, `CI_PROJECT_NAME`, `CI_PROJECT_NAMESPACE`, `CI_PROJECT_URL`, `CI_JOB_ID`, `CI_JOB_NAME`, `CI_JOB_STAGE`, `CI_JOB_URL`, `CI_JOB_TOKEN`, `CI_API_URL`, `CI_CONFIG_PATH`, `CI_SERVER_NAME`. Op een tag is `CI_COMMIT_BRANCH` leeg. `CI_PIPELINE_SOURCE` is `push`, `web` of `merge_request_event`.
66
67Een merge-request-pipeline start bij het openen van de merge request en bij elke push naar de bronbranch. `CI_PIPELINE_SOURCE` is dan `merge_request_event`. Daarbij horen `CI_MERGE_REQUEST_ID`, `CI_MERGE_REQUEST_IID`, `CI_MERGE_REQUEST_TITLE`, `CI_MERGE_REQUEST_SOURCE_BRANCH_NAME`, `CI_MERGE_REQUEST_SOURCE_BRANCH_SHA`, `CI_MERGE_REQUEST_TARGET_BRANCH_NAME`, `CI_MERGE_REQUEST_TARGET_BRANCH_SHA`, `CI_MERGE_REQUEST_PROJECT_PATH` en `CI_MERGE_REQUEST_PROJECT_URL`. De pipeline draait op de commit van de bronbranch. Jobs zonder `rules` en zonder `only: merge_requests` doen daar niet aan mee. `only: merge_requests` en `except: merge_requests` werken wel. Zodra zo'n bestand jobs voor de merge request kiest, mag er pas gemerged worden als de nieuwste merge-request-pipeline geslaagd is. Een job met `allow_failure` mag mislukken, maar moet wel af zijn. Een job die nog loopt, in de wachtrij staat of op manual staat, houdt de merge tegen.
66
68
67## Bewust niet
69## Bewust niet
68
70
internal/ci/eval.go +30 −5
@@ -53,6 +53,18 @@ func Variables(r Ref) map[string]string {
53 53 if env["CI_PIPELINE_SOURCE"] == "" {
54 54 env["CI_PIPELINE_SOURCE"] = "push"
55 55 }
56 if r.MR != nil {
57 m := r.MR
58 env["CI_MERGE_REQUEST_ID"] = fmt.Sprint(m.ID)
59 env["CI_MERGE_REQUEST_IID"] = fmt.Sprint(m.IID)
60 env["CI_MERGE_REQUEST_TITLE"] = m.Title
61 env["CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"] = m.SourceBranch
62 env["CI_MERGE_REQUEST_SOURCE_BRANCH_SHA"] = r.SHA
63 env["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"] = m.TargetBranch
64 env["CI_MERGE_REQUEST_TARGET_BRANCH_SHA"] = m.TargetSHA
65 env["CI_MERGE_REQUEST_PROJECT_PATH"] = m.ProjectPath
66 env["CI_MERGE_REQUEST_PROJECT_URL"] = m.ProjectURL
67 }
56 68 return env
57 69 }
58 70
@@ -196,7 +208,13 @@ func ruleMatch(r Rule, ev Eval) (bool, error) {
196 208 }
197 209
198 210 func onlyExcept(j Job, ev Eval) (bool, error) {
211 mr := ev.Env["CI_PIPELINE_SOURCE"] == "merge_request_event"
199 212 if len(j.Only) == 0 && len(j.Except) == 0 {
213 // Zonder rules of only draait een job op een branch of tag,
214 // niet in een merge-request-pipeline.
215 if mr {
216 return false, nil
217 }
200 218 return true, nil
201 219 }
202 220 name := ev.Env["CI_COMMIT_REF_NAME"]
@@ -205,13 +223,13 @@ func onlyExcept(j Job, ev Eval) (bool, error) {
205 223 }
206 224 tag := ev.Env["CI_COMMIT_TAG"] != ""
207 225 if len(j.Only) > 0 {
208 ok, err := refListed(j.Only, name, tag)
226 ok, err := refListed(j.Only, name, tag, mr)
209 227 if err != nil || !ok {
210 228 return false, err
211 229 }
212 230 }
213 231 if len(j.Except) > 0 {
214 ok, err := refListed(j.Except, name, tag)
232 ok, err := refListed(j.Except, name, tag, mr)
215 233 if err != nil {
216 234 return false, err
217 235 }
@@ -222,18 +240,25 @@ func onlyExcept(j Job, ev Eval) (bool, error) {
222 240 return true, nil
223 241 }
224 242
225 func refListed(list []string, name string, tag bool) (bool, error) {
243 func refListed(list []string, name string, tag, mr bool) (bool, error) {
226 244 for _, item := range list {
227 245 switch item {
246 case "merge_requests":
247 if mr {
248 return true, nil
249 }
228 250 case "branches":
229 if !tag {
251 if !tag && !mr {
230 252 return true, nil
231 253 }
232 254 case "tags":
233 if tag {
255 if tag && !mr {
234 256 return true, nil
235 257 }
236 258 default:
259 if mr {
260 continue
261 }
237 262 if strings.HasPrefix(item, "/") && strings.HasSuffix(item, "/") && len(item) > 2 {
238 263 re, err := regexp.Compile(item[1 : len(item)-1])
239 264 if err != nil {
@@ -53,6 +53,18 @@ func Variables(r Ref) map[string]string {
53 if env["CI_PIPELINE_SOURCE"] == "" {
53 if env["CI_PIPELINE_SOURCE"] == "" {
54 env["CI_PIPELINE_SOURCE"] = "push"
54 env["CI_PIPELINE_SOURCE"] = "push"
55 }
55 }
56 if r.MR != nil {
57 m := r.MR
58 env["CI_MERGE_REQUEST_ID"] = fmt.Sprint(m.ID)
59 env["CI_MERGE_REQUEST_IID"] = fmt.Sprint(m.IID)
60 env["CI_MERGE_REQUEST_TITLE"] = m.Title
61 env["CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"] = m.SourceBranch
62 env["CI_MERGE_REQUEST_SOURCE_BRANCH_SHA"] = r.SHA
63 env["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"] = m.TargetBranch
64 env["CI_MERGE_REQUEST_TARGET_BRANCH_SHA"] = m.TargetSHA
65 env["CI_MERGE_REQUEST_PROJECT_PATH"] = m.ProjectPath
66 env["CI_MERGE_REQUEST_PROJECT_URL"] = m.ProjectURL
67 }
56 return env
68 return env
57}
69}
58
70
@@ -196,7 +208,13 @@ func ruleMatch(r Rule, ev Eval) (bool, error) {
196}
208}
197
209
198func onlyExcept(j Job, ev Eval) (bool, error) {
210func onlyExcept(j Job, ev Eval) (bool, error) {
211 mr := ev.Env["CI_PIPELINE_SOURCE"] == "merge_request_event"
199 if len(j.Only) == 0 && len(j.Except) == 0 {
212 if len(j.Only) == 0 && len(j.Except) == 0 {
213 // Zonder rules of only draait een job op een branch of tag,
214 // niet in een merge-request-pipeline.
215 if mr {
216 return false, nil
217 }
200 return true, nil
218 return true, nil
201 }
219 }
202 name := ev.Env["CI_COMMIT_REF_NAME"]
220 name := ev.Env["CI_COMMIT_REF_NAME"]
@@ -205,13 +223,13 @@ func onlyExcept(j Job, ev Eval) (bool, error) {
205 }
223 }
206 tag := ev.Env["CI_COMMIT_TAG"] != ""
224 tag := ev.Env["CI_COMMIT_TAG"] != ""
207 if len(j.Only) > 0 {
225 if len(j.Only) > 0 {
208 ok, err := refListed(j.Only, name, tag)
226 ok, err := refListed(j.Only, name, tag, mr)
209 if err != nil || !ok {
227 if err != nil || !ok {
210 return false, err
228 return false, err
211 }
229 }
212 }
230 }
213 if len(j.Except) > 0 {
231 if len(j.Except) > 0 {
214 ok, err := refListed(j.Except, name, tag)
232 ok, err := refListed(j.Except, name, tag, mr)
215 if err != nil {
233 if err != nil {
216 return false, err
234 return false, err
217 }
235 }
@@ -222,18 +240,25 @@ func onlyExcept(j Job, ev Eval) (bool, error) {
222 return true, nil
240 return true, nil
223}
241}
224
242
225func refListed(list []string, name string, tag bool) (bool, error) {
243func refListed(list []string, name string, tag, mr bool) (bool, error) {
226 for _, item := range list {
244 for _, item := range list {
227 switch item {
245 switch item {
246 case "merge_requests":
247 if mr {
248 return true, nil
249 }
228 case "branches":
250 case "branches":
229 if !tag {
251 if !tag && !mr {
230 return true, nil
252 return true, nil
231 }
253 }
232 case "tags":
254 case "tags":
233 if tag {
255 if tag && !mr {
234 return true, nil
256 return true, nil
235 }
257 }
236 default:
258 default:
259 if mr {
260 continue
261 }
237 if strings.HasPrefix(item, "/") && strings.HasSuffix(item, "/") && len(item) > 2 {
262 if strings.HasPrefix(item, "/") && strings.HasSuffix(item, "/") && len(item) > 2 {
238 re, err := regexp.Compile(item[1 : len(item)-1])
263 re, err := regexp.Compile(item[1 : len(item)-1])
239 if err != nil {
264 if err != nil {
internal/ci/model.go +14 −0
@@ -78,6 +78,19 @@ type Eval struct {
78 78 Files []string
79 79 }
80 80
81 // MRInfo is de merge request waar een pipeline bij hoort.
82 // Leeg betekent een gewone branch- of tag-pipeline.
83 type MRInfo struct {
84 ID int64
85 IID int64
86 Title string
87 SourceBranch string
88 TargetBranch string
89 ProjectPath string
90 ProjectURL string
91 TargetSHA string
92 }
93
81 94 // Ref beschrijft de commit waarvoor voorgedefinieerde variabelen gelden.
82 95 type Ref struct {
83 96 SHA string
@@ -91,4 +104,5 @@ type Ref struct {
91 104 ProjectURL string
92 105 APIURL string
93 106 PipelineID int64
107 MR *MRInfo
94 108 }
@@ -78,6 +78,19 @@ type Eval struct {
78 Files []string
78 Files []string
79}
79}
80
80
81// MRInfo is de merge request waar een pipeline bij hoort.
82// Leeg betekent een gewone branch- of tag-pipeline.
83type MRInfo struct {
84 ID int64
85 IID int64
86 Title string
87 SourceBranch string
88 TargetBranch string
89 ProjectPath string
90 ProjectURL string
91 TargetSHA string
92}
93
81// Ref beschrijft de commit waarvoor voorgedefinieerde variabelen gelden.
94// Ref beschrijft de commit waarvoor voorgedefinieerde variabelen gelden.
82type Ref struct {
95type Ref struct {
83 SHA string
96 SHA string
@@ -91,4 +104,5 @@ type Ref struct {
91 ProjectURL string
104 ProjectURL string
92 APIURL string
105 APIURL string
93 PipelineID int64
106 PipelineID int64
107 MR *MRInfo
94}
108}
internal/ci/spec_test.go +48 −0
@@ -194,6 +194,54 @@ jobs:
194 194 }
195 195 }
196 196
197 func TestMergeRequestPipelineRules(t *testing.T) {
198 p, err := Parse([]byte(`
199 push_job:
200 image: alpine:3.20
201 script: ["echo push"]
202 mr_job:
203 image: alpine:3.20
204 script: ["echo mr"]
205 rules:
206 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
207 only_mr:
208 image: alpine:3.20
209 script: ["echo only"]
210 only: ["merge_requests"]
211 `))
212 if err != nil {
213 t.Fatal(err)
214 }
215 pushEnv := Variables(Ref{SHA: "abc", FullRef: "refs/heads/feature", Source: "push", DefaultBranch: "master", ProjectPath: "tim/app"})
216 push, err := p.Select(Eval{Env: pushEnv})
217 if err != nil {
218 t.Fatal(err)
219 }
220 if len(push) != 1 || push[0].Name != "push_job" {
221 t.Fatalf("push: %+v", namesOf(push))
222 }
223 mr := &MRInfo{ID: 4, IID: 2, Title: "Titel", SourceBranch: "feature", TargetBranch: "master", ProjectPath: "tim/app", ProjectURL: "https://git.example/tim/app", TargetSHA: "def"}
224 mrEnv := Variables(Ref{SHA: "abcdef", FullRef: "refs/heads/feature", Source: "merge_request_event", DefaultBranch: "master", ProjectPath: "tim/app", MR: mr})
225 if mrEnv["CI_MERGE_REQUEST_IID"] != "2" || mrEnv["CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"] != "feature" || mrEnv["CI_MERGE_REQUEST_TARGET_BRANCH_SHA"] != "def" || mrEnv["CI_MERGE_REQUEST_SOURCE_BRANCH_SHA"] != "abcdef" {
226 t.Fatalf("%v", mrEnv)
227 }
228 got, err := p.Select(Eval{Env: mrEnv})
229 if err != nil {
230 t.Fatal(err)
231 }
232 if strings.Join(namesOf(got), ",") != "mr_job,only_mr" {
233 t.Fatalf("mr: %v", namesOf(got))
234 }
235 }
236
237 func namesOf(jobs []Job) []string {
238 out := make([]string, len(jobs))
239 for i, j := range jobs {
240 out[i] = j.Name
241 }
242 return out
243 }
244
197 245 func TestMaskAndVariables(t *testing.T) {
198 246 if got := Mask("token=supersecret value", []string{"supersecret", "ab"}); got != "token=[MASKED] value" {
199 247 t.Fatal(got)
@@ -194,6 +194,54 @@ jobs:
194 }
194 }
195}
195}
196
196
197func TestMergeRequestPipelineRules(t *testing.T) {
198 p, err := Parse([]byte(`
199push_job:
200 image: alpine:3.20
201 script: ["echo push"]
202mr_job:
203 image: alpine:3.20
204 script: ["echo mr"]
205 rules:
206 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
207only_mr:
208 image: alpine:3.20
209 script: ["echo only"]
210 only: ["merge_requests"]
211`))
212 if err != nil {
213 t.Fatal(err)
214 }
215 pushEnv := Variables(Ref{SHA: "abc", FullRef: "refs/heads/feature", Source: "push", DefaultBranch: "master", ProjectPath: "tim/app"})
216 push, err := p.Select(Eval{Env: pushEnv})
217 if err != nil {
218 t.Fatal(err)
219 }
220 if len(push) != 1 || push[0].Name != "push_job" {
221 t.Fatalf("push: %+v", namesOf(push))
222 }
223 mr := &MRInfo{ID: 4, IID: 2, Title: "Titel", SourceBranch: "feature", TargetBranch: "master", ProjectPath: "tim/app", ProjectURL: "https://git.example/tim/app", TargetSHA: "def"}
224 mrEnv := Variables(Ref{SHA: "abcdef", FullRef: "refs/heads/feature", Source: "merge_request_event", DefaultBranch: "master", ProjectPath: "tim/app", MR: mr})
225 if mrEnv["CI_MERGE_REQUEST_IID"] != "2" || mrEnv["CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"] != "feature" || mrEnv["CI_MERGE_REQUEST_TARGET_BRANCH_SHA"] != "def" || mrEnv["CI_MERGE_REQUEST_SOURCE_BRANCH_SHA"] != "abcdef" {
226 t.Fatalf("%v", mrEnv)
227 }
228 got, err := p.Select(Eval{Env: mrEnv})
229 if err != nil {
230 t.Fatal(err)
231 }
232 if strings.Join(namesOf(got), ",") != "mr_job,only_mr" {
233 t.Fatalf("mr: %v", namesOf(got))
234 }
235}
236
237func namesOf(jobs []Job) []string {
238 out := make([]string, len(jobs))
239 for i, j := range jobs {
240 out[i] = j.Name
241 }
242 return out
243}
244
197func TestMaskAndVariables(t *testing.T) {
245func TestMaskAndVariables(t *testing.T) {
198 if got := Mask("token=supersecret value", []string{"supersecret", "ab"}); got != "token=[MASKED] value" {
246 if got := Mask("token=supersecret value", []string{"supersecret", "ab"}); got != "token=[MASKED] value" {
199 t.Fatal(got)
247 t.Fatal(got)
internal/i18n/catalog.go +6 −0
@@ -153,6 +153,11 @@ var en = map[string]string{
153 153 "mr.comments": "Comments",
154 154 "mr.noComments": "No comments yet.",
155 155 "mr.comment": "Comment",
156 "mr.pipeline": "Pipeline",
157 "mr.noPipeline": "No pipeline for this merge request.",
158 "mr.block.running": "The pipeline is still running.",
159 "mr.block.failed": "The pipeline failed.",
160 "mr.block.waiting": "This merge request is waiting for a pipeline.",
156 161
157 162 "pipe.title": "Pipelines",
158 163 "pipe.ref": "branch, empty is the default",
@@ -197,6 +202,7 @@ var en = map[string]string{
197 202 "pipe.play": "Play",
198 203 "pipe.src.push": "Push",
199 204 "pipe.src.web": "Web",
205 "pipe.src.merge_request_event": "Merge request",
200 206
201 207 "edit.title": "Pipeline Editor",
202 208 "edit.clean": "No issues found",
@@ -153,6 +153,11 @@ var en = map[string]string{
153 "mr.comments": "Comments",
153 "mr.comments": "Comments",
154 "mr.noComments": "No comments yet.",
154 "mr.noComments": "No comments yet.",
155 "mr.comment": "Comment",
155 "mr.comment": "Comment",
156 "mr.pipeline": "Pipeline",
157 "mr.noPipeline": "No pipeline for this merge request.",
158 "mr.block.running": "The pipeline is still running.",
159 "mr.block.failed": "The pipeline failed.",
160 "mr.block.waiting": "This merge request is waiting for a pipeline.",
156
161
157 "pipe.title": "Pipelines",
162 "pipe.title": "Pipelines",
158 "pipe.ref": "branch, empty is the default",
163 "pipe.ref": "branch, empty is the default",
@@ -197,6 +202,7 @@ var en = map[string]string{
197 "pipe.play": "Play",
202 "pipe.play": "Play",
198 "pipe.src.push": "Push",
203 "pipe.src.push": "Push",
199 "pipe.src.web": "Web",
204 "pipe.src.web": "Web",
205 "pipe.src.merge_request_event": "Merge request",
200
206
201 "edit.title": "Pipeline Editor",
207 "edit.title": "Pipeline Editor",
202 "edit.clean": "No issues found",
208 "edit.clean": "No issues found",
internal/i18n/catalog_lang.go +18 −0
@@ -161,6 +161,11 @@ var nl = map[string]string{
161 161 "mr.comments": "Reacties",
162 162 "mr.noComments": "Nog geen reacties.",
163 163 "mr.comment": "Reactie",
164 "mr.pipeline": "Pipeline",
165 "mr.noPipeline": "Geen pipeline voor deze merge request.",
166 "mr.block.running": "De pipeline loopt nog.",
167 "mr.block.failed": "De pipeline is mislukt.",
168 "mr.block.waiting": "Deze merge request wacht op een pipeline.",
164 169 "pipe.title": "Pipelines",
165 170 "pipe.ref": "branch, leeg is de standaard",
166 171 "pipe.none": "Nog geen pipelines. Voeg blue-ci-cd.yml toe in de root.",
@@ -204,6 +209,7 @@ var nl = map[string]string{
204 209 "pipe.play": "Start",
205 210 "pipe.src.push": "Push",
206 211 "pipe.src.web": "Web",
212 "pipe.src.merge_request_event": "Merge request",
207 213 "edit.title": "Pipeline-editor",
208 214 "edit.clean": "Geen problemen gevonden",
209 215 "edit.issues": "%d fouten",
@@ -682,6 +688,11 @@ var de = map[string]string{
682 688 "mr.comments": "Kommentare",
683 689 "mr.noComments": "Noch keine Kommentare.",
684 690 "mr.comment": "Kommentar",
691 "mr.pipeline": "Pipeline",
692 "mr.noPipeline": "Keine Pipeline für diesen Merge Request.",
693 "mr.block.running": "Die Pipeline läuft noch.",
694 "mr.block.failed": "Die Pipeline ist fehlgeschlagen.",
695 "mr.block.waiting": "Dieser Merge Request wartet auf eine Pipeline.",
685 696 "pipe.title": "Pipelines",
686 697 "pipe.ref": "Branch, leer ist der Standard",
687 698 "pipe.none": "Noch keine Pipelines. Lege blue-ci-cd.yml im Stammverzeichnis an.",
@@ -725,6 +736,7 @@ var de = map[string]string{
725 736 "pipe.play": "Starten",
726 737 "pipe.src.push": "Push",
727 738 "pipe.src.web": "Web",
739 "pipe.src.merge_request_event": "Merge request",
728 740 "edit.title": "Pipeline-Editor",
729 741 "edit.clean": "Keine Probleme gefunden",
730 742 "edit.issues": "%d Probleme",
@@ -1203,6 +1215,11 @@ var fr = map[string]string{
1203 1215 "mr.comments": "Commentaires",
1204 1216 "mr.noComments": "Pas encore de commentaires.",
1205 1217 "mr.comment": "Commentaire",
1218 "mr.pipeline": "Pipeline",
1219 "mr.noPipeline": "Pas de pipeline pour cette merge request.",
1220 "mr.block.running": "Le pipeline est encore en cours.",
1221 "mr.block.failed": "Le pipeline a échoué.",
1222 "mr.block.waiting": "Cette merge request attend un pipeline.",
1206 1223 "pipe.title": "Pipelines",
1207 1224 "pipe.ref": "branche, vide pour la branche par défaut",
1208 1225 "pipe.none": "Pas encore de pipelines. Ajoute blue-ci-cd.yml à la racine.",
@@ -1246,6 +1263,7 @@ var fr = map[string]string{
1246 1263 "pipe.play": "Lancer",
1247 1264 "pipe.src.push": "Push",
1248 1265 "pipe.src.web": "Web",
1266 "pipe.src.merge_request_event": "Merge request",
1249 1267 "edit.title": "Éditeur de pipeline",
1250 1268 "edit.clean": "Aucun problème",
1251 1269 "edit.issues": "%d problèmes",
@@ -161,6 +161,11 @@ var nl = map[string]string{
161 "mr.comments": "Reacties",
161 "mr.comments": "Reacties",
162 "mr.noComments": "Nog geen reacties.",
162 "mr.noComments": "Nog geen reacties.",
163 "mr.comment": "Reactie",
163 "mr.comment": "Reactie",
164 "mr.pipeline": "Pipeline",
165 "mr.noPipeline": "Geen pipeline voor deze merge request.",
166 "mr.block.running": "De pipeline loopt nog.",
167 "mr.block.failed": "De pipeline is mislukt.",
168 "mr.block.waiting": "Deze merge request wacht op een pipeline.",
164 "pipe.title": "Pipelines",
169 "pipe.title": "Pipelines",
165 "pipe.ref": "branch, leeg is de standaard",
170 "pipe.ref": "branch, leeg is de standaard",
166 "pipe.none": "Nog geen pipelines. Voeg blue-ci-cd.yml toe in de root.",
171 "pipe.none": "Nog geen pipelines. Voeg blue-ci-cd.yml toe in de root.",
@@ -204,6 +209,7 @@ var nl = map[string]string{
204 "pipe.play": "Start",
209 "pipe.play": "Start",
205 "pipe.src.push": "Push",
210 "pipe.src.push": "Push",
206 "pipe.src.web": "Web",
211 "pipe.src.web": "Web",
212 "pipe.src.merge_request_event": "Merge request",
207 "edit.title": "Pipeline-editor",
213 "edit.title": "Pipeline-editor",
208 "edit.clean": "Geen problemen gevonden",
214 "edit.clean": "Geen problemen gevonden",
209 "edit.issues": "%d fouten",
215 "edit.issues": "%d fouten",
@@ -682,6 +688,11 @@ var de = map[string]string{
682 "mr.comments": "Kommentare",
688 "mr.comments": "Kommentare",
683 "mr.noComments": "Noch keine Kommentare.",
689 "mr.noComments": "Noch keine Kommentare.",
684 "mr.comment": "Kommentar",
690 "mr.comment": "Kommentar",
691 "mr.pipeline": "Pipeline",
692 "mr.noPipeline": "Keine Pipeline für diesen Merge Request.",
693 "mr.block.running": "Die Pipeline läuft noch.",
694 "mr.block.failed": "Die Pipeline ist fehlgeschlagen.",
695 "mr.block.waiting": "Dieser Merge Request wartet auf eine Pipeline.",
685 "pipe.title": "Pipelines",
696 "pipe.title": "Pipelines",
686 "pipe.ref": "Branch, leer ist der Standard",
697 "pipe.ref": "Branch, leer ist der Standard",
687 "pipe.none": "Noch keine Pipelines. Lege blue-ci-cd.yml im Stammverzeichnis an.",
698 "pipe.none": "Noch keine Pipelines. Lege blue-ci-cd.yml im Stammverzeichnis an.",
@@ -725,6 +736,7 @@ var de = map[string]string{
725 "pipe.play": "Starten",
736 "pipe.play": "Starten",
726 "pipe.src.push": "Push",
737 "pipe.src.push": "Push",
727 "pipe.src.web": "Web",
738 "pipe.src.web": "Web",
739 "pipe.src.merge_request_event": "Merge request",
728 "edit.title": "Pipeline-Editor",
740 "edit.title": "Pipeline-Editor",
729 "edit.clean": "Keine Probleme gefunden",
741 "edit.clean": "Keine Probleme gefunden",
730 "edit.issues": "%d Probleme",
742 "edit.issues": "%d Probleme",
@@ -1203,6 +1215,11 @@ var fr = map[string]string{
1203 "mr.comments": "Commentaires",
1215 "mr.comments": "Commentaires",
1204 "mr.noComments": "Pas encore de commentaires.",
1216 "mr.noComments": "Pas encore de commentaires.",
1205 "mr.comment": "Commentaire",
1217 "mr.comment": "Commentaire",
1218 "mr.pipeline": "Pipeline",
1219 "mr.noPipeline": "Pas de pipeline pour cette merge request.",
1220 "mr.block.running": "Le pipeline est encore en cours.",
1221 "mr.block.failed": "Le pipeline a échoué.",
1222 "mr.block.waiting": "Cette merge request attend un pipeline.",
1206 "pipe.title": "Pipelines",
1223 "pipe.title": "Pipelines",
1207 "pipe.ref": "branche, vide pour la branche par défaut",
1224 "pipe.ref": "branche, vide pour la branche par défaut",
1208 "pipe.none": "Pas encore de pipelines. Ajoute blue-ci-cd.yml à la racine.",
1225 "pipe.none": "Pas encore de pipelines. Ajoute blue-ci-cd.yml à la racine.",
@@ -1246,6 +1263,7 @@ var fr = map[string]string{
1246 "pipe.play": "Lancer",
1263 "pipe.play": "Lancer",
1247 "pipe.src.push": "Push",
1264 "pipe.src.push": "Push",
1248 "pipe.src.web": "Web",
1265 "pipe.src.web": "Web",
1266 "pipe.src.merge_request_event": "Merge request",
1249 "edit.title": "Éditeur de pipeline",
1267 "edit.title": "Éditeur de pipeline",
1250 "edit.clean": "Aucun problème",
1268 "edit.clean": "Aucun problème",
1251 "edit.issues": "%d problèmes",
1269 "edit.issues": "%d problèmes",
internal/server/events.go +23 −58
@@ -32,7 +32,7 @@ func (s *Server) AfterPush(repo store.Repo, user store.User, before, after map[s
32 32 continue
33 33 }
34 34 if strings.HasPrefix(ref, "refs/tags/") {
35 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref])
35 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
36 36 continue
37 37 }
38 38 branch, ok := gitrepo.BranchOfRef(ref)
@@ -40,7 +40,8 @@ func (s *Server) AfterPush(repo store.Repo, user store.User, before, after map[s
40 40 continue
41 41 }
42 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])
43 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
44 s.startMergeRequestPipelines(context.Background(), repo, sha, branch, actor, before[ref])
44 45 web := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath()
45 46 s.Dispatch(repo.ID, webhook.Event{
46 47 Type: webhook.EventPush,
@@ -58,71 +59,35 @@ func isZero(sha string) bool {
58 59 return strings.Trim(sha, "0") == ""
59 60 }
60 61
61 func (s *Server) startPipeline(ctx context.Context, repo store.Repo, sha, ref, actor, source, before string) {
62 func (s *Server) startPipeline(ctx context.Context, repo store.Repo, sha, ref, actor, source, before string, mr *ci.MRInfo) int64 {
62 63 if !s.Cfg.CI.Enabled || s.StoragePath() == "" {
63 return
64 }
65 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
66 raw, err := exec.CommandContext(ctx, s.Git, "-C", dir, "show", sha+":blue-ci-cd.yml").Output()
67 if err != nil {
68 return
64 return 0
69 65 }
70 read := func(path string) ([]byte, error) {
71 if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") {
72 return nil, i18n.E("ci.err.rel", path)
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)
73 71 }
74 return exec.CommandContext(ctx, s.Git, "-C", dir, "show", sha+":"+path).Output()
72 if mr != nil && id != 0 {
73 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
74 }
75 return id
75 76 }
76 spec, err := ci.ParseIn(raw, read)
77 if err != nil {
78 _ = s.Store.RecordConfigFailure(ctx, repo.ID, sha, ref, err.Error())
79 return
77 if len(jobs) == 0 {
78 return 0
80 79 }
81 name, kind := ci.SplitRef(ref)
82 title := strings.TrimSpace(gitOne(ctx, s.Git, dir, "log", "-1", "--format=%s", sha))
83 files := gitLines(ctx, s.Git, dir, "ls-tree", "-r", "--name-only", sha)
84 changed := changedFiles(ctx, s.Git, dir, sha, before, files)
85 env := ci.Variables(ci.Ref{
86 SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title,
87 DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(),
88 })
89 active, err := spec.Select(ci.Eval{Env: env, Changed: changed, Files: files})
80 id, err := s.Store.CreatePipeline(ctx, repo.ID, sha, ref, source, actor, title, jobs)
90 81 if err != nil {
91 _ = s.Store.RecordConfigFailure(ctx, repo.ID, sha, ref, err.Error())
92 return
93 }
94 if len(active) == 0 {
95 return
96 }
97 var jobs []store.NewJob
98 for i, j := range active {
99 vars := map[string]string{}
100 for k, v := range spec.Variables {
101 vars[k] = v
102 }
103 for k, v := range j.Variables {
104 vars[k] = v
82 if s.Log != nil {
83 s.Log.Error("pipeline", "err", err)
105 84 }
106 needs := j.Needs
107 if needs == nil {
108 needs = []string{}
109 }
110 paths := j.Artifacts.Paths
111 if paths == nil {
112 paths = []string{}
113 }
114 jobs = append(jobs, store.NewJob{
115 Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image,
116 Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"),
117 Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet,
118 Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn,
119 AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags,
120 CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup,
121 })
85 return 0
122 86 }
123 if _, err := s.Store.CreatePipeline(ctx, repo.ID, sha, ref, source, actor, title, jobs); err != nil && s.Log != nil {
124 s.Log.Error("pipeline", "err", err)
87 if mr != nil {
88 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
125 89 }
90 return id
126 91 }
127 92
128 93 func gitOne(ctx context.Context, gitBin, dir string, args ...string) string {
@@ -32,7 +32,7 @@ func (s *Server) AfterPush(repo store.Repo, user store.User, before, after map[s
32 continue
32 continue
33 }
33 }
34 if strings.HasPrefix(ref, "refs/tags/") {
34 if strings.HasPrefix(ref, "refs/tags/") {
35 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref])
35 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
36 continue
36 continue
37 }
37 }
38 branch, ok := gitrepo.BranchOfRef(ref)
38 branch, ok := gitrepo.BranchOfRef(ref)
@@ -40,7 +40,8 @@ func (s *Server) AfterPush(repo store.Repo, user store.User, before, after map[s
40 continue
40 continue
41 }
41 }
42 s.recordPushContributions(repo, user, map[string]string{ref: before[ref]}, map[string]string{ref: sha})
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])
43 s.startPipeline(context.Background(), repo, sha, ref, actor, "push", before[ref], nil)
44 s.startMergeRequestPipelines(context.Background(), repo, sha, branch, actor, before[ref])
44 web := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath()
45 web := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath()
45 s.Dispatch(repo.ID, webhook.Event{
46 s.Dispatch(repo.ID, webhook.Event{
46 Type: webhook.EventPush,
47 Type: webhook.EventPush,
@@ -58,71 +59,35 @@ func isZero(sha string) bool {
58 return strings.Trim(sha, "0") == ""
59 return strings.Trim(sha, "0") == ""
59}
60}
60
61
61func (s *Server) startPipeline(ctx context.Context, repo store.Repo, sha, ref, actor, source, before string) {
62func (s *Server) startPipeline(ctx context.Context, repo store.Repo, sha, ref, actor, source, before string, mr *ci.MRInfo) int64 {
62 if !s.Cfg.CI.Enabled || s.StoragePath() == "" {
63 if !s.Cfg.CI.Enabled || s.StoragePath() == "" {
63 return
64 return 0
64 }
65 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
66 raw, err := exec.CommandContext(ctx, s.Git, "-C", dir, "show", sha+":blue-ci-cd.yml").Output()
67 if err != nil {
68 return
69 }
65 }
70 read := func(path string) ([]byte, error) {
66 title, jobs, broken := s.planPipeline(ctx, repo, sha, ref, source, before, mr)
71 if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") {
67 if broken != nil {
72 return nil, i18n.E("ci.err.rel", path)
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)
73 }
71 }
74 return exec.CommandContext(ctx, s.Git, "-C", dir, "show", sha+":"+path).Output()
72 if mr != nil && id != 0 {
73 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
74 }
75 return id
75 }
76 }
76 spec, err := ci.ParseIn(raw, read)
77 if len(jobs) == 0 {
77 if err != nil {
78 return 0
78 _ = s.Store.RecordConfigFailure(ctx, repo.ID, sha, ref, err.Error())
79 return
80 }
79 }
81 name, kind := ci.SplitRef(ref)
80 id, err := s.Store.CreatePipeline(ctx, repo.ID, sha, ref, source, actor, title, jobs)
82 title := strings.TrimSpace(gitOne(ctx, s.Git, dir, "log", "-1", "--format=%s", sha))
83 files := gitLines(ctx, s.Git, dir, "ls-tree", "-r", "--name-only", sha)
84 changed := changedFiles(ctx, s.Git, dir, sha, before, files)
85 env := ci.Variables(ci.Ref{
86 SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title,
87 DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(),
88 })
89 active, err := spec.Select(ci.Eval{Env: env, Changed: changed, Files: files})
90 if err != nil {
81 if err != nil {
91 _ = s.Store.RecordConfigFailure(ctx, repo.ID, sha, ref, err.Error())
82 if s.Log != nil {
92 return
83 s.Log.Error("pipeline", "err", err)
93 }
94 if len(active) == 0 {
95 return
96 }
97 var jobs []store.NewJob
98 for i, j := range active {
99 vars := map[string]string{}
100 for k, v := range spec.Variables {
101 vars[k] = v
102 }
103 for k, v := range j.Variables {
104 vars[k] = v
105 }
84 }
106 needs := j.Needs
85 return 0
107 if needs == nil {
108 needs = []string{}
109 }
110 paths := j.Artifacts.Paths
111 if paths == nil {
112 paths = []string{}
113 }
114 jobs = append(jobs, store.NewJob{
115 Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image,
116 Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"),
117 Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet,
118 Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn,
119 AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags,
120 CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup,
121 })
122 }
86 }
123 if _, err := s.Store.CreatePipeline(ctx, repo.ID, sha, ref, source, actor, title, jobs); err != nil && s.Log != nil {
87 if mr != nil {
124 s.Log.Error("pipeline", "err", err)
88 _ = s.Store.AttachPipelineMR(ctx, id, mr.ID)
125 }
89 }
90 return id
126}
91}
127
92
128func gitOne(ctx context.Context, gitBin, dir string, args ...string) string {
93func gitOne(ctx context.Context, gitBin, dir string, args ...string) string {
internal/server/mr_pipeline.go added +156 −0
@@ -0,0 +1,156 @@
1 package server
2
3 import (
4 "context"
5 "net/http"
6 "os/exec"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/ci"
10 "gitlab.com/Timfa/bluegit/internal/gitrepo"
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13 )
14
15 // planPipeline leest blue-ci-cd.yml en kiest de jobs.
16 // Een ontbrekend bestand geeft geen jobs en geen fout.
17 // broken is een kapot bestand; de aanroeper bewaart dan een mislukte pipeline.
18 func (s *Server) planPipeline(ctx context.Context, repo store.Repo, sha, ref, source, before string, mr *ci.MRInfo) (string, []store.NewJob, error) {
19 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
20 raw, err := gitShow(ctx, s.Git, dir, sha, "blue-ci-cd.yml")
21 if err != nil || len(raw) == 0 {
22 return "", nil, nil
23 }
24 read := func(path string) ([]byte, error) {
25 return gitShow(ctx, s.Git, dir, sha, path)
26 }
27 spec, err := ci.ParseIn(raw, read)
28 if err != nil {
29 return "", nil, err
30 }
31 name, kind := ci.SplitRef(ref)
32 title := strings.TrimSpace(gitOne(ctx, s.Git, dir, "log", "-1", "--format=%s", sha))
33 files := gitLines(ctx, s.Git, dir, "ls-tree", "-r", "--name-only", sha)
34 changed := changedFiles(ctx, s.Git, dir, sha, before, files)
35 base := strings.TrimRight(s.Cfg.BaseURL, "/")
36 env := ci.Variables(ci.Ref{
37 SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title,
38 DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(),
39 ProjectURL: base + "/" + repo.FullPath(), APIURL: base + "/api/v1", MR: mr,
40 })
41 active, err := spec.Select(ci.Eval{Env: env, Changed: changed, Files: files})
42 if err != nil {
43 return "", nil, err
44 }
45 var jobs []store.NewJob
46 for i, j := range active {
47 vars := map[string]string{}
48 for k, v := range spec.Variables {
49 vars[k] = v
50 }
51 for k, v := range j.Variables {
52 vars[k] = v
53 }
54 needs := j.Needs
55 if needs == nil {
56 needs = []string{}
57 }
58 paths := j.Artifacts.Paths
59 if paths == nil {
60 paths = []string{}
61 }
62 jobs = append(jobs, store.NewJob{
63 Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image,
64 Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"),
65 Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet,
66 Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn,
67 AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags,
68 CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup,
69 })
70 }
71 return title, jobs, nil
72 }
73
74 func gitShow(ctx context.Context, gitBin, dir, sha, path string) ([]byte, error) {
75 if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") {
76 return nil, i18n.E("ci.err.rel", path)
77 }
78 return exec.CommandContext(ctx, gitBin, "-C", dir, "show", sha+":"+path).Output()
79 }
80
81 func (s *Server) startMergeRequestPipelines(ctx context.Context, repo store.Repo, sha, branch, actor, before string) {
82 mrs, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch)
83 if err != nil {
84 return
85 }
86 for _, mr := range mrs {
87 s.startOneMergePipeline(ctx, repo, sha, actor, before, mr)
88 }
89 }
90
91 func (s *Server) startOneMergePipeline(ctx context.Context, repo store.Repo, sha, actor, before string, mr store.MergeRequest) {
92 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
93 base := strings.TrimRight(s.Cfg.BaseURL, "/")
94 info := &ci.MRInfo{
95 ID: mr.ID, IID: mr.Number, Title: mr.Title,
96 SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch,
97 ProjectPath: repo.FullPath(), ProjectURL: base + "/" + repo.FullPath(),
98 TargetSHA: gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.TargetBranch),
99 }
100 s.startPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, actor, "merge_request_event", before, info)
101 }
102
103 // mergeBlocks zijn de vertaalde redenen waarom mergen nog niet mag.
104 // Een pipeline is verplicht zodra blue-ci-cd.yml jobs voor deze merge request kiest.
105 func (s *Server) mergeBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string {
106 if mr.Status != "open" || !s.Cfg.CI.Enabled || s.StoragePath() == "" {
107 return nil
108 }
109 ctx := r.Context()
110 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
111 sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.SourceBranch)
112 required := false
113 if sha != "" {
114 info := &ci.MRInfo{
115 ID: mr.ID, IID: mr.Number, Title: mr.Title,
116 SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch,
117 ProjectPath: repo.FullPath(),
118 }
119 _, jobs, broken := s.planPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, "merge_request_event", "", info)
120 required = broken != nil || len(jobs) > 0
121 }
122 p, err := s.Store.LatestMergePipeline(ctx, repo.ID, mr.ID)
123 if store.IsNotFound(err) {
124 if required {
125 return []string{s.tr(r, "mr.block.waiting")}
126 }
127 return nil
128 }
129 if err != nil {
130 return []string{s.tr(r, "mr.block.waiting")}
131 }
132 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
133 if err != nil {
134 return []string{s.tr(r, "mr.block.waiting")}
135 }
136 switch store.PipelineBlocksMerge(jobs) {
137 case "running":
138 return []string{s.tr(r, "mr.block.running")}
139 case "failed":
140 return []string{s.tr(r, "mr.block.failed")}
141 case "waiting":
142 if required {
143 return []string{s.tr(r, "mr.block.waiting")}
144 }
145 }
146 return nil
147 }
148
149 func (s *Server) fillMRPipeline(r *http.Request, v *View, repo store.Repo, mr store.MergeRequest) {
150 p, err := s.Store.LatestMergePipeline(r.Context(), repo.ID, mr.ID)
151 if err != nil {
152 return
153 }
154 row := s.pipeRow(r, s.langOf(r), repo, p, map[string]store.User{})
155 v.MRPipe = &row
156 }
@@ -0,0 +1,156 @@
1package server
2
3import (
4 "context"
5 "net/http"
6 "os/exec"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/ci"
10 "gitlab.com/Timfa/bluegit/internal/gitrepo"
11 "gitlab.com/Timfa/bluegit/internal/i18n"
12 "gitlab.com/Timfa/bluegit/internal/store"
13)
14
15// planPipeline leest blue-ci-cd.yml en kiest de jobs.
16// Een ontbrekend bestand geeft geen jobs en geen fout.
17// broken is een kapot bestand; de aanroeper bewaart dan een mislukte pipeline.
18func (s *Server) planPipeline(ctx context.Context, repo store.Repo, sha, ref, source, before string, mr *ci.MRInfo) (string, []store.NewJob, error) {
19 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
20 raw, err := gitShow(ctx, s.Git, dir, sha, "blue-ci-cd.yml")
21 if err != nil || len(raw) == 0 {
22 return "", nil, nil
23 }
24 read := func(path string) ([]byte, error) {
25 return gitShow(ctx, s.Git, dir, sha, path)
26 }
27 spec, err := ci.ParseIn(raw, read)
28 if err != nil {
29 return "", nil, err
30 }
31 name, kind := ci.SplitRef(ref)
32 title := strings.TrimSpace(gitOne(ctx, s.Git, dir, "log", "-1", "--format=%s", sha))
33 files := gitLines(ctx, s.Git, dir, "ls-tree", "-r", "--name-only", sha)
34 changed := changedFiles(ctx, s.Git, dir, sha, before, files)
35 base := strings.TrimRight(s.Cfg.BaseURL, "/")
36 env := ci.Variables(ci.Ref{
37 SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title,
38 DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(),
39 ProjectURL: base + "/" + repo.FullPath(), APIURL: base + "/api/v1", MR: mr,
40 })
41 active, err := spec.Select(ci.Eval{Env: env, Changed: changed, Files: files})
42 if err != nil {
43 return "", nil, err
44 }
45 var jobs []store.NewJob
46 for i, j := range active {
47 vars := map[string]string{}
48 for k, v := range spec.Variables {
49 vars[k] = v
50 }
51 for k, v := range j.Variables {
52 vars[k] = v
53 }
54 needs := j.Needs
55 if needs == nil {
56 needs = []string{}
57 }
58 paths := j.Artifacts.Paths
59 if paths == nil {
60 paths = []string{}
61 }
62 jobs = append(jobs, store.NewJob{
63 Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image,
64 Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"),
65 Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet,
66 Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn,
67 AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags,
68 CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup,
69 })
70 }
71 return title, jobs, nil
72}
73
74func gitShow(ctx context.Context, gitBin, dir, sha, path string) ([]byte, error) {
75 if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") {
76 return nil, i18n.E("ci.err.rel", path)
77 }
78 return exec.CommandContext(ctx, gitBin, "-C", dir, "show", sha+":"+path).Output()
79}
80
81func (s *Server) startMergeRequestPipelines(ctx context.Context, repo store.Repo, sha, branch, actor, before string) {
82 mrs, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch)
83 if err != nil {
84 return
85 }
86 for _, mr := range mrs {
87 s.startOneMergePipeline(ctx, repo, sha, actor, before, mr)
88 }
89}
90
91func (s *Server) startOneMergePipeline(ctx context.Context, repo store.Repo, sha, actor, before string, mr store.MergeRequest) {
92 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
93 base := strings.TrimRight(s.Cfg.BaseURL, "/")
94 info := &ci.MRInfo{
95 ID: mr.ID, IID: mr.Number, Title: mr.Title,
96 SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch,
97 ProjectPath: repo.FullPath(), ProjectURL: base + "/" + repo.FullPath(),
98 TargetSHA: gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.TargetBranch),
99 }
100 s.startPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, actor, "merge_request_event", before, info)
101}
102
103// mergeBlocks zijn de vertaalde redenen waarom mergen nog niet mag.
104// Een pipeline is verplicht zodra blue-ci-cd.yml jobs voor deze merge request kiest.
105func (s *Server) mergeBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string {
106 if mr.Status != "open" || !s.Cfg.CI.Enabled || s.StoragePath() == "" {
107 return nil
108 }
109 ctx := r.Context()
110 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
111 sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.SourceBranch)
112 required := false
113 if sha != "" {
114 info := &ci.MRInfo{
115 ID: mr.ID, IID: mr.Number, Title: mr.Title,
116 SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch,
117 ProjectPath: repo.FullPath(),
118 }
119 _, jobs, broken := s.planPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, "merge_request_event", "", info)
120 required = broken != nil || len(jobs) > 0
121 }
122 p, err := s.Store.LatestMergePipeline(ctx, repo.ID, mr.ID)
123 if store.IsNotFound(err) {
124 if required {
125 return []string{s.tr(r, "mr.block.waiting")}
126 }
127 return nil
128 }
129 if err != nil {
130 return []string{s.tr(r, "mr.block.waiting")}
131 }
132 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
133 if err != nil {
134 return []string{s.tr(r, "mr.block.waiting")}
135 }
136 switch store.PipelineBlocksMerge(jobs) {
137 case "running":
138 return []string{s.tr(r, "mr.block.running")}
139 case "failed":
140 return []string{s.tr(r, "mr.block.failed")}
141 case "waiting":
142 if required {
143 return []string{s.tr(r, "mr.block.waiting")}
144 }
145 }
146 return nil
147}
148
149func (s *Server) fillMRPipeline(r *http.Request, v *View, repo store.Repo, mr store.MergeRequest) {
150 p, err := s.Store.LatestMergePipeline(r.Context(), repo.ID, mr.ID)
151 if err != nil {
152 return
153 }
154 row := s.pipeRow(r, s.langOf(r), repo, p, map[string]store.User{})
155 v.MRPipe = &row
156}
internal/server/mr_pipeline_test.go added +190 −0
@@ -0,0 +1,190 @@
1 package server
2
3 import (
4 "context"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/auth"
15 "gitlab.com/Timfa/bluegit/internal/config"
16 "gitlab.com/Timfa/bluegit/internal/store"
17 )
18
19 func TestMergeRequestPipelineBlocksUntilFinished(t *testing.T) {
20 if _, err := exec.LookPath("git"); err != nil {
21 t.Skip("git ontbreekt")
22 }
23 storage := t.TempDir()
24 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
25 if err != nil {
26 t.Fatal(err)
27 }
28 t.Cleanup(func() { _ = st.Close() })
29 cfg := config.Config{
30 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
31 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
32 Git: config.Git{Binary: "git", DefaultBranch: "master"},
33 CI: config.CI{Enabled: true, Docker: "docker"},
34 }
35 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
36 ts := httptest.NewServer(srv.Handler())
37 t.Cleanup(ts.Close)
38 jar, _ := cookiejar.New(nil)
39 tim := &http.Client{Jar: jar}
40 res, err := tim.Get(ts.URL + "/login")
41 if err != nil {
42 t.Fatal(err)
43 }
44 page := read(t, res)
45 res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{"csrf": {csrfOf(t, page)}, "name": {"mrpipes"}, "visibility": {"public"}})
46 if err != nil {
47 t.Fatal(err)
48 }
49 page = read(t, res)
50 res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}})
51 if err != nil {
52 t.Fatal(err)
53 }
54 token := between(read(t, res), `<p class="code">`, "</p>")
55 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/mrpipes.git"
56 work := t.TempDir()
57 git(t, work, "init", "-b", "master")
58 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
59 git(t, work, "config", "user.name", "tim")
60 yml := `stages: [test]
61 push_job:
62 stage: test
63 image: alpine:3.20
64 script: ["echo push"]
65 mr_job:
66 stage: test
67 image: alpine:3.20
68 script: ["echo mr"]
69 rules:
70 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
71 soft:
72 stage: test
73 image: alpine:3.20
74 script: ["echo soft"]
75 allow_failure: true
76 rules:
77 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
78 `
79 writeFile(t, filepath.Join(work, "blue-ci-cd.yml"), yml)
80 git(t, work, "add", "blue-ci-cd.yml")
81 git(t, work, "commit", "-m", "ci")
82 git(t, work, "remote", "add", "origin", remote)
83 git(t, work, "push", "-u", "origin", "master")
84 git(t, work, "checkout", "-b", "feature")
85 writeFile(t, filepath.Join(work, "README.md"), "feature\n")
86 git(t, work, "add", "README.md")
87 git(t, work, "commit", "-m", "feature")
88 git(t, work, "push", "-u", "origin", "feature")
89
90 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests")
91 if err != nil {
92 t.Fatal(err)
93 }
94 page = read(t, res)
95 res, err = tim.PostForm(ts.URL+"/tim/mrpipes/-/merge-requests", url.Values{
96 "csrf": {csrfOf(t, page)}, "title": {"voeg samen"}, "source": {"feature"}, "target": {"master"},
97 })
98 if err != nil {
99 t.Fatal(err)
100 }
101 page = read(t, res)
102 if !strings.Contains(page, "mr_job") || !strings.Contains(page, "soft") || strings.Contains(page, "push_job") {
103 t.Fatalf("mr-pipeline op de pagina:\n%s", page)
104 }
105 if !strings.Contains(page, "The pipeline is still running.") || !strings.Contains(page, "disabled") {
106 t.Fatalf("merge hoort geblokkeerd:\n%s", page)
107 }
108
109 ctx := context.Background()
110 repo, err := st.RepoByPath(ctx, "tim", "mrpipes")
111 if err != nil {
112 t.Fatal(err)
113 }
114 pipes, err := st.ListPipelines(ctx, repo.ID)
115 if err != nil {
116 t.Fatal(err)
117 }
118 var mrPipe store.Pipeline
119 pushNames := map[string]bool{}
120 for _, p := range pipes {
121 jobs, err := st.JobsByPipeline(ctx, p.ID)
122 if err != nil {
123 t.Fatal(err)
124 }
125 if p.Source == "push" {
126 for _, j := range jobs {
127 pushNames[j.Name] = true
128 }
129 continue
130 }
131 if p.Source == "merge_request_event" && (mrPipe.ID == 0 || p.ID > mrPipe.ID) {
132 mrPipe = p
133 }
134 }
135 if pushNames["mr_job"] || pushNames["soft"] || !pushNames["push_job"] {
136 t.Fatalf("push-jobs: %v", pushNames)
137 }
138 if mrPipe.ID == 0 || mrPipe.MRID == 0 {
139 t.Fatalf("mr-pipeline: %+v", mrPipe)
140 }
141 res, err = tim.PostForm(ts.URL+"/tim/mrpipes/-/merge-requests/1/merge", url.Values{"csrf": {csrfOf(t, page)}})
142 if err != nil {
143 t.Fatal(err)
144 }
145 if body := read(t, res); !strings.Contains(body, "The pipeline is still running.") || !strings.Contains(body, ">open<") && !strings.Contains(body, "open") {
146 t.Fatalf("merge tijdens de run:\n%s", body)
147 }
148 mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1)
149 if err != nil || mr.Status != "open" {
150 t.Fatalf("status %v %v", mr.Status, err)
151 }
152
153 jobs, _ := st.JobsByPipeline(ctx, mrPipe.ID)
154 for _, j := range jobs {
155 if j.Name == "mr_job" {
156 if err := st.FinishJob(ctx, j.ID, "success", "mr ok\n"); err != nil {
157 t.Fatal(err)
158 }
159 }
160 }
161 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests/1")
162 if err != nil {
163 t.Fatal(err)
164 }
165 if body := read(t, res); !strings.Contains(body, "The pipeline is still running.") {
166 t.Fatalf("allow_failure loopt nog:\n%s", body)
167 }
168 jobs, _ = st.JobsByPipeline(ctx, mrPipe.ID)
169 for _, j := range jobs {
170 if j.Name == "soft" {
171 if err := st.FinishJob(ctx, j.ID, "failed", "zacht\n"); err != nil {
172 t.Fatal(err)
173 }
174 }
175 }
176 if _, err := st.RefreshPipelineStatus(ctx, mrPipe.ID); err != nil {
177 t.Fatal(err)
178 }
179 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests/1")
180 if err != nil {
181 t.Fatal(err)
182 }
183 page = read(t, res)
184 if strings.Contains(page, "The pipeline is still running.") || strings.Contains(page, "disabled") {
185 t.Fatalf("merge hoort open:\n%s", page)
186 }
187 if !strings.Contains(page, "Warning") && !strings.Contains(page, "Passed") {
188 t.Fatalf("status na allow_failure:\n%s", page)
189 }
190 }
@@ -0,0 +1,190 @@
1package server
2
3import (
4 "context"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/auth"
15 "gitlab.com/Timfa/bluegit/internal/config"
16 "gitlab.com/Timfa/bluegit/internal/store"
17)
18
19func TestMergeRequestPipelineBlocksUntilFinished(t *testing.T) {
20 if _, err := exec.LookPath("git"); err != nil {
21 t.Skip("git ontbreekt")
22 }
23 storage := t.TempDir()
24 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
25 if err != nil {
26 t.Fatal(err)
27 }
28 t.Cleanup(func() { _ = st.Close() })
29 cfg := config.Config{
30 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
31 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
32 Git: config.Git{Binary: "git", DefaultBranch: "master"},
33 CI: config.CI{Enabled: true, Docker: "docker"},
34 }
35 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
36 ts := httptest.NewServer(srv.Handler())
37 t.Cleanup(ts.Close)
38 jar, _ := cookiejar.New(nil)
39 tim := &http.Client{Jar: jar}
40 res, err := tim.Get(ts.URL + "/login")
41 if err != nil {
42 t.Fatal(err)
43 }
44 page := read(t, res)
45 res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{"csrf": {csrfOf(t, page)}, "name": {"mrpipes"}, "visibility": {"public"}})
46 if err != nil {
47 t.Fatal(err)
48 }
49 page = read(t, res)
50 res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}})
51 if err != nil {
52 t.Fatal(err)
53 }
54 token := between(read(t, res), `<p class="code">`, "</p>")
55 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/mrpipes.git"
56 work := t.TempDir()
57 git(t, work, "init", "-b", "master")
58 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
59 git(t, work, "config", "user.name", "tim")
60 yml := `stages: [test]
61push_job:
62 stage: test
63 image: alpine:3.20
64 script: ["echo push"]
65mr_job:
66 stage: test
67 image: alpine:3.20
68 script: ["echo mr"]
69 rules:
70 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
71soft:
72 stage: test
73 image: alpine:3.20
74 script: ["echo soft"]
75 allow_failure: true
76 rules:
77 - if: $CI_PIPELINE_SOURCE == "merge_request_event"
78`
79 writeFile(t, filepath.Join(work, "blue-ci-cd.yml"), yml)
80 git(t, work, "add", "blue-ci-cd.yml")
81 git(t, work, "commit", "-m", "ci")
82 git(t, work, "remote", "add", "origin", remote)
83 git(t, work, "push", "-u", "origin", "master")
84 git(t, work, "checkout", "-b", "feature")
85 writeFile(t, filepath.Join(work, "README.md"), "feature\n")
86 git(t, work, "add", "README.md")
87 git(t, work, "commit", "-m", "feature")
88 git(t, work, "push", "-u", "origin", "feature")
89
90 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests")
91 if err != nil {
92 t.Fatal(err)
93 }
94 page = read(t, res)
95 res, err = tim.PostForm(ts.URL+"/tim/mrpipes/-/merge-requests", url.Values{
96 "csrf": {csrfOf(t, page)}, "title": {"voeg samen"}, "source": {"feature"}, "target": {"master"},
97 })
98 if err != nil {
99 t.Fatal(err)
100 }
101 page = read(t, res)
102 if !strings.Contains(page, "mr_job") || !strings.Contains(page, "soft") || strings.Contains(page, "push_job") {
103 t.Fatalf("mr-pipeline op de pagina:\n%s", page)
104 }
105 if !strings.Contains(page, "The pipeline is still running.") || !strings.Contains(page, "disabled") {
106 t.Fatalf("merge hoort geblokkeerd:\n%s", page)
107 }
108
109 ctx := context.Background()
110 repo, err := st.RepoByPath(ctx, "tim", "mrpipes")
111 if err != nil {
112 t.Fatal(err)
113 }
114 pipes, err := st.ListPipelines(ctx, repo.ID)
115 if err != nil {
116 t.Fatal(err)
117 }
118 var mrPipe store.Pipeline
119 pushNames := map[string]bool{}
120 for _, p := range pipes {
121 jobs, err := st.JobsByPipeline(ctx, p.ID)
122 if err != nil {
123 t.Fatal(err)
124 }
125 if p.Source == "push" {
126 for _, j := range jobs {
127 pushNames[j.Name] = true
128 }
129 continue
130 }
131 if p.Source == "merge_request_event" && (mrPipe.ID == 0 || p.ID > mrPipe.ID) {
132 mrPipe = p
133 }
134 }
135 if pushNames["mr_job"] || pushNames["soft"] || !pushNames["push_job"] {
136 t.Fatalf("push-jobs: %v", pushNames)
137 }
138 if mrPipe.ID == 0 || mrPipe.MRID == 0 {
139 t.Fatalf("mr-pipeline: %+v", mrPipe)
140 }
141 res, err = tim.PostForm(ts.URL+"/tim/mrpipes/-/merge-requests/1/merge", url.Values{"csrf": {csrfOf(t, page)}})
142 if err != nil {
143 t.Fatal(err)
144 }
145 if body := read(t, res); !strings.Contains(body, "The pipeline is still running.") || !strings.Contains(body, ">open<") && !strings.Contains(body, "open") {
146 t.Fatalf("merge tijdens de run:\n%s", body)
147 }
148 mr, err := st.MergeRequestByNumber(ctx, repo.ID, 1)
149 if err != nil || mr.Status != "open" {
150 t.Fatalf("status %v %v", mr.Status, err)
151 }
152
153 jobs, _ := st.JobsByPipeline(ctx, mrPipe.ID)
154 for _, j := range jobs {
155 if j.Name == "mr_job" {
156 if err := st.FinishJob(ctx, j.ID, "success", "mr ok\n"); err != nil {
157 t.Fatal(err)
158 }
159 }
160 }
161 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests/1")
162 if err != nil {
163 t.Fatal(err)
164 }
165 if body := read(t, res); !strings.Contains(body, "The pipeline is still running.") {
166 t.Fatalf("allow_failure loopt nog:\n%s", body)
167 }
168 jobs, _ = st.JobsByPipeline(ctx, mrPipe.ID)
169 for _, j := range jobs {
170 if j.Name == "soft" {
171 if err := st.FinishJob(ctx, j.ID, "failed", "zacht\n"); err != nil {
172 t.Fatal(err)
173 }
174 }
175 }
176 if _, err := st.RefreshPipelineStatus(ctx, mrPipe.ID); err != nil {
177 t.Fatal(err)
178 }
179 res, err = tim.Get(ts.URL + "/tim/mrpipes/-/merge-requests/1")
180 if err != nil {
181 t.Fatal(err)
182 }
183 page = read(t, res)
184 if strings.Contains(page, "The pipeline is still running.") || strings.Contains(page, "disabled") {
185 t.Fatalf("merge hoort open:\n%s", page)
186 }
187 if !strings.Contains(page, "Warning") && !strings.Contains(page, "Passed") {
188 t.Fatalf("status na allow_failure:\n%s", page)
189 }
190}
internal/server/repo_handlers.go +13 −1
@@ -204,6 +204,10 @@ func (s *Server) createMR(w http.ResponseWriter, r *http.Request) {
204 204 return
205 205 }
206 206 num := strconv.FormatInt(mr.Number, 10)
207 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
208 if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+source); sha != "" {
209 s.startOneMergePipeline(r.Context(), repo, sha, u.Username, "", mr)
210 }
207 211 s.Dispatch(repo.ID, pushEvent(repo, "merge_request", target, "", u.Username, s.tr(r, "hook.mr", num, mr.Title), repoURL(repo)+"/-/merge-requests/"+num))
208 212 s.recordContribution(u.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
209 213 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+num, s.tr(r, "flash.mrOpened"), "")
@@ -231,6 +235,10 @@ func (s *Server) showMR(w http.ResponseWriter, r *http.Request) {
231 235 v.CanComment = role.AtLeast(access.RoleReadonly)
232 236 v.CanMerge = mr.Status == "open" && role.CanMerge(access.BranchProtected(patterns, mr.TargetBranch))
233 237 v.CanApply = mr.Status == "open" && role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch))
238 if v.CanMerge {
239 v.MergeBlocks = s.mergeBlocks(r, repo, mr)
240 }
241 s.fillMRPipeline(r, &v, repo, mr)
234 242 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
235 243 diff, err := gitrepo.DiffStat(r.Context(), s.Git, dir, mr.TargetBranch, mr.SourceBranch)
236 244 if err == nil {
@@ -307,6 +315,10 @@ func (s *Server) mergeMR(w http.ResponseWriter, r *http.Request) {
307 315 http.Error(w, s.tr(r, "err.targetAccess"), http.StatusForbidden)
308 316 return
309 317 }
318 if blocks := s.mergeBlocks(r, repo, mr); len(blocks) > 0 {
319 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(mr.Number, 10), "", blocks[0])
320 return
321 }
310 322 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
311 323 before, _ := gitrepo.Refs(r.Context(), s.Git, dir)
312 324 work := s.StoragePath() + "/tmp/merge-" + strconv.FormatInt(mr.ID, 10)
@@ -401,7 +413,7 @@ func (s *Server) runPipeline(w http.ResponseWriter, r *http.Request) {
401 413 s.redirect(w, r, repoURL(repo)+"/-/pipelines", "", s.tr(r, "err.branchMissing"))
402 414 return
403 415 }
404 s.startPipeline(r.Context(), repo, shaOut, "refs/heads/"+ref, u.Username, "web", "")
416 s.startPipeline(r.Context(), repo, shaOut, "refs/heads/"+ref, u.Username, "web", "", nil)
405 417 s.redirect(w, r, repoURL(repo)+"/-/pipelines", s.tr(r, "flash.pipeline"), "")
406 418 }
407 419
@@ -204,6 +204,10 @@ func (s *Server) createMR(w http.ResponseWriter, r *http.Request) {
204 return
204 return
205 }
205 }
206 num := strconv.FormatInt(mr.Number, 10)
206 num := strconv.FormatInt(mr.Number, 10)
207 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
208 if sha := gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "refs/heads/"+source); sha != "" {
209 s.startOneMergePipeline(r.Context(), repo, sha, u.Username, "", mr)
210 }
207 s.Dispatch(repo.ID, pushEvent(repo, "merge_request", target, "", u.Username, s.tr(r, "hook.mr", num, mr.Title), repoURL(repo)+"/-/merge-requests/"+num))
211 s.Dispatch(repo.ID, pushEvent(repo, "merge_request", target, "", u.Username, s.tr(r, "hook.mr", num, mr.Title), repoURL(repo)+"/-/merge-requests/"+num))
208 s.recordContribution(u.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
212 s.recordContribution(u.ID, repo.ID, "merge_request", mrRef(mr.ID), mr.CreatedAt)
209 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+num, s.tr(r, "flash.mrOpened"), "")
213 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+num, s.tr(r, "flash.mrOpened"), "")
@@ -231,6 +235,10 @@ func (s *Server) showMR(w http.ResponseWriter, r *http.Request) {
231 v.CanComment = role.AtLeast(access.RoleReadonly)
235 v.CanComment = role.AtLeast(access.RoleReadonly)
232 v.CanMerge = mr.Status == "open" && role.CanMerge(access.BranchProtected(patterns, mr.TargetBranch))
236 v.CanMerge = mr.Status == "open" && role.CanMerge(access.BranchProtected(patterns, mr.TargetBranch))
233 v.CanApply = mr.Status == "open" && role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch))
237 v.CanApply = mr.Status == "open" && role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch))
238 if v.CanMerge {
239 v.MergeBlocks = s.mergeBlocks(r, repo, mr)
240 }
241 s.fillMRPipeline(r, &v, repo, mr)
234 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
242 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
235 diff, err := gitrepo.DiffStat(r.Context(), s.Git, dir, mr.TargetBranch, mr.SourceBranch)
243 diff, err := gitrepo.DiffStat(r.Context(), s.Git, dir, mr.TargetBranch, mr.SourceBranch)
236 if err == nil {
244 if err == nil {
@@ -307,6 +315,10 @@ func (s *Server) mergeMR(w http.ResponseWriter, r *http.Request) {
307 http.Error(w, s.tr(r, "err.targetAccess"), http.StatusForbidden)
315 http.Error(w, s.tr(r, "err.targetAccess"), http.StatusForbidden)
308 return
316 return
309 }
317 }
318 if blocks := s.mergeBlocks(r, repo, mr); len(blocks) > 0 {
319 s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(mr.Number, 10), "", blocks[0])
320 return
321 }
310 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
322 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
311 before, _ := gitrepo.Refs(r.Context(), s.Git, dir)
323 before, _ := gitrepo.Refs(r.Context(), s.Git, dir)
312 work := s.StoragePath() + "/tmp/merge-" + strconv.FormatInt(mr.ID, 10)
324 work := s.StoragePath() + "/tmp/merge-" + strconv.FormatInt(mr.ID, 10)
@@ -401,7 +413,7 @@ func (s *Server) runPipeline(w http.ResponseWriter, r *http.Request) {
401 s.redirect(w, r, repoURL(repo)+"/-/pipelines", "", s.tr(r, "err.branchMissing"))
413 s.redirect(w, r, repoURL(repo)+"/-/pipelines", "", s.tr(r, "err.branchMissing"))
402 return
414 return
403 }
415 }
404 s.startPipeline(r.Context(), repo, shaOut, "refs/heads/"+ref, u.Username, "web", "")
416 s.startPipeline(r.Context(), repo, shaOut, "refs/heads/"+ref, u.Username, "web", "", nil)
405 s.redirect(w, r, repoURL(repo)+"/-/pipelines", s.tr(r, "flash.pipeline"), "")
417 s.redirect(w, r, repoURL(repo)+"/-/pipelines", s.tr(r, "flash.pipeline"), "")
406}
418}
407
419
internal/server/server.go +2 −0
@@ -438,6 +438,8 @@ type View struct {
438 438 Branches []BranchRow
439 439 MRs []store.MergeRequest
440 440 MR *store.MergeRequest
441 MRPipe *PipeRow
442 MergeBlocks []string
441 443 Diff string
442 444 DiffFiles []DiffFileView
443 445 Pipes []store.Pipeline
@@ -438,6 +438,8 @@ type View struct {
438 Branches []BranchRow
438 Branches []BranchRow
439 MRs []store.MergeRequest
439 MRs []store.MergeRequest
440 MR *store.MergeRequest
440 MR *store.MergeRequest
441 MRPipe *PipeRow
442 MergeBlocks []string
441 Diff string
443 Diff string
442 DiffFiles []DiffFileView
444 DiffFiles []DiffFileView
443 Pipes []store.Pipeline
445 Pipes []store.Pipeline
internal/server/static/style.css +1 −0
@@ -374,6 +374,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }
374 374 border: 2px solid #fff; border-radius: 50%; border-right-color: transparent; box-sizing: border-box;
375 375 }
376 376 .stages { display: flex; align-items: center; }
377 .mrpipe { display: flex; align-items: center; gap: 0.8rem; margin: 0.2rem 0 1rem; }
377 378 .stage { position: relative; display: flex; align-items: center; }
378 379 .stage + .stage::before { content: ""; width: 14px; height: 2px; background: #c5d0dc; margin: 0 2px; }
379 380 button.stage-dot {
@@ -374,6 +374,7 @@ svg.ico { width: 14px; height: 14px; flex: none; }
374 border: 2px solid #fff; border-radius: 50%; border-right-color: transparent; box-sizing: border-box;
374 border: 2px solid #fff; border-radius: 50%; border-right-color: transparent; box-sizing: border-box;
375}
375}
376.stages { display: flex; align-items: center; }
376.stages { display: flex; align-items: center; }
377.mrpipe { display: flex; align-items: center; gap: 0.8rem; margin: 0.2rem 0 1rem; }
377.stage { position: relative; display: flex; align-items: center; }
378.stage { position: relative; display: flex; align-items: center; }
378.stage + .stage::before { content: ""; width: 14px; height: 2px; background: #c5d0dc; margin: 0 2px; }
379.stage + .stage::before { content: ""; width: 14px; height: 2px; background: #c5d0dc; margin: 0 2px; }
379button.stage-dot {
380button.stage-dot {
internal/server/templates/collab.html +24 −1
@@ -25,11 +25,33 @@
25 25 {{template "repotabs" .}}
26 26 <p><span class="pill">{{.MR.Status}}</span> {{.MR.SourceBranch}} → {{.MR.TargetBranch}} · {{.MR.Author}}</p>
27 27 {{if .MR.Description}}<pre>{{.MR.Description}}</pre>{{end}}
28 <h2>{{t "mr.pipeline"}}</h2>
29 {{if .MRPipe}}
30 {{$pipe := .MRPipe}}
31 <div class="mrpipe">
32 <a class="pipeid" href="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}">#{{$pipe.ID}}</a>
33 <span class="status {{$pipe.Status}}"><span class="mark"></span>{{$pipe.Label}}</span>
34 <div class="stages">
35 {{range $pipe.Stages}}
36 <span class="stage">
37 <button type="button" class="stage-dot {{.Status}}" data-tip="{{.Tip}}" aria-label="{{.Tip}}"><span class="mark"></span></button>
38 <div class="stage-pop" hidden>
39 <strong>{{t "pipe.stage"}}: {{.Name}}</strong>
40 <ul>
41 {{range .Jobs}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}/jobs/{{.ID}}"><span class="stage-dot sm {{.Status}}"><span class="mark"></span></span>{{.Name}}</a></li>{{end}}
42 </ul>
43 </div>
44 </span>
45 {{end}}
46 </div>
47 </div>
48 {{else}}<p class="muted">{{t "mr.noPipeline"}}</p>{{end}}
28 49 <h2>{{t "mr.commits"}}</h2>
29 50 <pre>{{if .Diff}}{{.Diff}}{{else}}{{t "mr.noDiff"}}{{end}}</pre>
30 51 {{if .CanMerge}}
52 {{range .MergeBlocks}}<p class="flash bad">{{.}}</p>{{end}}
31 53 <form method="post" action="/{{.Repo.FullPath}}/-/merge-requests/{{.MR.Number}}/merge">
32 <input type="hidden" name="csrf" value="{{.CSRF}}"><button type="submit">{{t "action.merge"}}</button>
54 <input type="hidden" name="csrf" value="{{.CSRF}}"><button type="submit"{{if .MergeBlocks}} disabled{{end}}>{{t "action.merge"}}</button>
33 55 </form>
34 56 {{end}}
35 57 {{if and .User (eq .MR.Status "open")}}
@@ -106,6 +128,7 @@
106 128 </form>
107 129 {{end}}
108 130 <script src="/assets/suggest.js"></script>
131 <script src="/assets/pipeline.js"></script>
109 132 {{template "layout-end" .}}
110 133 {{end}}
111 134
@@ -25,11 +25,33 @@
25{{template "repotabs" .}}
25{{template "repotabs" .}}
26<p><span class="pill">{{.MR.Status}}</span> {{.MR.SourceBranch}} → {{.MR.TargetBranch}} · {{.MR.Author}}</p>
26<p><span class="pill">{{.MR.Status}}</span> {{.MR.SourceBranch}} → {{.MR.TargetBranch}} · {{.MR.Author}}</p>
27{{if .MR.Description}}<pre>{{.MR.Description}}</pre>{{end}}
27{{if .MR.Description}}<pre>{{.MR.Description}}</pre>{{end}}
28<h2>{{t "mr.pipeline"}}</h2>
29{{if .MRPipe}}
30{{$pipe := .MRPipe}}
31<div class="mrpipe">
32 <a class="pipeid" href="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}">#{{$pipe.ID}}</a>
33 <span class="status {{$pipe.Status}}"><span class="mark"></span>{{$pipe.Label}}</span>
34 <div class="stages">
35 {{range $pipe.Stages}}
36 <span class="stage">
37 <button type="button" class="stage-dot {{.Status}}" data-tip="{{.Tip}}" aria-label="{{.Tip}}"><span class="mark"></span></button>
38 <div class="stage-pop" hidden>
39 <strong>{{t "pipe.stage"}}: {{.Name}}</strong>
40 <ul>
41 {{range .Jobs}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}/jobs/{{.ID}}"><span class="stage-dot sm {{.Status}}"><span class="mark"></span></span>{{.Name}}</a></li>{{end}}
42 </ul>
43 </div>
44 </span>
45 {{end}}
46 </div>
47</div>
48{{else}}<p class="muted">{{t "mr.noPipeline"}}</p>{{end}}
28<h2>{{t "mr.commits"}}</h2>
49<h2>{{t "mr.commits"}}</h2>
29<pre>{{if .Diff}}{{.Diff}}{{else}}{{t "mr.noDiff"}}{{end}}</pre>
50<pre>{{if .Diff}}{{.Diff}}{{else}}{{t "mr.noDiff"}}{{end}}</pre>
30{{if .CanMerge}}
51{{if .CanMerge}}
52{{range .MergeBlocks}}<p class="flash bad">{{.}}</p>{{end}}
31<form method="post" action="/{{.Repo.FullPath}}/-/merge-requests/{{.MR.Number}}/merge">
53<form method="post" action="/{{.Repo.FullPath}}/-/merge-requests/{{.MR.Number}}/merge">
32 <input type="hidden" name="csrf" value="{{.CSRF}}"><button type="submit">{{t "action.merge"}}</button>
54 <input type="hidden" name="csrf" value="{{.CSRF}}"><button type="submit"{{if .MergeBlocks}} disabled{{end}}>{{t "action.merge"}}</button>
33</form>
55</form>
34{{end}}
56{{end}}
35{{if and .User (eq .MR.Status "open")}}
57{{if and .User (eq .MR.Status "open")}}
@@ -106,6 +128,7 @@
106</form>
128</form>
107{{end}}
129{{end}}
108<script src="/assets/suggest.js"></script>
130<script src="/assets/suggest.js"></script>
131<script src="/assets/pipeline.js"></script>
109{{template "layout-end" .}}
132{{template "layout-end" .}}
110{{end}}
133{{end}}
111
134
internal/store/flow.go +79 −7
@@ -229,6 +229,74 @@ func (s *Store) SetMergeRequestStatus(ctx context.Context, id int64, status stri
229 229 return err
230 230 }
231 231
232 // OpenMergeRequestsBySource geeft open verzoeken waarvan de bronbranch deze is.
233 func (s *Store) OpenMergeRequestsBySource(ctx context.Context, repoID int64, branch string) ([]MergeRequest, error) {
234 rows, err := s.db.QueryContext(ctx, mrSelect+` WHERE m.repo_id = ? AND m.source_branch = ? AND m.status = 'open' ORDER BY m.number`, repoID, branch)
235 if err != nil {
236 return nil, err
237 }
238 defer rows.Close()
239 return scanMRList(rows)
240 }
241
242 func scanMRList(rows *sql.Rows) ([]MergeRequest, error) {
243 var out []MergeRequest
244 for rows.Next() {
245 var m MergeRequest
246 var created, updated string
247 if err := rows.Scan(&m.ID, &m.RepoID, &m.Number, &m.Title, &m.Description, &m.SourceBranch, &m.TargetBranch, &m.AuthorID, &m.Author, &m.Status, &created, &updated); err != nil {
248 return nil, err
249 }
250 m.CreatedAt = parseTime(created)
251 m.UpdatedAt = parseTime(updated)
252 out = append(out, m)
253 }
254 return out, rows.Err()
255 }
256
257 // AttachPipelineMR koppelt een pipeline aan een merge request.
258 func (s *Store) AttachPipelineMR(ctx context.Context, pipelineID, mrID int64) error {
259 _, err := s.db.ExecContext(ctx, `UPDATE pipelines SET mr_id = ? WHERE id = ?`, mrID, pipelineID)
260 return err
261 }
262
263 // LatestMergePipeline is de nieuwste pipeline van deze merge request.
264 func (s *Store) LatestMergePipeline(ctx context.Context, repoID, mrID int64) (Pipeline, error) {
265 p, err := scanPipelineRow(s.db.QueryRowContext(ctx, pipelineCols+` WHERE repo_id = ? AND mr_id = ? ORDER BY id DESC LIMIT 1`, repoID, mrID))
266 if errors.Is(err, sql.ErrNoRows) {
267 return Pipeline{}, errNotFound
268 }
269 return p, err
270 }
271
272 // PipelineBlocksMerge zegt of deze jobs een merge tegenhouden.
273 // Een lege tekst betekent dat elke job klaar is en geen harde fout heeft.
274 // allow_failure mag mislukt zijn, maar moet wel af zijn.
275 func PipelineBlocksMerge(jobs []Job) string {
276 if len(jobs) == 0 {
277 return "waiting"
278 }
279 hard, open := false, false
280 for _, j := range jobs {
281 switch j.Status {
282 case "success", "skipped":
283 case "failed":
284 if !j.AllowFailure {
285 hard = true
286 }
287 default:
288 open = true
289 }
290 }
291 if open {
292 return "running"
293 }
294 if hard {
295 return "failed"
296 }
297 return ""
298 }
299
232 300 // NewJob is een job die nog de wachtrij in moet.
233 301 type NewJob struct {
234 302 Name string
@@ -266,6 +334,7 @@ type Pipeline struct {
266 334 CreatedAt time.Time
267 335 StartedAt time.Time
268 336 FinishedAt time.Time
337 MRID int64
269 338 }
270 339
271 340 // Job is één container in een pipeline.
@@ -302,24 +371,27 @@ type Job struct {
302 371 }
303 372
304 373 // RecordConfigFailure bewaart een pipeline die niet kon starten.
305 func (s *Store) RecordConfigFailure(ctx context.Context, repoID int64, sha, ref, message string) error {
374 func (s *Store) RecordConfigFailure(ctx context.Context, repoID int64, sha, ref, message string) (int64, error) {
306 375 tx, err := s.db.BeginTx(ctx, nil)
307 376 if err != nil {
308 return err
377 return 0, err
309 378 }
310 379 defer func() { _ = tx.Rollback() }()
311 380 ts := now()
312 381 res, err := tx.ExecContext(ctx, `INSERT INTO pipelines (repo_id, commit_sha, ref, status, created_at, finished_at) VALUES (?, ?, ?, 'failed', ?, ?)`, repoID, sha, ref, ts, ts)
313 382 if err != nil {
314 return err
383 return 0, err
315 384 }
316 385 id, _ := res.LastInsertId()
317 386 _, err = tx.ExecContext(ctx, `INSERT INTO jobs (pipeline_id, repo_id, name, stage, position, status, image, script, needs_json, needs_set, variables_json, artifact_paths, artifact_expire_seconds, allow_failure, log, finished_at)
318 387 VALUES (?, ?, 'blue-ci-cd', 'test', 0, 'failed', 'alpine:3.20', 'exit 1', '[]', 0, '{}', '[]', 0, 0, ?, ?)`, id, repoID, message, ts)
319 388 if err != nil {
320 return err
389 return 0, err
390 }
391 if err := tx.Commit(); err != nil {
392 return 0, err
321 393 }
322 return tx.Commit()
394 return id, nil
323 395 }
324 396
325 397 // CreatePipeline maakt een pipeline met jobs in de wachtrij.
@@ -392,7 +464,7 @@ func (s *Store) ListPipelines(ctx context.Context, repoID int64) ([]Pipeline, er
392 464 return out, rows.Err()
393 465 }
394 466
395 const pipelineCols = `SELECT id, repo_id, commit_sha, ref, status, created_at, finished_at, source, actor, commit_title, started_at FROM pipelines`
467 const pipelineCols = `SELECT id, repo_id, commit_sha, ref, status, created_at, finished_at, source, actor, commit_title, started_at, mr_id FROM pipelines`
396 468
397 469 func scanPipeline(rows *sql.Rows) (Pipeline, error) {
398 470 return scanPipelineRow(rows)
@@ -401,7 +473,7 @@ func scanPipeline(rows *sql.Rows) (Pipeline, error) {
401 473 func scanPipelineRow(sc scanner) (Pipeline, error) {
402 474 var p Pipeline
403 475 var created, finished, started sql.NullString
404 if err := sc.Scan(&p.ID, &p.RepoID, &p.CommitSHA, &p.Ref, &p.Status, &created, &finished, &p.Source, &p.Actor, &p.CommitTitle, &started); err != nil {
476 if err := sc.Scan(&p.ID, &p.RepoID, &p.CommitSHA, &p.Ref, &p.Status, &created, &finished, &p.Source, &p.Actor, &p.CommitTitle, &started, &p.MRID); err != nil {
405 477 return Pipeline{}, err
406 478 }
407 479 p.CreatedAt = parseTime(created.String)
@@ -229,6 +229,74 @@ func (s *Store) SetMergeRequestStatus(ctx context.Context, id int64, status stri
229 return err
229 return err
230}
230}
231
231
232// OpenMergeRequestsBySource geeft open verzoeken waarvan de bronbranch deze is.
233func (s *Store) OpenMergeRequestsBySource(ctx context.Context, repoID int64, branch string) ([]MergeRequest, error) {
234 rows, err := s.db.QueryContext(ctx, mrSelect+` WHERE m.repo_id = ? AND m.source_branch = ? AND m.status = 'open' ORDER BY m.number`, repoID, branch)
235 if err != nil {
236 return nil, err
237 }
238 defer rows.Close()
239 return scanMRList(rows)
240}
241
242func scanMRList(rows *sql.Rows) ([]MergeRequest, error) {
243 var out []MergeRequest
244 for rows.Next() {
245 var m MergeRequest
246 var created, updated string
247 if err := rows.Scan(&m.ID, &m.RepoID, &m.Number, &m.Title, &m.Description, &m.SourceBranch, &m.TargetBranch, &m.AuthorID, &m.Author, &m.Status, &created, &updated); err != nil {
248 return nil, err
249 }
250 m.CreatedAt = parseTime(created)
251 m.UpdatedAt = parseTime(updated)
252 out = append(out, m)
253 }
254 return out, rows.Err()
255}
256
257// AttachPipelineMR koppelt een pipeline aan een merge request.
258func (s *Store) AttachPipelineMR(ctx context.Context, pipelineID, mrID int64) error {
259 _, err := s.db.ExecContext(ctx, `UPDATE pipelines SET mr_id = ? WHERE id = ?`, mrID, pipelineID)
260 return err
261}
262
263// LatestMergePipeline is de nieuwste pipeline van deze merge request.
264func (s *Store) LatestMergePipeline(ctx context.Context, repoID, mrID int64) (Pipeline, error) {
265 p, err := scanPipelineRow(s.db.QueryRowContext(ctx, pipelineCols+` WHERE repo_id = ? AND mr_id = ? ORDER BY id DESC LIMIT 1`, repoID, mrID))
266 if errors.Is(err, sql.ErrNoRows) {
267 return Pipeline{}, errNotFound
268 }
269 return p, err
270}
271
272// PipelineBlocksMerge zegt of deze jobs een merge tegenhouden.
273// Een lege tekst betekent dat elke job klaar is en geen harde fout heeft.
274// allow_failure mag mislukt zijn, maar moet wel af zijn.
275func PipelineBlocksMerge(jobs []Job) string {
276 if len(jobs) == 0 {
277 return "waiting"
278 }
279 hard, open := false, false
280 for _, j := range jobs {
281 switch j.Status {
282 case "success", "skipped":
283 case "failed":
284 if !j.AllowFailure {
285 hard = true
286 }
287 default:
288 open = true
289 }
290 }
291 if open {
292 return "running"
293 }
294 if hard {
295 return "failed"
296 }
297 return ""
298}
299
232// NewJob is een job die nog de wachtrij in moet.
300// NewJob is een job die nog de wachtrij in moet.
233type NewJob struct {
301type NewJob struct {
234 Name string
302 Name string
@@ -266,6 +334,7 @@ type Pipeline struct {
266 CreatedAt time.Time
334 CreatedAt time.Time
267 StartedAt time.Time
335 StartedAt time.Time
268 FinishedAt time.Time
336 FinishedAt time.Time
337 MRID int64
269}
338}
270
339
271// Job is één container in een pipeline.
340// Job is één container in een pipeline.
@@ -302,24 +371,27 @@ type Job struct {
302}
371}
303
372
304// RecordConfigFailure bewaart een pipeline die niet kon starten.
373// RecordConfigFailure bewaart een pipeline die niet kon starten.
305func (s *Store) RecordConfigFailure(ctx context.Context, repoID int64, sha, ref, message string) error {
374func (s *Store) RecordConfigFailure(ctx context.Context, repoID int64, sha, ref, message string) (int64, error) {
306 tx, err := s.db.BeginTx(ctx, nil)
375 tx, err := s.db.BeginTx(ctx, nil)
307 if err != nil {
376 if err != nil {
308 return err
377 return 0, err
309 }
378 }
310 defer func() { _ = tx.Rollback() }()
379 defer func() { _ = tx.Rollback() }()
311 ts := now()
380 ts := now()
312 res, err := tx.ExecContext(ctx, `INSERT INTO pipelines (repo_id, commit_sha, ref, status, created_at, finished_at) VALUES (?, ?, ?, 'failed', ?, ?)`, repoID, sha, ref, ts, ts)
381 res, err := tx.ExecContext(ctx, `INSERT INTO pipelines (repo_id, commit_sha, ref, status, created_at, finished_at) VALUES (?, ?, ?, 'failed', ?, ?)`, repoID, sha, ref, ts, ts)
313 if err != nil {
382 if err != nil {
314 return err
383 return 0, err
315 }
384 }
316 id, _ := res.LastInsertId()
385 id, _ := res.LastInsertId()
317 _, err = tx.ExecContext(ctx, `INSERT INTO jobs (pipeline_id, repo_id, name, stage, position, status, image, script, needs_json, needs_set, variables_json, artifact_paths, artifact_expire_seconds, allow_failure, log, finished_at)
386 _, err = tx.ExecContext(ctx, `INSERT INTO jobs (pipeline_id, repo_id, name, stage, position, status, image, script, needs_json, needs_set, variables_json, artifact_paths, artifact_expire_seconds, allow_failure, log, finished_at)
318 VALUES (?, ?, 'blue-ci-cd', 'test', 0, 'failed', 'alpine:3.20', 'exit 1', '[]', 0, '{}', '[]', 0, 0, ?, ?)`, id, repoID, message, ts)
387 VALUES (?, ?, 'blue-ci-cd', 'test', 0, 'failed', 'alpine:3.20', 'exit 1', '[]', 0, '{}', '[]', 0, 0, ?, ?)`, id, repoID, message, ts)
319 if err != nil {
388 if err != nil {
320 return err
389 return 0, err
390 }
391 if err := tx.Commit(); err != nil {
392 return 0, err
321 }
393 }
322 return tx.Commit()
394 return id, nil
323}
395}
324
396
325// CreatePipeline maakt een pipeline met jobs in de wachtrij.
397// CreatePipeline maakt een pipeline met jobs in de wachtrij.
@@ -392,7 +464,7 @@ func (s *Store) ListPipelines(ctx context.Context, repoID int64) ([]Pipeline, er
392 return out, rows.Err()
464 return out, rows.Err()
393}
465}
394
466
395const pipelineCols = `SELECT id, repo_id, commit_sha, ref, status, created_at, finished_at, source, actor, commit_title, started_at FROM pipelines`
467const pipelineCols = `SELECT id, repo_id, commit_sha, ref, status, created_at, finished_at, source, actor, commit_title, started_at, mr_id FROM pipelines`
396
468
397func scanPipeline(rows *sql.Rows) (Pipeline, error) {
469func scanPipeline(rows *sql.Rows) (Pipeline, error) {
398 return scanPipelineRow(rows)
470 return scanPipelineRow(rows)
@@ -401,7 +473,7 @@ func scanPipeline(rows *sql.Rows) (Pipeline, error) {
401func scanPipelineRow(sc scanner) (Pipeline, error) {
473func scanPipelineRow(sc scanner) (Pipeline, error) {
402 var p Pipeline
474 var p Pipeline
403 var created, finished, started sql.NullString
475 var created, finished, started sql.NullString
404 if err := sc.Scan(&p.ID, &p.RepoID, &p.CommitSHA, &p.Ref, &p.Status, &created, &finished, &p.Source, &p.Actor, &p.CommitTitle, &started); err != nil {
476 if err := sc.Scan(&p.ID, &p.RepoID, &p.CommitSHA, &p.Ref, &p.Status, &created, &finished, &p.Source, &p.Actor, &p.CommitTitle, &started, &p.MRID); err != nil {
405 return Pipeline{}, err
477 return Pipeline{}, err
406 }
478 }
407 p.CreatedAt = parseTime(created.String)
479 p.CreatedAt = parseTime(created.String)
internal/store/pipeline_status_test.go +18 −0
@@ -5,6 +5,24 @@ import (
5 5 "testing"
6 6 )
7 7
8 func TestPipelineBlocksMerge(t *testing.T) {
9 if PipelineBlocksMerge(nil) != "waiting" {
10 t.Fatal("leeg")
11 }
12 if PipelineBlocksMerge([]Job{{Status: "success"}, {Status: "failed", AllowFailure: true}, {Status: "skipped"}}) != "" {
13 t.Fatal("allow_failure mag mislukt zijn")
14 }
15 if PipelineBlocksMerge([]Job{{Status: "success"}, {Status: "queued", AllowFailure: true}}) != "running" {
16 t.Fatal("open allow_failure houdt tegen")
17 }
18 if PipelineBlocksMerge([]Job{{Status: "manual"}}) != "running" {
19 t.Fatal("manual is nog niet klaar")
20 }
21 if PipelineBlocksMerge([]Job{{Status: "failed"}}) != "failed" {
22 t.Fatal("harde fout")
23 }
24 }
25
8 26 func TestRefreshWaitsForQueuedJobs(t *testing.T) {
9 27 s := openTest(t)
10 28 ctx := context.Background()
@@ -5,6 +5,24 @@ import (
5 "testing"
5 "testing"
6)
6)
7
7
8func TestPipelineBlocksMerge(t *testing.T) {
9 if PipelineBlocksMerge(nil) != "waiting" {
10 t.Fatal("leeg")
11 }
12 if PipelineBlocksMerge([]Job{{Status: "success"}, {Status: "failed", AllowFailure: true}, {Status: "skipped"}}) != "" {
13 t.Fatal("allow_failure mag mislukt zijn")
14 }
15 if PipelineBlocksMerge([]Job{{Status: "success"}, {Status: "queued", AllowFailure: true}}) != "running" {
16 t.Fatal("open allow_failure houdt tegen")
17 }
18 if PipelineBlocksMerge([]Job{{Status: "manual"}}) != "running" {
19 t.Fatal("manual is nog niet klaar")
20 }
21 if PipelineBlocksMerge([]Job{{Status: "failed"}}) != "failed" {
22 t.Fatal("harde fout")
23 }
24}
25
8func TestRefreshWaitsForQueuedJobs(t *testing.T) {
26func TestRefreshWaitsForQueuedJobs(t *testing.T) {
9 s := openTest(t)
27 s := openTest(t)
10 ctx := context.Background()
28 ctx := context.Background()
internal/store/store.go +1 −0
@@ -81,6 +81,7 @@ func (s *Store) migrate() error {
81 81 `ALTER TABLE jobs ADD COLUMN cache_key TEXT NOT NULL DEFAULT ''`,
82 82 `ALTER TABLE jobs ADD COLUMN resource_group TEXT NOT NULL DEFAULT ''`,
83 83 `ALTER TABLE jobs ADD COLUMN optional_json TEXT NOT NULL DEFAULT '[]'`,
84 `ALTER TABLE pipelines ADD COLUMN mr_id INTEGER NOT NULL DEFAULT 0`,
84 85 }
85 86 for _, q := range alters {
86 87 if _, err := s.db.Exec(q); err != nil && !strings.Contains(strings.ToLower(err.Error()), "duplicate column") {
@@ -81,6 +81,7 @@ func (s *Store) migrate() error {
81 `ALTER TABLE jobs ADD COLUMN cache_key TEXT NOT NULL DEFAULT ''`,
81 `ALTER TABLE jobs ADD COLUMN cache_key TEXT NOT NULL DEFAULT ''`,
82 `ALTER TABLE jobs ADD COLUMN resource_group TEXT NOT NULL DEFAULT ''`,
82 `ALTER TABLE jobs ADD COLUMN resource_group TEXT NOT NULL DEFAULT ''`,
83 `ALTER TABLE jobs ADD COLUMN optional_json TEXT NOT NULL DEFAULT '[]'`,
83 `ALTER TABLE jobs ADD COLUMN optional_json TEXT NOT NULL DEFAULT '[]'`,
84 `ALTER TABLE pipelines ADD COLUMN mr_id INTEGER NOT NULL DEFAULT 0`,
84 }
85 }
85 for _, q := range alters {
86 for _, q := range alters {
86 if _, err := s.db.Exec(q); err != nil && !strings.Contains(strings.ToLower(err.Error()), "duplicate column") {
87 if _, err := s.db.Exec(q); err != nil && !strings.Contains(strings.ToLower(err.Error()), "duplicate column") {