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

fix(scm): prefer starlark file for starlark pipeline #1043

Merged
merged 5 commits into from
Jan 19, 2024
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: 4 additions & 2 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func (c *client) Config(ctx context.Context, u *library.User, r *library.Repo, r
// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// default pipeline file names
files := []string{".vela.yml", ".vela.yaml"}

// starlark support - prefer .star/.py, use default as fallback
if strings.EqualFold(r.GetPipelineType(), constants.PipelineTypeStarlark) {
files = append(files, ".vela.star", ".vela.py")
files = append([]string{".vela.star", ".vela.py"}, files...)
}

// set the reference for the options to capture the pipeline configuration
Expand Down Expand Up @@ -575,8 +577,8 @@ func (c *client) GetBranch(ctx context.Context, u *library.User, r *library.Repo
client := c.newClientToken(u.GetToken())

maxRedirects := 3
data, _, err := client.Repositories.GetBranch(ctx, r.GetOrg(), r.GetName(), branch, maxRedirects)

data, _, err := client.Repositories.GetBranch(ctx, r.GetOrg(), r.GetName(), branch, maxRedirects)
if err != nil {
wass3r marked this conversation as resolved.
Show resolved Hide resolved
wass3r marked this conversation as resolved.
Show resolved Hide resolved
return "", "", err
}
Expand Down
164 changes: 118 additions & 46 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func TestGithub_Config_YML(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yaml" {
c.Status(http.StatusNotFound)
if c.Param("path") == ".vela.yml" {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml.json")
return
}

c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml.json")
c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
Expand Down Expand Up @@ -79,24 +79,31 @@ func TestGithub_ConfigBackoff_YML(t *testing.T) {
resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// counter for api calls
count := 0

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yaml" {
c.Status(http.StatusNotFound)
// load the yml file on the second api call
if c.Param("path") == ".vela.yml" && count != 0 {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml.json")
return
}

c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml.json")
c.Status(http.StatusNotFound)

// increment api call counter
count++
})

s := httptest.NewServer(engine)
defer s.Close()

want, err := os.ReadFile("testdata/pipeline.yml")
if err != nil {
t.Errorf("Config reading file returned err: %v", err)
t.Errorf("ConfigBackoff reading file returned err: %v", err)
}

// setup types
Expand All @@ -111,22 +118,22 @@ func TestGithub_ConfigBackoff_YML(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
got, err := client.Config(context.TODO(), u, r, "")
got, err := client.ConfigBackoff(context.TODO(), u, r, "")

if resp.Code != http.StatusOK {
t.Errorf("Config returned %v, want %v", resp.Code, http.StatusOK)
t.Errorf("ConfigBackoff returned %v, want %v", resp.Code, http.StatusOK)
}

if err != nil {
t.Errorf("Config returned err: %v", err)
t.Errorf("ConfigBackoff returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("Config is %v, want %v", got, want)
t.Errorf("ConfigBackoff is %v, want %v", got, want)
}
}

func TestGithub_Config_YML_BadRequest(t *testing.T) {
func TestGithub_Config_YAML(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -135,12 +142,24 @@ func TestGithub_Config_YML_BadRequest(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
c.Status(http.StatusBadRequest)
if c.Param("path") == ".vela.yaml" {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yaml.json")
return
}

c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
defer s.Close()

want, err := os.ReadFile("testdata/pipeline.yml")
if err != nil {
t.Errorf("Config reading file returned err: %v", err)
}

// setup types
u := new(library.User)
u.SetName("foo")
Expand All @@ -159,16 +178,16 @@ func TestGithub_Config_YML_BadRequest(t *testing.T) {
t.Errorf("Config returned %v, want %v", resp.Code, http.StatusOK)
}

if err == nil {
t.Error("Config should have returned err")
if err != nil {
t.Errorf("Config returned err: %v", err)
}

if got != nil {
t.Errorf("Config is %v, want nil", got)
if !reflect.DeepEqual(got, want) {
t.Errorf("Config is %v, want %v", got, want)
}
}

func TestGithub_Config_YAML(t *testing.T) {
func TestGithub_Config_Star(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -177,20 +196,20 @@ func TestGithub_Config_YAML(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yml" {
c.Status(http.StatusNotFound)
if c.Param("path") == ".vela.star" {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/star.json")
return
}

c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yaml.json")
c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
defer s.Close()

want, err := os.ReadFile("testdata/pipeline.yml")
want, err := os.ReadFile("testdata/pipeline.star")
if err != nil {
t.Errorf("Config reading file returned err: %v", err)
}
Expand All @@ -203,6 +222,7 @@ func TestGithub_Config_YAML(t *testing.T) {
r := new(library.Repo)
r.SetOrg("foo")
r.SetName("bar")
r.SetPipelineType(constants.PipelineTypeStarlark)

client, _ := NewTest(s.URL)

Expand All @@ -222,7 +242,7 @@ func TestGithub_Config_YAML(t *testing.T) {
}
}

func TestGithub_Config_Star(t *testing.T) {
func TestGithub_Config_Star_Prefer(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -231,20 +251,25 @@ func TestGithub_Config_Star(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yml" {
// repo has .vela.yml and .vela.star
switch c.Param("path") {
case ".vela.yml":
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml.json")
case ".vela.star":
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/star.json")
default:
c.Status(http.StatusNotFound)
return
}

c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/star.json")
})

s := httptest.NewServer(engine)
defer s.Close()

want, err := os.ReadFile("testdata/pipeline.yml")
want, err := os.ReadFile("testdata/pipeline.star")
if err != nil {
t.Errorf("Config reading file returned err: %v", err)
}
Expand Down Expand Up @@ -286,20 +311,20 @@ func TestGithub_Config_Py(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yml" {
c.Status(http.StatusNotFound)
if c.Param("path") == ".vela.py" {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/py.json")
return
}

c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/py.json")
c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
defer s.Close()

want, err := os.ReadFile("testdata/pipeline.yml")
want, err := os.ReadFile("testdata/pipeline.star")
if err != nil {
t.Errorf("Config reading file returned err: %v", err)
}
Expand Down Expand Up @@ -341,11 +366,13 @@ func TestGithub_Config_YAML_BadRequest(t *testing.T) {

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
// first default not found
if c.Param("path") == ".vela.yml" {
c.Status(http.StatusNotFound)
return
}

// second default (.vela.yaml) causes bad request
c.Status(http.StatusBadRequest)
})

Expand Down Expand Up @@ -421,6 +448,55 @@ func TestGithub_Config_NotFound(t *testing.T) {
}
}

func TestGithub_Config_BadEncoding(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.GET("/api/v3/repos/foo/bar/contents/:path", func(c *gin.Context) {
if c.Param("path") == ".vela.yml" {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/yml_bad_encoding.json")
return
}

c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

r := new(library.Repo)
r.SetOrg("foo")
r.SetName("bar")

client, _ := NewTest(s.URL)

// run test
got, err := client.Config(context.TODO(), u, r, "")

if resp.Code != http.StatusOK {
t.Errorf("Config returned %v, want %v", resp.Code, http.StatusOK)
}

if err == nil {
t.Error("Config should have returned err")
}

if got != nil {
t.Errorf("Config is %v, want nil", got)
}
}

func TestGithub_Disable(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
Expand Down Expand Up @@ -1277,7 +1353,6 @@ func TestGithub_ListUserRepos(t *testing.T) {

// run test
got, err := client.ListUserRepos(context.TODO(), u)

if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down Expand Up @@ -1315,7 +1390,6 @@ func TestGithub_ListUserRepos_Ineligible(t *testing.T) {

// run test
got, err := client.ListUserRepos(context.TODO(), u)

if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down Expand Up @@ -1360,7 +1434,6 @@ func TestGithub_GetPullRequest(t *testing.T) {

// run test
gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(context.TODO(), u, r, 1)

if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down Expand Up @@ -1417,7 +1490,6 @@ func TestGithub_GetBranch(t *testing.T) {

// run test
gotBranch, gotCommit, err := client.GetBranch(context.TODO(), u, r, "main")

if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down
14 changes: 14 additions & 0 deletions scm/github/testdata/pipeline.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def main(ctx):
return {
'version': '1',
'steps': [
{
'name': 'build',
'image': 'golang:latest',
'commands': [
'go build',
'go test',
]
},
],
}
4 changes: 2 additions & 2 deletions scm/github/testdata/py.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"size": 5362,
"name": ".vela.py",
"path": ".vela.py",
"content": "LS0tCnZlcnNpb246ICIxIgoKbWV0YWRhdGE6CiAgb3M6IGxpbnV4CgpzdGVwczoKICAtIG5hbWU6IGJ1aWxkCiAgICBpbWFnZTogb3BlbmpkazpsYXRlc3QKICAgIHB1bGw6IHRydWUKICAgIGVudmlyb25tZW50OgogICAgICBHUkFETEVfVVNFUl9IT01FOiAuZ3JhZGxlCiAgICAgIEdSQURMRV9PUFRTOiAtRG9yZy5ncmFkbGUuZGFlbW9uPWZhbHNlIC1Eb3JnLmdyYWRsZS53b3JrZXJzLm1heD0xIC1Eb3JnLmdyYWRsZS5wYXJhbGxlbD1mYWxzZQogICAgY29tbWFuZHM6CiAgICAgIC0gLi9ncmFkbGV3IGJ1aWxkIGRpc3RUYXIK",
"content": "ZGVmIG1haW4oY3R4KToKICByZXR1cm4gewogICAgJ3ZlcnNpb24nOiAnMScsCiAgICAnc3RlcHMnOiBbCiAgICAgIHsKICAgICAgICAnbmFtZSc6ICdidWlsZCcsCiAgICAgICAgJ2ltYWdlJzogJ2dvbGFuZzpsYXRlc3QnLAogICAgICAgICdjb21tYW5kcyc6IFsKICAgICAgICAgICdnbyBidWlsZCcsCiAgICAgICAgICAnZ28gdGVzdCcsCiAgICAgICAgXQogICAgICB9LAogICAgXSwKfQo=",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
"url": "https://api.github.com/repos/octokit/octokit.rb/contents/.vela.py",
"git_url": "https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1",
Expand All @@ -15,4 +15,4 @@
"self": "https://api.github.com/repos/octokit/octokit.rb/contents/.vela.py",
"html": "https://github.com/octokit/octokit.rb/blob/main/.vela.py"
}
}
}
Loading
Loading