diff --git a/README.md b/README.md index c173c42..2ff0986 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/main.go b/main.go index 36ede20..66459a6 100644 --- a/main.go +++ b/main.go @@ -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"` } ) @@ -60,6 +61,7 @@ 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", } @@ -67,7 +69,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.Provider == "github" || spec.Provider == "gitlab" || spec.Provider == "bitbucket-server" || spec.Provider == "stash") { return fmt.Errorf("missing token") } if spec.BitBucketUser == "" && spec.Provider == "bitbucket" { @@ -130,6 +132,7 @@ func main() { BitBucketUser: spec.BitBucketUser, BitBucketPassword: spec.BitBucketPassword, GithubServer: spec.GithubServer, + GitlabServer: spec.GitlabServer, Token: spec.Token, StashServer: spec.StashServer, } diff --git a/plugin/plugin.go b/plugin/plugin.go index fdbd66a..b885fc8 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -25,6 +25,7 @@ type ( BitBucketUser string BitBucketPassword string GithubServer string + GitlabServer string StashServer string Token string } @@ -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 { diff --git a/providers/gitlab.go b/providers/gitlab.go new file mode 100644 index 0000000..b3c82e5 --- /dev/null +++ b/providers/gitlab.go @@ -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) +}