forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repositories.go
108 lines (95 loc) · 2.35 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
106
107
108
package bitbucket
import (
"errors"
"github.com/mitchellh/mapstructure"
)
//"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
repositories
}
type RepositoriesRes struct {
Page int32
Pagelen int32
MaxDepth int32
Size int32
Items []Repository
}
func (r *Repositories) ListForAccount(ro *RepositoriesOptions) (*RepositoriesRes, error) {
urlStr := r.c.requestUrl("/repositories/%s", ro.Owner)
if ro.Role != "" {
urlStr += "?role=" + ro.Role
}
repos, err := r.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeRepositorys(repos)
}
func (r *Repositories) ListForTeam(ro *RepositoriesOptions) (*RepositoriesRes, error) {
urlStr := r.c.requestUrl("/repositories/%s", ro.Owner)
if ro.Role != "" {
urlStr += "?role=" + ro.Role
}
repos, err := r.c.executeRaw("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeRepositorys(repos)
}
func (r *Repositories) ListPublic() (*RepositoriesRes, error) {
urlStr := r.c.requestUrl("/repositories/")
repos, err := r.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeRepositorys(repos)
}
func decodeRepositorys(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 {
var repo Repository
err := mapstructure.Decode(repoEntry, &repo)
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
}
max_depth, ok := reposResponseMap["max_width"].(float64)
if !ok {
max_depth = 0
}
size, ok := reposResponseMap["size"].(float64)
if !ok {
size = 0
}
repositories := RepositoriesRes{
Page: int32(page),
Pagelen: int32(pagelen),
MaxDepth: int32(max_depth),
Size: int32(size),
Items: repos,
}
return &repositories, nil
}