2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 08:49:39 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2015-07-25 08:49:39 +00:00
|
|
|
"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
|
|
|
"strings"
|
|
|
|
|
2022-01-03 15:35:46 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
|
2021-08-20 14:32:52 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-12-28 16:02:49 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/common"
|
2022-01-17 22:46:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2021-05-25 12:08:27 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/oauth2"
|
2022-01-17 22:46:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2015-07-25 08:49:39 +00:00
|
|
|
)
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
const (
|
2021-12-28 16:02:49 +00:00
|
|
|
defaultScope = "api"
|
|
|
|
perPage = 100
|
2021-10-03 12:42:47 +00:00
|
|
|
)
|
2016-05-01 23:30:00 +00:00
|
|
|
|
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
2021-10-03 12:42:47 +00:00
|
|
|
URL string // Gitlab server url.
|
|
|
|
ClientID string // Oauth2 client id.
|
|
|
|
ClientSecret string // Oauth2 client secret.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gitlab implements "Remote" interface
|
|
|
|
type Gitlab struct {
|
|
|
|
URL string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
SkipVerify bool
|
|
|
|
HideArchives bool
|
|
|
|
Search bool
|
2016-05-01 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return &Gitlab{
|
2021-10-03 12:42:47 +00:00
|
|
|
URL: opts.URL,
|
|
|
|
ClientID: opts.ClientID,
|
|
|
|
ClientSecret: opts.ClientSecret,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
2016-05-01 23:30:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// Login authenticates the session and returns the
|
|
|
|
// remote user details.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (g *Gitlab) Login(ctx context.Context, res http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2022-01-05 20:50:23 +00:00
|
|
|
config := &oauth2.Config{
|
2021-12-01 13:22:06 +00:00
|
|
|
ClientID: g.ClientID,
|
2021-10-03 12:42:47 +00:00
|
|
|
ClientSecret: g.ClientSecret,
|
|
|
|
Scope: defaultScope,
|
2015-09-30 01:21:17 +00:00
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", g.URL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", g.URL),
|
2021-12-13 19:22:09 +00:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 05:42:56 +00:00
|
|
|
// get the OAuth errors
|
|
|
|
if err := req.FormValue("error"); err != "" {
|
2016-12-19 16:22:11 +00:00
|
|
|
return nil, &remote.AuthError{
|
|
|
|
Err: err,
|
|
|
|
Description: req.FormValue("error_description"),
|
|
|
|
URI: req.FormValue("error_uri"),
|
2016-12-19 05:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// get the OAuth code
|
2022-01-05 20:50:23 +00:00
|
|
|
code := req.FormValue("code")
|
2015-09-30 01:21:17 +00:00
|
|
|
if len(code) == 0 {
|
2022-01-09 22:27:30 +00:00
|
|
|
authCodeURL, err := config.AuthCodeURL("drone")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("authCodeURL error: %v", err)
|
|
|
|
}
|
|
|
|
http.Redirect(res, req, authCodeURL, http.StatusSeeOther)
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 20:50:23 +00:00
|
|
|
trans := &oauth2.Transport{Config: config, Transport: &http.Transport{
|
2021-10-03 12:42:47 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: g.SkipVerify},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}}
|
2022-01-05 20:50:23 +00:00
|
|
|
token, err := trans.Exchange(code)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, fmt.Errorf("Error exchanging token. %s", err)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
client, err := newClient(g.URL, token.AccessToken, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
login, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
2016-02-14 00:38:31 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
user := &model.User{
|
|
|
|
Login: login.Username,
|
|
|
|
Email: login.Email,
|
|
|
|
Avatar: login.AvatarURL,
|
2021-12-01 13:22:06 +00:00
|
|
|
Token: token.AccessToken,
|
|
|
|
Secret: token.RefreshToken,
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(user.Avatar, "http") {
|
|
|
|
user.Avatar = g.URL + "/" + login.AvatarURL
|
2015-08-10 07:15:33 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return user, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Auth authenticates the session and returns the remote user login for the given token
|
|
|
|
func (g *Gitlab) Auth(ctx context.Context, token, _ string) (string, error) {
|
|
|
|
client, err := newClient(g.URL, token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
login, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return login.Username, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Teams fetches a list of team memberships from the remote system.
|
|
|
|
func (g *Gitlab) Teams(ctx context.Context, user *model.User) ([]*model.Team, error) {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
teams := make([]*model.Team, 0, perPage)
|
|
|
|
|
|
|
|
for i := 1; true; i++ {
|
|
|
|
batch, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{Page: i, PerPage: perPage},
|
|
|
|
AllAvailable: gitlab.Bool(false),
|
2021-10-08 16:35:56 +00:00
|
|
|
MinAccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions), // TODO: check what's best here
|
2021-10-03 12:42:47 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range batch {
|
|
|
|
teams = append(teams, &model.Team{
|
|
|
|
Login: batch[i].Name,
|
|
|
|
Avatar: batch[i].AvatarURL,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// getProject fetches the named repository from the remote system.
|
|
|
|
func (g *Gitlab) getProject(ctx context.Context, client *gitlab.Client, owner, name string) (*gitlab.Project, error) {
|
|
|
|
repo, _, err := client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, name), nil, gitlab.WithContext(ctx))
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-31 23:22:44 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return repo, nil
|
|
|
|
}
|
2016-01-31 23:22:44 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Repo fetches the named repository from the remote system.
|
|
|
|
func (g *Gitlab) Repo(ctx context.Context, user *model.User, owner, name string) (*model.Repo, error) {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, owner, name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
2015-07-26 22:29:51 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
return g.convertGitlabRepo(_repo)
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// Repos fetches a list of repos from the remote system.
|
2021-10-03 12:42:47 +00:00
|
|
|
func (g *Gitlab) Repos(ctx context.Context, user *model.User) ([]*model.Repo, error) {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2021-10-03 12:42:47 +00:00
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
repos := make([]*model.Repo, 0, perPage)
|
|
|
|
opts := &gitlab.ListProjectsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{PerPage: perPage},
|
2021-10-08 16:35:56 +00:00
|
|
|
MinAccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions), // TODO: check what's best here
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
if g.HideArchives {
|
|
|
|
opts.Archived = gitlab.Bool(false)
|
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
for i := 1; true; i++ {
|
|
|
|
opts.Page = i
|
|
|
|
batch, _, err := client.Projects.ListProjects(opts, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-31 22:55:59 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
for i := range batch {
|
|
|
|
repo, err := g.convertGitlabRepo(batch[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-03 15:35:46 +00:00
|
|
|
|
|
|
|
// TODO(648) remove when woodpecker understands nested repos
|
|
|
|
if strings.Count(repo.FullName, "/") > 1 {
|
|
|
|
log.Debug().Msgf("Skipping nested repository %s for user %s, because they are not supported, yet (see #648).", repo.FullName, user.Login)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
repos = append(repos, repo)
|
2017-07-14 19:58:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2016-01-31 22:55:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perm fetches the named repository from the remote system.
|
2022-01-03 14:33:36 +00:00
|
|
|
func (g *Gitlab) Perm(ctx context.Context, user *model.User, r *model.Repo) (*model.Perm, error) {
|
2021-10-03 12:42:47 +00:00
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-03 14:33:36 +00:00
|
|
|
repo, err := g.getProject(ctx, client, r.Owner, r.Name)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-07 14:35:53 +00:00
|
|
|
|
2016-01-12 15:40:13 +00:00
|
|
|
// repo owner is granted full access
|
2021-10-03 12:42:47 +00:00
|
|
|
if repo.Owner != nil && repo.Owner.Username == user.Login {
|
2017-07-14 19:58:38 +00:00
|
|
|
return &model.Perm{Push: true, Pull: true, Admin: true}, nil
|
2016-01-07 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// return permission for current user
|
|
|
|
return &model.Perm{
|
|
|
|
Pull: isRead(repo),
|
|
|
|
Push: isWrite(repo),
|
|
|
|
Admin: isAdmin(repo),
|
|
|
|
}, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 10:34:33 +00:00
|
|
|
// File fetches a file from the remote repository and returns in string format.
|
2021-10-03 12:42:47 +00:00
|
|
|
func (g *Gitlab) File(ctx context.Context, user *model.User, repo *model.Repo, build *model.Build, fileName string) ([]byte, error) {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
file, _, err := client.RepositoryFiles.GetRawFile(_repo.ID, fileName, &gitlab.GetRawFileOptions{Ref: &build.Commit}, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
return file, err
|
2017-03-18 11:25:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Dir fetches a folder from the remote repository
|
|
|
|
func (g *Gitlab) Dir(ctx context.Context, user *model.User, repo *model.Repo, build *model.Build, path string) ([]*remote.FileMeta, error) {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
files := make([]*remote.FileMeta, 0, perPage)
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts := &gitlab.ListTreeOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{PerPage: perPage},
|
|
|
|
Path: &path,
|
|
|
|
Ref: &build.Commit,
|
|
|
|
Recursive: gitlab.Bool(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; true; i++ {
|
|
|
|
opts.Page = 1
|
2021-12-01 13:22:06 +00:00
|
|
|
batch, _, err := client.Repositories.ListTree(_repo.ID, opts, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range batch {
|
|
|
|
if batch[i].Type != "blob" { // no file
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := g.File(ctx, user, repo, build, batch[i].Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files = append(files, &remote.FileMeta{
|
|
|
|
Name: batch[i].Path,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 07:16:15 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return files, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Status sends the commit status back to gitlab.
|
2021-12-28 16:02:49 +00:00
|
|
|
func (g *Gitlab) Status(ctx context.Context, user *model.User, repo *model.Repo, build *model.Build, proc *model.Proc) error {
|
2021-10-03 12:42:47 +00:00
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_, _, err = client.Commits.SetCommitStatus(_repo.ID, build.Commit, &gitlab.SetCommitStatusOptions{
|
2021-12-28 16:02:49 +00:00
|
|
|
State: getStatus(proc.State),
|
|
|
|
Description: gitlab.String(common.GetBuildStatusDescription(proc.State)),
|
|
|
|
TargetURL: gitlab.String(common.GetBuildStatusLink(repo, build, proc)),
|
|
|
|
Context: gitlab.String(common.GetBuildStatusContext(repo, build, proc)),
|
|
|
|
PipelineID: gitlab.Int(int(build.Number)),
|
2021-10-03 12:42:47 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
|
|
|
|
// 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 01:21:17 +00:00
|
|
|
func (g *Gitlab) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
2022-01-31 14:38:00 +00:00
|
|
|
login := ""
|
|
|
|
token := ""
|
|
|
|
|
|
|
|
if u != nil {
|
|
|
|
login = "oauth2"
|
|
|
|
token = u.Token
|
2015-09-02 03:42:18 +00:00
|
|
|
}
|
2022-01-31 14:38:00 +00:00
|
|
|
|
2022-02-26 01:54:15 +00:00
|
|
|
host, err := common.ExtractHostFromCloneURL(r.Clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return &model.Netrc{
|
2022-01-31 14:38:00 +00:00
|
|
|
Login: login,
|
|
|
|
Password: token,
|
2022-02-26 01:54:15 +00:00
|
|
|
Machine: host,
|
2016-05-01 23:30:00 +00:00
|
|
|
}, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 20:50:23 +00:00
|
|
|
func (g *Gitlab) getTokenAndWebURL(link string) (token, webURL string, err error) {
|
2021-12-19 00:12:09 +00:00
|
|
|
uri, err := url.Parse(link)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
token = uri.Query().Get("access_token")
|
|
|
|
webURL = fmt.Sprintf("%s://%s/api/hook", uri.Scheme, uri.Host)
|
|
|
|
return token, webURL, 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.
|
2021-10-03 12:42:47 +00:00
|
|
|
func (g *Gitlab) Activate(ctx context.Context, user *model.User, repo *model.Repo, link string) error {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-19 00:12:09 +00:00
|
|
|
token, webURL, err := g.getTokenAndWebURL(link)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
if len(token) == 0 {
|
|
|
|
return fmt.Errorf("no token found")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = client.Projects.AddProjectHook(_repo.ID, &gitlab.AddProjectHookOptions{
|
|
|
|
URL: gitlab.String(webURL),
|
|
|
|
Token: gitlab.String(token),
|
|
|
|
PushEvents: gitlab.Bool(true),
|
|
|
|
TagPushEvents: gitlab.Bool(true),
|
|
|
|
MergeRequestsEvents: gitlab.Bool(true),
|
|
|
|
DeploymentEvents: gitlab.Bool(true),
|
2021-10-03 12:42:47 +00:00
|
|
|
EnableSSLVerification: gitlab.Bool(!g.SkipVerify),
|
|
|
|
}, gitlab.WithContext(ctx))
|
2021-12-19 00:12:09 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return err
|
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.
|
2021-10-03 12:42:47 +00:00
|
|
|
func (g *Gitlab) Deactivate(ctx context.Context, user *model.User, repo *model.Repo, link string) error {
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
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
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
_, webURL, err := g.getTokenAndWebURL(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hookID := -1
|
|
|
|
listProjectHooksOptions := &gitlab.ListProjectHooksOptions{
|
|
|
|
PerPage: 10,
|
|
|
|
Page: 1,
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
hooks, resp, err := client.Projects.ListProjectHooks(_repo.ID, listProjectHooksOptions, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if hook.URL == webURL {
|
|
|
|
hookID = hook.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit the loop when we've seen all pages
|
|
|
|
if resp.CurrentPage >= resp.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the page number to get the next page
|
|
|
|
listProjectHooksOptions.Page = resp.NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
if hookID == -1 {
|
|
|
|
return fmt.Errorf("could not find hook to delete")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Projects.DeleteProjectHook(_repo.ID, hookID, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
return err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:47:55 +00:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
|
|
|
func (g *Gitlab) Branches(ctx context.Context, user *model.User, repo *model.Repo) ([]string, error) {
|
2022-02-26 16:36:00 +00:00
|
|
|
token := ""
|
|
|
|
if user != nil {
|
|
|
|
token = user.Token
|
|
|
|
}
|
|
|
|
client, err := newClient(g.URL, token, g.SkipVerify)
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
gitlabBranches, _, err := client.Branches.ListBranches(_repo.ID, &gitlab.ListBranchesOptions{}, gitlab.WithContext(ctx))
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branches := make([]string, 0)
|
|
|
|
for _, branch := range gitlabBranches {
|
|
|
|
branches = append(branches, branch.Name)
|
|
|
|
}
|
|
|
|
return branches, nil
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Hook parses the post-commit hook from the Request body
|
2015-07-25 08:49:39 +00:00
|
|
|
// and returns the required data in a standard format.
|
2022-01-17 22:46:59 +00:00
|
|
|
func (g *Gitlab) Hook(ctx context.Context, req *http.Request) (*model.Repo, *model.Build, error) {
|
2015-07-25 23:22:16 +00:00
|
|
|
defer req.Body.Close()
|
2021-10-03 12:42:47 +00:00
|
|
|
payload, err := ioutil.ReadAll(req.Body)
|
2015-07-25 23:22:16 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
return nil, nil, err
|
2015-07-25 23:22:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-12 20:40:36 +00:00
|
|
|
parsed, err := gitlab.ParseWebhook(gitlab.WebhookEventType(req), payload)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2015-12-12 17:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
switch event := parsed.(type) {
|
|
|
|
case *gitlab.MergeEvent:
|
2022-01-17 22:46:59 +00:00
|
|
|
mergeIID, repo, build, err := convertMergeRequestHook(event, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if build, err = g.loadChangedFilesFromMergeRequest(ctx, repo, build, mergeIID); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo, build, nil
|
2021-10-03 12:42:47 +00:00
|
|
|
case *gitlab.PushEvent:
|
2022-01-17 22:46:59 +00:00
|
|
|
return convertPushHook(event)
|
2021-10-03 12:42:47 +00:00
|
|
|
case *gitlab.TagEvent:
|
2022-01-17 22:46:59 +00:00
|
|
|
return convertTagHook(event)
|
2015-12-12 17:09:59 +00:00
|
|
|
default:
|
2021-10-03 12:42:47 +00:00
|
|
|
return nil, nil, nil
|
2015-12-12 17:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
|
|
|
func (g *Gitlab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *model.Repo, build *model.Build, mergeIID int) (*model.Build, error) {
|
|
|
|
_store, ok := store.TryFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("could not get store from context")
|
|
|
|
return build, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := _store.GetRepoName(tmpRepo.Owner + "/" + tmpRepo.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := _store.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := newClient(g.URL, user.Token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
changes, _, err := client.MergeRequests.GetMergeRequestChanges(_repo.ID, mergeIID, &gitlab.GetMergeRequestChangesOptions{}, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
files := make([]string, 0, len(changes.Changes)*2)
|
|
|
|
for _, file := range changes.Changes {
|
|
|
|
files = append(files, file.NewPath, file.OldPath)
|
|
|
|
}
|
|
|
|
build.ChangedFiles = utils.DedupStrings(files)
|
|
|
|
|
|
|
|
return build, nil
|
|
|
|
}
|