Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,19 @@ func CompileAndPublish(
// getPRNumberFromBuild is a helper function to
// extract the pull request number from a Build.
func getPRNumberFromBuild(b *types.Build) (int, error) {
// on PR merges and closures, grab number from message
if b.GetEventAction() == constants.ActionMerged && b.GetEvent() == constants.EventPull {
numStr := strings.TrimPrefix(b.GetMessage(), "Merged PR #")

return strconv.Atoi(numStr)
}

if b.GetEventAction() == constants.ActionClosed && b.GetEvent() == constants.EventPull {
numStr := strings.TrimPrefix(b.GetMessage(), "Closed PR #")

return strconv.Atoi(numStr)
}
Comment on lines +451 to +461
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. is there another way to get PR number?
  2. related to 1., is this GitHub specific message formatting?


// parse out pull request number from base ref
//
// pattern: refs/pull/1/head
Expand Down
60 changes: 60 additions & 0 deletions api/types/actions/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Pull struct {
Reopened *bool `json:"reopened"`
Labeled *bool `json:"labeled"`
Unlabeled *bool `json:"unlabeled"`
Merged *bool `json:"merged"`
Closed *bool `json:"closed"`
}

// FromMask returns the Pull type resulting from the provided integer mask.
Expand All @@ -23,6 +25,8 @@ func (a *Pull) FromMask(mask int64) *Pull {
a.SetReopened(mask&constants.AllowPullReopen > 0)
a.SetLabeled(mask&constants.AllowPullLabel > 0)
a.SetUnlabeled(mask&constants.AllowPullUnlabel > 0)
a.SetMerged(mask&constants.AllowPullMerged > 0)
a.SetClosed(mask&constants.AllowPullClosedUnmerged > 0)

return a
}
Expand Down Expand Up @@ -55,6 +59,14 @@ func (a *Pull) ToMask() int64 {
mask = mask | constants.AllowPullUnlabel
}

if a.GetMerged() {
mask = mask | constants.AllowPullMerged
}

if a.GetClosed() {
mask = mask | constants.AllowPullClosedUnmerged
}

return mask
}

Expand Down Expand Up @@ -124,6 +136,28 @@ func (a *Pull) GetUnlabeled() bool {
return *a.Unlabeled
}

// GetMerged returns the Merged field from the provided Pull. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Pull) GetMerged() bool {
// return zero value if Pull type or Merged field is nil
if a == nil || a.Merged == nil {
return false
}

return *a.Merged
}

// GetClosed returns the Closed field from the provided Pull. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Pull) GetClosed() bool {
// return zero value if Pull type or Closed field is nil
if a == nil || a.Closed == nil {
return false
}

return *a.Closed
}

// SetOpened sets the Pull Opened field.
//
// When the provided Pull type is nil, it
Expand Down Expand Up @@ -201,3 +235,29 @@ func (a *Pull) SetUnlabeled(v bool) {

a.Unlabeled = &v
}

// SetMerged sets the Pull Merged field.
//
// When the provided Pull type is nil, it
// will set nothing and immediately return.
func (a *Pull) SetMerged(v bool) {
// return if Pull type is nil
if a == nil {
return
}

a.Merged = &v
}

// SetClosed sets the Pull Closed field.
//
// When the provided Pull type is nil, it
// will set nothing and immediately return.
func (a *Pull) SetClosed(v bool) {
// return if Pull type is nil
if a == nil {
return
}

a.Closed = &v
}
22 changes: 21 additions & 1 deletion api/types/actions/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func TestActions_Pull_Getters(t *testing.T) {
if test.actions.GetUnlabeled() != test.want.GetUnlabeled() {
t.Errorf("GetUnlabeled is %v, want %v", test.actions.GetUnlabeled(), test.want.GetUnlabeled())
}

if test.actions.GetMerged() != test.want.GetMerged() {
t.Errorf("GetMerged is %v, want %v", test.actions.GetMerged(), test.want.GetMerged())
}

if test.actions.GetClosed() != test.want.GetClosed() {
t.Errorf("GetClosed is %v, want %v", test.actions.GetClosed(), test.want.GetClosed())
}
}
}

