diff --git a/trigger/skip.go b/trigger/skip.go index f211acb0..eedd8e24 100644 --- a/trigger/skip.go +++ b/trigger/skip.go @@ -33,6 +33,10 @@ func skipEvent(document *yaml.Pipeline, event string) bool { return !document.Trigger.Event.Match(event) } +func skipAction(document *yaml.Pipeline, action string) bool { + return !document.Trigger.Action.Match(action) +} + func skipInstance(document *yaml.Pipeline, instance string) bool { return !document.Trigger.Instance.Match(instance) } diff --git a/trigger/trigger.go b/trigger/trigger.go index c6b2acab..fd5f0e74 100644 --- a/trigger/trigger.go +++ b/trigger/trigger.go @@ -258,6 +258,9 @@ func (t *triggerer) Trigger(ctx context.Context, repo *core.Repository, base *co } else if skipEvent(pipeline, base.Event) { logger = logger.WithField("pipeline", pipeline.Name) logger.Infoln("trigger: skipping pipeline, does not match event") + } else if skipAction(pipeline, base.Action) { + logger = logger.WithField("pipeline", pipeline.Name).WithField("action", base.Action) + logger.Infoln("trigger: skipping pipeline, does not match action") } else if skipRef(pipeline, base.Ref) { logger = logger.WithField("pipeline", pipeline.Name) logger.Infoln("trigger: skipping pipeline, does not match ref") diff --git a/trigger/trigger_test.go b/trigger/trigger_test.go index 46537d0b..5b934f19 100644 --- a/trigger/trigger_test.go +++ b/trigger/trigger_test.go @@ -271,6 +271,35 @@ func TestTrigger_SkipEvent(t *testing.T) { } } +// this test verifies that no build should be scheduled if the +// hook action does not match the actions defined in the yaml. +func TestTrigger_SkipAction(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + + mockUsers := mock.NewMockUserStore(controller) + mockUsers.EXPECT().Find(noContext, dummyRepo.UserID).Return(dummyUser, nil) + + mockConfigService := mock.NewMockConfigService(controller) + mockConfigService.EXPECT().Find(gomock.Any(), gomock.Any()).Return(dummyYamlSkipAction, nil) + + triggerer := New( + mockConfigService, + nil, + nil, + nil, + nil, + nil, + mockUsers, + nil, + ) + + _, err := triggerer.Trigger(noContext, dummyRepo, dummyHook) + if err != nil { + t.Errorf("Expect build silenty skipped if event does not match") + } +} + // this test verifies that if the system cannot increment the // build number, the function must exit with error and must not // schedule a new build. @@ -407,6 +436,7 @@ var ( AuthorEmail: "octocat@hello-world.com", AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231", Sender: "octocat", + Action: "opened", } dummyBuild = &core.Build{ @@ -427,6 +457,7 @@ var ( AuthorEmail: "octocat@hello-world.com", AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231", Sender: "octocat", + Action: "opened", } dummyRepo = &core.Repository{ @@ -498,6 +529,14 @@ trigger: exclude: - push`, } + + dummyYamlSkipAction = &core.Config{ + Data: ` +kind: pipeline +trigger: + action: + exclude: + - opened`, } ignoreBuildFields = cmpopts.IgnoreFields(core.Build{},