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

add gitlab provider support #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ DRONE_CONVERT_PLUGIN_ENDPOINT=http://1.2.3.4:3000
DRONE_CONVERT_PLUGIN_SECRET=bea26a2221fd8090ea38720fc445eca6
```


## Gitlab Server

1. Create a github token via https://your-github-server-address/profile/personal_access_tokens with the scope of `repo`

2. Create a shares secret:

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

3. Download ran run the plugin:

```console
$ docker run -d \
--publish=3000:3000 \
--env=DRONE_DEBUG=true \
--env=DRONE_SECRET=f9b759a35620876fb46f42dffdc7d25b \
--env=TOKEN=9e6eij3ckzvpe9mrhnqcis6zf8dhopmm46e3pi96 \
--env=PROVIDER=gitlab \
--env=GITLAB_SERVER=https://your-gitlab-server-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=f9b759a35620876fb46f42dffdc7d25b
```

## Bitbucket Cloud

1. Create an "App password" via https://bitbucket.org/account/settings/app-passwords and select only "Read" under "Repositories"
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type (
BitBucketUser string `envconfig:"BITBUCKET_USER"`
BitBucketPassword string `envconfig:"BITBUCKET_PASSWORD"`
GithubServer string `envconfig:"GITHUB_SERVER"`
GitlabServer string `envconfig:"GITLAB_SERVER"`
StashServer string `envconfig:"STASH_SERVER"`
}
)
Expand All @@ -60,14 +61,15 @@ func validate(spec *spec) error {
// bitbucket-server support is deprecated in favor of stash, it will be removed in a future version
"bitbucket-server",
"github",
"gitlab",
"stash",
"gitee",
}
if !contains(providers, spec.Provider) {
return fmt.Errorf("unsupported provider")
}
}
if spec.Token == "" && (spec.Provider == "github" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") {
if spec.Token == "" && (spec.Provider == "github" || spec.Provider == "gitlab" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") {
return fmt.Errorf("missing token")
}
if spec.BitBucketUser == "" && spec.Provider == "bitbucket" {
Expand Down Expand Up @@ -130,6 +132,7 @@ func main() {
BitBucketUser: spec.BitBucketUser,
BitBucketPassword: spec.BitBucketPassword,
GithubServer: spec.GithubServer,
GitlabServer: spec.GitlabServer,
Token: spec.Token,
StashServer: spec.StashServer,
}
Expand Down
6 changes: 6 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type (
BitBucketUser string
BitBucketPassword string
GithubServer string
GitlabServer string
StashServer string
Token string
}
Expand Down Expand Up @@ -160,6 +161,11 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co
if err != nil {
return nil, err
}
case "gitlab":
changedFiles, err = providers.GetGitLabFilesChanged(req.Repo, req.Build, p.params.Token, p.params.GitlabServer)
if err != nil {
return nil, err
}
case "bitbucket":
changedFiles, err = providers.GetBitbucketFilesChanged(req.Repo, req.Build, p.params.BitBucketUser, p.params.BitBucketPassword, scm.ListOptions{})
if err != nil {
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.PrivateToken{
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)
}