More fixes for gitlab

This commit is contained in:
Kirill Zaitsev 2015-07-26 15:32:49 +03:00
parent c55cd56ca0
commit f889a29482
5 changed files with 37 additions and 10 deletions

View file

@ -7,6 +7,7 @@ all: concat bindata build
deps: deps:
go get github.com/jteeuwen/go-bindata/... go get github.com/jteeuwen/go-bindata/...
go get github.com/Bugagazavr/go-gitlab-client...
test: test:
go vet github.com/drone/drone/pkg/... go vet github.com/drone/drone/pkg/...

View file

@ -20,7 +20,7 @@ import (
"github.com/drone/drone/pkg/store" "github.com/drone/drone/pkg/store"
_ "github.com/drone/drone/pkg/remote/builtin/github" _ "github.com/drone/drone/pkg/remote/builtin/github"
_ "github.com/drone/drone/pkg/remote/gitlab" _ "github.com/drone/drone/pkg/remote/builtin/gitlab"
_ "github.com/drone/drone/pkg/store/builtin" _ "github.com/drone/drone/pkg/store/builtin"
_ "net/http/pprof" _ "net/http/pprof"

View file

@ -85,7 +85,7 @@ type Config struct {
} }
Gitlab struct { Gitlab struct {
URL string `envconfig:"optional"` Host string `envconfig:"optional"`
Client string `envconfig:"optional"` Client string `envconfig:"optional"`
Secret string `envconfig:"optional"` Secret string `envconfig:"optional"`
SkipVerify bool `envconfig:"optional"` SkipVerify bool `envconfig:"optional"`

View file

@ -18,7 +18,7 @@ import (
) )
const ( const (
DefaultScope = "repo" DefaultScope = "api"
) )
type Gitlab struct { type Gitlab struct {
@ -39,7 +39,7 @@ func init() {
func NewDriver(conf *config.Config) (remote.Remote, error) { func NewDriver(conf *config.Config) (remote.Remote, error) {
var gitlab = Gitlab{ var gitlab = Gitlab{
URL: conf.Gitlab.URL, URL: conf.Gitlab.Host,
Client: conf.Gitlab.Client, Client: conf.Gitlab.Client,
Secret: conf.Gitlab.Secret, Secret: conf.Gitlab.Secret,
AllowedOrgs: conf.Gitlab.Orgs, AllowedOrgs: conf.Gitlab.Orgs,
@ -151,7 +151,7 @@ func (r *Gitlab) Netrc(u *common.User) (*common.Netrc, error) {
// Activate activates a repository by adding a Post-commit hook and // Activate activates a repository by adding a Post-commit hook and
// a Public Deploy key, if applicable. // a Public Deploy key, if applicable.
func (r *Gitlab) Activate(user *common.User, repo *common.Repo, keys *common.Keypair, link string) error { func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypair, link string) error {
var client = NewClient(r.URL, user.Token, r.SkipVerify) var client = NewClient(r.URL, user.Token, r.SkipVerify)
var path = ns(repo.Owner, repo.Name) var path = ns(repo.Owner, repo.Name)
var title, err = GetKeyTitle(link) var title, err = GetKeyTitle(link)
@ -162,15 +162,14 @@ func (r *Gitlab) Activate(user *common.User, repo *common.Repo, keys *common.Key
// if the repository is private we'll need // if the repository is private we'll need
// to upload a github key to the repository // to upload a github key to the repository
if repo.Private { if repo.Private {
var err = client.AddProjectDeployKey(path, title, repo.Keys.Public) if err := client.AddProjectDeployKey(path, title, k.Public); err != nil {
if err != nil {
return err return err
} }
} }
// append the repo owner / name to the hook url since gitlab // append the repo owner / name to the hook url since gitlab
// doesn't send this detail in the post-commit hook // doesn't send this detail in the post-commit hook
link += "?owner=" + repo.Owner + "&name=" + repo.Name link += "&owner=" + repo.Owner + "&name=" + repo.Name
// add the hook // add the hook
return client.AddProjectHook(path, link, true, false, true) return client.AddProjectHook(path, link, true, false, true)
@ -199,7 +198,7 @@ func (r *Gitlab) Deactivate(user *common.User, repo *common.Repo, link string) e
if err != nil { if err != nil {
return err return err
} }
link += "?owner=" + repo.Owner + "&name=" + repo.Name link += "&owner=" + repo.Owner + "&name=" + repo.Name
for _, h := range hooks { for _, h := range hooks {
if link == h.Url { if link == h.Url {
if err := client.RemoveProjectHook(path, strconv.Itoa(h.Id)); err != nil { if err := client.RemoveProjectHook(path, strconv.Itoa(h.Id)); err != nil {
@ -235,21 +234,48 @@ func (r *Gitlab) Hook(req *http.Request) (*common.Hook, error) {
return nil, nil return nil, nil
} }
var cloneUrl = parsed.Repository.GitHttpUrl
if parsed.Repository.VisibilityLevel < 20 {
cloneUrl = parsed.Repository.GitSshUrl
}
var hook = new(common.Hook) var hook = new(common.Hook)
hook.Repo = &common.Repo{}
hook.Repo.Owner = req.FormValue("owner") hook.Repo.Owner = req.FormValue("owner")
hook.Repo.Name = req.FormValue("name") hook.Repo.Name = req.FormValue("name")
hook.Repo.Link = parsed.Repository.URL
hook.Repo.Clone = cloneUrl
hook.Repo.Branch = "master"
switch parsed.Repository.VisibilityLevel {
case 0:
hook.Repo.Private = true
case 10:
hook.Repo.Private = true
case 20:
hook.Repo.Private = false
}
hook.Repo.FullName = fmt.Sprintf("%s/%s", req.FormValue("owner"), req.FormValue("name"))
hook.Commit = &common.Commit{}
hook.Commit.Sha = parsed.After hook.Commit.Sha = parsed.After
hook.Commit.Branch = parsed.Branch() hook.Commit.Branch = parsed.Branch()
hook.Commit.Ref = parsed.Ref
hook.Commit.Remote = cloneUrl
var head = parsed.Head() var head = parsed.Head()
hook.Commit.Message = head.Message hook.Commit.Message = head.Message
hook.Commit.Timestamp = head.Timestamp hook.Commit.Timestamp = head.Timestamp
hook.Commit.Author = &common.Author{}
// extracts the commit author (ideally email) // extracts the commit author (ideally email)
// from the post-commit hook // from the post-commit hook
switch { switch {
case head.Author != nil: case head.Author != nil:
hook.Commit.Author.Email = head.Author.Email hook.Commit.Author.Email = head.Author.Email
hook.Commit.Author.Login = parsed.UserName
case head.Author == nil: case head.Author == nil:
hook.Commit.Author.Login = parsed.UserName hook.Commit.Author.Login = parsed.UserName
} }
@ -265,7 +291,7 @@ func (g *Gitlab) Oauth2Transport(r *http.Request) *oauth2.Transport {
ClientSecret: g.Secret, ClientSecret: g.Secret,
Scope: DefaultScope, Scope: DefaultScope,
AuthURL: fmt.Sprintf("%s/oauth/authorize", g.URL), AuthURL: fmt.Sprintf("%s/oauth/authorize", g.URL),
TokenURL: fmt.Sprintf("%s/oauth/access_token", g.URL), TokenURL: fmt.Sprintf("%s/oauth/token", g.URL),
RedirectURL: fmt.Sprintf("%s/authorize", httputil.GetURL(r)), RedirectURL: fmt.Sprintf("%s/authorize", httputil.GetURL(r)),
//settings.Server.Scheme, settings.Server.Hostname), //settings.Server.Scheme, settings.Server.Hostname),
}, },