2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2021-09-28 12:56:59 +02:00
|
|
|
"context"
|
2015-07-25 10:49:39 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-05-02 01:30:00 +02:00
|
|
|
"net"
|
2015-07-25 10:49:39 +02:00
|
|
|
"net/http"
|
2015-07-27 01:52:18 +02:00
|
|
|
"net/url"
|
2015-07-25 10:49:39 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/oauth2"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/gitlab/client"
|
2015-07-25 10:49:39 +02:00
|
|
|
)
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
const DefaultScope = "api"
|
|
|
|
|
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
|
|
|
URL string // Gogs server url.
|
|
|
|
Client string // Oauth2 client id.
|
|
|
|
Secret string // Oauth2 client secret.
|
|
|
|
Username string // Optional machine account username.
|
|
|
|
Password string // Optional machine account password.
|
|
|
|
PrivateMode bool // Gogs is running in private mode.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a Remote implementation that integrates with Gitlab, an open
|
|
|
|
// source Git service. See https://gitlab.com
|
|
|
|
func New(opts Opts) (remote.Remote, error) {
|
2021-09-24 16:29:26 +02:00
|
|
|
u, err := url.Parse(opts.URL)
|
2016-05-02 01:30:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-24 16:29:26 +02:00
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
2016-05-02 01:30:00 +02:00
|
|
|
if err == nil {
|
2021-09-24 16:29:26 +02:00
|
|
|
u.Host = host
|
2016-05-02 01:30:00 +02:00
|
|
|
}
|
|
|
|
return &Gitlab{
|
|
|
|
URL: opts.URL,
|
|
|
|
Client: opts.Client,
|
|
|
|
Secret: opts.Secret,
|
2021-09-24 16:29:26 +02:00
|
|
|
Machine: u.Host,
|
2016-05-02 01:30:00 +02:00
|
|
|
Username: opts.Username,
|
|
|
|
Password: opts.Password,
|
|
|
|
PrivateMode: opts.PrivateMode,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
|
|
|
}, nil
|
|
|
|
}
|
2015-07-25 10:49:39 +02:00
|
|
|
|
|
|
|
type Gitlab struct {
|
2016-03-22 12:34:33 +02:00
|
|
|
URL string
|
|
|
|
Client string
|
|
|
|
Secret string
|
2016-05-02 01:30:00 +02:00
|
|
|
Machine string
|
|
|
|
Username string
|
|
|
|
Password string
|
2016-03-22 12:34:33 +02:00
|
|
|
PrivateMode bool
|
|
|
|
SkipVerify bool
|
|
|
|
HideArchives bool
|
|
|
|
Search bool
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 22:08:17 +02:00
|
|
|
func Load(config string) *Gitlab {
|
2015-08-07 00:53:39 +02:00
|
|
|
url_, err := url.Parse(config)
|
2015-07-25 10:49:39 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
panic(err)
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
2015-08-07 00:53:39 +02:00
|
|
|
params := url_.Query()
|
|
|
|
url_.RawQuery = ""
|
2015-07-25 10:49:39 +02:00
|
|
|
|
2015-08-07 00:53:39 +02:00
|
|
|
gitlab := Gitlab{}
|
|
|
|
gitlab.URL = url_.String()
|
2015-08-09 05:51:12 +02:00
|
|
|
gitlab.Client = params.Get("client_id")
|
|
|
|
gitlab.Secret = params.Get("client_secret")
|
2016-05-02 01:30:00 +02:00
|
|
|
// gitlab.AllowedOrgs = params["orgs"]
|
2015-08-07 00:53:39 +02:00
|
|
|
gitlab.SkipVerify, _ = strconv.ParseBool(params.Get("skip_verify"))
|
2016-02-16 13:35:20 +02:00
|
|
|
gitlab.HideArchives, _ = strconv.ParseBool(params.Get("hide_archives"))
|
2016-05-02 01:30:00 +02:00
|
|
|
// gitlab.Open, _ = strconv.ParseBool(params.Get("open"))
|
2015-08-07 00:53:39 +02:00
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// switch params.Get("clone_mode") {
|
|
|
|
// case "oauth":
|
|
|
|
// gitlab.CloneMode = "oauth"
|
|
|
|
// default:
|
|
|
|
// gitlab.CloneMode = "token"
|
|
|
|
// }
|
2015-09-02 05:42:18 +02:00
|
|
|
|
2015-08-07 00:53:39 +02:00
|
|
|
// this is a temp workaround
|
|
|
|
gitlab.Search, _ = strconv.ParseBool(params.Get("search"))
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
return &gitlab
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// Login authenticates the session and returns the
|
|
|
|
// remote user details.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Login(ctx context.Context, res http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
var config = &oauth2.Config{
|
|
|
|
ClientId: g.Client,
|
|
|
|
ClientSecret: g.Secret,
|
|
|
|
Scope: DefaultScope,
|
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", g.URL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", g.URL),
|
2021-08-20 16:32:52 +02:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.Host),
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
trans_ := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: g.SkipVerify},
|
|
|
|
}
|
|
|
|
|
2016-12-19 07:42:56 +02:00
|
|
|
// get the OAuth errors
|
|
|
|
if err := req.FormValue("error"); err != "" {
|
2016-12-19 18:22:11 +02:00
|
|
|
return nil, &remote.AuthError{
|
|
|
|
Err: err,
|
|
|
|
Description: req.FormValue("error_description"),
|
|
|
|
URI: req.FormValue("error_uri"),
|
2016-12-19 07:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// get the OAuth code
|
|
|
|
var code = req.FormValue("code")
|
|
|
|
if len(code) == 0 {
|
|
|
|
http.Redirect(res, req, config.AuthCodeURL("drone"), http.StatusSeeOther)
|
2016-05-02 01:30:00 +02:00
|
|
|
return nil, nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var trans = &oauth2.Transport{Config: config, Transport: trans_}
|
|
|
|
var token_, err = trans.Exchange(code)
|
2015-07-25 10:49:39 +02:00
|
|
|
if err != nil {
|
2016-05-02 01:30:00 +02:00
|
|
|
return nil, fmt.Errorf("Error exchanging token. %s", err)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
client := NewClient(g.URL, token_.AccessToken, g.SkipVerify)
|
|
|
|
login, err := client.CurrentUser()
|
|
|
|
if err != nil {
|
2016-05-02 01:30:00 +02:00
|
|
|
return nil, err
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
2016-02-14 02:38:31 +02:00
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// if len(g.AllowedOrgs) != 0 {
|
|
|
|
// groups, err := client.AllGroups()
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("Could not check org membership. %s", err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var member bool
|
|
|
|
// for _, group := range groups {
|
|
|
|
// for _, allowedOrg := range g.AllowedOrgs {
|
|
|
|
// if group.Path == allowedOrg {
|
|
|
|
// member = true
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if !member {
|
|
|
|
// return nil, false, fmt.Errorf("User does not belong to correct group. Must belong to %v", g.AllowedOrgs)
|
|
|
|
// }
|
|
|
|
// }
|
2016-02-14 02:38:31 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
user := &model.User{}
|
2015-07-25 10:49:39 +02:00
|
|
|
user.Login = login.Username
|
|
|
|
user.Email = login.Email
|
2015-09-30 03:21:17 +02:00
|
|
|
user.Token = token_.AccessToken
|
|
|
|
user.Secret = token_.RefreshToken
|
2015-08-10 09:15:33 +02:00
|
|
|
|
|
|
|
if strings.HasPrefix(login.AvatarUrl, "http") {
|
|
|
|
user.Avatar = login.AvatarUrl
|
|
|
|
} else {
|
2015-09-02 05:42:18 +02:00
|
|
|
user.Avatar = g.URL + "/" + login.AvatarUrl
|
2015-08-10 09:15:33 +02:00
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
return user, nil
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Auth(ctx context.Context, token, secret string) (string, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
client := NewClient(g.URL, token, g.SkipVerify)
|
|
|
|
login, err := client.CurrentUser()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return login.Username, nil
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
2016-05-02 01:30:00 +02:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
groups, err := client.AllGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var teams []*model.Team
|
|
|
|
for _, group := range groups {
|
|
|
|
teams = append(teams, &model.Team{
|
|
|
|
Login: group.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
// Repo fetches the named repository from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Repo(ctx context.Context, u *model.User, owner, name string) (*model.Repo, error) {
|
2015-09-02 05:42:18 +02:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, owner, name)
|
2015-07-30 02:46:22 +02:00
|
|
|
if err != nil {
|
2015-07-28 08:38:15 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-25 10:49:39 +02:00
|
|
|
repo_, err := client.Project(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := &model.Repo{}
|
2015-07-25 10:49:39 +02:00
|
|
|
repo.Owner = owner
|
|
|
|
repo.Name = name
|
|
|
|
repo.FullName = repo_.PathWithNamespace
|
|
|
|
repo.Link = repo_.Url
|
|
|
|
repo.Clone = repo_.HttpRepoUrl
|
|
|
|
repo.Branch = "master"
|
|
|
|
|
2016-02-01 01:22:44 +02:00
|
|
|
repo.Avatar = repo_.AvatarUrl
|
|
|
|
|
|
|
|
if len(repo.Avatar) != 0 && !strings.HasPrefix(repo.Avatar, "http") {
|
|
|
|
repo.Avatar = fmt.Sprintf("%s/%s", g.URL, repo.Avatar)
|
|
|
|
}
|
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
if repo_.DefaultBranch != "" {
|
|
|
|
repo.Branch = repo_.DefaultBranch
|
|
|
|
}
|
|
|
|
|
2015-09-02 05:42:18 +02:00
|
|
|
if g.PrivateMode {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.IsPrivate = true
|
2015-07-27 00:29:51 +02:00
|
|
|
} else {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.IsPrivate = !repo_.Public
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
2015-07-27 00:29:51 +02:00
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// Repos fetches a list of repos from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
var repos = []*model.Repo{}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-02-16 13:35:20 +02:00
|
|
|
all, err := client.AllProjects(g.HideArchives)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return repos, err
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
for _, repo_ := range all {
|
|
|
|
var parts = strings.Split(repo_.PathWithNamespace, "/")
|
2015-09-30 03:21:17 +02:00
|
|
|
var owner = parts[0]
|
|
|
|
var name = parts[1]
|
2016-02-01 00:55:59 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
repo := &model.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
|
2016-02-01 00:55:59 +02:00
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if g.PrivateMode {
|
|
|
|
repo.IsPrivate = true
|
|
|
|
} else {
|
|
|
|
repo.IsPrivate = !repo_.Public
|
|
|
|
}
|
|
|
|
|
|
|
|
repos = append(repos, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2016-02-01 00:55:59 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perm fetches the named repository from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Perm(ctx context.Context, u *model.User, owner, name string) (*model.Perm, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2015-09-02 05:42:18 +02:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, owner, name)
|
2015-07-30 02:46:22 +02:00
|
|
|
if err != nil {
|
2015-07-28 08:38:15 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
repo, err := client.Project(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-07 16:35:53 +02:00
|
|
|
|
2016-01-12 17:40:13 +02:00
|
|
|
// repo owner is granted full access
|
|
|
|
if repo.Owner != nil && repo.Owner.Username == u.Login {
|
2017-07-14 21:58:38 +02:00
|
|
|
return &model.Perm{Push: true, Pull: true, Admin: true}, nil
|
2016-01-07 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2016-01-12 17:40:13 +02:00
|
|
|
// check permission for current user
|
2015-09-30 03:21:17 +02:00
|
|
|
m := &model.Perm{}
|
2015-07-25 10:49:39 +02:00
|
|
|
m.Admin = IsAdmin(repo)
|
|
|
|
m.Pull = IsRead(repo)
|
|
|
|
m.Push = IsWrite(repo)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2016-03-22 12:34:33 +02:00
|
|
|
// File fetches a file from the remote repository and returns in string format.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
|
|
|
|
var client = NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, r.Owner, r.Name)
|
2017-03-18 13:25:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
out, err := client.RepoRawFileRef(id, b.Commit, f)
|
2017-03-18 13:25:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *Gitlab) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {
|
2019-06-03 09:16:15 +02:00
|
|
|
return nil, fmt.Errorf("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-07-25 23:06:06 +02: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.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error {
|
2015-12-12 19:09:59 +02:00
|
|
|
client := NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
|
|
|
|
status := getStatus(b.Status)
|
|
|
|
desc := getDesc(b.Status)
|
|
|
|
|
|
|
|
client.SetStatus(
|
2021-09-28 12:56:59 +02:00
|
|
|
ns(r.Owner, r.Name),
|
2015-12-12 19:09:59 +02:00
|
|
|
b.Commit,
|
|
|
|
status,
|
|
|
|
desc,
|
|
|
|
strings.Replace(b.Ref, "refs/heads/", "", -1),
|
|
|
|
link,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gitlab statuses it's a new feature, just ignore error
|
|
|
|
// if gitlab version not support this
|
2015-07-25 10:49:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Netrc returns a .netrc file that can be used to clone
|
|
|
|
// private repositories from a remote system.
|
2016-05-02 01:30:00 +02:00
|
|
|
// func (g *Gitlab) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
|
|
|
// url_, err := url.Parse(g.URL)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// netrc := &model.Netrc{}
|
|
|
|
// netrc.Machine = url_.Host
|
|
|
|
//
|
|
|
|
// switch g.CloneMode {
|
|
|
|
// case "oauth":
|
|
|
|
// netrc.Login = "oauth2"
|
|
|
|
// netrc.Password = u.Token
|
|
|
|
// case "token":
|
|
|
|
// t := token.New(token.HookToken, r.FullName)
|
|
|
|
// netrc.Login = "drone-ci-token"
|
|
|
|
// netrc.Password, err = t.Sign(r.Hash)
|
|
|
|
// }
|
|
|
|
// return netrc, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Netrc returns a netrc file capable of authenticating Gitlab requests and
|
|
|
|
// cloning Gitlab repositories. The netrc will use the global machine account
|
|
|
|
// when configured.
|
2015-09-30 03:21:17 +02:00
|
|
|
func (g *Gitlab) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
2016-05-02 01:30:00 +02:00
|
|
|
if g.Password != "" {
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: g.Username,
|
|
|
|
Password: g.Password,
|
|
|
|
Machine: g.Machine,
|
|
|
|
}, nil
|
2015-09-02 05:42:18 +02:00
|
|
|
}
|
2016-05-02 01:30:00 +02:00
|
|
|
return &model.Netrc{
|
|
|
|
Login: "oauth2",
|
|
|
|
Password: u.Token,
|
|
|
|
Machine: g.Machine,
|
|
|
|
}, nil
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Activate activates a repository by adding a Post-commit hook and
|
|
|
|
// a Public Deploy key, if applicable.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
var client = NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, r.Owner, r.Name)
|
2015-07-30 02:46:22 +02:00
|
|
|
if err != nil {
|
2015-07-28 08:38:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:32:22 +02:00
|
|
|
uri, err := url.Parse(link)
|
2015-07-25 10:49:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:32:22 +02:00
|
|
|
droneUrl := fmt.Sprintf("%s://%s", uri.Scheme, uri.Host)
|
|
|
|
droneToken := uri.Query().Get("access_token")
|
2015-09-02 05:42:18 +02:00
|
|
|
ssl_verify := strconv.FormatBool(!g.SkipVerify)
|
2015-07-25 10:49:39 +02:00
|
|
|
|
2015-08-30 00:18:45 +02:00
|
|
|
return client.AddDroneService(id, map[string]string{
|
|
|
|
"token": droneToken,
|
|
|
|
"drone_url": droneUrl,
|
|
|
|
"enable_ssl_verification": ssl_verify,
|
|
|
|
})
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate removes a repository by removing all the post-commit hooks
|
|
|
|
// which are equal to link and removing the SSH deploy key.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (g *Gitlab) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
var client = NewClient(g.URL, u.Token, g.SkipVerify)
|
|
|
|
id, err := GetProjectId(g, client, r.Owner, r.Name)
|
2015-07-30 02:46:22 +02:00
|
|
|
if err != nil {
|
2015-07-28 08:38:15 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-07-25 10:49:39 +02:00
|
|
|
|
2015-08-22 05:32:22 +02:00
|
|
|
return client.DeleteDroneService(id)
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHook parses the post-commit hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
2015-09-30 03:21:17 +02:00
|
|
|
func (g *Gitlab) Hook(req *http.Request) (*model.Repo, *model.Build, error) {
|
2015-07-26 01:22:16 +02:00
|
|
|
defer req.Body.Close()
|
2015-07-25 10:49:39 +02:00
|
|
|
var payload, _ = ioutil.ReadAll(req.Body)
|
2015-12-12 14:20:01 +02:00
|
|
|
var parsed, err = client.ParseHook(payload)
|
2015-07-26 01:22:16 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
return nil, nil, err
|
2015-07-26 01:22:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:32:22 +02:00
|
|
|
switch parsed.ObjectKind {
|
|
|
|
case "merge_request":
|
|
|
|
return mergeRequest(parsed, req)
|
2015-08-22 21:55:08 +02:00
|
|
|
case "tag_push", "push":
|
2015-08-22 05:32:22 +02:00
|
|
|
return push(parsed, req)
|
|
|
|
default:
|
2015-09-30 03:21:17 +02:00
|
|
|
return nil, nil, nil
|
2015-07-26 01:02:33 +02:00
|
|
|
}
|
2015-08-22 05:32:22 +02:00
|
|
|
}
|
2015-07-26 01:02:33 +02:00
|
|
|
|
2015-12-12 14:20:01 +02:00
|
|
|
func mergeRequest(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Build, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
repo := &model.Repo{}
|
2016-02-28 19:11:50 +02:00
|
|
|
|
|
|
|
obj := parsed.ObjectAttributes
|
|
|
|
if obj == nil {
|
|
|
|
return nil, nil, fmt.Errorf("object_attributes key expected in merge request hook")
|
|
|
|
}
|
|
|
|
|
|
|
|
target := obj.Target
|
|
|
|
source := obj.Source
|
|
|
|
|
|
|
|
if target == nil && source == nil {
|
|
|
|
return nil, nil, fmt.Errorf("target and source keys expected in merge request hook")
|
|
|
|
} else if target == nil {
|
|
|
|
return nil, nil, fmt.Errorf("target key expected in merge request hook")
|
|
|
|
} else if source == nil {
|
|
|
|
return nil, nil, fmt.Errorf("source key exptected in merge request hook")
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.PathWithNamespace != "" {
|
|
|
|
var err error
|
|
|
|
if repo.Owner, repo.Name, err = ExtractFromPath(target.PathWithNamespace); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
repo.FullName = target.PathWithNamespace
|
|
|
|
} else {
|
|
|
|
repo.Owner = req.FormValue("owner")
|
|
|
|
repo.Name = req.FormValue("name")
|
|
|
|
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Link = target.WebUrl
|
|
|
|
|
|
|
|
if target.GitHttpUrl != "" {
|
|
|
|
repo.Clone = target.GitHttpUrl
|
|
|
|
} else {
|
|
|
|
repo.Clone = target.HttpUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.DefaultBranch != "" {
|
|
|
|
repo.Branch = target.DefaultBranch
|
|
|
|
} else {
|
|
|
|
repo.Branch = "master"
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.AvatarUrl != "" {
|
|
|
|
repo.Avatar = target.AvatarUrl
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
build := &model.Build{}
|
|
|
|
build.Event = "pull_request"
|
2016-02-28 19:11:50 +02:00
|
|
|
|
|
|
|
lastCommit := obj.LastCommit
|
|
|
|
if lastCommit == nil {
|
|
|
|
return nil, nil, fmt.Errorf("last_commit key expected in merge request hook")
|
|
|
|
}
|
|
|
|
|
|
|
|
build.Message = lastCommit.Message
|
|
|
|
build.Commit = lastCommit.Id
|
2015-09-30 03:21:17 +02:00
|
|
|
//build.Remote = parsed.ObjectAttributes.Source.HttpUrl
|
2015-08-22 05:32:22 +02:00
|
|
|
|
2017-02-08 08:43:55 +02:00
|
|
|
build.Ref = fmt.Sprintf("refs/merge-requests/%d/head", obj.IId)
|
2015-07-25 10:49:39 +02:00
|
|
|
|
2016-02-28 19:11:50 +02:00
|
|
|
build.Branch = obj.SourceBranch
|
|
|
|
|
|
|
|
author := lastCommit.Author
|
|
|
|
if author == nil {
|
|
|
|
return nil, nil, fmt.Errorf("author key expected in merge request hook")
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 19:11:50 +02:00
|
|
|
build.Author = author.Name
|
|
|
|
build.Email = author.Email
|
2015-08-30 02:34:05 +02:00
|
|
|
|
2016-02-01 01:49:52 +02:00
|
|
|
if len(build.Email) != 0 {
|
|
|
|
build.Avatar = GetUserAvatar(build.Email)
|
|
|
|
}
|
|
|
|
|
2016-02-28 19:11:50 +02:00
|
|
|
build.Title = obj.Title
|
|
|
|
build.Link = obj.Url
|
2015-08-22 05:32:22 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
return repo, build, nil
|
2015-08-22 05:32:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:20:01 +02:00
|
|
|
func push(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Build, error) {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := &model.Repo{}
|
2015-07-26 14:32:49 +02:00
|
|
|
|
2016-02-22 21:35:53 +02:00
|
|
|
// Since gitlab 8.5, used project instead repository key
|
|
|
|
// see https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md#web-hooks
|
|
|
|
if project := parsed.Project; project != nil {
|
2016-02-22 22:43:54 +02:00
|
|
|
var err error
|
|
|
|
if repo.Owner, repo.Name, err = ExtractFromPath(project.PathWithNamespace); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-07-26 14:32:49 +02:00
|
|
|
|
2016-02-22 21:35:53 +02:00
|
|
|
repo.Avatar = project.AvatarUrl
|
|
|
|
repo.Link = project.WebUrl
|
|
|
|
repo.Clone = project.GitHttpUrl
|
|
|
|
repo.FullName = project.PathWithNamespace
|
|
|
|
repo.Branch = project.DefaultBranch
|
|
|
|
|
|
|
|
switch project.VisibilityLevel {
|
|
|
|
case 0:
|
|
|
|
repo.IsPrivate = true
|
|
|
|
case 10:
|
|
|
|
repo.IsPrivate = true
|
|
|
|
case 20:
|
|
|
|
repo.IsPrivate = false
|
|
|
|
}
|
|
|
|
} else if repository := parsed.Repository; repository != nil {
|
2016-02-22 22:43:54 +02:00
|
|
|
repo.Owner = req.FormValue("owner")
|
|
|
|
repo.Name = req.FormValue("name")
|
2016-02-22 21:35:53 +02:00
|
|
|
repo.Link = repository.URL
|
|
|
|
repo.Clone = repository.GitHttpUrl
|
|
|
|
repo.Branch = "master"
|
|
|
|
repo.FullName = fmt.Sprintf("%s/%s", req.FormValue("owner"), req.FormValue("name"))
|
|
|
|
|
|
|
|
switch repository.VisibilityLevel {
|
|
|
|
case 0:
|
|
|
|
repo.IsPrivate = true
|
|
|
|
case 10:
|
|
|
|
repo.IsPrivate = true
|
|
|
|
case 20:
|
|
|
|
repo.IsPrivate = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, nil, fmt.Errorf("No project/repository keys given")
|
2015-07-26 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
build := &model.Build{}
|
|
|
|
build.Event = model.EventPush
|
|
|
|
build.Commit = parsed.After
|
|
|
|
build.Branch = parsed.Branch()
|
|
|
|
build.Ref = parsed.Ref
|
|
|
|
// hook.Commit.Remote = cloneUrl
|
2015-07-26 01:22:16 +02:00
|
|
|
|
|
|
|
var head = parsed.Head()
|
2015-09-30 03:21:17 +02:00
|
|
|
build.Message = head.Message
|
|
|
|
// build.Timestamp = head.Timestamp
|
2015-07-26 01:22:16 +02:00
|
|
|
|
|
|
|
// extracts the commit author (ideally email)
|
|
|
|
// from the post-commit hook
|
|
|
|
switch {
|
|
|
|
case head.Author != nil:
|
2015-09-30 03:21:17 +02:00
|
|
|
build.Email = head.Author.Email
|
|
|
|
build.Author = parsed.UserName
|
2016-02-01 01:49:52 +02:00
|
|
|
if len(build.Email) != 0 {
|
|
|
|
build.Avatar = GetUserAvatar(build.Email)
|
|
|
|
}
|
2015-07-26 01:22:16 +02:00
|
|
|
case head.Author == nil:
|
2015-09-30 03:21:17 +02:00
|
|
|
build.Author = parsed.UserName
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 05:31:42 +02:00
|
|
|
if strings.HasPrefix(build.Ref, "refs/tags/") {
|
|
|
|
build.Event = model.EventTag
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
return repo, build, nil
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ¯\_(ツ)_/¯
|
|
|
|
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 14:32:49 +02:00
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", g.URL),
|
2021-08-20 16:32:52 +02:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.Host),
|
2015-07-25 10:49:39 +02:00
|
|
|
//settings.Server.Scheme, settings.Server.Hostname),
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: g.SkipVerify},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 19:09:59 +02:00
|
|
|
const (
|
|
|
|
StatusPending = "pending"
|
|
|
|
StatusRunning = "running"
|
|
|
|
StatusSuccess = "success"
|
|
|
|
StatusFailure = "failed"
|
|
|
|
StatusCanceled = "canceled"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-18 10:49:27 +02:00
|
|
|
DescPending = "the build is pending"
|
|
|
|
DescRunning = "the buils is running"
|
2015-12-12 19:09:59 +02:00
|
|
|
DescSuccess = "the build was successful"
|
|
|
|
DescFailure = "the build failed"
|
|
|
|
DescCanceled = "the build canceled"
|
2017-03-18 10:49:27 +02:00
|
|
|
DescBlocked = "the build is pending approval"
|
|
|
|
DescDeclined = "the build was rejected"
|
2015-12-12 19:09:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// getStatus is a helper functin that converts a Drone
|
|
|
|
// status to a GitHub status.
|
|
|
|
func getStatus(status string) string {
|
|
|
|
switch status {
|
2017-03-18 10:49:27 +02:00
|
|
|
case model.StatusPending, model.StatusBlocked:
|
2015-12-12 19:09:59 +02:00
|
|
|
return StatusPending
|
|
|
|
case model.StatusRunning:
|
|
|
|
return StatusRunning
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return StatusSuccess
|
|
|
|
case model.StatusFailure, model.StatusError:
|
|
|
|
return StatusFailure
|
|
|
|
case model.StatusKilled:
|
|
|
|
return StatusCanceled
|
|
|
|
default:
|
|
|
|
return StatusFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDesc is a helper function that generates a description
|
|
|
|
// message for the build based on the status.
|
|
|
|
func getDesc(status string) string {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending:
|
|
|
|
return DescPending
|
|
|
|
case model.StatusRunning:
|
|
|
|
return DescRunning
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return DescSuccess
|
|
|
|
case model.StatusFailure, model.StatusError:
|
|
|
|
return DescFailure
|
|
|
|
case model.StatusKilled:
|
|
|
|
return DescCanceled
|
2017-03-18 10:49:27 +02:00
|
|
|
case model.StatusBlocked:
|
|
|
|
return DescBlocked
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return DescDeclined
|
2015-12-12 19:09:59 +02:00
|
|
|
default:
|
|
|
|
return DescFailure
|
|
|
|
}
|
|
|
|
}
|