diff --git a/api/schedule/create.go b/api/schedule/create.go index c50f11744..667eb3ca9 100644 --- a/api/schedule/create.go +++ b/api/schedule/create.go @@ -137,6 +137,12 @@ func CreateSchedule(c *gin.Context) { s.SetUpdatedAt(time.Now().UTC().Unix()) s.SetUpdatedBy(u.GetName()) + if input.GetBranch() == "" { + s.SetBranch(r.GetBranch()) + } else { + s.SetBranch(input.GetBranch()) + } + // set the active field based off the input provided if input.Active == nil { // default active field to true diff --git a/api/schedule/update.go b/api/schedule/update.go index 578639c8b..6c2a8b9b6 100644 --- a/api/schedule/update.go +++ b/api/schedule/update.go @@ -127,6 +127,9 @@ func UpdateSchedule(c *gin.Context) { // set the updated by field using claims s.SetUpdatedBy(u.GetName()) + if input.GetBranch() != "" { + s.SetBranch(input.GetBranch()) + } // update the schedule within the database s, err = database.FromContext(c).UpdateSchedule(ctx, s, true) diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index a8d927a1b..b65723177 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -184,7 +184,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler } // send API call to capture the commit sha for the branch - _, commit, err := scm.GetBranch(u, r) + _, commit, err := scm.GetBranch(u, r, s.GetBranch()) if err != nil { return fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err) } @@ -193,7 +193,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler b := new(library.Build) b.SetAuthor(s.GetCreatedBy()) - b.SetBranch(r.GetBranch()) + b.SetBranch(s.GetBranch()) b.SetClone(r.GetClone()) b.SetCommit(commit) b.SetDeploy(s.GetName()) diff --git a/database/integration_test.go b/database/integration_test.go index c87dec0ae..3e1ea546d 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -2108,6 +2108,7 @@ func newResources() *Resources { scheduleOne.SetUpdatedAt(time.Now().Add(time.Hour * 1).UTC().Unix()) scheduleOne.SetUpdatedBy("octokitty") scheduleOne.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) + scheduleOne.SetBranch("main") scheduleTwo := new(library.Schedule) scheduleTwo.SetID(2) @@ -2120,6 +2121,7 @@ func newResources() *Resources { scheduleTwo.SetUpdatedAt(time.Now().Add(time.Hour * 1).UTC().Unix()) scheduleTwo.SetUpdatedBy("octokitty") scheduleTwo.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) + scheduleTwo.SetBranch("main") secretOrg := new(library.Secret) secretOrg.SetID(1) diff --git a/database/schedule/count_active_test.go b/database/schedule/count_active_test.go index 5d555ea8c..dcf9d2cb0 100644 --- a/database/schedule/count_active_test.go +++ b/database/schedule/count_active_test.go @@ -23,6 +23,7 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -34,6 +35,7 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/count_repo_test.go b/database/schedule/count_repo_test.go index d2429c595..35c530d7c 100644 --- a/database/schedule/count_repo_test.go +++ b/database/schedule/count_repo_test.go @@ -28,6 +28,7 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -38,6 +39,7 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/count_test.go b/database/schedule/count_test.go index 91537d919..c98ba7224 100644 --- a/database/schedule/count_test.go +++ b/database/schedule/count_test.go @@ -22,6 +22,7 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -32,6 +33,7 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/create_test.go b/database/schedule/create_test.go index 4a987b577..272e91a41 100644 --- a/database/schedule/create_test.go +++ b/database/schedule/create_test.go @@ -22,6 +22,7 @@ func TestSchedule_Engine_CreateSchedule(t *testing.T) { _schedule.SetCreatedBy("user1") _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -31,9 +32,9 @@ func TestSchedule_Engine_CreateSchedule(t *testing.T) { // ensure the mock expects the query _mock.ExpectQuery(`INSERT INTO "schedules" -("repo_id","active","name","entry","created_at","created_by","updated_at","updated_by","scheduled_at","id") -VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING "id"`). - WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, 1). +("repo_id","active","name","entry","created_at","created_by","updated_at","updated_by","scheduled_at","branch","id") +VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) RETURNING "id"`). + WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main", 1). WillReturnRows(_rows) _sqlite := testSqlite(t) diff --git a/database/schedule/delete_test.go b/database/schedule/delete_test.go index e302da6b9..8dff8ad78 100644 --- a/database/schedule/delete_test.go +++ b/database/schedule/delete_test.go @@ -21,6 +21,7 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) { _schedule.SetCreatedBy("user1") _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/get_repo_test.go b/database/schedule/get_repo_test.go index b29072ec7..998b58fc3 100644 --- a/database/schedule/get_repo_test.go +++ b/database/schedule/get_repo_test.go @@ -29,14 +29,15 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) { _schedule.SetCreatedBy("user1") _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() // create expected result in mock _rows := sqlmock.NewRows( - []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at"}, - ).AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil) + []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at", "branch"}, + ).AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main") // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "schedules" WHERE repo_id = $1 AND name = $2 LIMIT 1`).WithArgs(1, "nightly").WillReturnRows(_rows) diff --git a/database/schedule/get_test.go b/database/schedule/get_test.go index b5175d8b0..bc4a61f75 100644 --- a/database/schedule/get_test.go +++ b/database/schedule/get_test.go @@ -23,14 +23,15 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) { _schedule.SetCreatedBy("user1") _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() // create expected result in mock _rows := sqlmock.NewRows( - []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at"}, - ).AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil) + []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at", "branch"}, + ).AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main") // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "schedules" WHERE id = $1 LIMIT 1`).WithArgs(1).WillReturnRows(_rows) diff --git a/database/schedule/list_active_test.go b/database/schedule/list_active_test.go index 90ae96cfe..2bce7ed06 100644 --- a/database/schedule/list_active_test.go +++ b/database/schedule/list_active_test.go @@ -24,6 +24,7 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -35,6 +36,7 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -47,8 +49,8 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { // create expected result in mock _rows = sqlmock.NewRows( - []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at"}). - AddRow(1, 1, true, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil) + []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at", "branch"}). + AddRow(1, 1, true, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main") // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "schedules" WHERE active = $1`).WithArgs(true).WillReturnRows(_rows) diff --git a/database/schedule/list_repo_test.go b/database/schedule/list_repo_test.go index 2536f73be..2d573448d 100644 --- a/database/schedule/list_repo_test.go +++ b/database/schedule/list_repo_test.go @@ -29,6 +29,7 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -39,6 +40,7 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -51,8 +53,8 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { // create expected result in mock _rows = sqlmock.NewRows( - []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at"}). - AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil) + []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at", "branch"}). + AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main") // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "schedules" WHERE repo_id = $1 ORDER BY id DESC LIMIT 10`).WithArgs(1).WillReturnRows(_rows) diff --git a/database/schedule/list_test.go b/database/schedule/list_test.go index 6dd64af6c..5b4095724 100644 --- a/database/schedule/list_test.go +++ b/database/schedule/list_test.go @@ -23,6 +23,7 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _scheduleOne.SetCreatedBy("user1") _scheduleOne.SetUpdatedAt(1) _scheduleOne.SetUpdatedBy("user2") + _scheduleOne.SetBranch("main") _scheduleTwo := testSchedule() _scheduleTwo.SetID(2) @@ -33,6 +34,7 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _scheduleTwo.SetCreatedBy("user1") _scheduleTwo.SetUpdatedAt(1) _scheduleTwo.SetUpdatedBy("user2") + _scheduleTwo.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -45,9 +47,9 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { // create expected result in mock _rows = sqlmock.NewRows( - []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at"}). - AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil). - AddRow(2, 2, false, "hourly", "0 * * * *", 1, "user1", 1, "user2", nil) + []string{"id", "repo_id", "active", "name", "entry", "created_at", "created_by", "updated_at", "updated_by", "scheduled_at", "branch"}). + AddRow(1, 1, false, "nightly", "0 0 * * *", 1, "user1", 1, "user2", nil, "main"). + AddRow(2, 2, false, "hourly", "0 * * * *", 1, "user1", 1, "user2", nil, "main") // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "schedules"`).WillReturnRows(_rows) diff --git a/database/schedule/schedule_test.go b/database/schedule/schedule_test.go index aae0403a5..b5916b3e3 100644 --- a/database/schedule/schedule_test.go +++ b/database/schedule/schedule_test.go @@ -187,6 +187,7 @@ func testSchedule() *library.Schedule { UpdatedAt: new(int64), UpdatedBy: new(string), ScheduledAt: new(int64), + Branch: new(string), } } diff --git a/database/schedule/table.go b/database/schedule/table.go index 7ea6eb873..9ebccf864 100644 --- a/database/schedule/table.go +++ b/database/schedule/table.go @@ -6,6 +6,7 @@ package schedule import ( "context" + "github.com/go-vela/types/constants" ) @@ -25,6 +26,7 @@ schedules ( updated_at INTEGER, updated_by VARCHAR(250), scheduled_at INTEGER, + branch VARCHAR(250), UNIQUE(repo_id, name) ); ` @@ -44,6 +46,7 @@ schedules ( updated_at INTEGER, updated_by TEXT, scheduled_at INTEGER, + branch TEXT, UNIQUE(repo_id, name) ); ` diff --git a/database/schedule/update_test.go b/database/schedule/update_test.go index c13e44ae8..235ccfc43 100644 --- a/database/schedule/update_test.go +++ b/database/schedule/update_test.go @@ -28,15 +28,16 @@ func TestSchedule_Engine_UpdateSchedule_Config(t *testing.T) { _schedule.SetCreatedBy("user1") _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() // ensure the mock expects the query _mock.ExpectExec(`UPDATE "schedules" -SET "repo_id"=$1,"active"=$2,"name"=$3,"entry"=$4,"created_at"=$5,"created_by"=$6,"updated_at"=$7,"updated_by"=$8,"scheduled_at"=$9 -WHERE "id" = $10`). - WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", NowTimestamp{}, "user2", nil, 1). +SET "repo_id"=$1,"active"=$2,"name"=$3,"entry"=$4,"created_at"=$5,"created_by"=$6,"updated_at"=$7,"updated_by"=$8,"scheduled_at"=$9,"branch"=$10 +WHERE "id" = $11`). + WithArgs(1, false, "nightly", "0 0 * * *", 1, "user1", NowTimestamp{}, "user2", nil, "main", 1). WillReturnResult(sqlmock.NewResult(1, 1)) _sqlite := testSqlite(t) @@ -107,6 +108,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) { _schedule.SetUpdatedAt(1) _schedule.SetUpdatedBy("user2") _schedule.SetScheduledAt(1) + _schedule.SetBranch("main") _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/go.mod b/go.mod index 2f200eccf..addeed1ab 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/drone/envsubst v1.0.3 github.com/gin-gonic/gin v1.9.1 github.com/go-playground/assert/v2 v2.2.0 - github.com/go-vela/types v0.20.2-0.20230821135955-6b577f36fdfe + github.com/go-vela/types v0.20.2-0.20230822144153-14b37585731d github.com/golang-jwt/jwt/v5 v5.0.0 github.com/google/go-cmp v0.5.9 github.com/google/go-github/v53 v53.2.0 diff --git a/go.sum b/go.sum index 45416d2f1..f109e9873 100644 --- a/go.sum +++ b/go.sum @@ -143,8 +143,8 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91 github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= -github.com/go-vela/types v0.20.2-0.20230821135955-6b577f36fdfe h1:5lw7hJmwLiymoSI0H8gr9Aiixifv2wOXvtH4NJJZB2k= -github.com/go-vela/types v0.20.2-0.20230821135955-6b577f36fdfe/go.mod h1:AXO4oQSygOBQ02fPapsKjQHkx2aQO3zTu7clpvVbXBY= +github.com/go-vela/types v0.20.2-0.20230822144153-14b37585731d h1:ag6trc3Ev+7hzifeWy0M9rHHjrO9nFCYgW8dlKdZ4j4= +github.com/go-vela/types v0.20.2-0.20230822144153-14b37585731d/go.mod h1:AXO4oQSygOBQ02fPapsKjQHkx2aQO3zTu7clpvVbXBY= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/scm/github/repo.go b/scm/github/repo.go index 5cd3b0999..e69c7220c 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -548,17 +548,17 @@ func (c *client) GetHTMLURL(u *library.User, org, repo, name, ref string) (strin } // GetBranch defines a function that retrieves a branch for a repo. -func (c *client) GetBranch(u *library.User, r *library.Repo) (string, string, error) { +func (c *client) GetBranch(u *library.User, r *library.Repo, branch string) (string, string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), "user": u.GetName(), - }).Tracef("retrieving branch %s for repo %s", r.GetBranch(), r.GetFullName()) + }).Tracef("retrieving branch %s for repo %s", branch, r.GetFullName()) // create GitHub OAuth client with user's token client := c.newClientToken(u.GetToken()) - data, _, err := client.Repositories.GetBranch(ctx, r.GetOrg(), r.GetName(), r.GetBranch(), true) + data, _, err := client.Repositories.GetBranch(ctx, r.GetOrg(), r.GetName(), branch, true) if err != nil { return "", "", err } diff --git a/scm/github/repo_test.go b/scm/github/repo_test.go index 1fcd3bf0c..858d60f18 100644 --- a/scm/github/repo_test.go +++ b/scm/github/repo_test.go @@ -1339,7 +1339,7 @@ func TestGithub_GetBranch(t *testing.T) { client, _ := NewTest(s.URL) // run test - gotBranch, gotCommit, err := client.GetBranch(u, r) + gotBranch, gotCommit, err := client.GetBranch(u, r, "main") if err != nil { t.Errorf("Status returned err: %v", err) diff --git a/scm/service.go b/scm/service.go index c92cbedbf..4de42362f 100644 --- a/scm/service.go +++ b/scm/service.go @@ -110,7 +110,7 @@ type Service interface { ListUserRepos(*library.User) ([]*library.Repo, error) // GetBranch defines a function that retrieves // a branch for a repo. - GetBranch(*library.User, *library.Repo) (string, string, error) + GetBranch(*library.User, *library.Repo, string) (string, string, error) // GetPullRequest defines a function that retrieves // a pull request for a repo. GetPullRequest(*library.User, *library.Repo, int) (string, string, string, string, error)