From 918f947fb3f07e53cc27fb28dbdd1d19ce69b002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Mon, 15 Jan 2024 14:00:45 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=8E=AF=E5=A2=83=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 32 +++++++++-- environments/environments.go | 20 +++++++ environments/list.go | 101 +++++++++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 8 +-- main.go | 2 + 6 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 environments/environments.go create mode 100644 environments/list.go diff --git a/README.md b/README.md index 49b799a..f7c3cd2 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,8 @@ 1. 一键发布到 github,可包含产物上传 2. 一键发布到 gitlab,可包含产物上传,可自定义域名(支持自建 gitlab),支持将产物文件名、链接导出为 map,可供 gitee 使用 3. 一键发布到 gitee,由于 gitee 暂不支持提供上传产物的 API 接口, - 本工具支持提供 json 文件(map 形式,键:代表文件名,值:代表下载链接)作为产物,本项目使用 [GitLink](https://www.gitlink.org.cn) 作为 - gitee 产物链接 + 本工具支持提供 json 文件(map 形式,键:代表文件名,值:代表下载链接)作为产物, + 本项目使用 [GitLink](https://www.gitlink.org.cn) 作为 gitee 产物链接 4. 一键发布到 gitlink,可包含产物上传(需要等到官方开放 token 功能,或者联系官方人员申请 token 才能使用), 本工具支持提供 json 文件(map 形式,键:代表文件名,值:代表下载链接)作为产物 @@ -180,7 +180,7 @@ NAME: gitlab-go - 基于 Go 语言开发的 GitLab 命令行工具 USAGE: - gitlab-go [global options] command [command options] [arguments...] + gitlab-go [global options] command [command options] VERSION: dev @@ -192,6 +192,7 @@ COMMANDS: access-request, access-requests, ar 群组和项目访问请求 API,中文文档:https://docs.gitlab.cn/jh/api/access_requests.html board, boards 项目议题板 API,中文文档:https://docs.gitlab.cn/jh/api/boards.html container-registry, cr 容器仓库 API,中文文档:https://docs.gitlab.cn/jh/api/container_registry.html + environments, env 环境 API,中文文档:https://docs.gitlab.cn/jh/api/environments.html instance-level-ci-variable, instance-level-ci-variables, ilcv 实例级 CI/CD 变量 API,中文文档:https://docs.gitlab.cn/jh/api/instance_level_ci_variables.html issue, issues 议题 API,中文文档:https://docs.gitlab.cn/jh/api/issues.html job-artifact, job-artifacts, ja 作业产物 API,中文文档:https://docs.gitlab.cn/jh/api/job_artifacts.html @@ -264,6 +265,31 @@ COPYRIGHT: --help, -h show help ``` +- [environments 环境 API](https://docs.gitlab.cn/jh/api/environments.html) + + ```shell + $ go run main.go environments --help + NAME: + gitlab-go environments - 环境 API,中文文档:https://docs.gitlab.cn/jh/api/environments.html + + USAGE: + gitlab-go environments command [command options] + + COMMANDS: + list 列出所有实例变量 + 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 + --id value 项目 ID 或 URL 编码的路径 + --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) + --print-json 打印 JSON (default: false) + --print-time 打印时间 (default: false) + --help, -h show help + ``` + - [container-registry - 容器仓库 API](https://docs.gitlab.cn/jh/api/container_registry.html) ```shell diff --git a/environments/environments.go b/environments/environments.go new file mode 100644 index 0000000..4a73259 --- /dev/null +++ b/environments/environments.go @@ -0,0 +1,20 @@ +package environments + +import ( + "github.com/urfave/cli/v2" + "github.com/xuxiaowei-com-cn/gitlab-go/flag" +) + +// Environments 环境 API https://docs.gitlab.cn/jh/api/environments.html +func Environments() *cli.Command { + return &cli.Command{ + Name: "environments", + Aliases: []string{"env"}, + Usage: "环境 API,中文文档:https://docs.gitlab.cn/jh/api/environments.html", + Flags: append(flag.Common(), flag.Id(false), + flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime()), + Subcommands: []*cli.Command{ + List(), + }, + } +} diff --git a/environments/list.go b/environments/list.go new file mode 100644 index 0000000..f03e280 --- /dev/null +++ b/environments/list.go @@ -0,0 +1,101 @@ +package environments + +import ( + "encoding/json" + "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" + "log" +) + +// List 列举环境 https://docs.gitlab.cn/jh/api/environments.html#%E5%88%97%E4%B8%BE%E7%8E%AF%E5%A2%83 +func List() *cli.Command { + return &cli.Command{Name: "list", + Usage: "列出所有实例变量", + Flags: append(flag.CommonTokenRequired(), flag.Id(true), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime()), + Action: func(context *cli.Context) error { + var baseUrl = context.String(constant.BaseUrl) + var token = context.String(constant.Token) + var id = context.String(constant.Id) + var page = context.Int(constant.Page) + var perPage = context.Int(constant.PerPage) + var printJson = context.Bool(constant.PrintJson) + var printTime = context.Bool(constant.PrintTime) + + gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl)) + if err != nil { + return err + } + + opt := &gitlab.ListEnvironmentsOptions{ + ListOptions: gitlab.ListOptions{ + Page: page, + PerPage: perPage, + }, + } + environments, response, err := gitClient.Environments.ListEnvironments(id, opt) + 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 _, environment := range environments { + jsonData, err := json.Marshal(environment) + if err != nil { + panic(err) + } + + log.Printf("\n%s\n", string(jsonData)) + fmt.Println("") + } + } else { + for _, environment := range environments { + jsonData, err := json.Marshal(environment) + if err != nil { + panic(err) + } + + fmt.Printf("%s\n", string(jsonData)) + fmt.Println("") + } + } + } else { + if printTime { + for _, environment := range environments { + log.Printf("ID: %d\n", environment.ID) + log.Printf("Name: %s\n", environment.Name) + log.Printf("Slug: %s\n", environment.Slug) + log.Printf("State: %s\n", environment.State) + log.Printf("Tier: %s\n", environment.Tier) + log.Printf("ExternalURL: %s\n", environment.ExternalURL) + log.Printf("CreatedAt: %s\n", environment.CreatedAt) + log.Printf("UpdatedAt: %s\n", environment.UpdatedAt) + + fmt.Println("") + } + } else { + for _, environment := range environments { + fmt.Printf("ID: %d\n", environment.ID) + fmt.Printf("Name: %s\n", environment.Name) + fmt.Printf("Slug: %s\n", environment.Slug) + fmt.Printf("State: %s\n", environment.State) + fmt.Printf("Tier: %s\n", environment.Tier) + fmt.Printf("ExternalURL: %s\n", environment.ExternalURL) + fmt.Printf("CreatedAt: %s\n", environment.CreatedAt) + fmt.Printf("UpdatedAt: %s\n", environment.UpdatedAt) + + fmt.Println("") + } + } + } + + return nil + }, + } +} diff --git a/go.mod b/go.mod index eac4cd6..6887682 100644 --- a/go.mod +++ b/go.mod @@ -17,8 +17,8 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.5 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect - golang.org/x/oauth2 v0.15.0 // indirect + golang.org/x/oauth2 v0.16.0 // indirect golang.org/x/time v0.5.0 // indirect google.golang.org/appengine v1.6.8 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect ) diff --git a/go.sum b/go.sum index 09fb542..eb61461 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ= -golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM= +golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ= +golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -66,8 +66,8 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 345ed5c..a40d2cb 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/xuxiaowei-com-cn/gitlab-go/access_requests" "github.com/xuxiaowei-com-cn/gitlab-go/boards" "github.com/xuxiaowei-com-cn/gitlab-go/container_registry" + "github.com/xuxiaowei-com-cn/gitlab-go/environments" "github.com/xuxiaowei-com-cn/gitlab-go/instance_level_ci_variables" "github.com/xuxiaowei-com-cn/gitlab-go/issues" "github.com/xuxiaowei-com-cn/gitlab-go/job_artifacts" @@ -70,6 +71,7 @@ func main() { access_requests.AccessRequests(), boards.Boards(), container_registry.ContainerRegistry(), + environments.Environments(), instance_level_ci_variables.InstanceLevelCiVariables(), issues.Issues(), job_artifacts.JobsArtifacts(),