forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repositories.go
105 lines (93 loc) · 2.36 KB
/
repositories.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package bitbucket
import (
"errors"
"fmt"
)
//"github.com/k0kubun/pp"
type Repositories struct {
c *Client
PullRequests *PullRequests
Issues *Issues
Pipelines *Pipelines
Repository *Repository
Commits *Commits
Diff *Diff
BranchRestrictions *BranchRestrictions
Webhooks *Webhooks
Downloads *Downloads
DeployKeys *DeployKeys
repositories
}
type RepositoriesRes struct {
Page int32
Pagelen int32
Size int32
Items []Repository
}
func (r *Repositories) ListForAccount(ro *RepositoriesOptions) (*RepositoriesRes, error) {
urlPath := "/repositories"
if ro.Owner != "" {
urlPath += fmt.Sprintf("/%s", ro.Owner)
}
urlStr := r.c.requestUrl(urlPath)
if ro.Role != "" {
urlStr += "?role=" + ro.Role
}
if ro.Keyword != nil && *ro.Keyword != "" {
if ro.Role == "" {
urlStr += "?"
}
// https://developer.atlassian.com/cloud/bitbucket/rest/intro/#operators
urlStr += fmt.Sprintf("q=full_name ~ \"%s\"", *ro.Keyword)
}
repos, err := r.c.executePaginated("GET", urlStr, "", ro.Page)
if err != nil {
return nil, err
}
return decodeRepositories(repos)
}
// Deprecated: Use ListForAccount instead
func (r *Repositories) ListForTeam(ro *RepositoriesOptions) (*RepositoriesRes, error) {
return r.ListForAccount(ro)
}
func (r *Repositories) ListPublic() (*RepositoriesRes, error) {
urlStr := r.c.requestUrl("/repositories/")
repos, err := r.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodeRepositories(repos)
}
func decodeRepositories(reposResponse interface{}) (*RepositoriesRes, error) {
reposResponseMap, ok := reposResponse.(map[string]interface{})
if !ok {
return nil, errors.New("Not a valid format")
}
repoArray := reposResponseMap["values"].([]interface{})
var repos []Repository
for _, repoEntry := range repoArray {
repo, err := decodeRepository(repoEntry)
if err == nil {
repos = append(repos, *repo)
}
}
page, ok := reposResponseMap["page"].(float64)
if !ok {
page = 0
}
pagelen, ok := reposResponseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
size, ok := reposResponseMap["size"].(float64)
if !ok {
size = 0
}
repositories := RepositoriesRes{
Page: int32(page),
Pagelen: int32(pagelen),
Size: int32(size),
Items: repos,
}
return &repositories, nil
}