From cef6c565ab6ee9302b0e9fc07c2623d4b80418dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Thu, 26 Oct 2023 15:21:09 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B5=81=E6=B0=B4=E7=BA=BF=20API:?= =?UTF-8?q?=20=E5=88=97=E5=87=BA=E9=A1=B9=E7=9B=AE=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BF:=20=E9=80=92=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + pipelines/list.go | 140 ++++++++++++++++++++++++----------------- pipelines/pipelines.go | 2 +- 3 files changed, 84 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index f384e18..31eb246 100644 --- a/README.md +++ b/README.md @@ -411,6 +411,7 @@ COPYRIGHT: --per-page value 每页列出的项目数(默认:20;最大:100),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 20) --print-json 打印 JSON (default: false) --print-time 打印时间 (default: false) + --recursion 递归 (default: false) --id value 项目 ID 或 URL 编码的路径 --help, -h show help ``` diff --git a/pipelines/list.go b/pipelines/list.go index 0bfbe77..60553d6 100644 --- a/pipelines/list.go +++ b/pipelines/list.go @@ -15,7 +15,7 @@ func List() *cli.Command { return &cli.Command{ Name: "list", Usage: "列出项目流水线", - Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), + Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), flag.Recursion(), flag.Id(true)), Action: func(context *cli.Context) error { var baseUrl = context.String(constant.BaseUrl) @@ -26,74 +26,98 @@ func List() *cli.Command { var perPage = context.Int(constant.PerPage) var printJson = context.Bool(constant.PrintJson) var printTime = context.Bool(constant.PrintTime) + var recursion = context.Bool(constant.Recursion) gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl)) if err != nil { return err } - opt := &gitlab.ListProjectPipelinesOptions{ - ListOptions: gitlab.ListOptions{ - Page: page, - PerPage: perPage, - }, - Sort: &sort, - } - pipelineInfos, response, err := gitClient.Pipelines.ListProjectPipelines(id, opt) + err = ListProjectPipelinesPrintln(gitClient, id, page, perPage, sort, printJson, printTime, recursion) if err != nil { return err } - log.Printf("Page %d, PerPage: %d, Response StatusCode: %d\n", page, perPage, response.Response.StatusCode) - - fmt.Println("") - - if printJson { - if printTime { - for _, pipelineInfo := range pipelineInfos { - jsonData, err := json.Marshal(pipelineInfo) - if err != nil { - panic(err) - } - - log.Printf("\n%s\n", string(jsonData)) - fmt.Println("") - } - } else { - for _, pipelineInfo := range pipelineInfos { - jsonData, err := json.Marshal(pipelineInfo) - if err != nil { - panic(err) - } - - fmt.Printf("%s\n", string(jsonData)) - fmt.Println("") - } - } - } else { - if printTime { - for _, pipelineInfo := range pipelineInfos { - log.Printf("ID: %d\n", pipelineInfo.ID) - log.Printf("IID: %d\n", pipelineInfo.IID) - log.Printf("ProjectID: %d\n", pipelineInfo.ProjectID) - log.Printf("Status: %s\n", pipelineInfo.Status) - log.Printf("CreatedAt: %s\n", pipelineInfo.CreatedAt) - - fmt.Println("") - } - } else { - for _, pipelineInfo := range pipelineInfos { - fmt.Printf("ID: %d\n", pipelineInfo.ID) - fmt.Printf("IID: %d\n", pipelineInfo.IID) - fmt.Printf("ProjectID: %d\n", pipelineInfo.ProjectID) - fmt.Printf("Status: %s\n", pipelineInfo.Status) - fmt.Printf("CreatedAt: %s\n", pipelineInfo.CreatedAt) - - fmt.Println("") - } - } - } return nil }, } } + +func ListProjectPipelines(gitClient *gitlab.Client, id interface{}, page int, perPage int, sort string) ([]*gitlab.PipelineInfo, *gitlab.Response, error) { + opt := &gitlab.ListProjectPipelinesOptions{ + ListOptions: gitlab.ListOptions{ + Page: page, + PerPage: perPage, + }, + Sort: &sort, + } + pipelineInfos, response, err := gitClient.Pipelines.ListProjectPipelines(id, opt) + return pipelineInfos, response, err +} + +func ListProjectPipelinesPrintln(gitClient *gitlab.Client, id interface{}, page int, perPage int, sort string, printJson bool, printTime bool, recursion bool) error { + + pipelineInfos, response, err := 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) + + fmt.Println("") + + if printJson { + if printTime { + for _, pipelineInfo := range pipelineInfos { + jsonData, err := json.Marshal(pipelineInfo) + if err != nil { + panic(err) + } + + log.Printf("\n%s\n", string(jsonData)) + fmt.Println("") + } + } else { + for _, pipelineInfo := range pipelineInfos { + jsonData, err := json.Marshal(pipelineInfo) + if err != nil { + panic(err) + } + + fmt.Printf("%s\n", string(jsonData)) + fmt.Println("") + } + } + } else { + if printTime { + for _, pipelineInfo := range pipelineInfos { + log.Printf("ID: %d\n", pipelineInfo.ID) + log.Printf("IID: %d\n", pipelineInfo.IID) + log.Printf("ProjectID: %d\n", pipelineInfo.ProjectID) + log.Printf("Status: %s\n", pipelineInfo.Status) + log.Printf("CreatedAt: %s\n", pipelineInfo.CreatedAt) + + fmt.Println("") + } + } else { + for _, pipelineInfo := range pipelineInfos { + fmt.Printf("ID: %d\n", pipelineInfo.ID) + fmt.Printf("IID: %d\n", pipelineInfo.IID) + fmt.Printf("ProjectID: %d\n", pipelineInfo.ProjectID) + fmt.Printf("Status: %s\n", pipelineInfo.Status) + fmt.Printf("CreatedAt: %s\n", pipelineInfo.CreatedAt) + + fmt.Println("") + } + } + } + + if recursion && len(pipelineInfos) > 0 { + err := ListProjectPipelinesPrintln(gitClient, id, page+1, perPage, sort, printJson, printTime, recursion) + if err != nil { + return err + } + } + + return nil +} diff --git a/pipelines/pipelines.go b/pipelines/pipelines.go index 5e39190..f6f3716 100644 --- a/pipelines/pipelines.go +++ b/pipelines/pipelines.go @@ -11,7 +11,7 @@ func Pipelines() *cli.Command { Name: "pipeline", Aliases: []string{"pipelines", "pl"}, Usage: "流水线 API,中文文档:https://docs.gitlab.cn/jh/api/pipelines.html", - Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), + Flags: append(flag.Common(), flag.Sort(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), flag.Recursion(), flag.Id(false)), Subcommands: []*cli.Command{ List(),