Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

enhance: adding a branch field to scheduled builds #934

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions api/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions database/schedule/count_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
2 changes: 2 additions & 0 deletions database/schedule/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
2 changes: 2 additions & 0 deletions database/schedule/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
7 changes: 4 additions & 3 deletions database/schedule/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }()
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions database/schedule/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }()
Expand Down
5 changes: 3 additions & 2 deletions database/schedule/get_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions database/schedule/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions database/schedule/list_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions database/schedule/list_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions database/schedule/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() }()
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions database/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func testSchedule() *library.Schedule {
UpdatedAt: new(int64),
UpdatedBy: new(string),
ScheduledAt: new(int64),
Branch: new(string),
}
}

Expand Down
3 changes: 3 additions & 0 deletions database/schedule/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package schedule

import (
"context"

"github.com/go-vela/types/constants"
)

Expand All @@ -25,6 +26,7 @@ schedules (
updated_at INTEGER,
updated_by VARCHAR(250),
scheduled_at INTEGER,
branch VARCHAR(250),
UNIQUE(repo_id, name)
);
`
Expand All @@ -44,6 +46,7 @@ schedules (
updated_at INTEGER,
updated_by TEXT,
scheduled_at INTEGER,
branch TEXT,
UNIQUE(repo_id, name)
);
`
Expand Down
8 changes: 5 additions & 3 deletions database/schedule/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() }()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down