Skip to content

Commit

Permalink
add support for travis-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Feb 2, 2020
1 parent f04405b commit 04ea24c
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/cicd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/rajatjindal/krew-release-bot/pkg/cicd/circleci"
"github.com/rajatjindal/krew-release-bot/pkg/cicd/github"
"github.com/rajatjindal/krew-release-bot/pkg/cicd/travisci"
)

//Provider defines CI/CD provider interface
Expand All @@ -27,5 +28,9 @@ func GetProvider() Provider {
return &circleci.Provider{}
}

if os.Getenv("TRAVIS") == "true" {
return &travisci.Provider{}
}

return nil
}
75 changes: 75 additions & 0 deletions pkg/cicd/travisci/travisci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package travisci

import (
"fmt"
"os"
"path/filepath"
"strings"
)

//Provider implements provider interface
type Provider struct{}

//GetTag returns tag
func (p *Provider) GetTag() (string, error) {
ref := os.Getenv("TRAVIS_TAG")
if ref == "" {
return "", fmt.Errorf("TRAVIS_TAG env variable not found")
}

return ref, nil
}

//GetOwnerAndRepo gets the owner and repo from the env
func (p *Provider) GetOwnerAndRepo() (string, string, error) {
repoFromEnv := os.Getenv("TRAVIS_REPO_SLUG")
if repoFromEnv == "" {
return "", "", fmt.Errorf("env TRAVIS_REPO_SLUG not set")
}

s := strings.Split(repoFromEnv, "/")
if len(s) != 2 {
return "", "", fmt.Errorf("env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found %q", repoFromEnv)
}

return s[0], s[1], nil
}

//GetActor gets the owner and repo from the env
func (p *Provider) GetActor() (string, error) {
owner, _, err := p.GetOwnerAndRepo()
if err != nil {
return "", err
}

if owner == "" {
return "", fmt.Errorf("failed to find actor for the release")
}

return owner, nil
}

//getInputForAction gets input to action
func getInputForAction(key string) string {
return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key)))
}

//GetWorkDirectory gets workdir
func (p *Provider) GetWorkDirectory() string {
workdirInput := getInputForAction("workdir")
if workdirInput != "" {
return workdirInput
}

return os.Getenv("TRAVIS_BUILD_DIR")
}

//GetTemplateFile returns the template file
func (p *Provider) GetTemplateFile() string {
templateFile := getInputForAction("krew_template_file")
if templateFile != "" {
return filepath.Join(p.GetWorkDirectory(), templateFile)
}

return filepath.Join(p.GetWorkDirectory(), ".krew.yaml")
}
155 changes: 155 additions & 0 deletions pkg/cicd/travisci/travisci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package travisci

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetOwnerAndRepo(t *testing.T) {
testcases := []struct {
name string
setup func()
expectedOwner string
expectedRepo string
expectedError string
}{
{
name: "TRAVIS_REPO_SLUG is set as expected",
setup: func() {
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-repo")
},
expectedOwner: "foo-bar",
expectedRepo: "my-awesome-repo",
},
{
name: "TRAVIS_REPO_SLUG is set in incorrect format",
setup: func() {
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar:my-awesome-repo")
},
expectedError: `env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found "foo-bar:my-awesome-repo"`,
},
{
name: "TRAVIS_REPO_SLUG is set in incorrect format 2",
setup: func() {
os.Setenv("TRAVIS_REPO_SLUG", "my-awesome-repo")
},
expectedError: `env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found "my-awesome-repo"`,
},
{
name: "TRAVIS_REPO_SLUG environment is not set",
expectedError: `env TRAVIS_REPO_SLUG not set`,
},
}

p := &Provider{}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
os.Clearenv()

if tc.setup != nil {
tc.setup()
}

owner, repo, err := p.GetOwnerAndRepo()

assert.Equal(t, tc.expectedOwner, owner)
assert.Equal(t, tc.expectedRepo, repo)
assertError(t, tc.expectedError, err)
})
}
}

func TestGetActionActor(t *testing.T) {
testcases := []struct {
name string
setup func()
expectedActor string
expectedError string
}{
{
name: "env TRAVIS_REPO_SLUG is set as expected",
setup: func() {
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-plugin")
},
expectedActor: "foo-bar",
},
{
name: "env TRAVIS_REPO_SLUG is not set",
expectedError: "env TRAVIS_REPO_SLUG not set",
},
}

p := &Provider{}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
os.Clearenv()

if tc.setup != nil {
tc.setup()
}

actor, err := p.GetActor()
assert.Equal(t, tc.expectedActor, actor)
assertError(t, tc.expectedError, err)
})
}
}
func TestGetTag(t *testing.T) {
testcases := []struct {
name string
setup func()
expectedTag string
expectedError string
}{
{
name: "env TRAVIS_TAG is setup",
setup: func() {
os.Setenv("TRAVIS_TAG", "v5.0.0")
},
expectedTag: "v5.0.0",
},
{
name: "TRAVIS_TAG is not set",
setup: func() {
os.Unsetenv("TRAVIS_TAG")
},
expectedError: `TRAVIS_TAG env variable not found`,
},
}

p := &Provider{}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
os.Clearenv()

if tc.setup != nil {
tc.setup()
}

tag, err := p.GetTag()
assert.Equal(t, tc.expectedTag, tag)
assertError(t, tc.expectedError, err)
})
}
}

func assertError(t *testing.T, expectedError string, err error) {
if expectedError == "" {
assert.Nil(t, err)
}

if expectedError != "" {
assert.NotNil(t, err)
if err != nil {
assert.Equal(t, expectedError, err.Error())
}
}
}

func setupEnvironment() {
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-plugin")
os.Setenv("TRAVIS_TAG", "v0.0.2")
os.Setenv("TRAVIS_BUILD_DIR", "./data/")
}

0 comments on commit 04ea24c

Please sign in to comment.