-
Notifications
You must be signed in to change notification settings - Fork 168
/
repos.go
161 lines (143 loc) · 4.67 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
package server
import (
"context"
"fmt"
"net/http"
"github.com/caicloud/nirvana/log"
)
// RepositoriesService handles communication with the repository related.
type RepositoriesService struct {
v1Client *V1Client
}
// Repository contains data from a BitBucket Server Repository.
type Repository struct {
Slug string `json:"slug"`
ID int `json:"id"`
Name string `json:"name"`
ScmID string `json:"scmId"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
Forkable bool `json:"forkable"`
Project Project `json:"project"`
Public bool `json:"public"`
Links struct {
Clone []CloneLink `json:"clone"`
Self []SelfLink `json:"self"`
} `json:"links"`
}
// Project contains data from a BitBucket Server Project.
type Project struct {
Key string `json:"key"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Public bool `json:"public"`
Type string `json:"type"`
Links struct {
Self []SelfLink `json:"self"`
} `json:"links"`
}
// Repositories is a set of repositories.
type Repositories struct {
Pagination
Values []Repository `json:"values"`
}
// Branch contains git Branch information.
type Branch struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
LatestCommit string `json:"latestCommit"`
IsDefault bool `json:"isDefault"`
}
// Branches is a set of branches.
type Branches struct {
Pagination
Values []Branch `json:"values"`
}
// Tag contains git Tag information.
type Tag struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
LatestCommit string `json:"latestCommit"`
Hash string `json:"hash"`
}
// Tags is a set of tags.
type Tags struct {
Pagination
Values []Tag `json:"values"`
}
// Files is a set of files' name in a repo.
type Files struct {
Pagination
Values []string `json:"values"`
}
// StatusReq represents the options of creating commit status.
type StatusReq struct {
State string `json:"state"`
Key string `json:"key"`
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
}
// ListRepositories list repositories in the Bitbucket Server.
func (server *RepositoriesService) ListRepositories(ctx context.Context, project string, opts *ListOpts) (*Repositories, *http.Response, error) {
var u string
if len(project) == 0 {
u = "rest/api/1.0/repos"
} else {
u = fmt.Sprintf("rest/api/1.0/projects/%s/repos", project)
}
req, err := server.v1Client.NewRequest(http.MethodGet, u, nil, opts)
if err != nil {
return nil, nil, err
}
var repos Repositories
resp, err := server.v1Client.Do(req, &repos)
return &repos, resp, err
}
// ListBranches list branches on the repository.
func (server *RepositoriesService) ListBranches(ctx context.Context, project string, repo string, opt *ListOpts) (*Branches, *http.Response, error) {
u := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches", project, repo)
req, err := server.v1Client.NewRequest(http.MethodGet, u, nil, opt)
if err != nil {
return nil, nil, err
}
var branches Branches
resp, err := server.v1Client.Do(req, &branches)
log.Infof("branch: %+v, error: %+v", branches, err)
return &branches, resp, err
}
// ListFiles list files on the repository.
func (server *RepositoriesService) ListFiles(ctx context.Context, project string, repo string, opt *ListOpts) (*Files, *http.Response, error) {
u := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/files", project, repo)
req, err := server.v1Client.NewRequest(http.MethodGet, u, nil, opt)
if err != nil {
return nil, nil, err
}
var files Files
resp, err := server.v1Client.Do(req, &files)
return &files, resp, err
}
// ListTags list tags on the repository.
func (server *RepositoriesService) ListTags(ctx context.Context, project string, repo string, opt *ListOpts) (*Tags, *http.Response, error) {
u := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/tags", project, repo)
req, err := server.v1Client.NewRequest(http.MethodGet, u, nil, opt)
if err != nil {
return nil, nil, err
}
var tags Tags
resp, err := server.v1Client.Do(req, &tags)
return &tags, resp, err
}
// CreateStatus create a commit status.
func (server *RepositoriesService) CreateStatus(ctx context.Context, commitID string, input *StatusReq) (*http.Response, error) {
u := fmt.Sprintf("rest/build-status/1.0/commits/%s", commitID)
req, err := server.v1Client.NewRequest(http.MethodPost, u, input, nil)
if err != nil {
return nil, err
}
resp, err := server.v1Client.Do(req, nil)
return resp, err
}