Skip to content

Commit

Permalink
Bitbucket stash (#79)
Browse files Browse the repository at this point in the history
adding stash support
  • Loading branch information
Jim Sheldon authored Mar 15, 2022
1 parent 9ae6066 commit e59ca23
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 124 deletions.
122 changes: 78 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,37 @@
package main

import (
"fmt"
"io"
"net/http"

"github.com/meltwater/drone-convert-pathschanged/plugin"

"github.com/drone/drone-go/plugin/converter"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/meltwater/drone-convert-pathschanged/plugin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)

// spec provides the plugin settings.
type spec struct {
Bind string `envconfig:"DRONE_BIND"`
Debug bool `envconfig:"DRONE_DEBUG"`
Text bool `envconfig:"DRONE_LOGS_TEXT"`
Secret string `envconfig:"DRONE_SECRET"`

Provider string `envconfig:"PROVIDER"`
Token string `envconfig:"TOKEN"`
BitBucketAddress string `envconfig:"BB_ADDRESS"`
BitBucketUser string `envconfig:"BITBUCKET_USER"`
BitBucketPassword string `envconfig:"BITBUCKET_PASSWORD"`
GithubServer string `envconfig:"GITHUB_SERVER"`
}
type (
spec struct {
Bind string `envconfig:"DRONE_BIND"`
Debug bool `envconfig:"DRONE_DEBUG"`
Text bool `envconfig:"DRONE_LOGS_TEXT"`
Secret string `envconfig:"DRONE_SECRET"`

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"`
BitBucketPassword string `envconfig:"BITBUCKET_PASSWORD"`
GithubServer string `envconfig:"GITHUB_SERVER"`
StashServer string `envconfig:"STASH_SERVER"`
}
)

func contains(s []string, str string) bool {
for _, v := range s {
Expand All @@ -40,11 +46,54 @@ func contains(s []string, str string) bool {
return false
}

func validate(spec *spec) error {
if spec.Secret == "" {
return fmt.Errorf("missing secret key")
}
if spec.Provider == "" {
return fmt.Errorf("missing provider")
} else {
providers := []string{
"bitbucket",
// bitbucket-server support is deprecated in favor of stash, it will be removed in a future version
"bitbucket-server",
"github",
"stash",
}
if !contains(providers, spec.Provider) {
return fmt.Errorf("unsupported provider")
}
}
if spec.Token == "" && (spec.Provider == "github" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") {
return fmt.Errorf("missing token")
}
if spec.BitBucketUser == "" && spec.Provider == "bitbucket" {
return fmt.Errorf("missing bitbucket user")
}
if spec.BitBucketPassword == "" && spec.Provider == "bitbucket" {
return fmt.Errorf("missing bitbucket password")
}
if spec.BitBucketAddress == "" && spec.Provider == "bitbucket-server" {
return fmt.Errorf("missing bitbucket server address")
} else if spec.BitBucketAddress != "" && spec.Provider == "bitbucket-server" {
// backwards compatible support for bitbucket-server, this will be removed in a future version
spec.StashServer = spec.BitBucketAddress
spec.Provider = "stash"

logrus.Warningln("bitbucket-server support is deprecated, please use stash")
}
if spec.StashServer == "" && spec.Provider == "stash" {
return fmt.Errorf("missing stash server")
}

return nil
}

func main() {
spec := new(spec)
err := envconfig.Process("", spec)
if err != nil {
logrus.Fatal(err)
logrus.Fatalln(err)
}

if spec.Debug {
Expand All @@ -55,41 +104,26 @@ func main() {
} else {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
if spec.Secret == "" {
logrus.Fatalln("missing secret key")
}
if spec.Provider == "" {
logrus.Fatalln("missing provider")
} else {
providers := []string{"bitbucket", "bitbucket-server", "github"}
if !contains(providers, spec.Provider) {
logrus.Fatalln("invalid provider:", spec.Provider)
}
}
if spec.Token == "" && (spec.Provider == "github" || spec.Provider == "bitbucket-server") {
logrus.Fatalln("missing token")
}
if spec.BitBucketUser == "" && spec.Provider == "bitbucket" {
logrus.Fatalln("missing bitbucket user")
}
if spec.BitBucketPassword == "" && spec.Provider == "bitbucket" {
logrus.Fatalln("missing bitbucket password")
}
if spec.BitBucketAddress == "" && spec.Provider == "bitbucket-server" {
logrus.Fatalln("missing bitbucket server address")

err = validate(spec)
if err != nil {
logrus.Fatalln(err)
}

if spec.Bind == "" {
spec.Bind = ":3000"
}

params := &plugin.Params{
BitBucketUser: spec.BitBucketUser,
BitBucketPassword: spec.BitBucketPassword,
GithubServer: spec.GithubServer,
Token: spec.Token,
StashServer: spec.StashServer,
}

handler := converter.Handler(
plugin.New(
spec.Token,
spec.Provider,
spec.GithubServer,
spec.BitBucketUser,
spec.BitBucketPassword,
),
plugin.New(spec.Provider, params),
spec.Secret,
logrus.StandardLogger(),
)
Expand Down
161 changes: 161 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"testing"
)

func TestValidateSecretMissing(t *testing.T) {
s := &spec{
Secret: "",
}

got := validate(s)

want := "missing secret key"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateProviderMissing(t *testing.T) {
s := &spec{
Provider: "",
Secret: "abcdefg",
}

got := validate(s)

want := "missing provider"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateProviderUnsupported(t *testing.T) {
s := &spec{
Provider: "unsupported",
Secret: "abcdefg",
}

got := validate(s)

want := "unsupported provider"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateTokenMissing(t *testing.T) {
// bitbucket-server/stash and github use tokens for authentication
providers := []string{
"bitbucket-server",
"github",
"stash",
}
for _, provider := range providers {
s := &spec{
Provider: provider,
Secret: "abcdefg",
Token: "",
}

got := validate(s)

want := "missing token"

if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}
}

func TestValidateBitbucketUserMissing(t *testing.T) {
s := &spec{
BitBucketUser: "",
Provider: "bitbucket",
Secret: "abcdefg",
}

got := validate(s)

want := "missing bitbucket user"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateBitbucketPasswordMissing(t *testing.T) {
s := &spec{
BitBucketUser: "centauri",
BitBucketPassword: "",
Provider: "bitbucket",
Secret: "abcdefg",
}

got := validate(s)

want := "missing bitbucket password"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

func TestValidateBitbucketServerAddressMissing(t *testing.T) {
s := &spec{
BitBucketAddress: "",
Provider: "bitbucket-server",
Secret: "abcdefg",
Token: "abcdefg",
}

got := validate(s)

want := "missing bitbucket server address"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}

// this tests backwards compatibility with bitbucket-server for stash
func TestValidateBitbucketServerStashCompatibility(t *testing.T) {
s := &spec{
BitBucketAddress: "example.com",
Provider: "bitbucket-server",
Secret: "abcdefg",
Token: "abcdefg",
}

err := validate(s)
if err != nil {
t.Error(err)
}

// validate should replace 'Provider' with 'stash' and set 'StashServer'
want := &spec{
BitBucketAddress: "example.com",
Provider: "stash",
StashServer: "example.com",
Secret: "abcdefg",
Token: "abcdefg",
}

if *s != *want {
t.Errorf("wanted %+v, got %+v", *want, *s)
}
}

func TestValidateStashServerMissing(t *testing.T) {
s := &spec{
Provider: "stash",
Secret: "abcdefg",
StashServer: "",
Token: "abcdefg",
}

got := validate(s)

want := "missing stash server"
if got.Error() != want {
t.Errorf("wanted %s, got %s", want, got)
}
}
4 changes: 2 additions & 2 deletions plugin/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func parsePipelines(data string, build drone.Build, repo drone.Repo, changedFile
if resource.Trigger.Attrs == nil {
resource.Trigger.Attrs = make(map[string]interface{})
}
resource.Trigger.Attrs["event"] = map[string][]string{"exclude": []string{"*"}}
resource.Trigger.Attrs["event"] = map[string][]string{"exclude": {"*"}}
}
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func parsePipelines(data string, build drone.Build, repo drone.Repo, changedFile
if step.When.Attrs == nil {
step.When.Attrs = make(map[string]interface{})
}
step.When.Attrs["event"] = map[string][]string{"exclude": []string{"*"}}
step.When.Attrs["event"] = map[string][]string{"exclude": {"*"}}
}
}
}
Expand Down
Loading

0 comments on commit e59ca23

Please sign in to comment.