Skip to content

Commit

Permalink
feat(rulesets): add support for PR labeled events (#1081)
Browse files Browse the repository at this point in the history
* Process PR label event

1. Add API changes for updating repos to leverage label events
2. Process PR label events in scm package
3. Update compiler to use label value in its purging process

* Add labeled event to PR struct

* Merge branch 'main' into feat/pr-labeled-action

* Add support for PR unlabeled event

1. Update process PR event helper function
2. Update tests

* chore: remove legacy event code from repo and secret

* Extend label rule to include other PR events

* Update go.mod

* Fix linter warnings

* Add tests for webhook and native compiler

* Update test for webhook

---------

Co-authored-by: Easton Crupper <[email protected]>
Co-authored-by: ecrupper <[email protected]>
Co-authored-by: Tim Huynh <[email protected]>
  • Loading branch information
4 people committed Apr 5, 2024
1 parent fb31ea5 commit e7917a8
Show file tree
Hide file tree
Showing 16 changed files with 1,529 additions and 6 deletions.
2 changes: 2 additions & 0 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type CompileAndPublishConfig struct {
BaseErr string
Source string
Comment string
Labels []string
Retries int
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,19 @@ func PostWebhook(c *gin.Context) {
return
}

var prComment string
var (
prComment string
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{
Build: b,
Expand All @@ -285,6 +293,7 @@ func PostWebhook(c *gin.Context) {
BaseErr: baseErr,
Source: "webhook",
Comment: prComment,
Labels: prLabels,
Retries: 3,
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ type Engine interface {
// WithUser defines a function that sets
// the library user type in the Engine.
WithUser(*library.User) Engine
// WithLabel defines a function that sets
// 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
Expand Down
1 change: 1 addition & 0 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +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.labels,
}

switch {
Expand Down
2 changes: 2 additions & 0 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
If: yaml.Rules{
Branch: []string{"main"},
},
Operator: "and",
},
},
&yaml.Step{
Expand All @@ -466,6 +467,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
If: yaml.Rules{
Branch: []string{"dev"},
},
Operator: "and",
},
},
}
Expand Down
10 changes: 10 additions & 0 deletions compiler/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type client struct {
metadata *types.Metadata
repo *library.Repo
user *library.User
labels []string
}

// New returns a Pipeline implementation that integrates with the supported registries.
Expand Down Expand Up @@ -210,3 +211,12 @@ func (c *client) WithUser(u *library.User) compiler.Engine {

return c
}

// 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
}
20 changes: 20 additions & 0 deletions compiler/native/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,23 @@ func TestNative_WithUser(t *testing.T) {
t.Errorf("WithUser is %v, want %v", got, want)
}
}

func TestNative_WithLabels(t *testing.T) {
// setup types
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(nil, set, nil)

labels := []string{"documentation", "enhancement"}
want, _ := New(c)
want.labels = []string{"documentation", "enhancement"}

// run test
got, err := New(c)
if err != nil {
t.Errorf("Unable to create new compiler: %v", err)
}

if !reflect.DeepEqual(got.WithLabels(labels), want) {
t.Errorf("WithLocalTemplates is %v, want %v", got, want)
}
}
2 changes: 2 additions & 0 deletions database/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ func testEvents() *library.Events {
Edited: new(bool),
Synchronize: new(bool),
Reopened: new(bool),
Labeled: new(bool),
Unlabeled: new(bool),
},
Deployment: &actions.Deploy{
Created: new(bool),
Expand Down
2 changes: 2 additions & 0 deletions database/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func testEvents() *library.Events {
Edited: new(bool),
Synchronize: new(bool),
Reopened: new(bool),
Labeled: new(bool),
Unlabeled: new(bool),
},
Deployment: &actions.Deploy{
Created: new(bool),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/drone/envsubst v1.0.3
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/assert/v2 v2.2.0
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32
github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v59 v59.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32 h1:fqmNnM1LdH3Zg1zCADfgR7a51EOSZvLFAB2Em4CG+Pg=
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb h1:jHao/NNRswInMLEb0m1OJ84d1m/LGWMCmaeRqSDnWQY=
github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
Loading

0 comments on commit e7917a8

Please sign in to comment.