项目级别 CI/CD 变量 API:列出项目变量

This commit is contained in:
徐晓伟 2024-01-16 09:32:49 +08:00
parent 026a48c896
commit 0260f9bb39
4 changed files with 144 additions and 0 deletions

View File

@ -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

View File

@ -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(),

View File

@ -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
},
}
}

View File

@ -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(),
},
}
}