diff --git a/constants/repo.go b/constants/repo.go index b4feea99..c6e8277d 100644 --- a/constants/repo.go +++ b/constants/repo.go @@ -17,15 +17,15 @@ const ( PipelineTypeStarlark = "starlark" ) -// Repo ApproveForkBuild types. +// Repo ApproveBuild types. const ( - // ApproveAlways defines the CI strategy of having a repo administrator approve + // ApproveForkAlways defines the CI strategy of having a repo administrator approve // all builds triggered from a forked PR. - ApproveAlways = "always" + ApproveForkAlways = "fork-always" - // ApproveNoWrite defines the CI strategy of having a repo administrator approve + // ApproveForkNoWrite defines the CI strategy of having a repo administrator approve // all builds triggered from a forked PR where the author does not have write access. - ApproveNoWrite = "no-write" + ApproveForkNoWrite = "fork-no-write" // ApproveOnce defines the CI strategy of having a repo administrator approve // all builds triggered from an outside contributor if this is their first time contributing. diff --git a/database/build.go b/database/build.go index 204a5e77..d8a7e49b 100644 --- a/database/build.go +++ b/database/build.go @@ -62,6 +62,8 @@ type Build struct { Host sql.NullString `sql:"host"` Runtime sql.NullString `sql:"runtime"` Distribution sql.NullString `sql:"distribution"` + ApprovedAt sql.NullInt64 `sql:"approved_at"` + ApprovedBy sql.NullString `sql:"approved_by"` } // Crop prepares the Build type for inserting into the database by @@ -92,7 +94,7 @@ func (b *Build) Crop() *Build { // value for the field, the valid flag is set to // false causing it to be NULL in the database. // -//nolint:gocyclo // ignore cyclomatic complexity due to number of fields +//nolint:gocyclo,funlen // ignore cyclomatic complexity due to number of fields func (b *Build) Nullify() *Build { if b == nil { return nil @@ -248,6 +250,16 @@ func (b *Build) Nullify() *Build { b.Distribution.Valid = false } + // check if the ApprovedAt field should be false + if b.ApprovedAt.Int64 == 0 { + b.ApprovedAt.Valid = false + } + + // check if the ApprovedBy field should be false + if len(b.ApprovedBy.String) == 0 { + b.ApprovedBy.Valid = false + } + return b } @@ -287,6 +299,8 @@ func (b *Build) ToLibrary() *library.Build { build.SetHost(b.Host.String) build.SetRuntime(b.Runtime.String) build.SetDistribution(b.Distribution.String) + build.SetApprovedAt(b.ApprovedAt.Int64) + build.SetApprovedBy(b.ApprovedBy.String) return build } @@ -328,6 +342,7 @@ func (b *Build) Validate() error { b.Host = sql.NullString{String: sanitize(b.Host.String), Valid: b.Host.Valid} b.Runtime = sql.NullString{String: sanitize(b.Runtime.String), Valid: b.Runtime.Valid} b.Distribution = sql.NullString{String: sanitize(b.Distribution.String), Valid: b.Distribution.Valid} + b.ApprovedBy = sql.NullString{String: sanitize(b.ApprovedBy.String), Valid: b.ApprovedBy.Valid} return nil } @@ -367,6 +382,8 @@ func BuildFromLibrary(b *library.Build) *Build { Host: sql.NullString{String: b.GetHost(), Valid: true}, Runtime: sql.NullString{String: b.GetRuntime(), Valid: true}, Distribution: sql.NullString{String: b.GetDistribution(), Valid: true}, + ApprovedAt: sql.NullInt64{Int64: b.GetApprovedAt(), Valid: true}, + ApprovedBy: sql.NullString{String: b.GetApprovedBy(), Valid: true}, } return build.Nullify() diff --git a/database/build_test.go b/database/build_test.go index 972b4cf2..90458c44 100644 --- a/database/build_test.go +++ b/database/build_test.go @@ -139,6 +139,8 @@ func TestDatabase_Build_ToLibrary(t *testing.T) { want.SetRuntime("docker") want.SetDistribution("linux") want.SetDeployPayload(raw.StringSliceMap{"foo": "test1", "bar": "test2"}) + want.SetApprovedAt(1563474076) + want.SetApprovedBy("OctoCat") // run test got := testBuild().ToLibrary() @@ -228,6 +230,8 @@ func TestDatabase_BuildFromLibrary(t *testing.T) { b.SetRuntime("docker") b.SetDistribution("linux") b.SetDeployPayload(raw.StringSliceMap{"foo": "test1", "bar": "test2"}) + b.SetApprovedAt(1563474076) + b.SetApprovedBy("OctoCat") want := testBuild() @@ -286,5 +290,7 @@ func testBuild() *Build { Host: sql.NullString{String: "example.company.com", Valid: true}, Runtime: sql.NullString{String: "docker", Valid: true}, Distribution: sql.NullString{String: "linux", Valid: true}, + ApprovedAt: sql.NullInt64{Int64: 1563474076, Valid: true}, + ApprovedBy: sql.NullString{String: "OctoCat", Valid: true}, } } diff --git a/database/repo.go b/database/repo.go index f6c8f56b..1c185b68 100644 --- a/database/repo.go +++ b/database/repo.go @@ -44,31 +44,31 @@ var ( // Repo is the database representation of a repo. type Repo struct { - ID sql.NullInt64 `sql:"id"` - UserID sql.NullInt64 `sql:"user_id"` - Hash sql.NullString `sql:"hash"` - Org sql.NullString `sql:"org"` - Name sql.NullString `sql:"name"` - FullName sql.NullString `sql:"full_name"` - Link sql.NullString `sql:"link"` - Clone sql.NullString `sql:"clone"` - Branch sql.NullString `sql:"branch"` - Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"` - BuildLimit sql.NullInt64 `sql:"build_limit"` - Timeout sql.NullInt64 `sql:"timeout"` - Counter sql.NullInt32 `sql:"counter"` - Visibility sql.NullString `sql:"visibility"` - Private sql.NullBool `sql:"private"` - Trusted sql.NullBool `sql:"trusted"` - Active sql.NullBool `sql:"active"` - AllowPull sql.NullBool `sql:"allow_pull"` - AllowPush sql.NullBool `sql:"allow_push"` - AllowDeploy sql.NullBool `sql:"allow_deploy"` - AllowTag sql.NullBool `sql:"allow_tag"` - AllowComment sql.NullBool `sql:"allow_comment"` - PipelineType sql.NullString `sql:"pipeline_type"` - PreviousName sql.NullString `sql:"previous_name"` - ApproveForkBuild sql.NullString `sql:"approve_fork_build"` + ID sql.NullInt64 `sql:"id"` + UserID sql.NullInt64 `sql:"user_id"` + Hash sql.NullString `sql:"hash"` + Org sql.NullString `sql:"org"` + Name sql.NullString `sql:"name"` + FullName sql.NullString `sql:"full_name"` + Link sql.NullString `sql:"link"` + Clone sql.NullString `sql:"clone"` + Branch sql.NullString `sql:"branch"` + Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"` + BuildLimit sql.NullInt64 `sql:"build_limit"` + Timeout sql.NullInt64 `sql:"timeout"` + Counter sql.NullInt32 `sql:"counter"` + Visibility sql.NullString `sql:"visibility"` + Private sql.NullBool `sql:"private"` + Trusted sql.NullBool `sql:"trusted"` + Active sql.NullBool `sql:"active"` + AllowPull sql.NullBool `sql:"allow_pull"` + AllowPush sql.NullBool `sql:"allow_push"` + AllowDeploy sql.NullBool `sql:"allow_deploy"` + AllowTag sql.NullBool `sql:"allow_tag"` + AllowComment sql.NullBool `sql:"allow_comment"` + PipelineType sql.NullString `sql:"pipeline_type"` + PreviousName sql.NullString `sql:"previous_name"` + ApproveBuild sql.NullString `sql:"approve_build"` } // Decrypt will manipulate the existing repo hash by @@ -200,8 +200,8 @@ func (r *Repo) Nullify() *Repo { } // check if the ApproveForkBuild field should be false - if len(r.ApproveForkBuild.String) == 0 { - r.ApproveForkBuild.Valid = false + if len(r.ApproveBuild.String) == 0 { + r.ApproveBuild.Valid = false } return r @@ -236,7 +236,7 @@ func (r *Repo) ToLibrary() *library.Repo { repo.SetAllowComment(r.AllowComment.Bool) repo.SetPipelineType(r.PipelineType.String) repo.SetPreviousName(r.PreviousName.String) - repo.SetApproveForkBuild(r.ApproveForkBuild.String) + repo.SetApproveBuild(r.ApproveBuild.String) return repo } @@ -308,31 +308,31 @@ func (r *Repo) Validate() error { // to a database repo type. func RepoFromLibrary(r *library.Repo) *Repo { repo := &Repo{ - ID: sql.NullInt64{Int64: r.GetID(), Valid: true}, - UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true}, - Hash: sql.NullString{String: r.GetHash(), Valid: true}, - Org: sql.NullString{String: r.GetOrg(), Valid: true}, - Name: sql.NullString{String: r.GetName(), Valid: true}, - FullName: sql.NullString{String: r.GetFullName(), Valid: true}, - Link: sql.NullString{String: r.GetLink(), Valid: true}, - Clone: sql.NullString{String: r.GetClone(), Valid: true}, - Branch: sql.NullString{String: r.GetBranch(), Valid: true}, - Topics: pq.StringArray(r.GetTopics()), - BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true}, - Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true}, - Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true}, - Visibility: sql.NullString{String: r.GetVisibility(), Valid: true}, - Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true}, - Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true}, - Active: sql.NullBool{Bool: r.GetActive(), Valid: true}, - AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true}, - AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true}, - AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true}, - AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true}, - AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true}, - PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true}, - PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true}, - ApproveForkBuild: sql.NullString{String: r.GetApproveForkBuild(), Valid: true}, + ID: sql.NullInt64{Int64: r.GetID(), Valid: true}, + UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true}, + Hash: sql.NullString{String: r.GetHash(), Valid: true}, + Org: sql.NullString{String: r.GetOrg(), Valid: true}, + Name: sql.NullString{String: r.GetName(), Valid: true}, + FullName: sql.NullString{String: r.GetFullName(), Valid: true}, + Link: sql.NullString{String: r.GetLink(), Valid: true}, + Clone: sql.NullString{String: r.GetClone(), Valid: true}, + Branch: sql.NullString{String: r.GetBranch(), Valid: true}, + Topics: pq.StringArray(r.GetTopics()), + BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true}, + Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true}, + Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true}, + Visibility: sql.NullString{String: r.GetVisibility(), Valid: true}, + Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true}, + Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true}, + Active: sql.NullBool{Bool: r.GetActive(), Valid: true}, + AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true}, + AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true}, + AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true}, + AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true}, + AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true}, + PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true}, + PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true}, + ApproveBuild: sql.NullString{String: r.GetApproveBuild(), Valid: true}, } return repo.Nullify() diff --git a/database/repo_test.go b/database/repo_test.go index 63dad32a..cf7d2436 100644 --- a/database/repo_test.go +++ b/database/repo_test.go @@ -107,19 +107,19 @@ func TestDatabase_Repo_Nullify(t *testing.T) { var r *Repo want := &Repo{ - ID: sql.NullInt64{Int64: 0, Valid: false}, - UserID: sql.NullInt64{Int64: 0, Valid: false}, - Hash: sql.NullString{String: "", Valid: false}, - Org: sql.NullString{String: "", Valid: false}, - Name: sql.NullString{String: "", Valid: false}, - FullName: sql.NullString{String: "", Valid: false}, - Link: sql.NullString{String: "", Valid: false}, - Clone: sql.NullString{String: "", Valid: false}, - Branch: sql.NullString{String: "", Valid: false}, - Timeout: sql.NullInt64{Int64: 0, Valid: false}, - Visibility: sql.NullString{String: "", Valid: false}, - PipelineType: sql.NullString{String: "", Valid: false}, - ApproveForkBuild: sql.NullString{String: "", Valid: false}, + ID: sql.NullInt64{Int64: 0, Valid: false}, + UserID: sql.NullInt64{Int64: 0, Valid: false}, + Hash: sql.NullString{String: "", Valid: false}, + Org: sql.NullString{String: "", Valid: false}, + Name: sql.NullString{String: "", Valid: false}, + FullName: sql.NullString{String: "", Valid: false}, + Link: sql.NullString{String: "", Valid: false}, + Clone: sql.NullString{String: "", Valid: false}, + Branch: sql.NullString{String: "", Valid: false}, + Timeout: sql.NullInt64{Int64: 0, Valid: false}, + Visibility: sql.NullString{String: "", Valid: false}, + PipelineType: sql.NullString{String: "", Valid: false}, + ApproveBuild: sql.NullString{String: "", Valid: false}, } // setup tests @@ -179,7 +179,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) { want.SetAllowComment(false) want.SetPipelineType("yaml") want.SetPreviousName("oldName") - want.SetApproveForkBuild(constants.ApproveNever) + want.SetApproveBuild(constants.ApproveNever) // run test got := testRepo().ToLibrary() @@ -333,7 +333,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { r.SetAllowComment(false) r.SetPipelineType("yaml") r.SetPreviousName("oldName") - r.SetApproveForkBuild(constants.ApproveNever) + r.SetApproveBuild(constants.ApproveNever) want := testRepo() @@ -349,30 +349,30 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { // type with all fields set to a fake value. func testRepo() *Repo { return &Repo{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - UserID: sql.NullInt64{Int64: 1, Valid: true}, - Hash: sql.NullString{String: "superSecretHash", Valid: true}, - Org: sql.NullString{String: "github", Valid: true}, - Name: sql.NullString{String: "octocat", Valid: true}, - FullName: sql.NullString{String: "github/octocat", Valid: true}, - Link: sql.NullString{String: "https://github.com/github/octocat", Valid: true}, - Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true}, - Branch: sql.NullString{String: "main", Valid: true}, - Topics: []string{"cloud", "security"}, - BuildLimit: sql.NullInt64{Int64: 10, Valid: true}, - Timeout: sql.NullInt64{Int64: 30, Valid: true}, - Counter: sql.NullInt32{Int32: 0, Valid: true}, - Visibility: sql.NullString{String: "public", Valid: true}, - Private: sql.NullBool{Bool: false, Valid: true}, - Trusted: sql.NullBool{Bool: false, Valid: true}, - Active: sql.NullBool{Bool: true, Valid: true}, - AllowPull: sql.NullBool{Bool: false, Valid: true}, - AllowPush: sql.NullBool{Bool: true, Valid: true}, - AllowDeploy: sql.NullBool{Bool: false, Valid: true}, - AllowTag: sql.NullBool{Bool: false, Valid: true}, - AllowComment: sql.NullBool{Bool: false, Valid: true}, - PipelineType: sql.NullString{String: "yaml", Valid: true}, - PreviousName: sql.NullString{String: "oldName", Valid: true}, - ApproveForkBuild: sql.NullString{String: constants.ApproveNever, Valid: true}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + UserID: sql.NullInt64{Int64: 1, Valid: true}, + Hash: sql.NullString{String: "superSecretHash", Valid: true}, + Org: sql.NullString{String: "github", Valid: true}, + Name: sql.NullString{String: "octocat", Valid: true}, + FullName: sql.NullString{String: "github/octocat", Valid: true}, + Link: sql.NullString{String: "https://github.com/github/octocat", Valid: true}, + Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, + Topics: []string{"cloud", "security"}, + BuildLimit: sql.NullInt64{Int64: 10, Valid: true}, + Timeout: sql.NullInt64{Int64: 30, Valid: true}, + Counter: sql.NullInt32{Int32: 0, Valid: true}, + Visibility: sql.NullString{String: "public", Valid: true}, + Private: sql.NullBool{Bool: false, Valid: true}, + Trusted: sql.NullBool{Bool: false, Valid: true}, + Active: sql.NullBool{Bool: true, Valid: true}, + AllowPull: sql.NullBool{Bool: false, Valid: true}, + AllowPush: sql.NullBool{Bool: true, Valid: true}, + AllowDeploy: sql.NullBool{Bool: false, Valid: true}, + AllowTag: sql.NullBool{Bool: false, Valid: true}, + AllowComment: sql.NullBool{Bool: false, Valid: true}, + PipelineType: sql.NullString{String: "yaml", Valid: true}, + PreviousName: sql.NullString{String: "oldName", Valid: true}, + ApproveBuild: sql.NullString{String: constants.ApproveNever, Valid: true}, } } diff --git a/library/build.go b/library/build.go index 34edee9d..ca091267 100644 --- a/library/build.go +++ b/library/build.go @@ -46,6 +46,8 @@ type Build struct { Host *string `json:"host,omitempty"` Runtime *string `json:"runtime,omitempty"` Distribution *string `json:"distribution,omitempty"` + ApprovedAt *int64 `json:"approved_at,omitempty"` + ApprovedBy *string `json:"approved_by,omitempty"` } // Duration calculates and returns the total amount of @@ -83,6 +85,8 @@ func (b *Build) Duration() string { // provided from the fields of the Build type. func (b *Build) Environment(workspace, channel string) map[string]string { envs := map[string]string{ + "VELA_BUILD_APPROVED_AT": ToString(b.GetApprovedAt()), + "VELA_BUILD_APPROVED_BY": ToString(b.GetApprovedBy()), "VELA_BUILD_AUTHOR": ToString(b.GetAuthor()), "VELA_BUILD_AUTHOR_EMAIL": ToString(b.GetEmail()), "VELA_BUILD_BASE_REF": ToString(b.GetBaseRef()), @@ -600,6 +604,32 @@ func (b *Build) GetDistribution() string { return *b.Distribution } +// GetApprovedAt returns the ApprovedAt 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) GetApprovedAt() int64 { + // return zero value if Build type or ApprovedAt field is nil + if b == nil || b.ApprovedAt == nil { + return 0 + } + + return *b.ApprovedAt +} + +// GetApprovedBy returns the ApprovedBy 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) GetApprovedBy() string { + // return zero value if Build type or ApprovedBy field is nil + if b == nil || b.ApprovedBy == nil { + return "" + } + + return *b.ApprovedBy +} + // SetID sets the ID field. // // When the provided Build type is nil, it @@ -1003,11 +1033,39 @@ func (b *Build) SetDistribution(v string) { b.Distribution = &v } +// SetApprovedAt sets the ApprovedAt field. +// +// When the provided Build type is nil, it +// will set nothing and immediately return. +func (b *Build) SetApprovedAt(v int64) { + // return if Build type is nil + if b == nil { + return + } + + b.ApprovedAt = &v +} + +// SetApprovedBy sets the ApprovedBy field. +// +// When the provided Build type is nil, it +// will set nothing and immediately return. +func (b *Build) SetApprovedBy(v string) { + // return if Build type is nil + if b == nil { + return + } + + b.ApprovedBy = &v +} + // String implements the Stringer interface for the Build type. // //nolint:dupl // this is duplicated in the test func (b *Build) String() string { return fmt.Sprintf(`{ + ApprovedAt: %d, + ApprovedBy: %s, Author: %s, BaseRef: %s, Branch: %s, @@ -1040,6 +1098,8 @@ func (b *Build) String() string { Status: %s, Title: %s, }`, + b.GetApprovedAt(), + b.GetApprovedBy(), b.GetAuthor(), b.GetBaseRef(), b.GetBranch(), diff --git a/library/build_test.go b/library/build_test.go index 64e4c5ca..339daa90 100644 --- a/library/build_test.go +++ b/library/build_test.go @@ -86,6 +86,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: testBuild(), want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -138,6 +140,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: _comment, want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -193,6 +197,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: _deploy, want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -250,6 +256,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: _deployTag, want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -309,6 +317,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: _pull, want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -366,6 +376,8 @@ func TestLibrary_Build_Environment(t *testing.T) { { build: _tag, want: map[string]string{ + "VELA_BUILD_APPROVED_AT": "1563474076", + "VELA_BUILD_APPROVED_BY": "OctoCat", "VELA_BUILD_AUTHOR": "OctoKitty", "VELA_BUILD_AUTHOR_EMAIL": "OctoKitty@github.com", "VELA_BUILD_BASE_REF": "", @@ -570,6 +582,14 @@ func TestLibrary_Build_Getters(t *testing.T) { if test.build.GetDistribution() != test.want.GetDistribution() { t.Errorf("GetDistribution is %v, want %v", test.build.GetDistribution(), test.want.GetDistribution()) } + + if test.build.GetApprovedAt() != test.want.GetApprovedAt() { + t.Errorf("GetApprovedAt is %v, want %v", test.build.GetApprovedAt(), test.want.GetApprovedAt()) + } + + if test.build.GetApprovedBy() != test.want.GetApprovedBy() { + t.Errorf("GetApprovedBy is %v, want %v", test.build.GetApprovedBy(), test.want.GetApprovedBy()) + } } } @@ -625,6 +645,8 @@ func TestLibrary_Build_Setters(t *testing.T) { test.build.SetHost(test.want.GetHost()) test.build.SetRuntime(test.want.GetRuntime()) test.build.SetDistribution(test.want.GetDistribution()) + test.build.SetApprovedAt(test.want.GetApprovedAt()) + test.build.SetApprovedBy(test.want.GetApprovedBy()) if test.build.GetID() != test.want.GetID() { t.Errorf("SetID is %v, want %v", test.build.GetID(), test.want.GetID()) @@ -749,6 +771,14 @@ func TestLibrary_Build_Setters(t *testing.T) { if test.build.GetDistribution() != test.want.GetDistribution() { t.Errorf("SetDistribution is %v, want %v", test.build.GetDistribution(), test.want.GetDistribution()) } + + if test.build.GetApprovedAt() != test.want.GetApprovedAt() { + t.Errorf("SetApprovedAt is %v, want %v", test.build.GetApprovedAt(), test.want.GetApprovedAt()) + } + + if test.build.GetApprovedBy() != test.want.GetApprovedBy() { + t.Errorf("SetApprovedBy is %v, want %v", test.build.GetApprovedBy(), test.want.GetApprovedBy()) + } } } @@ -757,6 +787,8 @@ func TestLibrary_Build_String(t *testing.T) { b := testBuild() want := fmt.Sprintf(`{ + ApprovedAt: %d, + ApprovedBy: %s, Author: %s, BaseRef: %s, Branch: %s, @@ -789,6 +821,8 @@ func TestLibrary_Build_String(t *testing.T) { Status: %s, Title: %s, }`, + b.GetApprovedAt(), + b.GetApprovedBy(), b.GetAuthor(), b.GetBaseRef(), b.GetBranch(), @@ -865,6 +899,8 @@ func testBuild() *Build { b.SetHost("example.company.com") b.SetRuntime("docker") b.SetDistribution("linux") + b.SetApprovedAt(1563474076) + b.SetApprovedBy("OctoCat") return b } diff --git a/library/repo.go b/library/repo.go index 0aebc7e8..054316a1 100644 --- a/library/repo.go +++ b/library/repo.go @@ -11,57 +11,57 @@ import ( // // swagger:model Repo type Repo struct { - ID *int64 `json:"id,omitempty"` - UserID *int64 `json:"user_id,omitempty"` - Hash *string `json:"-"` - Org *string `json:"org,omitempty"` - Name *string `json:"name,omitempty"` - FullName *string `json:"full_name,omitempty"` - Link *string `json:"link,omitempty"` - Clone *string `json:"clone,omitempty"` - Branch *string `json:"branch,omitempty"` - Topics *[]string `json:"topics,omitempty"` - BuildLimit *int64 `json:"build_limit,omitempty"` - Timeout *int64 `json:"timeout,omitempty"` - Counter *int `json:"counter,omitempty"` - Visibility *string `json:"visibility,omitempty"` - Private *bool `json:"private,omitempty"` - Trusted *bool `json:"trusted,omitempty"` - Active *bool `json:"active,omitempty"` - AllowPull *bool `json:"allow_pull,omitempty"` - AllowPush *bool `json:"allow_push,omitempty"` - AllowDeploy *bool `json:"allow_deploy,omitempty"` - AllowTag *bool `json:"allow_tag,omitempty"` - AllowComment *bool `json:"allow_comment,omitempty"` - PipelineType *string `json:"pipeline_type,omitempty"` - PreviousName *string `json:"previous_name,omitempty"` - ApproveForkBuild *string `json:"approve_fork_build,omitempty"` + ID *int64 `json:"id,omitempty"` + UserID *int64 `json:"user_id,omitempty"` + Hash *string `json:"-"` + Org *string `json:"org,omitempty"` + Name *string `json:"name,omitempty"` + FullName *string `json:"full_name,omitempty"` + Link *string `json:"link,omitempty"` + Clone *string `json:"clone,omitempty"` + Branch *string `json:"branch,omitempty"` + Topics *[]string `json:"topics,omitempty"` + BuildLimit *int64 `json:"build_limit,omitempty"` + Timeout *int64 `json:"timeout,omitempty"` + Counter *int `json:"counter,omitempty"` + Visibility *string `json:"visibility,omitempty"` + Private *bool `json:"private,omitempty"` + Trusted *bool `json:"trusted,omitempty"` + Active *bool `json:"active,omitempty"` + AllowPull *bool `json:"allow_pull,omitempty"` + AllowPush *bool `json:"allow_push,omitempty"` + AllowDeploy *bool `json:"allow_deploy,omitempty"` + AllowTag *bool `json:"allow_tag,omitempty"` + AllowComment *bool `json:"allow_comment,omitempty"` + PipelineType *string `json:"pipeline_type,omitempty"` + PreviousName *string `json:"previous_name,omitempty"` + ApproveBuild *string `json:"approve_build,omitempty"` } // Environment returns a list of environment variables // provided from the fields of the Repo type. func (r *Repo) Environment() map[string]string { return map[string]string{ - "VELA_REPO_ACTIVE": ToString(r.GetActive()), - "VELA_REPO_ALLOW_COMMENT": ToString(r.GetAllowComment()), - "VELA_REPO_ALLOW_DEPLOY": ToString(r.GetAllowDeploy()), - "VELA_REPO_ALLOW_PULL": ToString(r.GetAllowPull()), - "VELA_REPO_ALLOW_PUSH": ToString(r.GetAllowPush()), - "VELA_REPO_ALLOW_TAG": ToString(r.GetAllowTag()), - "VELA_REPO_BRANCH": ToString(r.GetBranch()), - "VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","), - "VELA_REPO_BUILD_LIMIT": ToString(r.GetBuildLimit()), - "VELA_REPO_CLONE": ToString(r.GetClone()), - "VELA_REPO_FULL_NAME": ToString(r.GetFullName()), - "VELA_REPO_LINK": ToString(r.GetLink()), - "VELA_REPO_NAME": ToString(r.GetName()), - "VELA_REPO_ORG": ToString(r.GetOrg()), - "VELA_REPO_PRIVATE": ToString(r.GetPrivate()), - "VELA_REPO_TIMEOUT": ToString(r.GetTimeout()), - "VELA_REPO_TRUSTED": ToString(r.GetTrusted()), - "VELA_REPO_VISIBILITY": ToString(r.GetVisibility()), - "VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()), - "VELA_REPO_APPROVE_FORK_BUILD": ToString(r.GetApproveForkBuild()), + "VELA_REPO_ACTIVE": ToString(r.GetActive()), + "VELA_REPO_ALLOW_COMMENT": ToString(r.GetAllowComment()), + "VELA_REPO_ALLOW_DEPLOY": ToString(r.GetAllowDeploy()), + "VELA_REPO_ALLOW_PULL": ToString(r.GetAllowPull()), + "VELA_REPO_ALLOW_PUSH": ToString(r.GetAllowPush()), + "VELA_REPO_ALLOW_TAG": ToString(r.GetAllowTag()), + "VELA_REPO_BRANCH": ToString(r.GetBranch()), + "VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","), + "VELA_REPO_BUILD_LIMIT": ToString(r.GetBuildLimit()), + "VELA_REPO_CLONE": ToString(r.GetClone()), + "VELA_REPO_FULL_NAME": ToString(r.GetFullName()), + "VELA_REPO_LINK": ToString(r.GetLink()), + "VELA_REPO_NAME": ToString(r.GetName()), + "VELA_REPO_ORG": ToString(r.GetOrg()), + "VELA_REPO_PRIVATE": ToString(r.GetPrivate()), + "VELA_REPO_TIMEOUT": ToString(r.GetTimeout()), + "VELA_REPO_TRUSTED": ToString(r.GetTrusted()), + "VELA_REPO_VISIBILITY": ToString(r.GetVisibility()), + "VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()), + "VELA_REPO_APPROVE_BUILD": ToString(r.GetApproveBuild()), // deprecated environment variables "REPOSITORY_ACTIVE": ToString(r.GetActive()), @@ -395,17 +395,17 @@ func (r *Repo) GetPreviousName() string { return *r.PreviousName } -// GetApproveForkBuild returns the ApproveForkBuild field. +// GetApproveBuild returns the ApproveBuild field. // // When the provided Repo type is nil, or the field within // the type is nil, it returns the zero value for the field. -func (r *Repo) GetApproveForkBuild() string { - // return zero value if Repo type or ApproveForkBuild field is nil - if r == nil || r.ApproveForkBuild == nil { +func (r *Repo) GetApproveBuild() string { + // return zero value if Repo type or ApproveBuild field is nil + if r == nil || r.ApproveBuild == nil { return "" } - return *r.ApproveForkBuild + return *r.ApproveBuild } // SetID sets the ID field. @@ -720,17 +720,17 @@ func (r *Repo) SetPreviousName(v string) { r.PreviousName = &v } -// SetApproveForkBuild sets the ApproveForkBuild field. +// SetApproveBuild sets the ApproveBuild field. // // When the provided Repo type is nil, it // will set nothing and immediately return. -func (r *Repo) SetApproveForkBuild(v string) { +func (r *Repo) SetApproveBuild(v string) { // return if Repo type is nil if r == nil { return } - r.ApproveForkBuild = &v + r.ApproveBuild = &v } // String implements the Stringer interface for the Repo type. @@ -742,7 +742,7 @@ func (r *Repo) String() string { AllowPull: %t, AllowPush: %t, AllowTag: %t, - ApproveForkBuild: %s, + ApproveBuild: %s, Branch: %s, BuildLimit: %d, Clone: %s, @@ -767,7 +767,7 @@ func (r *Repo) String() string { r.GetAllowPull(), r.GetAllowPush(), r.GetAllowTag(), - r.GetApproveForkBuild(), + r.GetApproveBuild(), r.GetBranch(), r.GetBuildLimit(), r.GetClone(), diff --git a/library/repo_test.go b/library/repo_test.go index 020612df..e1b38afd 100644 --- a/library/repo_test.go +++ b/library/repo_test.go @@ -13,42 +13,42 @@ import ( func TestLibrary_Repo_Environment(t *testing.T) { // setup types want := map[string]string{ - "VELA_REPO_ACTIVE": "true", - "VELA_REPO_ALLOW_COMMENT": "false", - "VELA_REPO_ALLOW_DEPLOY": "false", - "VELA_REPO_ALLOW_PULL": "false", - "VELA_REPO_ALLOW_PUSH": "true", - "VELA_REPO_ALLOW_TAG": "false", - "VELA_REPO_BRANCH": "main", - "VELA_REPO_TOPICS": "cloud,security", - "VELA_REPO_BUILD_LIMIT": "10", - "VELA_REPO_CLONE": "https://github.com/github/octocat.git", - "VELA_REPO_FULL_NAME": "github/octocat", - "VELA_REPO_LINK": "https://github.com/github/octocat", - "VELA_REPO_NAME": "octocat", - "VELA_REPO_ORG": "github", - "VELA_REPO_PRIVATE": "false", - "VELA_REPO_TIMEOUT": "30", - "VELA_REPO_TRUSTED": "false", - "VELA_REPO_VISIBILITY": "public", - "VELA_REPO_PIPELINE_TYPE": "", - "VELA_REPO_APPROVE_FORK_BUILD": "never", - "REPOSITORY_ACTIVE": "true", - "REPOSITORY_ALLOW_COMMENT": "false", - "REPOSITORY_ALLOW_DEPLOY": "false", - "REPOSITORY_ALLOW_PULL": "false", - "REPOSITORY_ALLOW_PUSH": "true", - "REPOSITORY_ALLOW_TAG": "false", - "REPOSITORY_BRANCH": "main", - "REPOSITORY_CLONE": "https://github.com/github/octocat.git", - "REPOSITORY_FULL_NAME": "github/octocat", - "REPOSITORY_LINK": "https://github.com/github/octocat", - "REPOSITORY_NAME": "octocat", - "REPOSITORY_ORG": "github", - "REPOSITORY_PRIVATE": "false", - "REPOSITORY_TIMEOUT": "30", - "REPOSITORY_TRUSTED": "false", - "REPOSITORY_VISIBILITY": "public", + "VELA_REPO_ACTIVE": "true", + "VELA_REPO_ALLOW_COMMENT": "false", + "VELA_REPO_ALLOW_DEPLOY": "false", + "VELA_REPO_ALLOW_PULL": "false", + "VELA_REPO_ALLOW_PUSH": "true", + "VELA_REPO_ALLOW_TAG": "false", + "VELA_REPO_BRANCH": "main", + "VELA_REPO_TOPICS": "cloud,security", + "VELA_REPO_BUILD_LIMIT": "10", + "VELA_REPO_CLONE": "https://github.com/github/octocat.git", + "VELA_REPO_FULL_NAME": "github/octocat", + "VELA_REPO_LINK": "https://github.com/github/octocat", + "VELA_REPO_NAME": "octocat", + "VELA_REPO_ORG": "github", + "VELA_REPO_PRIVATE": "false", + "VELA_REPO_TIMEOUT": "30", + "VELA_REPO_TRUSTED": "false", + "VELA_REPO_VISIBILITY": "public", + "VELA_REPO_PIPELINE_TYPE": "", + "VELA_REPO_APPROVE_BUILD": "never", + "REPOSITORY_ACTIVE": "true", + "REPOSITORY_ALLOW_COMMENT": "false", + "REPOSITORY_ALLOW_DEPLOY": "false", + "REPOSITORY_ALLOW_PULL": "false", + "REPOSITORY_ALLOW_PUSH": "true", + "REPOSITORY_ALLOW_TAG": "false", + "REPOSITORY_BRANCH": "main", + "REPOSITORY_CLONE": "https://github.com/github/octocat.git", + "REPOSITORY_FULL_NAME": "github/octocat", + "REPOSITORY_LINK": "https://github.com/github/octocat", + "REPOSITORY_NAME": "octocat", + "REPOSITORY_ORG": "github", + "REPOSITORY_PRIVATE": "false", + "REPOSITORY_TIMEOUT": "30", + "REPOSITORY_TRUSTED": "false", + "REPOSITORY_VISIBILITY": "public", } // run test @@ -169,8 +169,8 @@ func TestLibrary_Repo_Getters(t *testing.T) { t.Errorf("GetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName()) } - if test.repo.GetApproveForkBuild() != test.want.GetApproveForkBuild() { - t.Errorf("GetApproveForkBuild is %v, want %v", test.repo.GetApproveForkBuild(), test.want.GetApproveForkBuild()) + if test.repo.GetApproveBuild() != test.want.GetApproveBuild() { + t.Errorf("GetApproveForkBuild is %v, want %v", test.repo.GetApproveBuild(), test.want.GetApproveBuild()) } } } @@ -220,7 +220,7 @@ func TestLibrary_Repo_Setters(t *testing.T) { test.repo.SetAllowComment(test.want.GetAllowComment()) test.repo.SetPipelineType(test.want.GetPipelineType()) test.repo.SetPreviousName(test.want.GetPreviousName()) - test.repo.SetApproveForkBuild(test.want.GetApproveForkBuild()) + test.repo.SetApproveBuild(test.want.GetApproveBuild()) if test.repo.GetID() != test.want.GetID() { t.Errorf("SetID is %v, want %v", test.repo.GetID(), test.want.GetID()) @@ -314,8 +314,8 @@ func TestLibrary_Repo_Setters(t *testing.T) { t.Errorf("SetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName()) } - if test.repo.GetApproveForkBuild() != test.want.GetApproveForkBuild() { - t.Errorf("SetApproveForkBuild is %v, want %v", test.repo.GetApproveForkBuild(), test.want.GetApproveForkBuild()) + if test.repo.GetApproveBuild() != test.want.GetApproveBuild() { + t.Errorf("SetApproveForkBuild is %v, want %v", test.repo.GetApproveBuild(), test.want.GetApproveBuild()) } } } @@ -331,7 +331,7 @@ func TestLibrary_Repo_String(t *testing.T) { AllowPull: %t, AllowPush: %t, AllowTag: %t, - ApproveForkBuild: %s, + ApproveBuild: %s, Branch: %s, BuildLimit: %d, Clone: %s, @@ -356,7 +356,7 @@ func TestLibrary_Repo_String(t *testing.T) { r.GetAllowPull(), r.GetAllowPush(), r.GetAllowTag(), - r.GetApproveForkBuild(), + r.GetApproveBuild(), r.GetBranch(), r.GetBuildLimit(), r.GetClone(), @@ -411,7 +411,7 @@ func testRepo() *Repo { r.SetAllowComment(false) r.SetPipelineType("") r.SetPreviousName("") - r.SetApproveForkBuild(constants.ApproveNever) + r.SetApproveBuild(constants.ApproveNever) return r }