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

WIP: Add support for GitLab provider #53

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,58 @@ $ docker run -d \
--env=DRONE_SECRET=bea26a2221fd8090ea38720fc445eca6 \
--env=TOKEN=9e6eij3ckzvpe9mrhnqcis6zf8dhopmm46e3pi96 \
--env=PROVIDER=bitbucket-server \
--env=BB_ADDRESS=https://your-bitbucket-address \
--restart=always \
--name=converter meltwater/drone-convert-pathschanged
```

4. Update your Drone server configuration to include the plugin address and the shared secret.

```text
DRONE_CONVERT_PLUGIN_ENDPOINT=http://1.2.3.4:3000
DRONE_CONVERT_PLUGIN_SECRET=bea26a2221fd8090ea38720fc445eca6
```

## GitLab

_GitLab support is currently considered experimental_

1. Create a BitBucket access token via https://your-bitbucket-address/plugins/servlet/access-tokens/manage with read-only rights

2. Create a shared secret:

```console
$ openssl rand -hex 16
bea26a2221fd8090ea38720fc445eca6
```

3. (A) Download and run the plugin (using gitlab.com):

```console
$ docker run -d \
--publish=3000:3000 \
--env=DRONE_DEBUG=true \
--env=DRONE_SECRET=bea26a2221fd8090ea38720fc445eca6 \
--env=TOKEN=9e6eij3ckzvpe9mrhnqcis6zf8dhopmm46e3pi96 \
--env=PROVIDER=gitlab \
--restart=always \
--name=converter meltwater/drone-convert-pathschanged
```

3. (B) Download and run the plugin (using self-hosted GitLab)
```console
$ docker run -d \
--publish=3000:3000 \
--env=DRONE_DEBUG=true \
--env=DRONE_SECRET=bea26a2221fd8090ea38720fc445eca6 \
--env=TOKEN=9e6eij3ckzvpe9mrhnqcis6zf8dhopmm46e3pi96 \
--env=PROVIDER=gitlab \
--env=GITLAB_ADDRESS=https://gitlab.example.com \
--restart=always \
--name=converter meltwater/drone-convert-pathschanged
```


4. Update your Drone server configuration to include the plugin address and the shared secret.

```text
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ github.com/drone/drone-go v1.1.0 h1:2mritc5b7PhQWvILNyzaImZMRWVbMmmZ5Q0UDwwO7SI=
github.com/drone/drone-go v1.1.0/go.mod h1:GxyeGClYohaKNYJv/ZpsmVHtMJ7WhoT+uDaJNcDIrk4=
github.com/drone/go-scm v1.7.1 h1:wME/n7Qdo70VJ+WXZanJHjLtNWONEfjNsO2iwHDdlkE=
github.com/drone/go-scm v1.7.1/go.mod h1:lXwfbyrIJwFFME5TpzavkwO2T5X8yBK6t6cve7g91x0=
github.com/drone/go-scm v1.8.0 h1:kDHu38a11loKf6uaBu75TmY1YPwsSaZdseET738Oy0o=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20200810125852-15f2a16ca820 h1:WZjcz0B/K3rhvsu2HAmFRpgChrGVIBfvrmhwK2AdYYQ=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20200810125852-15f2a16ca820/go.mod h1:LB3osS9X2JMYmTzcCArHHLrndBAfcVLQAvUddfs+ONs=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -86,12 +85,10 @@ github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type spec struct {
Provider string `envconfig:"PROVIDER"`
Token string `envconfig:"TOKEN"`
BitBucketAddress string `envconfig:"BB_ADDRESS"`
GitLabAddress string `envconfig:"GITLAB_ADDRESS"`
}

