提交 97e1c5cc 编写于 作者: 徐晓伟's avatar 徐晓伟

归档所有项目(混合命令,多接口命令)

上级 9a0f73dd
......@@ -197,6 +197,7 @@ COMMANDS:
pipeline, pipelines, pl 流水线 API,中文文档:https://docs.gitlab.cn/jh/api/pipelines.html
project, projects, p 项目 API,中文文档:https://docs.gitlab.cn/jh/api/projects.html
mix-delete, mix-rm 删除(混合命令,多接口命令)
mix-archive 归档(混合命令,多接口命令)
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
......@@ -512,6 +513,27 @@ COPYRIGHT:
--help, -h show help
```
- 归档(混合命令,多接口命令)
```shell
$ go run main.go mix-archive --help
NAME:
gitlab-go mix-archive - 归档(混合命令,多接口命令)
USAGE:
gitlab-go mix-archive command [command options] [arguments...]
COMMANDS:
all 归档所有项目(混合命令,多接口命令)
help, h Shows a list of commands or help for one command
OPTIONS:
--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
--owned 当前用户明确拥有的项目。 (default: false)
--help, -h show help
```
### test
```shell
......
......@@ -23,4 +23,5 @@ const (
IssueId = "issue-id"
IssueIdRange = "issue-id-range"
Recursion = "recursion"
Owned = "owned"
)
......@@ -176,6 +176,14 @@ func Recursion() cli.Flag {
}
}
func Owned(required bool) cli.Flag {
return &cli.BoolFlag{
Name: constant.Owned,
Usage: "当前用户明确拥有的项目。",
Required: required,
}
}
func IssueIdRange(required bool) cli.Flag {
return &cli.StringSliceFlag{
Name: constant.IssueIdRange,
......
......@@ -77,6 +77,7 @@ func main() {
pipelines.Pipelines(),
projects.Projects(),
mix.Delete(),
mix.Archive(),
},
}
......
package mix
import (
"github.com/urfave/cli/v2"
"github.com/xuxiaowei-com-cn/gitlab-go/constant"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
"github.com/xuxiaowei-com-cn/gitlab-go/projects"
"log"
)
// ArchiveAll 归档所有项目
func ArchiveAll() *cli.Command {
return &cli.Command{
Name: "all",
Usage: "归档所有项目(混合命令,多接口命令)",
Flags: append(flag.CommonTokenRequired(), flag.Owned(true)),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
var token = context.String(constant.Token)
var owned = context.Bool(constant.Owned)
projectList, err := projects.ListProjects(&owned, token, baseUrl, 1, 100)
if err != nil {
return err
}
for index, project := range projectList {
log.Printf("Project Index: %d, WebURL: %s", index, project.WebURL)
err = projects.ArchiveProject(token, baseUrl, project.PathWithNamespace)
if err != nil {
return err
}
}
return nil
},
}
}
package mix
import (
"github.com/urfave/cli/v2"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
)
// Archive 归档
func Archive() *cli.Command {
return &cli.Command{
Name: "mix-archive",
Usage: "归档(混合命令,多接口命令)",
Flags: append(flag.Common(), flag.Owned(false)),
Subcommands: []*cli.Command{
ArchiveAll(),
},
}
}
......@@ -2,7 +2,9 @@ package projects
import (
"github.com/urfave/cli/v2"
"github.com/xanzy/go-gitlab"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
"log"
)
const (
......@@ -23,3 +25,58 @@ func Projects() *cli.Command {
},
}
}
// ListProjects 列出群组 https://docs.gitlab.cn/jh/api/projects.html#%E5%88%97%E5%87%BA%E6%89%80%E6%9C%89%E9%A1%B9%E7%9B%AE
func ListProjects(owned *bool, token string, baseUrl string, page int, perPage int) ([]*gitlab.Project, error) {
var results []*gitlab.Project
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
return nil, err
}
opt := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: page,
PerPage: perPage,
},
Owned: owned,
}
projects, response, err := gitClient.Projects.ListProjects(opt)
if err != nil {
return nil, err
}
log.Printf("ListProjects Page %d, PerPage: %d, Response StatusCode: %d\n", page, perPage, response.Response.StatusCode)
results = append(results, projects...)
if len(projects) != 0 {
projects, err = ListProjects(owned, token, baseUrl, page+1, perPage)
if err != nil {
return nil, err
}
results = append(results, projects...)
}
return results, err
}
func ArchiveProject(token string, baseUrl string, pathWithNamespace string) error {
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
return err
}
_, response, err := gitClient.Projects.ArchiveProject(pathWithNamespace)
if err != nil {
return err
}
log.Printf("ArchiveProject PathWithNamespace: %s, Response StatusCode: %d\n", pathWithNamespace, response.Response.StatusCode)
return nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册