Expand Down Expand Up @@ -80,6 +88,8 @@ func TestActions_Pull_Setters(t *testing.T) {
test.actions.SetReopened(test.want.GetReopened())
test.actions.SetLabeled(test.want.GetLabeled())
test.actions.SetUnlabeled(test.want.GetUnlabeled())
test.actions.SetMerged(test.want.GetMerged())
test.actions.SetClosed(test.want.GetClosed())

if test.actions.GetOpened() != test.want.GetOpened() {
t.Errorf("SetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened())
Expand All @@ -104,6 +114,14 @@ func TestActions_Pull_Setters(t *testing.T) {
if test.actions.GetUnlabeled() != test.want.GetUnlabeled() {
t.Errorf("SetUnlabeled is %v, want %v", test.actions.GetUnlabeled(), test.want.GetUnlabeled())
}

if test.actions.GetMerged() != test.want.GetMerged() {
t.Errorf("SetMerged is %v, want %v", test.actions.GetMerged(), test.want.GetMerged())
}

if test.actions.GetClosed() != test.want.GetClosed() {
t.Errorf("SetClosed is %v, want %v", test.actions.GetClosed(), test.want.GetClosed())
}
}
}

Expand All @@ -125,7 +143,7 @@ func TestActions_Pull_ToMask(t *testing.T) {
// setup types
actions := testPull()

want := int64(constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen | constants.AllowPullUnlabel)
want := int64(constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen | constants.AllowPullUnlabel | constants.AllowPullMerged)

// run test
got := actions.ToMask()
Expand All @@ -143,6 +161,8 @@ func testPull() *Pull {
pr.SetReopened(true)
pr.SetLabeled(false)
pr.SetUnlabeled(true)
pr.SetMerged(true)
pr.SetClosed(false)

return pr
}
1 change: 1 addition & 0 deletions api/types/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func testMask() int64 {
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowPullUnlabel |
constants.AllowPullMerged |
constants.AllowDeployCreate |
constants.AllowCommentCreate |
constants.AllowSchedule,
Expand Down
30 changes: 27 additions & 3 deletions api/types/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ func (b *Build) Environment(workspace, channel string) map[string]string {
// check if the Build event is comment
if strings.EqualFold(b.GetEvent(), constants.EventComment) {
// capture the pull request number
number := ToString(strings.SplitN(b.GetRef(), "/", 4)[2])
var number string

split := strings.SplitN(b.GetRef(), "/", 4)

if len(split) == 4 {
number = ToString(split[2])
}

// add the pull request number to the list
envs["BUILD_PULL_REQUEST_NUMBER"] = number
Expand Down Expand Up @@ -184,7 +190,19 @@ func (b *Build) Environment(workspace, channel string) map[string]string {
// check if the Build event is pull_request
if strings.EqualFold(b.GetEvent(), constants.EventPull) {
// capture the pull request number
number := ToString(strings.SplitN(b.GetRef(), "/", 4)[2])
var number string

if b.GetEventAction() == constants.ActionMerged {
number = strings.TrimPrefix(b.GetMessage(), "Merged PR #")
} else if b.GetEventAction() == constants.ActionClosed {
number = strings.TrimPrefix(b.GetMessage(), "Closed PR #")
} else {
split := strings.SplitN(b.GetRef(), "/", 4)

if len(split) == 4 {
number = ToString(split[2])
}
}

// add the pull request number to the list
envs["BUILD_PULL_REQUEST_NUMBER"] = number
Expand All @@ -198,7 +216,13 @@ func (b *Build) Environment(workspace, channel string) map[string]string {
// check if the Build event is tag
if strings.EqualFold(b.GetEvent(), constants.EventTag) {
// capture the tag reference
tag := ToString(strings.SplitN(b.GetRef(), "refs/tags/", 2)[1])
var tag string

split := strings.SplitN(b.GetRef(), "refs/tags/", 2)

if len(split) == 2 {
tag = ToString(split[1])
}

// add the tag reference to the list
envs["BUILD_TAG"] = tag
Expand Down
16 changes: 16 additions & 0 deletions api/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func NewEventsFromSlice(events []string) (*Events, error) {
mask = mask | constants.AllowPullLabel
case constants.EventPull + ":" + constants.ActionUnlabeled:
mask = mask | constants.AllowPullUnlabel
case constants.EventPull + ":" + constants.ActionMerged:
mask = mask | constants.AllowPullMerged
case constants.EventPull + ":" + constants.ActionClosed:
mask = mask | constants.AllowPullClosedUnmerged

// deployment actions
case constants.EventDeploy, constants.EventDeployAlternate, constants.EventDeploy + ":" + constants.ActionCreated:
Expand Down Expand Up @@ -124,6 +128,10 @@ func (e *Events) Allowed(event, action string) bool {
allowed = e.GetPullRequest().GetLabeled()
case constants.EventPull + ":" + constants.ActionUnlabeled:
allowed = e.GetPullRequest().GetUnlabeled()
case constants.EventPull + ":" + constants.ActionMerged:
allowed = e.GetPullRequest().GetMerged()
case constants.EventPull + ":" + constants.ActionClosed:
allowed = e.GetPullRequest().GetClosed()
case constants.EventTag:
allowed = e.GetPush().GetTag()
case constants.EventComment + ":" + constants.ActionCreated:
Expand Down Expand Up @@ -176,6 +184,14 @@ func (e *Events) List() []string {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionUnlabeled)
}

if e.GetPullRequest().GetMerged() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionMerged)
}

if e.GetPullRequest().GetClosed() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionClosed)
}

if e.GetPush().GetTag() {
eventSlice = append(eventSlice, constants.EventTag)
}
Expand Down
18 changes: 16 additions & 2 deletions api/types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestTypes_Events_List(t *testing.T) {
"pull_request:synchronize",
"pull_request:reopened",
"pull_request:unlabeled",
"pull_request:merged",
"tag",
"comment:created",
"schedule",
Expand All @@ -133,6 +134,7 @@ func TestTypes_Events_List(t *testing.T) {
wantTwo := []string{
"pull_request:edited",
"pull_request:labeled",
"pull_request:closed",
"deployment",
"comment:edited",
"delete:tag",
Expand Down Expand Up @@ -162,6 +164,7 @@ func TestTypes_Events_NewEventsFromMask_ToDatabase(t *testing.T) {
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowPullUnlabel |
constants.AllowPullMerged |
constants.AllowCommentCreate |
constants.AllowSchedule,
)
Expand All @@ -171,6 +174,7 @@ func TestTypes_Events_NewEventsFromMask_ToDatabase(t *testing.T) {
constants.AllowPullEdit |
constants.AllowCommentEdit |
constants.AllowPullLabel |
constants.AllowPullClosedUnmerged |
constants.AllowDeployCreate,
)

Expand Down Expand Up @@ -215,13 +219,13 @@ func Test_NewEventsFromSlice(t *testing.T) {
}{
{
name: "action specific events to e1",
events: []string{"push:branch", "push:tag", "delete:branch", "pull_request:opened", "pull_request:synchronize", "pull_request:reopened", "comment:created", "schedule:run", "pull_request:unlabeled"},
events: []string{"push:branch", "push:tag", "delete:branch", "pull_request:opened", "pull_request:synchronize", "pull_request:reopened", "pull_request:unlabeled", "pull_request:merged", "comment:created", "schedule:run"},
want: e1,
failure: false,
},
{
name: "action specific events to e2",
events: []string{"delete:tag", "pull_request:edited", "deployment:created", "comment:edited", "pull_request:labeled"},
events: []string{"delete:tag", "pull_request:edited", "pull_request:closed", "deployment:created", "comment:edited", "pull_request:labeled"},
want: e2,
failure: false,
},
Expand All @@ -242,6 +246,8 @@ func Test_NewEventsFromSlice(t *testing.T) {
Synchronize: &tBool,
Labeled: &fBool,
Unlabeled: &fBool,
Merged: &fBool,
Closed: &fBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
Expand Down Expand Up @@ -273,6 +279,8 @@ func Test_NewEventsFromSlice(t *testing.T) {
Synchronize: &tBool,
Labeled: &fBool,
Unlabeled: &fBool,
Merged: &fBool,
Closed: &fBool,
},
Deployment: &actions.Deploy{
Created: &fBool,
Expand Down Expand Up @@ -340,6 +348,8 @@ func TestTypes_Events_Allowed(t *testing.T) {
{event: "pull_request", action: "reopened", want: true},
{event: "pull_request", action: "labeled", want: false},
{event: "pull_request", action: "unlabeled", want: true},
{event: "pull_request", action: "merged", want: true},
{event: "pull_request", action: "closed", want: false},
{event: "deployment", action: "created", want: false},
{event: "comment", action: "created", want: true},
{event: "comment", action: "edited", want: false},
Expand Down Expand Up @@ -381,6 +391,8 @@ func testEvents() (*Events, *Events) {
Reopened: &tBool,
Labeled: &fBool,
Unlabeled: &tBool,
Merged: &tBool,
Closed: &fBool,
},
Deployment: &actions.Deploy{
Created: &fBool,
Expand Down Expand Up @@ -408,6 +420,8 @@ func testEvents() (*Events, *Events) {
Reopened: &fBool,
Labeled: &tBool,
Unlabeled: &fBool,
Merged: &fBool,
Closed: &tBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
Expand Down
4 changes: 2 additions & 2 deletions api/types/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTypes_Repo_Environment(t *testing.T) {
// setup types
want := map[string]string{
"VELA_REPO_ACTIVE": "true",
"VELA_REPO_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,tag,comment:created,schedule,delete:branch",
"VELA_REPO_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,pull_request:merged,tag,comment:created,schedule,delete:branch",
"VELA_REPO_BRANCH": "main",
"VELA_REPO_TOPICS": "cloud,security",
"VELA_REPO_BUILD_LIMIT": "10",
Expand All @@ -34,7 +34,7 @@ func TestTypes_Repo_Environment(t *testing.T) {
"VELA_REPO_APPROVAL_TIMEOUT": "7",
"VELA_REPO_OWNER": "octocat",
"REPOSITORY_ACTIVE": "true",
"REPOSITORY_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,tag,comment:created,schedule,delete:branch",
"REPOSITORY_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,pull_request:merged,tag,comment:created,schedule,delete:branch",
"REPOSITORY_BRANCH": "main",
"REPOSITORY_CLONE": "https://github.com/github/octocat.git",
"REPOSITORY_FULL_NAME": "github/octocat",
Expand Down
Loading
Loading