2015-07-25 08:49:39 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-07-26 23:52:18 +00:00
|
|
|
"net/url"
|
2015-07-25 08:49:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-07-26 23:25:20 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client"
|
2015-07-25 08:49:39 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/hashicorp/golang-lru"
|
2015-09-02 03:42:18 +00:00
|
|
|
"github.com/drone/drone/pkg/hash"
|
2015-07-25 08:49:39 +00:00
|
|
|
"github.com/drone/drone/pkg/oauth2"
|
|
|
|
"github.com/drone/drone/pkg/remote"
|
|
|
|
common "github.com/drone/drone/pkg/types"
|
|
|
|
"github.com/drone/drone/pkg/utils/httputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-07-26 12:32:49 +00:00
|
|
|
DefaultScope = "api"
|
2015-07-25 08:49:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Gitlab struct {
|
|
|
|
URL string
|
|
|
|
Client string
|
|
|
|
Secret string
|
|
|
|
AllowedOrgs []string
|
2015-09-02 03:42:18 +00:00
|
|
|
CloneMode string
|
2015-07-25 08:49:39 +00:00
|
|
|
Open bool
|
|
|
|
PrivateMode bool
|
|
|
|
SkipVerify bool
|
2015-07-30 00:46:22 +00:00
|
|
|
Search bool
|
2015-07-25 08:49:39 +00:00
|
|
|
|
|
|
|
cache *lru.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
remote.Register("gitlab", NewDriver)
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:53:39 +00:00
|
|
|
func NewDriver(config string) (remote.Remote, error) {
|
|
|
|
url_, err := url.Parse(config)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-06 22:53:39 +00:00
|
|
|
params := url_.Query()
|
|
|
|
url_.RawQuery = ""
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2015-08-06 22:53:39 +00:00
|
|
|
gitlab := Gitlab{}
|
|
|
|
gitlab.URL = url_.String()
|
2015-08-09 03:51:12 +00:00
|
|
|
gitlab.Client = params.Get("client_id")
|
|
|
|
gitlab.Secret = params.Get("client_secret")
|
2015-08-06 22:53:39 +00:00
|
|
|
gitlab.AllowedOrgs = params["orgs"]
|
|
|
|
gitlab.SkipVerify, _ = strconv.ParseBool(params.Get("skip_verify"))
|
|
|
|
gitlab.Open, _ = strconv.ParseBool(params.Get("open"))
|
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
switch params.Get("clone_mode") {
|
|
|
|
case "oauth":
|
|
|
|
gitlab.CloneMode = "oauth"
|
|
|
|
default:
|
|
|
|
gitlab.CloneMode = "token"
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:53:39 +00:00
|
|
|
// this is a temp workaround
|
|
|
|
gitlab.Search, _ = strconv.ParseBool(params.Get("search"))
|
|
|
|
|
|
|
|
// here we cache permissions to avoid too many api
|
|
|
|
// calls. this should really be moved outise the
|
|
|
|
// remote plugin into the app
|
|
|
|
gitlab.cache, err = lru.New(1028)
|
|
|
|
return &gitlab, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Login(token, secret string) (*common.User, error) {
|
|
|
|
client := NewClient(g.URL, token, g.SkipVerify)
|
2015-07-25 08:49:39 +00:00
|
|
|
var login, err = client.CurrentUser()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user := common.User{}
|
|
|
|
user.Login = login.Username
|
|
|
|
user.Email = login.Email
|
|
|
|
user.Token = token
|
|
|
|
user.Secret = secret
|
2015-08-10 07:15:33 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(login.AvatarUrl, "http") {
|
|
|
|
user.Avatar = login.AvatarUrl
|
|
|
|
} else {
|
2015-09-02 03:42:18 +00:00
|
|
|
user.Avatar = g.URL + "/" + login.AvatarUrl
|
2015-08-10 07:15:33 +00:00
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Orgs fetches the organizations for the given user.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Orgs(u *common.User) ([]string, error) {
|
2015-07-25 08:49:39 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repo fetches the named repository from the remote system.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Repo(u *common.User, owner, name string) (*common.Repo, error) {
|
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, owner, name)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
repo_, err := client.Project(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := &common.Repo{}
|
|
|
|
repo.Owner = owner
|
|
|
|
repo.Name = name
|
|
|
|
repo.FullName = repo_.PathWithNamespace
|
|
|
|
repo.Link = repo_.Url
|
|
|
|
repo.Clone = repo_.HttpRepoUrl
|
|
|
|
repo.Branch = "master"
|
|
|
|
|
|
|
|
if repo_.DefaultBranch != "" {
|
|
|
|
repo.Branch = repo_.DefaultBranch
|
|
|
|
}
|
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
if g.PrivateMode {
|
2015-07-25 08:49:39 +00:00
|
|
|
repo.Private = true
|
2015-07-26 22:29:51 +00:00
|
|
|
} else {
|
|
|
|
repo.Private = !repo_.Public
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
2015-07-26 22:29:51 +00:00
|
|
|
|
2015-07-25 08:49:39 +00:00
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perm fetches the named repository from the remote system.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Perm(u *common.User, owner, name string) (*common.Perm, error) {
|
2015-07-25 08:49:39 +00:00
|
|
|
key := fmt.Sprintf("%s/%s/%s", u.Login, owner, name)
|
2015-09-02 03:42:18 +00:00
|
|
|
val, ok := g.cache.Get(key)
|
2015-07-25 08:49:39 +00:00
|
|
|
if ok {
|
|
|
|
return val.(*common.Perm), nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, owner, name)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:49:39 +00:00
|
|
|
repo, err := client.Project(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := &common.Perm{}
|
|
|
|
m.Admin = IsAdmin(repo)
|
|
|
|
m.Pull = IsRead(repo)
|
|
|
|
m.Push = IsWrite(repo)
|
2015-09-02 03:42:18 +00:00
|
|
|
g.cache.Add(key, m)
|
2015-07-25 08:49:39 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetScript fetches the build script (.drone.yml) from the remote
|
|
|
|
// repository and returns in string format.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Script(user *common.User, repo *common.Repo, build *common.Build) ([]byte, error) {
|
|
|
|
var client = NewClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, repo.Owner, repo.Name)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.RepoRawFile(id, build.Commit.Sha, ".drone.yml")
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 21:06:06 +00:00
|
|
|
// NOTE Currently gitlab doesn't support status for commits and events,
|
|
|
|
// also if we want get MR status in gitlab we need implement a special plugin for gitlab,
|
|
|
|
// gitlab uses API to fetch build status on client side. But for now we skip this.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Status(u *common.User, repo *common.Repo, b *common.Build) error {
|
2015-07-25 08:49:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Netrc returns a .netrc file that can be used to clone
|
|
|
|
// private repositories from a remote system.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Netrc(u *common.User, r *common.Repo) (*common.Netrc, error) {
|
|
|
|
url_, err := url.Parse(g.URL)
|
2015-07-26 23:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netrc := &common.Netrc{}
|
2015-09-02 03:42:18 +00:00
|
|
|
|
|
|
|
switch g.CloneMode {
|
|
|
|
case "oauth":
|
|
|
|
netrc.Login = "oauth2"
|
|
|
|
netrc.Password = u.Token
|
|
|
|
case "token":
|
|
|
|
netrc.Login = "drone-ci-token"
|
|
|
|
netrc.Password = hash.New(r.FullName, r.Hash)
|
|
|
|
}
|
2015-07-26 23:52:18 +00:00
|
|
|
netrc.Machine = url_.Host
|
|
|
|
return netrc, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Activate activates a repository by adding a Post-commit hook and
|
|
|
|
// a Public Deploy key, if applicable.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypair, link string) error {
|
|
|
|
var client = NewClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, repo.Owner, repo.Name)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
uri, err := url.Parse(link)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
droneUrl := fmt.Sprintf("%s://%s", uri.Scheme, uri.Host)
|
|
|
|
droneToken := uri.Query().Get("access_token")
|
2015-09-02 03:42:18 +00:00
|
|
|
ssl_verify := strconv.FormatBool(!g.SkipVerify)
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2015-08-29 22:18:45 +00:00
|
|
|
return client.AddDroneService(id, map[string]string{
|
|
|
|
"token": droneToken,
|
|
|
|
"drone_url": droneUrl,
|
|
|
|
"enable_ssl_verification": ssl_verify,
|
|
|
|
})
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate removes a repository by removing all the post-commit hooks
|
|
|
|
// which are equal to link and removing the SSH deploy key.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Deactivate(user *common.User, repo *common.Repo, link string) error {
|
|
|
|
var client = NewClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, repo.Owner, repo.Name)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
return client.DeleteDroneService(id)
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHook parses the post-commit hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Hook(req *http.Request) (*common.Hook, error) {
|
2015-07-25 23:22:16 +00:00
|
|
|
defer req.Body.Close()
|
2015-07-25 08:49:39 +00:00
|
|
|
var payload, _ = ioutil.ReadAll(req.Body)
|
2015-07-25 23:22:16 +00:00
|
|
|
var parsed, err = gogitlab.ParseHook(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
switch parsed.ObjectKind {
|
|
|
|
case "merge_request":
|
|
|
|
return mergeRequest(parsed, req)
|
2015-08-22 19:55:08 +00:00
|
|
|
case "tag_push", "push":
|
2015-08-22 03:32:22 +00:00
|
|
|
return push(parsed, req)
|
|
|
|
default:
|
2015-07-25 23:02:33 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-08-22 03:32:22 +00:00
|
|
|
}
|
2015-07-25 23:02:33 +00:00
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
func mergeRequest(parsed *gogitlab.HookPayload, req *http.Request) (*common.Hook, error) {
|
|
|
|
var hook = new(common.Hook)
|
|
|
|
|
|
|
|
hook.Repo = &common.Repo{}
|
|
|
|
hook.Repo.Owner = req.FormValue("owner")
|
|
|
|
hook.Repo.Name = req.FormValue("name")
|
|
|
|
hook.Repo.FullName = fmt.Sprintf("%s/%s", hook.Repo.Owner, hook.Repo.Name)
|
2015-08-30 00:34:05 +00:00
|
|
|
hook.Repo.Link = parsed.ObjectAttributes.Target.WebUrl
|
2015-08-22 03:32:22 +00:00
|
|
|
hook.Repo.Clone = parsed.ObjectAttributes.Target.HttpUrl
|
|
|
|
hook.Repo.Branch = "master"
|
|
|
|
|
|
|
|
hook.Commit = &common.Commit{}
|
|
|
|
hook.Commit.Message = parsed.ObjectAttributes.LastCommit.Message
|
|
|
|
hook.Commit.Sha = parsed.ObjectAttributes.LastCommit.Id
|
|
|
|
hook.Commit.Remote = parsed.ObjectAttributes.Source.HttpUrl
|
|
|
|
|
2015-08-30 00:34:05 +00:00
|
|
|
if parsed.ObjectAttributes.SourceProjectId == parsed.ObjectAttributes.TargetProjectId {
|
2015-08-22 03:32:22 +00:00
|
|
|
hook.Commit.Ref = fmt.Sprintf("refs/heads/%s", parsed.ObjectAttributes.SourceBranch)
|
|
|
|
} else {
|
|
|
|
hook.Commit.Ref = fmt.Sprintf("refs/merge-requests/%d/head", parsed.ObjectAttributes.IId)
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 00:34:05 +00:00
|
|
|
hook.Commit.Branch = parsed.ObjectAttributes.SourceBranch
|
|
|
|
hook.Commit.Timestamp = parsed.ObjectAttributes.LastCommit.Timestamp
|
|
|
|
|
2015-08-22 03:32:22 +00:00
|
|
|
hook.Commit.Author = &common.Author{}
|
|
|
|
hook.Commit.Author.Login = parsed.ObjectAttributes.LastCommit.Author.Name
|
|
|
|
hook.Commit.Author.Email = parsed.ObjectAttributes.LastCommit.Author.Email
|
|
|
|
|
|
|
|
hook.PullRequest = &common.PullRequest{}
|
|
|
|
hook.PullRequest.Number = parsed.ObjectAttributes.IId
|
2015-08-30 00:34:05 +00:00
|
|
|
hook.PullRequest.Title = parsed.ObjectAttributes.Title
|
|
|
|
hook.PullRequest.Link = parsed.ObjectAttributes.Url
|
2015-08-22 03:32:22 +00:00
|
|
|
|
|
|
|
return hook, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func push(parsed *gogitlab.HookPayload, req *http.Request) (*common.Hook, error) {
|
2015-07-26 12:32:49 +00:00
|
|
|
var cloneUrl = parsed.Repository.GitHttpUrl
|
|
|
|
|
2015-07-25 21:06:06 +00:00
|
|
|
var hook = new(common.Hook)
|
2015-07-26 12:32:49 +00:00
|
|
|
hook.Repo = &common.Repo{}
|
2015-07-25 23:22:16 +00:00
|
|
|
hook.Repo.Owner = req.FormValue("owner")
|
|
|
|
hook.Repo.Name = req.FormValue("name")
|
2015-07-26 12:32:49 +00:00
|
|
|
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{}
|
2015-07-25 23:22:16 +00:00
|
|
|
hook.Commit.Sha = parsed.After
|
|
|
|
hook.Commit.Branch = parsed.Branch()
|
2015-07-26 12:32:49 +00:00
|
|
|
hook.Commit.Ref = parsed.Ref
|
|
|
|
hook.Commit.Remote = cloneUrl
|
2015-07-25 23:22:16 +00:00
|
|
|
|
|
|
|
var head = parsed.Head()
|
|
|
|
hook.Commit.Message = head.Message
|
|
|
|
hook.Commit.Timestamp = head.Timestamp
|
2015-07-26 12:32:49 +00:00
|
|
|
hook.Commit.Author = &common.Author{}
|
2015-07-25 23:22:16 +00:00
|
|
|
|
|
|
|
// extracts the commit author (ideally email)
|
|
|
|
// from the post-commit hook
|
|
|
|
switch {
|
|
|
|
case head.Author != nil:
|
|
|
|
hook.Commit.Author.Email = head.Author.Email
|
2015-07-26 12:32:49 +00:00
|
|
|
hook.Commit.Author.Login = parsed.UserName
|
2015-07-25 23:22:16 +00:00
|
|
|
case head.Author == nil:
|
|
|
|
hook.Commit.Author.Login = parsed.UserName
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hook, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ¯\_(ツ)_/¯
|
|
|
|
func (g *Gitlab) Oauth2Transport(r *http.Request) *oauth2.Transport {
|
|
|
|
return &oauth2.Transport{
|
|
|
|
Config: &oauth2.Config{
|
|
|
|
ClientId: g.Client,
|
|
|
|
ClientSecret: g.Secret,
|
|
|
|
Scope: DefaultScope,
|
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", g.URL),
|
2015-07-26 12:32:49 +00:00
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", g.URL),
|
2015-07-25 08:49:39 +00:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", httputil.GetURL(r)),
|
|
|
|
//settings.Server.Scheme, settings.Server.Hostname),
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: g.SkipVerify},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessor method, to allowed remote organizations field.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) GetOrgs() []string {
|
|
|
|
return g.AllowedOrgs
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accessor method, to open field.
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) GetOpen() bool {
|
|
|
|
return g.Open
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return default scope for GitHub
|
2015-09-02 03:42:18 +00:00
|
|
|
func (g *Gitlab) Scope() string {
|
2015-07-25 08:49:39 +00:00
|
|
|
return DefaultScope
|
|
|
|
}
|