Skip to content

Commit

Permalink
Extend label rule to include other PR events
Browse files Browse the repository at this point in the history
  • Loading branch information
wsan3 committed Apr 1, 2024
1 parent f12e159 commit 52df527
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 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
5 changes: 5 additions & 0 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -285,6 +289,7 @@ func PostWebhook(c *gin.Context) {
BaseErr: baseErr,
Source: "webhook",
Comment: prComment,
Labels: prLabels,
Retries: 3,
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / build

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / build

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / validate

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / validate

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / database_sql

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:15-alpine)

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:16-alpine)

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L73

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
Raw output
compiler/native/compile.go:73:3: unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
	"github.com/go-vela/server/compiler/native"
	^

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L73

unknown field Label in struct literal of type pipeline.RuleData (typecheck)
Raw output
compiler/native/compile.go:73:3: unknown field Label in struct literal of type pipeline.RuleData (typecheck)
// SPDX-License-Identifier: Apache-2.0

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L73

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
Raw output
compiler/native/compile.go:73:3: unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
	"github.com/go-vela/server/compiler/native"
	^

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L73

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
Raw output
compiler/native/compile.go:73:3: unknown field Label in struct literal of type pipeline.RuleData) (typecheck)
	"github.com/go-vela/server/compiler/native"
	^

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / test

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / test

unknown field Label in struct literal of type pipeline.RuleData

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Label in struct literal of type pipeline.RuleData (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / full-review

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / full-review

unknown field Label in struct literal of type pipeline.RuleData (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / full-review

unknown field Label in struct literal of type pipeline.RuleData) (typecheck)

Check failure on line 73 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

unknown field Label in struct literal of type pipeline.RuleData
}

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: 5 additions & 5 deletions compiler/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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,

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / build

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / build

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / validate

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / validate

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / database_sql

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:15-alpine)

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:16-alpine)

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/webhook.go#L333

unknown field Labels in struct literal of type types.PullRequest) (typecheck)
Raw output
scm/github/webhook.go:333:4: unknown field Labels in struct literal of type types.PullRequest) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/webhook.go#L333

unknown field Labels in struct literal of type types.PullRequest) (typecheck)
Raw output
scm/github/webhook.go:333:4: unknown field Labels in struct literal of type types.PullRequest) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/webhook.go#L333

unknown field Labels in struct literal of type types.PullRequest) (typecheck)
Raw output
scm/github/webhook.go:333:4: unknown field Labels in struct literal of type types.PullRequest) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/webhook.go#L333

unknown field Labels in struct literal of type types.PullRequest (typecheck)
Raw output
scm/github/webhook.go:333:4: unknown field Labels in struct literal of type types.PullRequest (typecheck)
// SPDX-License-Identifier: Apache-2.0

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labels in struct literal of type types.PullRequest

Check failure on line 333 in scm/github/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

unknown field Labels in struct literal of type types.PullRequest
},
Hook: h,
Repo: r,
Expand Down

0 comments on commit 52df527

Please sign in to comment.