diff --git a/api/oi_config.go b/api/oi_config.go index acd082951..b2984b38b 100644 --- a/api/oi_config.go +++ b/api/oi_config.go @@ -48,6 +48,7 @@ func GetOpenIDConfig(c *gin.Context) { "build_number", "build_id", "repo", + "pull_fork", "token_type", "actor", "actor_scm_id", diff --git a/api/types/build.go b/api/types/build.go index 70c33b8e7..2d6fa7041 100644 --- a/api/types/build.go +++ b/api/types/build.go @@ -38,6 +38,7 @@ type Build struct { Commit *string `json:"commit,omitempty"` Sender *string `json:"sender,omitempty"` SenderSCMID *string `json:"sender_scm_id,omitempty"` + Fork *bool `json:"fork,omitempty"` Author *string `json:"author,omitempty"` Email *string `json:"email,omitempty"` Link *string `json:"link,omitempty"` @@ -191,6 +192,7 @@ func (b *Build) Environment(workspace, channel string) map[string]string { envs["VELA_PULL_REQUEST"] = number envs["VELA_PULL_REQUEST_SOURCE"] = b.GetHeadRef() envs["VELA_PULL_REQUEST_TARGET"] = b.GetBaseRef() + envs["VELA_PULL_REQUEST_FORK"] = ToString(b.GetFork()) } // check if the Build event is tag @@ -516,6 +518,19 @@ func (b *Build) GetSenderSCMID() string { return *b.SenderSCMID } +// GetFork returns the Fork field. +// +// When the provided Build type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (b *Build) GetFork() bool { + // return zero value if Build type or Fork field is nil + if b == nil || b.Fork == nil { + return false + } + + return *b.Fork +} + // GetAuthor returns the Author field. // // When the provided Build type is nil, or the field within @@ -971,6 +986,19 @@ func (b *Build) SetSenderSCMID(v string) { b.SenderSCMID = &v } +// SetFork sets the Fork field. +// +// When the provided Build type is nil, it +// will set nothing and immediately return. +func (b *Build) SetFork(v bool) { + // return if Build type is nil + if b == nil { + return + } + + b.Fork = &v +} + // SetAuthor sets the Author field. // // When the provided Build type is nil, it @@ -1148,6 +1176,7 @@ func (b *Build) String() string { Event: %s, EventAction: %s, Finished: %d, + Fork: %t, HeadRef: %s, Host: %s, ID: %d, @@ -1184,6 +1213,7 @@ func (b *Build) String() string { b.GetEvent(), b.GetEventAction(), b.GetFinished(), + b.GetFork(), b.GetHeadRef(), b.GetHost(), b.GetID(), diff --git a/api/types/build_test.go b/api/types/build_test.go index ffb0656f1..9733475a2 100644 --- a/api/types/build_test.go +++ b/api/types/build_test.go @@ -79,6 +79,7 @@ func TestTypes_Build_Environment(t *testing.T) { _pull.SetEvent("pull_request") _pull.SetEventAction("opened") _pull.SetRef("refs/pulls/1/head") + _pull.SetFork(false) _tag := testBuild() _tag.SetEvent("tag") @@ -363,6 +364,7 @@ func TestTypes_Build_Environment(t *testing.T) { "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "changes", "VELA_PULL_REQUEST_TARGET": "", + "VELA_PULL_REQUEST_FORK": "false", "BUILD_AUTHOR": "OctoKitty", "BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "BUILD_BASE_REF": "", @@ -563,6 +565,14 @@ func TestTypes_Build_Getters(t *testing.T) { t.Errorf("GetSender is %v, want %v", test.build.GetSender(), test.want.GetSender()) } + if test.build.GetSenderSCMID() != test.want.GetSenderSCMID() { + t.Errorf("GetSenderSCMID is %v, want %v", test.build.GetSenderSCMID(), test.want.GetSenderSCMID()) + } + + if test.build.GetFork() != test.want.GetFork() { + t.Errorf("GetFork is %v, want %v", test.build.GetFork(), test.want.GetFork()) + } + if test.build.GetAuthor() != test.want.GetAuthor() { t.Errorf("GetAuthor is %v, want %v", test.build.GetAuthor(), test.want.GetAuthor()) } @@ -657,6 +667,7 @@ func TestTypes_Build_Setters(t *testing.T) { test.build.SetCommit(test.want.GetCommit()) test.build.SetSender(test.want.GetSender()) test.build.SetSenderSCMID(test.want.GetSenderSCMID()) + test.build.SetFork(test.want.GetFork()) test.build.SetAuthor(test.want.GetAuthor()) test.build.SetEmail(test.want.GetEmail()) test.build.SetLink(test.want.GetLink()) @@ -762,6 +773,10 @@ func TestTypes_Build_Setters(t *testing.T) { t.Errorf("SetSenderSCMID is %v, want %v", test.build.GetSenderSCMID(), test.want.GetSenderSCMID()) } + if test.build.GetFork() != test.want.GetFork() { + t.Errorf("SetFork is %v, want %v", test.build.GetFork(), test.want.GetFork()) + } + if test.build.GetAuthor() != test.want.GetAuthor() { t.Errorf("SetAuthor is %v, want %v", test.build.GetAuthor(), test.want.GetAuthor()) } @@ -835,6 +850,7 @@ func TestTypes_Build_String(t *testing.T) { Event: %s, EventAction: %s, Finished: %d, + Fork: %t, HeadRef: %s, Host: %s, ID: %d, @@ -871,6 +887,7 @@ func TestTypes_Build_String(t *testing.T) { b.GetEvent(), b.GetEventAction(), b.GetFinished(), + b.GetFork(), b.GetHeadRef(), b.GetHost(), b.GetID(), @@ -925,6 +942,7 @@ func testBuild() *Build { b.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163") b.SetSender("OctoKitty") b.SetSenderSCMID("123") + b.SetFork(false) b.SetAuthor("OctoKitty") b.SetEmail("OctoKitty@github.com") b.SetLink("https://example.company.com/github/octocat/1") diff --git a/api/types/oidc.go b/api/types/oidc.go index c6ba560e7..44ec2ee52 100644 --- a/api/types/oidc.go +++ b/api/types/oidc.go @@ -31,6 +31,7 @@ type OpenIDClaims struct { Image string `json:"image,omitempty"` ImageName string `json:"image_name,omitempty"` ImageTag string `json:"image_tag,omitempty"` + PullFork bool `json:"pull_fork,omitempty"` Ref string `json:"ref,omitempty"` Repo string `json:"repo,omitempty"` Request string `json:"request,omitempty"` diff --git a/api/webhook/post.go b/api/webhook/post.go index 3cb1017d2..6cbc17a72 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -537,7 +537,7 @@ func PostWebhook(c *gin.Context) { responded := false // if the webhook was from a Pull event from a forked repository, verify it is allowed to run - if webhook.PullRequest.IsFromFork { + if b.GetFork() { l.Tracef("inside %s workflow for fork PR build %s/%d", repo.GetApproveBuild(), repo.GetFullName(), b.GetNumber()) switch repo.GetApproveBuild() { diff --git a/compiler/native/environment_test.go b/compiler/native/environment_test.go index fae1f4552..b9ddcdec0 100644 --- a/compiler/native/environment_test.go +++ b/compiler/native/environment_test.go @@ -610,11 +610,11 @@ func TestNative_environment(t *testing.T) { // pull_request { w: workspace, - b: &api.Build{ID: &num64, Repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, SenderSCMID: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, + b: &api.Build{ID: &num64, Repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, SenderSCMID: &str, Fork: &booL, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, m: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str, OpenIDIssuer: str}}, r: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, u: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_SERVER_ADDR": "foo", "VELA_OPEN_ID_ISSUER": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SENDER_SCM_ID": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_ID_TOKEN_REQUEST_URL": "foo/api/v1/repos/foo/builds/1/id_token"}, + want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_SERVER_ADDR": "foo", "VELA_OPEN_ID_ISSUER": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SENDER_SCM_ID": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_FORK": "false", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_ID_TOKEN_REQUEST_URL": "foo/api/v1/repos/foo/builds/1/id_token"}, }, // deployment { @@ -631,7 +631,7 @@ func TestNative_environment(t *testing.T) { for _, test := range tests { got := environment(test.b, test.m, test.r, test.u) - if diff := cmp.Diff(got, test.want); diff != "" { + if diff := cmp.Diff(test.want, got); diff != "" { t.Errorf("environment mismatch (-want +got):\n%s", diff) } } @@ -721,11 +721,11 @@ func Test_client_EnvironmentBuild(t *testing.T) { }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "tag", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/tags/1", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TAG": "1", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_SERVER_ADDR": "foo", "VELA_OPEN_ID_ISSUER": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "tag", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/tags/1", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SENDER_SCM_ID": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TAG": "1", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_ID_TOKEN_REQUEST_URL": "foo/api/v1/repos/foo/builds/1/id_token"}, }, {"pull_request", fields{ - build: &api.Build{ID: &num64, Repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, SenderSCMID: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, + build: &api.Build{ID: &num64, Repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, SenderSCMID: &str, Fork: &booL, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, metadata: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str, OpenIDIssuer: str}}, repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, user: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_SERVER_ADDR": "foo", "VELA_OPEN_ID_ISSUER": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SENDER_SCM_ID": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_ID_TOKEN_REQUEST_URL": "foo/api/v1/repos/foo/builds/1/id_token"}, + }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_SERVER_ADDR": "foo", "VELA_OPEN_ID_ISSUER": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_PULL_REQUEST_FORK": "false", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SENDER_SCM_ID": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_ID_TOKEN_REQUEST_URL": "foo/api/v1/repos/foo/builds/1/id_token"}, }, {"deployment", fields{ build: &api.Build{ID: &num64, Repo: &api.Repo{ID: &num64, Owner: &api.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, Number: &num, Parent: &num, Event: &deploy, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &target, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, SenderSCMID: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, diff --git a/database/build/clean.go b/database/build/clean.go index 521e24121..879e87f90 100644 --- a/database/build/clean.go +++ b/database/build/clean.go @@ -8,29 +8,24 @@ import ( "github.com/sirupsen/logrus" - api "github.com/go-vela/server/api/types" "github.com/go-vela/server/constants" - "github.com/go-vela/server/database/types" ) // CleanBuilds updates builds to an error with a provided message with a created timestamp prior to a defined moment. func (e *engine) CleanBuilds(ctx context.Context, msg string, before int64) (int64, error) { logrus.Tracef("cleaning pending or running builds created prior to %d", before) - b := new(api.Build) - b.SetStatus(constants.StatusError) - b.SetError(msg) - b.SetFinished(time.Now().UTC().Unix()) - - build := types.BuildFromAPI(b) - // send query to the database result := e.client. WithContext(ctx). Table(constants.TableBuild). Where("created < ?", before). Where("status = 'running' OR status = 'pending'"). - Updates(build) + Updates(map[string]interface{}{ + "status": constants.StatusError, + "error": msg, + "finished": time.Now().UTC().Unix(), + }) return result.RowsAffected, result.Error } diff --git a/database/build/clean_test.go b/database/build/clean_test.go index 179dd2fb8..bbaba4b39 100644 --- a/database/build/clean_test.go +++ b/database/build/clean_test.go @@ -66,8 +66,8 @@ func TestBuild_Engine_CleanBuilds(t *testing.T) { defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() // ensure the mock expects the name query - _mock.ExpectExec(`UPDATE "builds" SET "status"=$1,"error"=$2,"finished"=$3,"deploy_payload"=$4 WHERE created < $5 AND (status = 'running' OR status = 'pending')`). - WithArgs("error", "msg", NowTimestamp{}, AnyArgument{}, 3). + _mock.ExpectExec(`UPDATE "builds" SET "error"=$1,"finished"=$2,"status"=$3 WHERE created < $4 AND (status = 'running' OR status = 'pending')`). + WithArgs("msg", NowTimestamp{}, "error", 3). WillReturnResult(sqlmock.NewResult(1, 2)) _sqlite := testSqlite(t) diff --git a/database/build/create_test.go b/database/build/create_test.go index 9f9d72399..219cd8209 100644 --- a/database/build/create_test.go +++ b/database/build/create_test.go @@ -42,9 +42,9 @@ func TestBuild_Engine_CreateBuild(t *testing.T) { // ensure the mock expects the query _mock.ExpectQuery(`INSERT INTO "builds" -("repo_id","pipeline_id","number","parent","event","event_action","status","error","enqueued","created","started","finished","deploy","deploy_number","deploy_payload","clone","source","title","message","commit","sender","sender_scm_id","author","email","link","branch","ref","base_ref","head_ref","host","runtime","distribution","approved_at","approved_by","id") -VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35) RETURNING "id"`). - WithArgs(1, nil, 1, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, AnyArgument{}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 1). +("repo_id","pipeline_id","number","parent","event","event_action","status","error","enqueued","created","started","finished","deploy","deploy_number","deploy_payload","clone","source","title","message","commit","sender","sender_scm_id","fork","author","email","link","branch","ref","base_ref","head_ref","host","runtime","distribution","approved_at","approved_by","id") +VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36) RETURNING "id"`). + WithArgs(1, nil, 1, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, AnyArgument{}, nil, nil, nil, nil, nil, nil, nil, false, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 1). WillReturnRows(_rows) _sqlite := testSqlite(t) diff --git a/database/build/table.go b/database/build/table.go index 756a621cb..c250df1db 100644 --- a/database/build/table.go +++ b/database/build/table.go @@ -37,6 +37,7 @@ builds ( commit VARCHAR(500), sender VARCHAR(250), sender_scm_id VARCHAR(250), + fork BOOLEAN, author VARCHAR(250), email VARCHAR(500), link VARCHAR(1000), @@ -82,6 +83,7 @@ builds ( 'commit' TEXT, sender TEXT, sender_scm_id TEXT, + fork BOOLEAN, author TEXT, email TEXT, link TEXT, diff --git a/database/build/update_test.go b/database/build/update_test.go index 4c0c1137e..fe6a23596 100644 --- a/database/build/update_test.go +++ b/database/build/update_test.go @@ -41,9 +41,9 @@ func TestBuild_Engine_UpdateBuild(t *testing.T) { // ensure the mock expects the query _mock.ExpectExec(`UPDATE "builds" -SET "repo_id"=$1,"pipeline_id"=$2,"number"=$3,"parent"=$4,"event"=$5,"event_action"=$6,"status"=$7,"error"=$8,"enqueued"=$9,"created"=$10,"started"=$11,"finished"=$12,"deploy"=$13,"deploy_number"=$14,"deploy_payload"=$15,"clone"=$16,"source"=$17,"title"=$18,"message"=$19,"commit"=$20,"sender"=$21,"sender_scm_id"=$22,"author"=$23,"email"=$24,"link"=$25,"branch"=$26,"ref"=$27,"base_ref"=$28,"head_ref"=$29,"host"=$30,"runtime"=$31,"distribution"=$32,"approved_at"=$33,"approved_by"=$34 -WHERE "id" = $35`). - WithArgs(1, nil, 1, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, AnyArgument{}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 1). +SET "repo_id"=$1,"pipeline_id"=$2,"number"=$3,"parent"=$4,"event"=$5,"event_action"=$6,"status"=$7,"error"=$8,"enqueued"=$9,"created"=$10,"started"=$11,"finished"=$12,"deploy"=$13,"deploy_number"=$14,"deploy_payload"=$15,"clone"=$16,"source"=$17,"title"=$18,"message"=$19,"commit"=$20,"sender"=$21,"sender_scm_id"=$22,"fork"=$23,"author"=$24,"email"=$25,"link"=$26,"branch"=$27,"ref"=$28,"base_ref"=$29,"head_ref"=$30,"host"=$31,"runtime"=$32,"distribution"=$33,"approved_at"=$34,"approved_by"=$35 +WHERE "id" = $36`). + WithArgs(1, nil, 1, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, AnyArgument{}, nil, nil, nil, nil, nil, nil, nil, false, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 1). WillReturnResult(sqlmock.NewResult(1, 1)) _sqlite := testSqlite(t) diff --git a/database/integration_test.go b/database/integration_test.go index 89eda666a..ce9a3ccfe 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -2539,6 +2539,7 @@ func newResources() *Resources { buildOne.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163") buildOne.SetSender("OctoKitty") buildOne.SetSenderSCMID("123") + buildOne.SetFork(false) buildOne.SetAuthor("OctoKitty") buildOne.SetEmail("OctoKitty@github.com") buildOne.SetLink("https://example.company.com/github/octocat/1") @@ -2576,6 +2577,7 @@ func newResources() *Resources { buildTwo.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135164") buildTwo.SetSender("OctoKitty") buildTwo.SetSenderSCMID("123") + buildTwo.SetFork(false) buildTwo.SetAuthor("OctoKitty") buildTwo.SetEmail("OctoKitty@github.com") buildTwo.SetLink("https://example.company.com/github/octocat/2") diff --git a/database/testutils/api_resources.go b/database/testutils/api_resources.go index 18f619afd..70cc45d65 100644 --- a/database/testutils/api_resources.go +++ b/database/testutils/api_resources.go @@ -42,6 +42,7 @@ func APIBuild() *api.Build { Commit: new(string), Sender: new(string), SenderSCMID: new(string), + Fork: new(bool), Author: new(string), Email: new(string), Link: new(string), diff --git a/database/types/build.go b/database/types/build.go index f3aa22cd3..0ea840b9b 100644 --- a/database/types/build.go +++ b/database/types/build.go @@ -55,6 +55,7 @@ type Build struct { Commit sql.NullString `sql:"commit"` Sender sql.NullString `sql:"sender"` SenderSCMID sql.NullString `sql:"sender_scm_id"` + Fork sql.NullBool `sql:"fork"` Author sql.NullString `sql:"author"` Email sql.NullString `sql:"email"` Link sql.NullString `sql:"link"` @@ -315,6 +316,7 @@ func (b *Build) ToAPI() *api.Build { build.SetCommit(b.Commit.String) build.SetSender(b.Sender.String) build.SetSenderSCMID(b.SenderSCMID.String) + build.SetFork(b.Fork.Bool) build.SetAuthor(b.Author.String) build.SetEmail(b.Email.String) build.SetLink(b.Link.String) @@ -401,6 +403,7 @@ func BuildFromAPI(b *api.Build) *Build { Commit: sql.NullString{String: b.GetCommit(), Valid: true}, Sender: sql.NullString{String: b.GetSender(), Valid: true}, SenderSCMID: sql.NullString{String: b.GetSenderSCMID(), Valid: true}, + Fork: sql.NullBool{Bool: b.GetFork(), Valid: true}, Author: sql.NullString{String: b.GetAuthor(), Valid: true}, Email: sql.NullString{String: b.GetEmail(), Valid: true}, Link: sql.NullString{String: b.GetLink(), Valid: true}, diff --git a/database/types/build_test.go b/database/types/build_test.go index 372e09fc4..3bba2b634 100644 --- a/database/types/build_test.go +++ b/database/types/build_test.go @@ -66,6 +66,7 @@ func TestTypes_Build_Nullify(t *testing.T) { Message: sql.NullString{String: "", Valid: false}, Commit: sql.NullString{String: "", Valid: false}, Sender: sql.NullString{String: "", Valid: false}, + Fork: sql.NullBool{Bool: false, Valid: false}, Author: sql.NullString{String: "", Valid: false}, Email: sql.NullString{String: "", Valid: false}, Link: sql.NullString{String: "", Valid: false}, @@ -134,6 +135,7 @@ func TestTypes_Build_ToAPI(t *testing.T) { want.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163") want.SetSender("OctoKitty") want.SetSenderSCMID("123") + want.SetFork(false) want.SetAuthor("OctoKitty") want.SetEmail("OctoKitty@github.com") want.SetLink("https://example.company.com/github/octocat/1") @@ -230,6 +232,7 @@ func TestTypes_Build_BuildFromAPI(t *testing.T) { b.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163") b.SetSender("OctoKitty") b.SetSenderSCMID("123") + b.SetFork(false) b.SetAuthor("OctoKitty") b.SetEmail("OctoKitty@github.com") b.SetLink("https://example.company.com/github/octocat/1") @@ -294,6 +297,7 @@ func testBuild() *Build { Commit: sql.NullString{String: "48afb5bdc41ad69bf22588491333f7cf71135163", Valid: true}, Sender: sql.NullString{String: "OctoKitty", Valid: true}, SenderSCMID: sql.NullString{String: "123", Valid: true}, + Fork: sql.NullBool{Bool: false, Valid: true}, Author: sql.NullString{String: "OctoKitty", Valid: true}, Email: sql.NullString{String: "OctoKitty@github.com", Valid: true}, Link: sql.NullString{String: "https://example.company.com/github/octocat/1", Valid: true}, diff --git a/internal/token/mint.go b/internal/token/mint.go index 8fe074aab..75c8c4487 100644 --- a/internal/token/mint.go +++ b/internal/token/mint.go @@ -27,6 +27,7 @@ type Claims struct { IsActive bool `json:"is_active,omitempty"` IsAdmin bool `json:"is_admin,omitempty"` Repo string `json:"repo,omitempty"` + PullFork bool `json:"pull_fork,omitempty"` TokenType string `json:"token_type,omitempty"` Image string `json:"image,omitempty"` Request string `json:"request,omitempty"` @@ -103,6 +104,7 @@ func (tm *Manager) MintToken(mto *MintTokenOpts) (string, error) { } claims.Repo = mto.Repo + claims.PullFork = mto.Build.GetFork() claims.Subject = fmt.Sprintf("repo:%s:ref:%s:event:%s", mto.Repo, mto.Build.GetRef(), mto.Build.GetEvent()) claims.BuildID = mto.Build.GetID() claims.BuildNumber = mto.Build.GetNumber() @@ -162,6 +164,7 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab claims.BuildID = mto.Build.GetID() claims.Repo = mto.Repo claims.Event = fmt.Sprintf("%s:%s", mto.Build.GetEvent(), mto.Build.GetEventAction()) + claims.PullFork = mto.Build.GetFork() claims.SHA = mto.Build.GetCommit() claims.Ref = mto.Build.GetRef() claims.Subject = fmt.Sprintf("repo:%s:ref:%s:event:%s", mto.Repo, mto.Build.GetRef(), mto.Build.GetEvent()) diff --git a/internal/webhook.go b/internal/webhook.go index 7edb365c2..6020d2ab1 100644 --- a/internal/webhook.go +++ b/internal/webhook.go @@ -16,10 +16,9 @@ var ( // PullRequest defines the data pulled from PRs while // processing a webhook. type PullRequest struct { - Comment string - Number int - IsFromFork bool - Labels []string + Comment string + Number int + Labels []string } // Webhook defines a struct that is used to return diff --git a/mock/server/build.go b/mock/server/build.go index 786f26cb0..3a9905d16 100644 --- a/mock/server/build.go +++ b/mock/server/build.go @@ -84,6 +84,7 @@ const ( "commit": "48afb5bdc41ad69bf22588491333f7cf71135163", "sender": "OctoKitty", "sender_scm_id": "0", + "fork": false, "author": "OctoKitty", "email": "octokitty@github.com", "link": "https://vela.example.company.com/github/octocat/1", diff --git a/router/middleware/build/build_test.go b/router/middleware/build/build_test.go index aacf14949..8db7c8000 100644 --- a/router/middleware/build/build_test.go +++ b/router/middleware/build/build_test.go @@ -77,6 +77,7 @@ func TestBuild_Establish(t *testing.T) { want.SetCommit("") want.SetSender("") want.SetSenderSCMID("") + want.SetFork(false) want.SetAuthor("") want.SetEmail("") want.SetLink("") diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 632a42e1b..91a3854f2 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -330,14 +330,14 @@ func (c *client) processPREvent(h *api.Hook, payload *github.PullRequestEvent) ( } // 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()) + + b.SetFork(payload.GetPullRequest().GetHead().GetRepo().GetFork() && + !strings.EqualFold(payload.GetPullRequest().GetBase().GetRepo().GetFullName(), payload.GetPullRequest().GetHead().GetRepo().GetFullName())) return &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: payload.GetNumber(), - IsFromFork: fromFork, - Labels: prLabels, + Number: payload.GetNumber(), + Labels: prLabels, }, Hook: h, Repo: r, diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index 1ef1685fe..0d342ccdb 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -370,6 +370,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantBuild.SetCommit("34c5c7793cb3b279e22454cb6750c80560547b3a") wantBuild.SetSender("Codertocat") wantBuild.SetSenderSCMID("21031067") + wantBuild.SetFork(false) wantBuild.SetAuthor("Codertocat") wantBuild.SetEmail("") wantBuild.SetBranch("main") @@ -377,6 +378,10 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantBuild.SetBaseRef("main") wantBuild.SetHeadRef("changes") + wantBuildFork := *wantBuild + tBool := true + wantBuildFork.Fork = &tBool + wantBuild2 := new(api.Build) wantBuild2.SetEvent("pull_request") wantBuild2.SetEventAction("labeled") @@ -387,6 +392,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantBuild2.SetCommit("34c5c7793cb3b279e22454cb6750c80560547b3a") wantBuild2.SetSender("Codertocat") wantBuild2.SetSenderSCMID("21031067") + wantBuild2.SetFork(false) wantBuild2.SetAuthor("Codertocat") wantBuild2.SetEmail("") wantBuild2.SetBranch("main") @@ -404,6 +410,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantBuild3.SetCommit("34c5c7793cb3b279e22454cb6750c80560547b3a") wantBuild3.SetSender("Codertocat") wantBuild3.SetSenderSCMID("21031067") + wantBuild3.SetFork(false) wantBuild3.SetAuthor("Codertocat") wantBuild3.SetEmail("") wantBuild3.SetBranch("main") @@ -421,6 +428,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantBuild4.SetCommit("34c5c7793cb3b279e22454cb6750c80560547b3a") wantBuild4.SetSender("Codertocat") wantBuild4.SetSenderSCMID("21031067") + wantBuild4.SetFork(false) wantBuild4.SetAuthor("Codertocat") wantBuild4.SetEmail("") wantBuild4.SetBranch("main") @@ -439,8 +447,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: false, + Number: wantHook.GetNumber(), }, Hook: wantHook, Repo: wantRepo, @@ -452,12 +459,11 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request_fork.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: true, + Number: wantHook.GetNumber(), }, Hook: wantHook, Repo: wantRepo, - Build: wantBuild, + Build: &wantBuildFork, }, }, { @@ -465,8 +471,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request_fork_same-repo.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: false, + Number: wantHook.GetNumber(), }, Hook: wantHook, Repo: wantRepo, @@ -496,9 +501,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request_labeled.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: false, - Labels: []string{"documentation"}, + Number: wantHook.GetNumber(), + Labels: []string{"documentation"}, }, Hook: wantHook, Repo: wantRepo, @@ -510,9 +514,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request_unlabeled.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: false, - Labels: []string{"documentation"}, + Number: wantHook.GetNumber(), + Labels: []string{"documentation"}, }, Hook: wantHook, Repo: wantRepo, @@ -524,9 +527,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { testData: "testdata/hooks/pull_request_edited_while_labeled.json", want: &internal.Webhook{ PullRequest: internal.PullRequest{ - Number: wantHook.GetNumber(), - IsFromFork: false, - Labels: []string{"documentation", "enhancement"}, + Number: wantHook.GetNumber(), + Labels: []string{"documentation", "enhancement"}, }, Hook: wantHook, Repo: wantRepo,