diff --git a/README.md b/README.md index 0944a982b7ed6afcd05b7271d7ff38bc9bf7e76b..ec5356eeb7d80f6b59e8f2a0b530905cea1c48ba 100644 --- a/README.md +++ b/README.md @@ -446,7 +446,7 @@ COPYRIGHT: ``` - 删除(混合命令) - + ```shell $ go run main.go mix-delete --help NAME: @@ -456,8 +456,9 @@ COPYRIGHT: gitlab-go mix-delete command [command options] [arguments...] COMMANDS: - artifact, artifacts 根据项目路径/ID、流水线IID范围删除产物(混合命令,立即删除) - help, h Shows a list of commands or help for one command + artifact, artifacts 根据项目路径/ID、流水线IID范围删除产物(混合命令,立即删除) + all-artifact, all-artifacts 根据项目路径/ID删除所有产物(混合命令,立即删除) + 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%] diff --git a/mix/delete_all_artifacts.go b/mix/delete_all_artifacts.go new file mode 100644 index 0000000000000000000000000000000000000000..e95db53c6d35b7878ee6e7b788f076aa0a93928f --- /dev/null +++ b/mix/delete_all_artifacts.go @@ -0,0 +1,106 @@ +package mix + +import ( + "fmt" + "github.com/urfave/cli/v2" + "github.com/xanzy/go-gitlab" + "github.com/xuxiaowei-com-cn/gitlab-go/constant" + "github.com/xuxiaowei-com-cn/gitlab-go/flag" + "github.com/xuxiaowei-com-cn/gitlab-go/pipelines" + "log" +) + +// DeleteAllArtifacts 删除所有产物 +func DeleteAllArtifacts() *cli.Command { + return &cli.Command{ + Name: "all-artifact", + Aliases: []string{"all-artifacts"}, + Usage: "根据项目路径/ID删除所有产物(混合命令,立即删除)", + Flags: append(flag.CommonTokenRequired(), flag.Sort(), flag.Page(), flag.PerPage(), flag.Id(true)), + Action: func(context *cli.Context) error { + var baseUrl = context.String(constant.BaseUrl) + var token = context.String(constant.Token) + var sortStr = context.String(constant.Sort) + var id = context.String(constant.Id) + var page = context.Int(constant.Page) + var perPage = context.Int(constant.PerPage) + + gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl)) + if err != nil { + return err + } + + err = DeleteAllArtifactsRecursion(gitClient, id, page, perPage, sortStr) + if err != nil { + return err + } + + return nil + }, + } +} + +func DeleteAllArtifactsRecursion(gitClient *gitlab.Client, id interface{}, page int, perPage int, sort string) error { + + pipelineInfos, response, err := pipelines.ListProjectPipelines(gitClient, id, page, perPage, sort) + + if err != nil { + return err + } + + log.Printf("Page %d, PerPage: %d, Response StatusCode: %d\n", page, perPage, response.Response.StatusCode) + + for _, pipelineInfo := range pipelineInfos { + fmt.Printf("%d\n", pipelineInfo.IID) + + err = ExecuteDeleteAllArtifacts(gitClient, id, pipelineInfo.ID, 1, 100) + if err != nil { + return err + } + + } + + if len(pipelineInfos) > 0 { + err := DeleteAllArtifactsRecursion(gitClient, id, page+1, perPage, sort) + if err != nil { + return err + } + } + + return nil +} + +func ExecuteDeleteAllArtifacts(gitClient *gitlab.Client, id interface{}, pipelineInfoId int, page int, perPage int) error { + fmt.Printf("执行删除 %d \n", pipelineInfoId) + + opt := &gitlab.ListJobsOptions{ + ListOptions: gitlab.ListOptions{ + Page: page, + PerPage: perPage, + }, + } + + jobs, response, err := gitClient.Jobs.ListPipelineJobs(id, pipelineInfoId, opt) + if err != nil { + return err + } + + log.Printf("List Project %s Pipeline %d Jobs Response StatusCode: %d\n", id, pipelineInfoId, response.Response.StatusCode) + + for _, job := range jobs { + response, err = gitClient.Jobs.DeleteArtifacts(id, job.ID) + if err != nil { + return err + } + log.Printf("Delete Project %s Job %d Response StatusCode: %d\n", id, job.ID, response.Response.StatusCode) + } + + if len(jobs) == perPage { + err = ExecuteDeleteAllArtifacts(gitClient, id, pipelineInfoId, page+1, perPage) + if err != nil { + return err + } + } + + return nil +} diff --git a/mix/mix_delete.go b/mix/mix_delete.go index 4b7ee98e3b9efcbf3dd265cab561ddef799a65b2..941616de1ad70a401af49748450cbb8459c40eb4 100644 --- a/mix/mix_delete.go +++ b/mix/mix_delete.go @@ -14,6 +14,7 @@ func Delete() *cli.Command { Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.Id(false), flag.IIdRange(false)), Subcommands: []*cli.Command{ DeleteArtifacts(), + DeleteAllArtifacts(), }, } }