process pull request action conditions in triggers

This commit is contained in:
Lucian Jones 2019-06-25 14:48:46 +12:00
parent 27404eb3e6
commit 93e052a713
3 changed files with 46 additions and 0 deletions

View file

@ -33,6 +33,10 @@ func skipEvent(document *yaml.Pipeline, event string) bool {
return !document.Trigger.Event.Match(event) 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 { func skipInstance(document *yaml.Pipeline, instance string) bool {
return !document.Trigger.Instance.Match(instance) return !document.Trigger.Instance.Match(instance)
} }

View file

@ -258,6 +258,9 @@ func (t *triggerer) Trigger(ctx context.Context, repo *core.Repository, base *co
} else if skipEvent(pipeline, base.Event) { } else if skipEvent(pipeline, base.Event) {
logger = logger.WithField("pipeline", pipeline.Name) logger = logger.WithField("pipeline", pipeline.Name)
logger.Infoln("trigger: skipping pipeline, does not match event") 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) { } else if skipRef(pipeline, base.Ref) {
logger = logger.WithField("pipeline", pipeline.Name) logger = logger.WithField("pipeline", pipeline.Name)
logger.Infoln("trigger: skipping pipeline, does not match ref") logger.Infoln("trigger: skipping pipeline, does not match ref")

View file

@ -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 // this test verifies that if the system cannot increment the
// build number, the function must exit with error and must not // build number, the function must exit with error and must not
// schedule a new build. // schedule a new build.
@ -407,6 +436,7 @@ var (
AuthorEmail: "octocat@hello-world.com", AuthorEmail: "octocat@hello-world.com",
AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231", AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231",
Sender: "octocat", Sender: "octocat",
Action: "opened",
} }
dummyBuild = &core.Build{ dummyBuild = &core.Build{
@ -427,6 +457,7 @@ var (
AuthorEmail: "octocat@hello-world.com", AuthorEmail: "octocat@hello-world.com",
AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231", AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231",
Sender: "octocat", Sender: "octocat",
Action: "opened",
} }
dummyRepo = &core.Repository{ dummyRepo = &core.Repository{
@ -498,6 +529,14 @@ trigger:
exclude: exclude:
- push`, - push`,
} }
dummyYamlSkipAction = &core.Config{
Data: `
kind: pipeline
trigger:
action:
exclude:
- opened`,
} }
ignoreBuildFields = cmpopts.IgnoreFields(core.Build{}, ignoreBuildFields = cmpopts.IgnoreFields(core.Build{},