func contains(s []string, str string) bool {
Expand Down Expand Up @@ -61,14 +62,18 @@ func main() {
if spec.Provider == "" {
logrus.Fatalln("missing provider")
} else {
providers := []string{"bitbucket-server", "github"}
providers := []string{"bitbucket-server", "github", "gitlab"}
if !contains(providers, spec.Provider) {
logrus.Fatalln("invalid provider:", spec.Provider)
}
}
if spec.BitBucketAddress == "" && spec.Provider == "bitbucket-server" {
logrus.Fatalln("missing bitbucket server address")
}
if spec.GitLabAddress == "" && spec.Provider == "gitlab" {
spec.GitLabAddress = "https://gitlab.com"
logrus.Info("no gitlab address provided, using 'https://gitlab.com'")
}
if spec.Bind == "" {
spec.Bind = ":3000"
}
Expand All @@ -77,6 +82,8 @@ func main() {
plugin.New(
spec.Token,
spec.Provider,
spec.BitBucketAddress,
spec.GitLabAddress,
),
spec.Secret,
logrus.StandardLogger(),
Expand Down
16 changes: 12 additions & 4 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
token string
provider string
bitbucketAddress string
gitlabAddress string
}

resource struct {
Expand Down Expand Up @@ -104,10 +105,12 @@ func marshal(in []*resource) ([]byte, error) {
}

// New returns a new conversion plugin.
func New(token string, provider string) converter.Plugin {
func New(token string, provider string, bitbucketAddress string, gitlabAddress string) converter.Plugin {
return &plugin{
token: token,
provider: provider,
token: token,
provider: provider,
bitbucketAddress: bitbucketAddress,
gitlabAddress: gitlabAddress,
}
}

Expand Down Expand Up @@ -153,7 +156,12 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co
return nil, err
}
case "bitbucket-server":
changedFiles, err = providers.GetBBFilesChanged(req.Repo, req.Build, p.token)
changedFiles, err = providers.GetBBFilesChanged(req.Repo, req.Build, p.token, p.bitbucketAddress)
if err != nil {
return nil, err
}
case "gitlab":
changedFiles, err = providers.GetGitLabFilesChanged(req.Repo, req.Build, p.token, p.gitlabAddress)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var noContext = context.Background()

func TestNewEmptyPipeline(t *testing.T) {

providers := []string{"github", "bitbucket-server"}
providers := []string{"github", "bitbucket-server", "gitlab"}

req := &converter.Request{
Build: drone.Build{},
Expand All @@ -34,7 +34,7 @@ func TestNewEmptyPipeline(t *testing.T) {
}

for _, provider := range providers {
plugin := New("invalidtoken", provider)
plugin := New("invalidtoken", provider, "", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ this_is_invalid_yaml
},
}

plugin := New("invalidtoken", "")
plugin := New("invalidtoken", "", "", "")

_, err := plugin.Convert(noContext, req)
if err == nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ steps:
},
}

plugin := New("invalidtoken", "unsupported")
plugin := New("invalidtoken", "unsupported", "", "")

_, err := plugin.Convert(noContext, req)
if err == nil {
Expand Down Expand Up @@ -163,7 +163,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -236,7 +236,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -306,7 +306,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down Expand Up @@ -379,7 +379,7 @@ steps:
},
}

plugin := New("invalidtoken", "github")
plugin := New("invalidtoken", "github", "", "")

config, err := plugin.Convert(noContext, req)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions providers/bitbucket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package providers
import (
"context"
"encoding/json"

"github.com/drone/drone-go/drone"
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
"os"
)

type bitbucketDiffs struct {
Expand All @@ -16,10 +16,9 @@ type bitbucketDiffs struct {
} `json:"diffs"`
}

func GetBBFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string, error) {
func GetBBFilesChanged(repo drone.Repo, build drone.Build, token string, bitbucketAddress string) ([]string, error) {
var files []string
var ctx context.Context
bitbucketAddress := os.Getenv("BB_ADDRESS")
params := map[string]interface{}{
"since": build.Before,
}
Expand Down
64 changes: 64 additions & 0 deletions providers/gitlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package providers

import (
"context"
"net/http"

"github.com/drone/drone-go/drone"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/gitlab"
"github.com/drone/go-scm/scm/transport"

"github.com/prometheus/client_golang/prometheus"
)

func GetGitLabFilesChanged(repo drone.Repo, build drone.Build, token string, uri string) ([]string, error) {
var err error

newctx := context.Background()
client, err := gitlab.New(uri)
if err != nil {
return nil, err
}
client.Client = &http.Client{
Transport: &transport.BearerToken{
Token: token,
},
}

var changes []*scm.Change
var result *scm.Response

if build.Before == "" || build.Before == scm.EmptyCommit {
changes, result, err = client.Git.ListChanges(newctx, repo.Slug, build.After, scm.ListOptions{})
if err != nil {
return nil, err
}
} else {
changes, result, err = client.Git.CompareChanges(newctx, repo.Slug, build.Before, build.After, scm.ListOptions{})
if err != nil {
return nil, err
}
}

GitLabApiCount.Set(float64(result.Rate.Remaining))

var files []string
for _, c := range changes {
files = append(files, c.Path)
}

return files, nil
}

var (
GitLabApiCount = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "gitlab_api_calls_remaining",
Help: "Total number of github api calls per hour remaining",
})
)

func init() {
prometheus.MustRegister(GitLabApiCount)
}