diff --git a/pkg/config/config.go b/pkg/config/config.go index 1825260a6..0dcd610b1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -91,6 +91,7 @@ type Config struct { SkipVerify bool `envconfig:"optional"` Open bool `envconfig:"optional"` Orgs []string `envconfig:"optional"` + Search bool `envconfig:"optional"` } } diff --git a/pkg/remote/builtin/gitlab/gitlab.go b/pkg/remote/builtin/gitlab/gitlab.go index 1b322879d..6a816d868 100644 --- a/pkg/remote/builtin/gitlab/gitlab.go +++ b/pkg/remote/builtin/gitlab/gitlab.go @@ -30,6 +30,7 @@ type Gitlab struct { Open bool PrivateMode bool SkipVerify bool + Search bool cache *lru.Cache } @@ -46,6 +47,7 @@ func NewDriver(conf *config.Config) (remote.Remote, error) { AllowedOrgs: conf.Gitlab.Orgs, Open: conf.Gitlab.Open, SkipVerify: conf.Gitlab.SkipVerify, + Search: conf.Gitlab.Search, } var err error 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. func (r *Gitlab) Repo(u *common.User, owner, name string) (*common.Repo, error) { 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) if err != nil { 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) - id := ns(owner, name) + id, err := GetProjectId(r, client, owner, name) + if err != nil { + return nil, err + } + repo, err := client.Project(id) if err != nil { 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. 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 path = ns(repo.Owner, repo.Name) - return client.RepoRawFile(path, build.Commit.Sha, ".drone.yml") + id, err := GetProjectId(r, client, repo.Owner, repo.Name) + 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, @@ -164,8 +177,12 @@ func (r *Gitlab) Netrc(u *common.User) (*common.Netrc, error) { // a Public Deploy key, if applicable. 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 path = ns(repo.Owner, repo.Name) - var title, err = GetKeyTitle(link) + id, err := GetProjectId(r, client, repo.Owner, repo.Name) + if err != nil { + return err + } + + title, err := GetKeyTitle(link) if err != nil { 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 // to upload a github key to the repository 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 } } @@ -183,36 +200,39 @@ func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypai link += "&owner=" + repo.Owner + "&name=" + repo.Name // 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 // which are equal to link and removing the SSH deploy key. func (r *Gitlab) Deactivate(user *common.User, repo *common.Repo, link string) error { 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 { return err } var pubkey = strings.TrimSpace(repo.Keys.Public) for _, k := range keys { 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 } break } } - hooks, err := client.ProjectHooks(path) + hooks, err := client.ProjectHooks(id) if err != nil { return err } link += "&owner=" + repo.Owner + "&name=" + repo.Name for _, h := range hooks { 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 } break diff --git a/pkg/remote/builtin/gitlab/helper.go b/pkg/remote/builtin/gitlab/helper.go index ac7f49f5d..adc448d26 100644 --- a/pkg/remote/builtin/gitlab/helper.go +++ b/pkg/remote/builtin/gitlab/helper.go @@ -3,6 +3,7 @@ package gitlab import ( "fmt" "net/url" + "strconv" "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) { 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 + } +}