diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index ea908862f..f60dcaa56 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -31,6 +31,7 @@ type CompileAndPublishConfig struct { BaseErr string Source string Comment string + Labels []string Retries int } @@ -287,6 +288,7 @@ func CompileAndPublish( WithMetadata(cfg.Metadata). WithRepo(repo). WithUser(u). + WithLabels(cfg.Labels). Compile(pipelineFile) if err != nil { // format the error message with extra information diff --git a/api/webhook/post.go b/api/webhook/post.go index 8ebf0bf7f..1ddcb0d2b 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -273,9 +273,13 @@ func PostWebhook(c *gin.Context) { } var prComment string + var prLabels []string if strings.EqualFold(b.GetEvent(), constants.EventComment) { prComment = webhook.PullRequest.Comment } + if strings.EqualFold(b.GetEvent(), constants.EventPull) { + prLabels = webhook.PullRequest.Labels + } // construct CompileAndPublishConfig config := build.CompileAndPublishConfig{ @@ -285,6 +289,7 @@ func PostWebhook(c *gin.Context) { BaseErr: baseErr, Source: "webhook", Comment: prComment, + Labels: prLabels, Retries: 3, } diff --git a/compiler/engine.go b/compiler/engine.go index f1f416cce..1778c08a0 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -141,8 +141,8 @@ type Engine interface { // the library user type in the Engine. WithUser(*library.User) Engine // WithLabel defines a function that sets - // the label in the Engine. - WithLabel(string) Engine + // the label(s) in the Engine. + WithLabels([]string) Engine // WithUser defines a function that sets // the private github client in the Engine. WithPrivateGitHub(string, string) Engine diff --git a/compiler/native/compile.go b/compiler/native/compile.go index 70aba717c..e2e1f0718 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -70,7 +70,7 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, err Repo: c.repo.GetFullName(), Tag: strings.TrimPrefix(c.build.GetRef(), "refs/tags/"), Target: c.build.GetDeploy(), - Label: c.label, + Label: c.labels, } switch { diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index df04741e3..6d9479a53 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -450,6 +450,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { If: yaml.Rules{ Branch: []string{"main"}, }, + Operator: "and", }, }, &yaml.Step{ @@ -466,6 +467,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { If: yaml.Rules{ Branch: []string{"dev"}, }, + Operator: "and", }, }, } diff --git a/compiler/native/native.go b/compiler/native/native.go index c5acf0a2d..048f22307 100644 --- a/compiler/native/native.go +++ b/compiler/native/native.go @@ -42,7 +42,7 @@ type client struct { metadata *types.Metadata repo *library.Repo user *library.User - label string + labels []string } // New returns a Pipeline implementation that integrates with the supported registries. @@ -212,10 +212,10 @@ func (c *client) WithUser(u *library.User) compiler.Engine { return c } -// WithComment sets the comment in the Engine. -func (c *client) WithLabel(label string) compiler.Engine { - if label != "" { - c.label = label +// WithLabels sets the label(s) in the Engine. +func (c *client) WithLabels(labels []string) compiler.Engine { + if len(labels) != 0 { + c.labels = labels } return c diff --git a/scm/github/webhook.go b/scm/github/webhook.go index d53b988aa..08ce41464 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -311,6 +311,17 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven b.SetEmail(payload.GetPullRequest().GetHead().GetUser().GetEmail()) } + var prLabels []string + if strings.EqualFold(payload.GetAction(), "labeled") || + strings.EqualFold(payload.GetAction(), "unlabeled") { + prLabels = append(prLabels, payload.GetLabel().GetName()) + } else { + labels := payload.GetPullRequest().Labels + for _, label := range labels { + prLabels = append(prLabels, label.GetName()) + } + } + // determine if pull request head is a fork and does not match the repo name of base fromFork := payload.GetPullRequest().GetHead().GetRepo().GetFork() && !strings.EqualFold(payload.GetPullRequest().GetBase().GetRepo().GetFullName(), payload.GetPullRequest().GetHead().GetRepo().GetFullName()) @@ -319,7 +330,7 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven PullRequest: types.PullRequest{ Number: payload.GetNumber(), IsFromFork: fromFork, - Label: payload.GetLabel().GetName(), + Labels: prLabels, }, Hook: h, Repo: r,