项目 API: 列出所有项目: 增加参数

This commit is contained in:
徐晓伟 2023-10-15 18:27:17 +08:00
parent d800187847
commit 6fb1f377be
4 changed files with 30 additions and 7 deletions

View File

@ -369,6 +369,10 @@ COPYRIGHT:
--base-url value 实例地址例如https://gitlab.xuxiaowei.com.cn/api/v4 (default: "https://gitlab.com/api/v4") [%CI_API_V4_URL%]
--token value your_access_token
--sort value 按照 asc 或者 desc 排序 (default: "desc")
--page value 页码默认1中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 1)
--per-page value 每页列出的项目数默认20最大100中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 20)
--search value 根据 title 和 description 搜索议题。
--search-namespaces 匹配搜索条件时包括上级命名空间。默认为 false。 (default: false)
--help, -h show help
```

View File

@ -26,6 +26,7 @@ const (
OrderBy = "order-by"
Scope = "scope"
Search = "search"
SearchNamespaces = "search-namespaces"
State = "state"
UpdatedAfter = "updated-after"
UpdatedBefore = "updated-before"

View File

@ -116,6 +116,14 @@ func Search() cli.Flag {
}
}
func SearchNamespaces() cli.Flag {
return &cli.BoolFlag{
Name: constant.SearchNamespaces,
Value: false,
Usage: "匹配搜索条件时包括上级命名空间。默认为 false。",
}
}
func State() cli.Flag {
return &cli.StringFlag{
Name: constant.State,

View File

@ -14,16 +14,20 @@ func Projects() *cli.Command {
Name: "project",
Aliases: []string{"projects", "p"},
Usage: "项目 API中文文档https://docs.gitlab.cn/jh/api/projects.html",
Flags: append(flag.Common(), flag.Sort()),
Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.Search(), flag.SearchNamespaces()),
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "列出所有项目",
Flags: append(flag.Common(), flag.Sort()),
Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.Search(), flag.SearchNamespaces()),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
var token = context.String(constant.Token)
var sort = context.String(constant.Sort)
var page = context.Int(constant.Page)
var perPage = context.Int(constant.PerPage)
var search = context.String(constant.Search)
var searchNamespaces = context.Bool(constant.SearchNamespaces)
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
@ -31,7 +35,13 @@ func Projects() *cli.Command {
}
opt := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: page,
PerPage: perPage,
},
Sort: &sort,
Search: &search,
SearchNamespaces: &searchNamespaces,
}
projects, response, err := gitClient.Projects.ListProjects(opt)
log.Printf("Response StatusCode: %d\n", response.Response.StatusCode)