forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
201 lines (169 loc) · 4.39 KB
/
repos.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package bitbucket
import (
"fmt"
)
type Repo struct {
Name string `json:"name"`
Slug string `json:"slug"`
Owner string `json:"owner"`
Scm string `json:"scm"`
Logo string `json:"logo"`
Language string `json:"language"`
Private bool `json:"is_private"`
IsFork bool `json:"is_fork"`
ForkOf *Repo `json:"fork_of"`
}
type Branch struct {
Branch string `json:"branch"`
Message string `json:"message"`
Author string `json:"author"`
RawAuthor string `json:"raw_author"`
Node string `json:"node"`
RawNode string `json:"raw_node"`
Files []*BranchFile `json:"files"`
}
type BranchFile struct {
File string `json:"file"`
Type string `json:"type"`
}
type RepoResource struct {
client *Client
}
// Gets the repositories owned by the individual or team account.
func (r *RepoResource) List() ([]*Repo, error) {
repos := []*Repo{}
const path = "/user/repositories"
if err := r.client.do("GET", path, nil, nil, &repos); err != nil {
return nil, err
}
return repos, nil
}
// Gets the repositories list from the account's dashboard.
func (r *RepoResource) ListDashboard() ([]*Account, error) {
var m [][]interface{}
const path = "/user/repositories/dashboard"
if err := r.client.do("GET", path, nil, nil, &m); err != nil {
return nil, err
}
return unmarshalAccounts(m), nil
}
// Gets the repositories list from the account's dashboard, and
// converts the response to a list of Repos, instead of a
// list of Accounts.
func (r *RepoResource) ListDashboardRepos() ([]*Repo, error) {
accounts, err := r.ListDashboard()
if err != nil {
return nil, nil
}
repos := []*Repo{}
for _, acct := range accounts {
repos = append(repos, acct.Repos...)
}
return repos, nil
}
// Gets the list of Branches for the repository
func (r *RepoResource) ListBranches(owner, slug string) ([]*Branch, error) {
branchMap := map[string]*Branch{}
path := fmt.Sprintf("/repositories/%s/%s/branches", owner, slug)
if err := r.client.do("GET", path, nil, nil, &branchMap); err != nil {
return nil, err
}
// The list is returned in a map ...
// we really want a slice
branches := []*Branch{}
for _, branch := range branchMap {
branches = append(branches, branch)
}
return branches, nil
}
// Gets the repositories list for the named user.
func (r *RepoResource) ListUser(owner string) ([]*Repo, error) {
repos := []*Repo{}
path := fmt.Sprintf("/repositories/%s", owner)
if err := r.client.do("GET", path, nil, nil, &repos); err != nil {
return nil, err
}
return repos, nil
}
// Gets the named repository.
func (r *RepoResource) Find(owner, slug string) (*Repo, error) {
repo := Repo{}
path := fmt.Sprintf("/repositories/%s/%s", owner, slug)
if err := r.client.do("GET", path, nil, nil, &repo); err != nil {
return nil, err
}
return &repo, nil
}
// -----------------------------------------------------------------------------
// Helper Functions to parse odd Bitbucket JSON structure
func unmarshalAccounts(m [][]interface{}) []*Account {
accts := []*Account{}
for i := range m {
a := Account{}
for j := range m[i] {
switch v := m[i][j].(type) {
case []interface{}:
a.Repos = unmarshalRepos(v)
case map[string]interface{}:
a.User = unmarshalUser(v)
default: // Unknown...return error?
}
}
accts = append(accts, &a)
}
return accts
}
func unmarshalUser(m map[string]interface{}) *User {
u := User{}
for k, v := range m {
switch k {
case "username":
u.Username = v.(string)
case "first_name":
u.FirstName = v.(string)
case "last_name":
u.LastName = v.(string)
case "display_name":
u.DisplayName = v.(string)
case "avatar":
u.Avatar = v.(string)
case "is_team":
u.IsTeam = v.(bool)
}
}
return &u
}
func unmarshalRepo(m map[string]interface{}) *Repo {
r := Repo{}
for k, v := range m {
// make sure v.(type) is correct type each time?
switch k {
case "name":
r.Name = v.(string)
case "slug":
r.Slug = v.(string)
case "owner":
r.Owner = v.(string)
case "scm":
r.Scm = v.(string)
case "logo":
r.Logo = v.(string)
case "language":
r.Language = v.(string)
case "is_private":
r.Private = v.(bool)
}
}
return &r
}
func unmarshalRepos(m []interface{}) []*Repo {
r := []*Repo{}
for i := range m {
switch v := m[i].(type) {
case map[string]interface{}:
r = append(r, unmarshalRepo(v))
//default: fmt.Println("BAD")
}
}
return r
}