mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-23 17:00:30 +00:00
Merge remote-tracking branch 'upstream/0.4-database' into 0.4-database
This commit is contained in:
commit
4270573536
3 changed files with 49 additions and 13 deletions
|
@ -91,6 +91,7 @@ type Config struct {
|
||||||
SkipVerify bool `envconfig:"optional"`
|
SkipVerify bool `envconfig:"optional"`
|
||||||
Open bool `envconfig:"optional"`
|
Open bool `envconfig:"optional"`
|
||||||
Orgs []string `envconfig:"optional"`
|
Orgs []string `envconfig:"optional"`
|
||||||
|
Search bool `envconfig:"optional"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ type Gitlab struct {
|
||||||
Open bool
|
Open bool
|
||||||
PrivateMode bool
|
PrivateMode bool
|
||||||
SkipVerify bool
|
SkipVerify bool
|
||||||
|
Search bool
|
||||||
|
|
||||||
cache *lru.Cache
|
cache *lru.Cache
|
||||||
}
|
}
|
||||||
|
@ -46,6 +47,7 @@ func NewDriver(conf *config.Config) (remote.Remote, error) {
|
||||||
AllowedOrgs: conf.Gitlab.Orgs,
|
AllowedOrgs: conf.Gitlab.Orgs,
|
||||||
Open: conf.Gitlab.Open,
|
Open: conf.Gitlab.Open,
|
||||||
SkipVerify: conf.Gitlab.SkipVerify,
|
SkipVerify: conf.Gitlab.SkipVerify,
|
||||||
|
Search: conf.Gitlab.Search,
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
gitlab.cache, err = lru.New(1028)
|
gitlab.cache, err = lru.New(1028)
|
||||||
|
@ -82,7 +84,10 @@ func (r *Gitlab) Orgs(u *common.User) ([]string, error) {
|
||||||
// Repo fetches the named repository from the remote system.
|
// Repo fetches the named repository from the remote system.
|
||||||
func (r *Gitlab) Repo(u *common.User, owner, name string) (*common.Repo, error) {
|
func (r *Gitlab) Repo(u *common.User, owner, name string) (*common.Repo, error) {
|
||||||
client := NewClient(r.URL, u.Token, r.SkipVerify)
|
client := NewClient(r.URL, u.Token, r.SkipVerify)
|
||||||
id := ns(owner, name)
|
id, err := GetProjectId(r, client, owner, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
repo_, err := client.Project(id)
|
repo_, err := client.Project(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -118,7 +123,11 @@ func (r *Gitlab) Perm(u *common.User, owner, name string) (*common.Perm, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := NewClient(r.URL, u.Token, r.SkipVerify)
|
client := NewClient(r.URL, u.Token, r.SkipVerify)
|
||||||
id := ns(owner, name)
|
id, err := GetProjectId(r, client, owner, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
repo, err := client.Project(id)
|
repo, err := client.Project(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -135,8 +144,12 @@ func (r *Gitlab) Perm(u *common.User, owner, name string) (*common.Perm, error)
|
||||||
// repository and returns in string format.
|
// repository and returns in string format.
|
||||||
func (r *Gitlab) Script(user *common.User, repo *common.Repo, build *common.Build) ([]byte, error) {
|
func (r *Gitlab) Script(user *common.User, repo *common.Repo, build *common.Build) ([]byte, 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)
|
id, err := GetProjectId(r, client, repo.Owner, repo.Name)
|
||||||
return client.RepoRawFile(path, build.Commit.Sha, ".drone.yml")
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.RepoRawFile(id, build.Commit.Sha, ".drone.yml")
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE Currently gitlab doesn't support status for commits and events,
|
// NOTE Currently gitlab doesn't support status for commits and events,
|
||||||
|
@ -164,8 +177,12 @@ func (r *Gitlab) Netrc(u *common.User) (*common.Netrc, error) {
|
||||||
// a Public Deploy key, if applicable.
|
// a Public Deploy key, if applicable.
|
||||||
func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *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)
|
id, err := GetProjectId(r, client, repo.Owner, repo.Name)
|
||||||
var title, err = GetKeyTitle(link)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
title, err := GetKeyTitle(link)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -173,7 +190,7 @@ func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypai
|
||||||
// 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 {
|
||||||
if err := client.AddProjectDeployKey(path, title, k.Public); err != nil {
|
if err := client.AddProjectDeployKey(id, title, k.Public); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,36 +200,39 @@ func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypai
|
||||||
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(id, link, true, false, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deactivate removes a repository by removing all the post-commit hooks
|
// Deactivate removes a repository by removing all the post-commit hooks
|
||||||
// which are equal to link and removing the SSH deploy key.
|
// which are equal to link and removing the SSH deploy key.
|
||||||
func (r *Gitlab) Deactivate(user *common.User, repo *common.Repo, link string) error {
|
func (r *Gitlab) Deactivate(user *common.User, repo *common.Repo, 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)
|
id, err := GetProjectId(r, client, repo.Owner, repo.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
keys, err := client.ProjectDeployKeys(path)
|
keys, err := client.ProjectDeployKeys(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var pubkey = strings.TrimSpace(repo.Keys.Public)
|
var pubkey = strings.TrimSpace(repo.Keys.Public)
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
if pubkey == strings.TrimSpace(k.Key) {
|
if pubkey == strings.TrimSpace(k.Key) {
|
||||||
if err := client.RemoveProjectDeployKey(path, strconv.Itoa(k.Id)); err != nil {
|
if err := client.RemoveProjectDeployKey(id, strconv.Itoa(k.Id)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hooks, err := client.ProjectHooks(path)
|
hooks, err := client.ProjectHooks(id)
|
||||||
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(id, strconv.Itoa(h.Id)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
|
@ -3,6 +3,7 @@ package gitlab
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/drone/drone/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client"
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client"
|
||||||
)
|
)
|
||||||
|
@ -82,3 +83,17 @@ func ns(owner, name string) string {
|
||||||
func GetUserEmail(client *gogitlab.Gitlab, defaultURL string) (*gogitlab.Gitlab, error) {
|
func GetUserEmail(client *gogitlab.Gitlab, defaultURL string) (*gogitlab.Gitlab, error) {
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetProjectId(r *Gitlab, client *gogitlab.Gitlab, owner, name string) (projectId string, err error) {
|
||||||
|
if r.Search {
|
||||||
|
_projectId, err := client.SearchProjectId(owner, name)
|
||||||
|
if err != nil || _projectId == 0 {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
projectId := strconv.Itoa(_projectId)
|
||||||
|
return projectId, nil
|
||||||
|
} else {
|
||||||
|
projectId := ns(owner, name)
|
||||||
|
return projectId, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue