...
 
Commits (5)
    https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/918f947fb3f07e53cc27fb28dbdd1d19ce69b002 :sparkles: 环境 API 2024-01-15T14:17:34+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/a1f455d09516f84030a841614782fe0a1588d450 :construction_worker: 修正 rules 2024-01-15T14:46:22+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/377f1cd8cc73a2dd5252a4398cc0b21e8d641382 :memo: GOPROXY 2024-01-15T16:47:21+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/032b7fe1507eb5d364590ead037d2d1742c50327 :bug: 修正 Usage 2024-01-15T16:55:31+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/6caebc97c5e1db70ff0a98c0631a1b7c8d6092c0 :sparkles: 环境 API:创建新环境 2024-01-15T17:10:20+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn
......@@ -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 形式,键:代表文件名,值:代表下载链接)作为产物
......@@ -140,6 +140,7 @@
```shell
go env -w GOPROXY=https://goproxy.cn,direct
# go env -w GOPROXY=https://proxy.golang.org,direct
# go env -w GOPROXY=https://goproxy.io,direct
# go env -w GOPROXY=https://mirrors.aliyun.com/goproxy,direct
# go env -w GOPROXY=https://mirrors.cloud.tencent.com/go,direct
......@@ -180,7 +181,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 +193,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 +266,35 @@ 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 列举环境
create 创建新环境
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 编码的路径
--name value 环境名称
--external_url value 该环境的链接位置
--tier value 新环境的层级。允许设置的值为 production, staging, testing, development 和 other
--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
......
......@@ -31,4 +31,7 @@ const (
SkipProjectWikiPath = "skip-project-wiki-path"
AutoSkipExistFolder = "auto-skip-exist-folder"
AllowFailure = "allow-failure"
EnvName = "name"
EnvExternalUrl = "external_url"
EnvTier = "tier"
)
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"
)
// Create 创建新环境 https://docs.gitlab.cn/jh/api/environments.html#%E5%88%9B%E5%BB%BA%E6%96%B0%E7%8E%AF%E5%A2%83
func Create() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "创建新环境",
Flags: append(flag.CommonTokenRequired(), flag.Id(true), flag.EnvName(true), flag.EnvExternalUrl(), flag.EnvTier(),
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 name = context.String(constant.EnvName)
var externalURL = context.String(constant.EnvExternalUrl)
var tier = context.String(constant.EnvTier)
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.CreateEnvironmentOptions{
Name: &name,
ExternalURL: &externalURL,
}
if tier != "" {
opt.Tier = &tier
}
environment, response, err := gitClient.Environments.CreateEnvironment(id, opt)
if err != nil {
return err
}
log.Printf("Response StatusCode: %d\n", response.Response.StatusCode)
fmt.Println("")
if printJson {
if printTime {
jsonData, err := json.Marshal(environment)
if err != nil {
panic(err)
}
log.Printf("\n%s\n", string(jsonData))
fmt.Println("")
} else {
jsonData, err := json.Marshal(environment)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(jsonData))
fmt.Println("")
}
} else {
if printTime {
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 {
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
},
}
}
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.EnvName(false), flag.EnvExternalUrl(), flag.EnvTier(),
flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime()),
Subcommands: []*cli.Command{
List(),
Create(),
},
}
}
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
},
}
}
......@@ -249,3 +249,25 @@ func AllowFailure() cli.Flag {
Value: false,
}
}
func EnvName(required bool) cli.Flag {
return &cli.StringFlag{
Name: constant.EnvName,
Usage: "环境名称",
Required: required,
}
}
func EnvExternalUrl() cli.Flag {
return &cli.StringFlag{
Name: constant.EnvExternalUrl,
Usage: "该环境的链接位置",
}
}
func EnvTier() cli.Flag {
return &cli.StringFlag{
Name: constant.EnvTier,
Usage: "新环境的层级。允许设置的值为 production, staging, testing, development 和 other",
}
}
......@@ -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
)
......@@ -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=
......
......@@ -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(),
......
......@@ -216,6 +216,8 @@ sync-framagit:
rules:
# GitCode 实例不执行
- if: $CI_SERVER_HOST == 'gitcode.net'
# 不执行
when: never
# framagit.org 不同步到自己
- if: $CI_SERVER_HOST == 'framagit.org'
# 不执行
......