From 436aa8f1de4cca7b8b8e4e45480eb85130512195 Mon Sep 17 00:00:00 2001 From: Carlo Mandelli Date: Thu, 22 Aug 2019 16:19:44 +0200 Subject: [PATCH] skip run with special commit --- internal/services/gateway/action/run.go | 10 ++++ tests/setup_test.go | 71 +++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/internal/services/gateway/action/run.go b/internal/services/gateway/action/run.go index 38ddddb..3fee31d 100644 --- a/internal/services/gateway/action/run.go +++ b/internal/services/gateway/action/run.go @@ -19,6 +19,7 @@ import ( "encoding/json" "net/http" "path" + "regexp" "agola.io/agola/internal/config" gitsource "agola.io/agola/internal/gitsources" @@ -66,6 +67,10 @@ const ( AnnotationPullRequestLink = "pull_request_link" ) +var ( + SkipRunMessage = regexp.MustCompile(`.*\[ci skip\].*`) +) + func (h *ActionHandler) GetRun(ctx context.Context, runID string) (*rsapitypes.RunResponse, error) { runResp, resp, err := h.runserviceClient.GetRun(ctx, runID, nil) if err != nil { @@ -483,6 +488,11 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e } for _, run := range config.Runs { + if SkipRunMessage.MatchString(req.Message) { + h.log.Debugf("skipping run since special commit message") + continue + } + if match := types.MatchWhen(run.When.ToWhen(), req.Branch, req.Tag, req.Ref); !match { h.log.Debugf("skipping run since when condition doesn't match") continue diff --git a/tests/setup_test.go b/tests/setup_test.go index e969457..7b8c46b 100644 --- a/tests/setup_test.go +++ b/tests/setup_test.go @@ -451,7 +451,7 @@ func createProject(ctx context.Context, t *testing.T, giteaClient *gitea.Client, return giteaRepo, project } -func push(t *testing.T, config, cloneURL, remoteToken string) { +func push(t *testing.T, config, cloneURL, remoteToken, message string) { gitfs := memfs.New() f, err := gitfs.Create(".agola/config.jsonnet") if err != nil { @@ -480,7 +480,7 @@ func push(t *testing.T, config, cloneURL, remoteToken string) { if _, err := wt.Add(".agola/config.jsonnet"); err != nil { t.Fatalf("unexpected err: %v", err) } - _, err = wt.Commit("commit", &git.CommitOptions{ + _, err = wt.Commit(message, &git.CommitOptions{ Author: &object.Signature{ Name: "user01", Email: "user01@example.com", @@ -510,6 +510,7 @@ func TestPush(t *testing.T) { config string num int annotations map[string]string + message string }{ { name: "test push", @@ -544,6 +545,7 @@ func TestPush(t *testing.T) { "ref": "refs/heads/master", "ref_type": "branch", }, + message: "commit", }, { name: "test push with unmatched branch", @@ -575,7 +577,68 @@ func TestPush(t *testing.T) { ], } `, - num: 0, + num: 0, + message: "commit", + }, + { + name: "test push with [ci skip] in subject", + config: ` + { + runs: [ + { + name: 'run01', + tasks: [ + { + name: 'task01', + runtime: { + containers: [ + { + image: 'alpine/git', + }, + ], + }, + steps: [ + { type: 'clone' }, + { type: 'run', command: 'env' }, + ], + }, + ], + }, + ], + } + `, + num: 0, + message: "[ci skip] commit", + }, + { + name: "test push with [ci skip] in body", + config: ` + { + runs: [ + { + name: 'run01', + tasks: [ + { + name: 'task01', + runtime: { + containers: [ + { + image: 'alpine/git', + }, + ], + }, + steps: [ + { type: 'clone' }, + { type: 'run', command: 'env' }, + ], + }, + ], + }, + ], + } + `, + num: 0, + message: "commit\n\n[ci skip] body", }, } @@ -604,7 +667,7 @@ func TestPush(t *testing.T) { giteaRepo, project := createProject(ctx, t, giteaClient, gwClient) - push(t, tt.config, giteaRepo.CloneURL, giteaToken) + push(t, tt.config, giteaRepo.CloneURL, giteaToken, tt.message) _ = testutil.Wait(30*time.Second, func() (bool, error) { runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false)