-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
101 lines (88 loc) · 2.76 KB
/
repo.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
package goscm
import (
"encoding/json"
"strconv"
)
type RepoContainer struct {
Page int `json:"page"`
PageTotal int `json:"pageTotal"`
Embedded struct {
Repositories []Repository `json:"repositories"`
} `json:"_embedded"`
}
type Repository struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Contact string `json:"contact"`
Archived bool `json:"archived"`
LastModified string `json:"lastModified,omitempty"`
Links Links `json:"_links,omitempty"`
}
type Links struct {
ProtocolUrl []ProtocolUrl `json:"protocol,omitempty"`
}
type ProtocolUrl struct {
Name string `json:"name,omitempty"`
Href string `json:"href,omitempty"`
}
type RepoListFilter struct {
Limit int `json:"limit"`
ShowArchived bool `json:"showArchived"`
}
func (c *Client) NewRepoListFilter() *RepoListFilter {
something := &RepoListFilter{}
something.Limit = 10
something.ShowArchived = true
return something
}
// ListRepos List all repositories which the user may see
func (c *Client) ListRepos(filter *RepoListFilter) (RepoContainer, error) {
repoContainer := RepoContainer{}
err := c.getJson("/api/v2/repositories?pageSize="+strconv.FormatInt(int64(filter.Limit), 10)+"&archived="+strconv.FormatBool(filter.ShowArchived), &repoContainer, nil)
if err != nil {
return RepoContainer{}, err
}
return repoContainer, nil
}
// ListReposByNamespace List all repositories which the user may see by namespace
func (c *Client) ListReposByNamespace(namespace string, filter *RepoListFilter) (RepoContainer, error) {
repoContainer := RepoContainer{}
err := c.getJson("/api/v2/repositories/"+namespace+"?pageSize="+strconv.FormatInt(int64(filter.Limit), 10)+"&archived="+strconv.FormatBool(filter.ShowArchived), &repoContainer, nil)
if err != nil {
return RepoContainer{}, err
}
return repoContainer, nil
}
// GetRepo Get single repository by namespace and name
func (c *Client) GetRepo(namespace string, name string) (Repository, error) {
repo := Repository{}
err := c.getJson("/api/v2/repositories/"+namespace+"/"+name, &repo, nil)
if err != nil {
return Repository{}, err
}
return repo, nil
}
// CreateRepo Create new repository
func (c *Client) CreateRepo(repo Repository) (Repository, error) {
headers := make(map[string]string)
headers["Content-Type"] = mimeTypeRepo
bytes, err := json.Marshal(repo)
if err != nil {
return repo, err
}
err = c.post("/api/v2/repositories/", bytes, headers)
if err != nil {
return repo, err
}
return repo, nil
}
// DeleteRepo Delete repository
func (c *Client) DeleteRepo(namespace string, name string) error {
err := c.delete("/api/v2/repositories/"+namespace+"/"+name, nil)
if err != nil {
return err
}
return nil
}