Skip to content

Commit c12b012

Browse files
committed
feat(repository api): add repo_label_add/repo_labels_get/repo_desp_update APIs
Add repository APIs
1 parent c29196f commit c12b012

1 file changed

Lines changed: 188 additions & 13 deletions

File tree

api/repositories.go

Lines changed: 188 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strconv"
7+
"time"
68

79
"github.com/moooofly/harbor-go-client/utils"
810
)
911

1012
func init() {
13+
utils.Parser.AddCommand("repo_label_add",
14+
"Add a label to the repository.",
15+
"This endpoint adds an already existing label (global or project specific) to the repository.",
16+
&repoLabelAdd)
17+
utils.Parser.AddCommand("repo_labels_get",
18+
"Get labels of a repository.",
19+
"This endpoint gets labels of a repository specified by the repo_name. NOTE: This API gets '401 Unauthorized' all the time, even when logging in as admin user.",
20+
&repoLabelsGet)
21+
utils.Parser.AddCommand("repo_desp_update",
22+
"Update description of the repository.",
23+
"This endpoint is used to update description of the repository.",
24+
&repoUpdate)
25+
utils.Parser.AddCommand("repo_del",
26+
"Delete a repository by repo_name.",
27+
"This endpoint let user delete a repository by repo_name.",
28+
&repoDel)
1129
utils.Parser.AddCommand("repos_list",
1230
"Get repositories accompany with relevant project and repo name.",
1331
"This endpoint let user search repositories accompanying with relevant project ID and repo name.",
@@ -16,15 +34,170 @@ func init() {
1634
"Get public repositories which are accessed most.",
1735
"This endpoint aims to let users see the most popular public repositories",
1836
&reposTop)
19-
utils.Parser.AddCommand("repo_del",
20-
"Delete a repository by repo_name.",
21-
"This endpoint let user delete a repository by repo_name.",
22-
&repodel)
37+
}
38+
39+
type repositoryLabelAdd struct {
40+
RepoName string `short:"n" long:"repo_name" description:"(REQUIRED) The name of repository that you want to add a label." required:"yes"`
41+
ID int `short:"i" long:"id" description:"(REQUIRED) The ID of the already existing label." required:"yes" json:"id"`
42+
Name string `long:"name" description:"The name of this label." default:"" json:"name"`
43+
Description string `long:"description" description:"The description of this label." default:"" json:"description"`
44+
Color string `long:"color" description:"The color code of this label. (e.g. Format: #A9B6BE)" default:"" json:"color"`
45+
Scope string `long:"scope" description:"The scope of this label. ('p' indicats project scope, 'g' indicates global scope)" default:"" json:"scope"`
46+
ProjectID int `long:"project_id" description:"Which project (id) this label belongs to when created. ('0' indicates global label, others indicate specific project)" default:"" json:"project_id"`
47+
CreationTime string `long:"creation_time" description:"The creation time of this label. default time.Now()" default:"" json:"creation_time"`
48+
UpdateTime string `long:"update_time" description:"The update time of this label. default time.Now()" default:"" json:"update_time"`
49+
Deleted bool `long:"deleted" description:"not sure" json:"deleted"`
50+
}
51+
52+
var repoLabelAdd repositoryLabelAdd
53+
54+
func (x *repositoryLabelAdd) Execute(args []string) error {
55+
PostRepoLabelAdd(utils.URLGen("/api/repositories"))
56+
return nil
57+
}
58+
59+
// PostRepoLabelAdd add a label to the repository.
60+
//
61+
// params:
62+
// repo_name - (REQUIRED) The name of repository that you want to add a label.
63+
// id - (REQUIRED) The ID of the already existing label.
64+
// name - The name of this label.
65+
// description - The description of this label.
66+
// color - The color code of this label. (e.g. Format: #A9B6BE)
67+
// scope - The scope of this label. ('p' indicats project scope, 'g' indicates global scope)
68+
// project_id - Which project (id) this label belongs to when created. ('0' indicates global label, others indicate specific project)
69+
// creation_time - The creation time of this label. default time.Now()
70+
// update_time - The update time of this label. default time.Now()
71+
// deleted - not sure
72+
//
73+
// format:
74+
// POST /repositories/{repo_name}/labels
75+
/*
76+
curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/plain' -d '{ \
77+
"id": 2, \
78+
"name": "test-name", \
79+
"description": "test-description", \
80+
"color": "test-color", \
81+
"scope": "test-scope", \
82+
"project_id": 10, \
83+
"deleted": true \
84+
}' 'https://localhost/api/repositories/temp_5%2Fhello-world/labels'
85+
*/
86+
func PostRepoLabelAdd(baseURL string) {
87+
if repoLabelAdd.CreationTime == "" || repoLabelAdd.UpdateTime == "" {
88+
now := time.Now().Format("2006-01-02T15:04:05Z")
89+
repoLabelAdd.CreationTime = now
90+
repoLabelAdd.UpdateTime = now
91+
}
92+
93+
targetURL := baseURL + "/" + repoLabelAdd.RepoName + "/labels"
94+
fmt.Println("==> POST", targetURL)
95+
96+
// Read beegosessionID from .cookie.yaml
97+
c, err := utils.CookieLoad()
98+
if err != nil {
99+
fmt.Println("Error:", err)
100+
return
101+
}
102+
103+
t, err := json.Marshal(&repoLabelAdd)
104+
if err != nil {
105+
fmt.Println("error:", err)
106+
return
107+
}
108+
109+
fmt.Println("==> label add:", string(t))
110+
111+
utils.Request.Post(targetURL).
112+
Set("Cookie", "harbor-lang=zh-cn; beegosessionID="+c.BeegosessionID).
113+
Send(string(t)).
114+
End(utils.PrintStatus)
115+
}
116+
117+
type repositoryLabelsGet struct {
118+
RepoName string `short:"n" long:"repo_name" description:"(REQUIRED) The name of repository." required:"yes"`
119+
}
120+
121+
var repoLabelsGet repositoryLabelsGet
122+
123+
func (x *repositoryLabelsGet) Execute(args []string) error {
124+
GetRepoLabels(utils.URLGen("/api/repositories"))
125+
return nil
126+
}
127+
128+
// GetRepoLabels get labels of a repository specified by the repo_name.
129+
//
130+
// params:
131+
// repo_name - (REQUIRED) The name of repository.
132+
//
133+
// format:
134+
// GET /repositories/{repo_name}/labels
135+
//
136+
// e.g. curl -X GET --header 'Accept: application/json' 'https://localhost/api/repositories/temp_5%2Fhello-world/labels'
137+
func GetRepoLabels(baseURL string) {
138+
targetURL := baseURL + "/" + repoLabelsGet.RepoName + "/labels"
139+
fmt.Println("==> GET", targetURL)
140+
141+
utils.Request.Get(targetURL).End(utils.PrintStatus)
142+
}
143+
144+
type repoDescriptionUpdate struct {
145+
RepoName string `short:"n" long:"repo_name" description:"(REQUIRED) Repo name for filtering results." required:"yes" json:"-"`
146+
Description string `short:"d" long:"description" description:"(REQUIRED) The description of the repository." required:"yes" json:"description"`
147+
}
148+
149+
var repoUpdate repoDescriptionUpdate
150+
151+
func (x *repoDescriptionUpdate) Execute(args []string) error {
152+
PutRepoDescriptionUpdate(utils.URLGen("/api/repositories"))
153+
return nil
154+
}
155+
156+
// PutRepoDescriptionUpdate is used to update description of the repository.
157+
//
158+
// params:
159+
// repo_name - (REQUIRED) The name of repository which will be deleted.
160+
// description - (REQUIRED) The description of the repository.
161+
//
162+
// format:
163+
// PUT /repositories/{repo_name}
164+
//
165+
// e.g.
166+
/*
167+
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: text/plain' -d '{ \
168+
"description": "change" \
169+
}' 'https://localhost/api/repositories/temp_5%2Fhello-world'
170+
*/
171+
172+
func PutRepoDescriptionUpdate(baseURL string) {
173+
targetURL := baseURL + "/" + repoUpdate.RepoName
174+
fmt.Println("==> PUT", targetURL)
175+
176+
// Read beegosessionID from .cookie.yaml
177+
c, err := utils.CookieLoad()
178+
if err != nil {
179+
fmt.Println("Error:", err)
180+
return
181+
}
182+
183+
t, err := json.Marshal(&repoUpdate)
184+
if err != nil {
185+
fmt.Println("error:", err)
186+
return
187+
}
188+
189+
fmt.Println("==> description:", string(t))
190+
191+
utils.Request.Put(targetURL).
192+
Set("Cookie", "harbor-lang=zh-cn; beegosessionID="+c.BeegosessionID).
193+
Send(string(t)).
194+
End(utils.PrintStatus)
23195
}
24196

25197
type repositoriesList struct {
26198
ProjectID int `short:"j" long:"project_id" description:"(REQUIRED) Relevant project ID." required:"yes"`
27199
RepoName string `short:"n" long:"repo_name" description:"Repo name for filtering results." default:""`
200+
LabelID int `short:"l" long:"label_id" description:"The ID of label used to filter the result." default:"0"`
28201
Page int `short:"p" long:"page" description:"The page nubmer, default is 1." default:"1"`
29202
PageSize int `short:"s" long:"page_size" description:"The size of per page, default is 10, maximum is 100." default:"10"`
30203
}
@@ -51,7 +224,7 @@ type repositoryDel struct {
51224
RepoName string `short:"n" long:"repo_name" description:"(REQUIRED) The name of repository which will be deleted." required:"yes"`
52225
}
53226

54-
var repodel repositoryDel
227+
var repoDel repositoryDel
55228

56229
func (x *repositoryDel) Execute(args []string) error {
57230
DelRepoByRepoName(utils.URLGen("/api/repositories"))
@@ -61,15 +234,17 @@ func (x *repositoryDel) Execute(args []string) error {
61234
// GetReposByPrjID let user search repositories accompanying with relevant project ID and repo name.
62235
//
63236
// params:
64-
// project_id - (REQUIRED) Relevant project ID.
65-
// q - Repo name for filtering results.
66-
// page - The page nubmer, default is 1.
67-
// pageSize - The size of per page, default is 10, maximum is 100.
237+
// project_id - (REQUIRED) Relevant project ID.
238+
// q - Repo name for filtering results.
239+
// label_id - The ID of label used to filter the result.
240+
// page - The page nubmer, default is 1.
241+
// pageSize - The size of per page, default is 10, maximum is 100.
68242
//
69-
// e.g. curl -X GET --header 'Accept: application/json' 'https://localhost/api/repositories?project_id=1&q=prj&page=1&page_size=10'
243+
// e.g. curl -X GET --header 'Accept: application/json' 'https://localhost/api/repositories?project_id=1&q=prj&label_id=100&page=1&page_size=10'
70244
func GetReposByPrjID(baseURL string) {
71245
targetURL := baseURL + "?project_id=" + strconv.Itoa(reposList.ProjectID) +
72246
"&q=" + reposList.RepoName +
247+
"&label_id=" + strconv.Itoa(reposList.LabelID) +
73248
"&page=" + strconv.Itoa(reposList.Page) +
74249
"&page_size=" + strconv.Itoa(reposList.PageSize)
75250
fmt.Println("==> GET", targetURL)
@@ -89,7 +264,7 @@ func GetReposByPrjID(baseURL string) {
89264
// GetTopRepos aims to let users see the most popular public repositories
90265
//
91266
// params:
92-
// count - The number of the requested public repositories, default is 10 if not provided.
267+
// count - The number of the requested public repositories, default is 10 if not provided.
93268
//
94269
// e.g. curl -X GET --header 'Accept: application/json' 'https://localhost/api/repositories/top?count=3'
95270
func GetTopRepos(baseURL string) {
@@ -102,11 +277,11 @@ func GetTopRepos(baseURL string) {
102277
// DelRepoByRepoName let user delete a repository with name.
103278
//
104279
// params:
105-
// repo_name - (REQUIRED) The name of repository which will be deleted.
280+
// repo_name - (REQUIRED) The name of repository which will be deleted.
106281
//
107282
// e.g. curl -X DELETE --header 'Accept: text/plain' 'https://localhost/api/repositories/prj1%2Fhello-world'
108283
func DelRepoByRepoName(baseURL string) {
109-
targetURL := baseURL + "/" + repodel.RepoName
284+
targetURL := baseURL + "/" + repoDel.RepoName
110285
fmt.Println("==> DELETE", targetURL)
111286

112287
// Read beegosessionID from .cookie.yaml

0 commit comments

Comments
 (0)