Skip to content

Commit

Permalink
Update tests (#7)
Browse files Browse the repository at this point in the history
* Add simple tests for new features

* Add test to catch drone token

* Fix old test to check token from env{
  • Loading branch information
smartfin authored Oct 30, 2024
1 parent 992e442 commit 7d33fcb
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 12 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type (
Text bool `envconfig:"DRONE_LOGS_TEXT"`
Secret string `envconfig:"DRONE_SECRET"`

Provider string `envconfig:"PROVIDER"`
Token string `envconfig:"TOKEN"`
UseDroneAuth bool `default:"false" envconfig:"USE_DRONE_AUTH"` // set to 'true' to use Drone token for auth (passed from Drone CI in drone-go > 1.7.1)
Provider string `envconfig:"PROVIDER"`
Token string `envconfig:"TOKEN"`
// BB_ADDRESS is deprecated in favor of STASH_SERVER, it will be removed in a future version
BitBucketAddress string `envconfig:"BB_ADDRESS"`
BitBucketUser string `envconfig:"BITBUCKET_USER"`
Expand Down Expand Up @@ -67,7 +68,7 @@ func validate(spec *spec) error {
return fmt.Errorf("unsupported provider")
}
}
if spec.Token == "" && (spec.Provider == "github" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") {
if spec.Token == "" && spec.UseDroneAuth == false && (spec.Provider == "github" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") {
return fmt.Errorf("missing token")
}
if spec.BitBucketUser == "" && spec.Provider == "bitbucket" {
Expand All @@ -89,7 +90,7 @@ func validate(spec *spec) error {
return fmt.Errorf("missing stash server")
}

if spec.Token == "" && spec.Provider == "gitee" {
if spec.Token == "" && spec.UseDroneAuth == false && spec.Provider == "gitee" {
return fmt.Errorf("missing gitee token")
}

Expand Down Expand Up @@ -132,6 +133,7 @@ func main() {
GithubServer: spec.GithubServer,
Token: spec.Token,
StashServer: spec.StashServer,
UseDroneAuth: spec.UseDroneAuth,
}

handler := converter.Handler(
Expand Down
15 changes: 15 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,18 @@ func TestValidateGiteeMissing(t *testing.T) {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateForcedDroneAuth(t *testing.T) {
// drone uses a secret for authentication
s := &spec{
Secret: "abcdefg",
Provider: "github",
UseDroneAuth: true,
}

got := validate(s)

if got != nil {
t.Errorf("wanted no errors, got %s", got)
}
}
21 changes: 14 additions & 7 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
GithubServer string
StashServer string
Token string
UseDroneAuth bool
}

plugin struct {
Expand Down Expand Up @@ -119,6 +120,15 @@ func New(provider string, p *Params) converter.Plugin {
}
}

// GetToken returns the token to use for the SCM provider
func (p *plugin) GetToken(req *converter.Request) string {
if p.params.UseDroneAuth {
return req.Token.Access
}

return p.params.Token
}

func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Config, error) {

// set some default fields for logs
Expand Down Expand Up @@ -149,10 +159,7 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co
return nil, err
}

//token := req.Token.Access
//if p.params.Token != "" {
// token = p.params.Token
//}
token := p.GetToken(req)

if pathSeen {
requestLogger.Infoln("a path field was seen")
Expand All @@ -161,7 +168,7 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co

switch p.provider {
case "github":
changedFiles, err = providers.GetGithubFilesChanged(req.Repo, req.Build, req.Token.Access, p.params.GithubServer)
changedFiles, err = providers.GetGithubFilesChanged(req.Repo, req.Build, token, p.params.GithubServer)
if err != nil {
return nil, err
}
Expand All @@ -171,12 +178,12 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co
return nil, err
}
case "stash":
changedFiles, err = providers.GetStashFilesChanged(req.Repo, req.Build, p.params.StashServer, p.params.Token, scm.ListOptions{})
changedFiles, err = providers.GetStashFilesChanged(req.Repo, req.Build, p.params.StashServer, token, scm.ListOptions{})
if err != nil {
return nil, err
}
case "gitee":
changedFiles, err = providers.GetGiteeFilesChanged(req.Repo, req.Build, p.params.Token)
changedFiles, err = providers.GetGiteeFilesChanged(req.Repo, req.Build, token)
if err != nil {
return nil, err
}
Expand Down
91 changes: 90 additions & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ steps:
// github provider
func TestNewGithubCommitExcludeStep(t *testing.T) {
gock.New("https://api.github.com").
MatchHeader("Authorization", "^Bearer token_from_env$").
Get("/repos/meltwater/drone-convert-pathschanged/commits/6ee3cf41d995a79857e0db41c47bf619e6546571").
Reply(200).
Type("application/json").
Expand All @@ -168,7 +169,7 @@ steps:
`

params := &Params{
Token: "invalidtoken",
Token: "token_from_env",
}

req := &converter.Request{
Expand Down Expand Up @@ -605,3 +606,91 @@ name: default
t.Log(diff)
}
}

// use Drone auth token
func TestNewUseDroneAuth(t *testing.T) {
defer gock.Off()

// WE check that `MatchHeader` catch value of token that is passed from Drone
gock.New("https://api.github.com").
MatchHeader("Authorization", "^Bearer drone_token$").
Get("/repos/meltwater/drone-convert-pathschanged/commits/6ee3cf41d995a79857e0db41c47bf619e6546571").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("../providers/testdata/github/commit.json")

before := `
kind: pipeline
type: docker
name: default
steps:
- name: message
image: busybox
commands:
- echo "This step will be excluded when .drone.yml is changed"
when:
paths:
exclude:
- .drone.yml
`

params := &Params{
Token: "invalidtoken",
UseDroneAuth: true,
}

req := &converter.Request{
Token: drone.Token{
Access: "drone_token",
Refresh: "drone_refresh_token",
},
Build: drone.Build{
Before: "",
After: "6ee3cf41d995a79857e0db41c47bf619e6546571",
},
Config: drone.Config{
Data: before,
},
Repo: drone.Repo{
Namespace: "meltwater",
Name: "drone-convert-pathschanged",
Slug: "meltwater/drone-convert-pathschanged",
Config: ".drone.yml",
},
}

plugin := New("github", params)

config, err := plugin.Convert(noContext, req)
if err != nil {
t.Error(err)
return
}

after := `kind: pipeline
type: docker
steps:
- when:
paths:
exclude:
- .drone.yml
event:
exclude:
- '*'
commands:
- echo "This step will be excluded when .drone.yml is changed"
image: busybox
name: message
name: default
`
want := &drone.Config{
Data: after,
}

if diff := cmp.Diff(config.Data, want.Data); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

0 comments on commit 7d33fcb

Please sign in to comment.