From 0260f9bb39591f80806bb23b7d7bf5c1cf6098eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Tue, 16 Jan 2024 09:32:49 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=A1=B9=E7=9B=AE=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=20CI/CD=20=E5=8F=98=E9=87=8F=20API=EF=BC=9A=E5=88=97?= =?UTF-8?q?=E5=87=BA=E9=A1=B9=E7=9B=AE=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 +++++ main.go | 2 + project_level_variables/list.go | 98 +++++++++++++++++++ .../project_level_variables.go | 19 ++++ 4 files changed, 144 insertions(+) create mode 100644 project_level_variables/list.go create mode 100644 project_level_variables/project_level_variables.go diff --git a/README.md b/README.md index 8b00651..1718e3e 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,7 @@ COMMANDS: job-artifact, job-artifacts, ja 作业产物 API,中文文档:https://docs.gitlab.cn/jh/api/job_artifacts.html job, jobs, j 作业 API,中文文档:https://docs.gitlab.cn/jh/api/jobs.html pipeline, pipelines, pl 流水线 API,中文文档:https://docs.gitlab.cn/jh/api/pipelines.html + project-level-variables, project-level-variable, plv 项目级别 CI/CD 变量 API,中文文档:https://docs.gitlab.cn/jh/api/project_level_variables.html project, projects, p 项目 API,中文文档:https://docs.gitlab.cn/jh/api/projects.html mix-archive 归档(混合命令,多接口命令) mix-delete, mix-rm 删除(混合命令,多接口命令) @@ -490,6 +491,30 @@ COPYRIGHT: --help, -h show help ``` +- [project-level-variables 项目级别 CI/CD 变量 API](https://docs.gitlab.cn/jh/api/project_level_variables.html) + + ```shell + $ go run main.go project-level-variables --help + NAME: + gitlab-go project-level-variables - 项目级别 CI/CD 变量 API,中文文档:https://docs.gitlab.cn/jh/api/project_level_variables.html + + USAGE: + gitlab-go project-level-variables 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 + --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 + ``` + - [project - 项目 API](https://docs.gitlab.cn/jh/api/projects.html) ```shell diff --git a/main.go b/main.go index 6050f7c..a431126 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/xuxiaowei-com-cn/gitlab-go/jobs" "github.com/xuxiaowei-com-cn/gitlab-go/mix" "github.com/xuxiaowei-com-cn/gitlab-go/pipelines" + "github.com/xuxiaowei-com-cn/gitlab-go/project_level_variables" "github.com/xuxiaowei-com-cn/gitlab-go/projects" "gopkg.in/yaml.v3" "log" @@ -77,6 +78,7 @@ func main() { job_artifacts.JobsArtifacts(), jobs.Jobs(), pipelines.Pipelines(), + project_level_variables.ProjectLevelVariables(), projects.Projects(), mix.Archive(), mix.Delete(), diff --git a/project_level_variables/list.go b/project_level_variables/list.go new file mode 100644 index 0000000..e5ead1a --- /dev/null +++ b/project_level_variables/list.go @@ -0,0 +1,98 @@ +package project_level_variables + +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/project_level_variables.html#%E5%88%97%E5%87%BA%E9%A1%B9%E7%9B%AE%E5%8F%98%E9%87%8F +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.ListProjectVariablesOptions{ + Page: page, + PerPage: perPage, + } + projectVariables, response, err := gitClient.ProjectVariables.ListVariables(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 _, projectVariable := range projectVariables { + jsonData, err := json.Marshal(projectVariable) + if err != nil { + panic(err) + } + + log.Printf("\n%s\n", string(jsonData)) + fmt.Println("") + } + } else { + for _, projectVariable := range projectVariables { + jsonData, err := json.Marshal(projectVariable) + if err != nil { + panic(err) + } + + fmt.Printf("%s\n", string(jsonData)) + fmt.Println("") + } + } + } else { + if printTime { + for _, projectVariable := range projectVariables { + log.Printf("Key: %s\n", projectVariable.Key) + log.Printf("Value: %s\n", projectVariable.Value) + log.Printf("VariableType: %s\n", projectVariable.VariableType) + log.Printf("Protected: %t\n", projectVariable.Protected) + log.Printf("Masked: %t\n", projectVariable.Masked) + log.Printf("Raw: %t\n", projectVariable.Raw) + log.Printf("EnvironmentScope: %s\n", projectVariable.EnvironmentScope) + + fmt.Println("") + } + } else { + for _, projectVariable := range projectVariables { + fmt.Printf("Key: %s\n", projectVariable.Key) + fmt.Printf("Value: %s\n", projectVariable.Value) + fmt.Printf("VariableType: %s\n", projectVariable.VariableType) + fmt.Printf("Protected: %t\n", projectVariable.Protected) + fmt.Printf("Masked: %t\n", projectVariable.Masked) + fmt.Printf("Raw: %t\n", projectVariable.Raw) + fmt.Printf("EnvironmentScope: %s\n", projectVariable.EnvironmentScope) + + fmt.Println("") + } + } + } + + return nil + }, + } +} diff --git a/project_level_variables/project_level_variables.go b/project_level_variables/project_level_variables.go new file mode 100644 index 0000000..c28aaae --- /dev/null +++ b/project_level_variables/project_level_variables.go @@ -0,0 +1,19 @@ +package project_level_variables + +import ( + "github.com/urfave/cli/v2" + "github.com/xuxiaowei-com-cn/gitlab-go/flag" +) + +// ProjectLevelVariables 项目级别 CI/CD 变量 API https://docs.gitlab.cn/jh/api/project_level_variables.html +func ProjectLevelVariables() *cli.Command { + return &cli.Command{ + Name: "project-level-variables", + Aliases: []string{"project-level-variable", "plv"}, + Usage: "项目级别 CI/CD 变量 API,中文文档:https://docs.gitlab.cn/jh/api/project_level_variables.html", + Flags: append(flag.Common(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime()), + Subcommands: []*cli.Command{ + List(), + }, + } +}