Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rulesets): add support for PR labeled events #1081

Merged
merged 19 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions api/build/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,26 @@ func ApproveBuild(c *gin.Context) {
"user": u.GetName(),
})

// verify build is in correct status
if !strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) {
retErr := fmt.Errorf("unable to approve build %s/%d: build not in pending approval state", r.GetFullName(), b.GetNumber())
util.HandleError(c, http.StatusBadRequest, retErr)

return
}

// verify user is not the sender of the build
if strings.EqualFold(u.GetName(), b.GetSender()) {
retErr := fmt.Errorf("unable to approve build %s/%d: approver cannot be the sender of the build", r.GetFullName(), b.GetNumber())
util.HandleError(c, http.StatusBadRequest, retErr)

return
}

logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber())

// send API call to capture the repo owner
u, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
owner, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err)

Expand All @@ -105,6 +114,7 @@ func ApproveBuild(c *gin.Context) {
return
}

// set fields
b.SetStatus(constants.StatusPending)
b.SetApprovedAt(time.Now().Unix())
b.SetApprovedBy(u.GetName())
Expand All @@ -122,7 +132,7 @@ func ApproveBuild(c *gin.Context) {
database.FromContext(c),
b,
r,
u,
owner,
b.GetHost(),
)

Expand Down
1 change: 1 addition & 0 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ func PostWebhook(c *gin.Context) {
WithMetadata(m).
WithRepo(repo).
WithUser(u).
WithLabel(webhook.PullRequest.Label).
Compile(config)
if err != nil {
// format the error message with extra information
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 in the Engine.
WithLabel(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 @@
Repo: c.repo.GetFullName(),
Tag: strings.TrimPrefix(c.build.GetRef(), "refs/tags/"),
Target: c.build.GetDeploy(),
Label: c.label,

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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / Analyze (go)

unknown field Label in struct literal of type pipeline.RuleData
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
}

switch {
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
label 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
}

// WithComment sets the comment in the Engine.
func (c *client) WithLabel(label string) compiler.Engine {
if label != "" {
c.label = label
}

return c
}
1 change: 1 addition & 0 deletions database/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
Edited: new(bool),
Synchronize: new(bool),
Reopened: new(bool),
Labeled: new(bool),

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / database_sql

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:15-alpine)

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:16-alpine)

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/repo/repo_test.go#L219

unknown field Labeled in struct literal of type actions.Pull (typecheck)
Raw output
database/repo/repo_test.go:219:4: unknown field Labeled in struct literal of type actions.Pull (typecheck)
// SPDX-License-Identifier: Apache-2.0

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / full-review

unknown field Labeled in struct literal of type actions.Pull (typecheck)

Check failure on line 219 in database/repo/repo_test.go

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Labeled in struct literal of type actions.Pull (typecheck)
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
},
Deployment: &actions.Deploy{
Created: new(bool),
Expand Down
1 change: 1 addition & 0 deletions database/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@
Edited: new(bool),
Synchronize: new(bool),
Reopened: new(bool),
Labeled: new(bool),

Check failure on line 244 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labeled in struct literal of type actions.Pull
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
},

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / database_sql

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:15-alpine)

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:16-alpine)

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / test

unknown field Labeled in struct literal of type actions.Pull

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/secret/secret_test.go#L245

unknown field Labeled in struct literal of type actions.Pull (typecheck)
Raw output
database/secret/secret_test.go:245:4: unknown field Labeled in struct literal of type actions.Pull (typecheck)
// SPDX-License-Identifier: Apache-2.0

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / full-review

unknown field Labeled in struct literal of type actions.Pull (typecheck)

Check failure on line 245 in database/secret/secret_test.go

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Labeled in struct literal of type actions.Pull (typecheck)
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
Deployment: &actions.Deploy{
Created: new(bool),
},
Expand Down
6 changes: 4 additions & 2 deletions scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@
return &types.Webhook{Hook: h}, nil
}

// skip if the pull request action is not opened, synchronize, reopened, or edited
// skip if the pull request action is not opened, synchronize, reopened, edited, or labeled
if !strings.EqualFold(payload.GetAction(), "opened") &&
!strings.EqualFold(payload.GetAction(), "synchronize") &&
!strings.EqualFold(payload.GetAction(), "reopened") &&
!strings.EqualFold(payload.GetAction(), "edited") {
!strings.EqualFold(payload.GetAction(), "edited") &&
!strings.EqualFold(payload.GetAction(), "labeled") {
return &types.Webhook{Hook: h}, nil
}

Expand Down Expand Up @@ -317,6 +318,7 @@
PullRequest: types.PullRequest{
Number: payload.GetNumber(),
IsFromFork: fromFork,
Label: payload.GetLabel().GetName(),

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

View workflow job for this annotation

GitHub Actions / build

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / validate

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / test

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / build

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / validate

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / database_sql

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:15-alpine)

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / database_postgres (postgres:16-alpine)

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / test

unknown field Label in struct literal of type types.PullRequest

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

View workflow job for this annotation

GitHub Actions / golangci

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

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

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

View workflow job for this annotation

GitHub Actions / golangci

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

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

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

View workflow job for this annotation

GitHub Actions / golangci

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

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

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

View workflow job for this annotation

GitHub Actions / golangci

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

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

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

View workflow job for this annotation

GitHub Actions / full-review

unknown field Label in struct literal of type types.PullRequest) (typecheck)

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

View workflow job for this annotation

GitHub Actions / diff-review

unknown field Label in struct literal of type types.PullRequest) (typecheck)

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

View workflow job for this annotation

GitHub Actions / Analyze (go)

unknown field Label in struct literal of type types.PullRequest
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
wsan3 marked this conversation as resolved.
Show resolved Hide resolved
},
Hook: h,
Repo: r,
Expand Down
Loading