2014-06-04 21:25:38 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2014-09-02 07:18:17 +00:00
|
|
|
"io/ioutil"
|
2014-06-04 21:25:38 +00:00
|
|
|
"net/http"
|
2014-06-09 22:47:35 +00:00
|
|
|
"net/url"
|
2014-09-05 17:37:29 +00:00
|
|
|
"strconv"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
"github.com/Bugagazavr/go-gitlab-client"
|
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Gitlab struct {
|
2014-11-26 22:21:52 +00:00
|
|
|
url string
|
|
|
|
SkipVerify bool
|
2015-01-12 22:59:06 +00:00
|
|
|
Open bool
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 22:59:06 +00:00
|
|
|
func New(url string, skipVerify, open bool) *Gitlab {
|
2014-11-26 22:21:52 +00:00
|
|
|
return &Gitlab{
|
|
|
|
url: url,
|
|
|
|
SkipVerify: skipVerify,
|
2015-01-12 22:59:06 +00:00
|
|
|
Open: open,
|
2014-11-26 22:21:52 +00:00
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// Authorize handles authentication with thrid party remote systems,
|
|
|
|
// such as github or bitbucket, and returns user data.
|
|
|
|
func (r *Gitlab) Authorize(res http.ResponseWriter, req *http.Request) (*model.Login, error) {
|
|
|
|
var username = req.FormValue("username")
|
|
|
|
var password = req.FormValue("password")
|
|
|
|
|
2014-11-26 22:21:52 +00:00
|
|
|
var client = NewClient(r.url, "", r.SkipVerify)
|
2014-09-02 07:18:17 +00:00
|
|
|
var session, err = client.GetSession(username, password)
|
2014-06-09 22:47:35 +00:00
|
|
|
if err != nil {
|
2014-09-02 07:18:17 +00:00
|
|
|
return nil, err
|
2014-06-09 22:47:35 +00:00
|
|
|
}
|
2014-09-02 07:18:17 +00:00
|
|
|
|
|
|
|
var login = new(model.Login)
|
|
|
|
login.ID = int64(session.Id)
|
|
|
|
login.Access = session.PrivateToken
|
|
|
|
login.Login = session.UserName
|
|
|
|
login.Name = session.Name
|
|
|
|
login.Email = session.Email
|
|
|
|
return login, nil
|
2014-06-09 22:47:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// GetKind returns the identifier of this remote GitHub instane.
|
|
|
|
func (r *Gitlab) GetKind() string {
|
|
|
|
return model.RemoteGitlab
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHost returns the hostname of this remote GitHub instance.
|
|
|
|
func (r *Gitlab) GetHost() string {
|
|
|
|
uri, _ := url.Parse(r.url)
|
|
|
|
return uri.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepos fetches all repositories that the specified
|
|
|
|
// user has access to in the remote system.
|
|
|
|
func (r *Gitlab) GetRepos(user *model.User) ([]*model.Repo, error) {
|
|
|
|
|
|
|
|
var repos []*model.Repo
|
2014-11-26 22:21:52 +00:00
|
|
|
var client = NewClient(r.url, user.Access, r.SkipVerify)
|
2014-09-02 07:18:17 +00:00
|
|
|
var list, err = client.AllProjects()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var remote = r.GetKind()
|
|
|
|
var hostname = r.GetHost()
|
|
|
|
|
|
|
|
for _, item := range list {
|
|
|
|
var repo = model.Repo{
|
|
|
|
UserID: user.ID,
|
|
|
|
Remote: remote,
|
|
|
|
Host: hostname,
|
|
|
|
Owner: item.Namespace.Path,
|
|
|
|
Name: item.Path,
|
|
|
|
Private: !item.Public,
|
|
|
|
CloneURL: item.HttpRepoUrl,
|
|
|
|
GitURL: item.HttpRepoUrl,
|
|
|
|
SSHURL: item.SshRepoUrl,
|
2014-10-27 11:32:44 +00:00
|
|
|
URL: item.Url,
|
2014-09-02 07:18:17 +00:00
|
|
|
Role: &model.Perm{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if repo.Private {
|
|
|
|
repo.CloneURL = repo.SSHURL
|
|
|
|
}
|
|
|
|
|
2014-09-03 08:23:45 +00:00
|
|
|
// if the user is the owner we can assume full access,
|
|
|
|
// otherwise check for the permission items.
|
|
|
|
if repo.Owner == user.Login {
|
|
|
|
repo.Role = new(model.Perm)
|
|
|
|
repo.Role.Admin = true
|
|
|
|
repo.Role.Write = true
|
|
|
|
repo.Role.Read = true
|
|
|
|
} else {
|
2014-09-06 19:07:47 +00:00
|
|
|
// Fetch current project
|
|
|
|
project, err := client.Project(strconv.Itoa(item.Id))
|
|
|
|
if err != nil || project.Permissions == nil {
|
|
|
|
continue
|
|
|
|
}
|
2014-09-05 17:37:29 +00:00
|
|
|
repo.Role.Admin = IsAdmin(project)
|
|
|
|
repo.Role.Write = IsWrite(project)
|
|
|
|
repo.Role.Read = IsRead(project)
|
2014-09-03 08:23:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
repos = append(repos, &repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repos, err
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// GetScript fetches the build script (.drone.yml) from the remote
|
|
|
|
// repository and returns in string format.
|
|
|
|
func (r *Gitlab) GetScript(user *model.User, repo *model.Repo, hook *model.Hook) ([]byte, error) {
|
2014-11-26 22:21:52 +00:00
|
|
|
var client = NewClient(r.url, user.Access, r.SkipVerify)
|
2014-09-02 07:18:17 +00:00
|
|
|
var path = ns(repo.Owner, repo.Name)
|
|
|
|
return client.RepoRawFile(path, hook.Sha, ".drone.yml")
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// Activate activates a repository by adding a Post-commit hook and
|
|
|
|
// a Public Deploy key, if applicable.
|
|
|
|
func (r *Gitlab) Activate(user *model.User, repo *model.Repo, link string) error {
|
2014-11-26 22:21:52 +00:00
|
|
|
var client = NewClient(r.url, user.Access, r.SkipVerify)
|
2014-09-02 07:18:17 +00:00
|
|
|
var path = ns(repo.Owner, repo.Name)
|
|
|
|
var title, err = GetKeyTitle(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the repository is private we'll need
|
|
|
|
// to upload a github key to the repository
|
|
|
|
if repo.Private {
|
|
|
|
var err = client.AddProjectDeployKey(path, title, repo.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the repo owner / name to the hook url since gitlab
|
|
|
|
// doesn't send this detail in the post-commit hook
|
2014-10-22 07:41:25 +00:00
|
|
|
link += "?owner=" + repo.Owner + "&name=" + repo.Name
|
2014-09-02 07:18:17 +00:00
|
|
|
|
|
|
|
// add the hook
|
|
|
|
return client.AddProjectHook(path, link, true, false, true)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// ParseHook parses the post-commit hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
|
|
|
func (r *Gitlab) ParseHook(req *http.Request) (*model.Hook, error) {
|
|
|
|
|
|
|
|
defer req.Body.Close()
|
|
|
|
var payload, _ = ioutil.ReadAll(req.Body)
|
|
|
|
var parsed, err = gogitlab.ParseHook(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:08:56 +00:00
|
|
|
if len(parsed.After) == 0 || parsed.TotalCommitsCount == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
if parsed.ObjectKind == "merge_request" {
|
|
|
|
// TODO (bradrydzewski) figure out how to handle merge requests
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parsed.After) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var hook = new(model.Hook)
|
|
|
|
hook.Owner = req.FormValue("owner")
|
|
|
|
hook.Repo = req.FormValue("name")
|
|
|
|
hook.Sha = parsed.After
|
|
|
|
hook.Branch = parsed.Branch()
|
|
|
|
|
|
|
|
var head = parsed.Head()
|
|
|
|
hook.Message = head.Message
|
|
|
|
hook.Timestamp = head.Timestamp
|
|
|
|
|
|
|
|
// extracts the commit author (ideally email)
|
|
|
|
// from the post-commit hook
|
|
|
|
switch {
|
|
|
|
case head.Author != nil:
|
|
|
|
hook.Author = head.Author.Email
|
|
|
|
case head.Author == nil:
|
|
|
|
hook.Author = parsed.UserName
|
|
|
|
}
|
|
|
|
|
|
|
|
return hook, nil
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2015-01-12 22:59:06 +00:00
|
|
|
|
|
|
|
func (r *Gitlab) OpenRegistration() bool {
|
|
|
|
return r.Open
|
|
|
|
}
|