-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathprojects.go
57 lines (50 loc) · 1.27 KB
/
projects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gitlab
import (
"fmt"
"strings"
gogitlab "github.com/xanzy/go-gitlab"
)
type Projects struct {
*gogitlab.ProjectsService
client *Client
}
type NotFound struct {
s string
}
func (e *NotFound) Error() string {
return e.s
}
// ProjectByPath searches and returns a project with the given path.
// Both project and error can be nil, if no project was found.
func (srv *Projects) ByPath(path string) (proj *gogitlab.Project, err error) {
path = strings.TrimPrefix(strings.TrimSuffix(path, ".git"), "/")
p := strings.Split(path, "/")
findProject := func(p *gogitlab.Project) bool {
if p.PathWithNamespace == path {
proj = p
return true
}
return false
}
err = srv.Search(p[1], &gogitlab.SearchProjectsOptions{}, findProject)
if err == nil && proj == nil {
err = &NotFound{fmt.Sprintf("repository with path '%s' was not found", path)}
}
return
}
func (srv *Projects) Search(query string, opts *gogitlab.SearchProjectsOptions, stop func(*gogitlab.Project) bool) error {
projects, resp, err := srv.SearchProjects(query, opts)
if err != nil {
return err
}
for _, p := range projects {
if stop(p) {
return nil
}
}
if resp.NextPage > 0 && resp.NextPage < resp.LastPage {
opts.Page = resp.NextPage
return srv.Search(query, opts, stop)
}
return nil
}