Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
徐晓伟工作室
gitlab-go
提交
67202104
G
gitlab-go
项目概览
徐晓伟工作室
/
gitlab-go
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-go
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
67202104
编写于
1月 16, 2024
作者:
徐晓伟
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
受保护的分支 API:列出受保护的分支
上级
6cff8dc2
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
208 addition
and
0 deletion
+208
-0
README.md
README.md
+27
-0
constant/common.go
constant/common.go
+1
-0
flag/flag.go
flag/flag.go
+7
-0
main.go
main.go
+2
-0
protected_branches/list.go
protected_branches/list.go
+151
-0
protected_branches/protected_branches.go
protected_branches/protected_branches.go
+20
-0
未找到文件。
README.md
浏览文件 @
67202104
...
...
@@ -201,6 +201,7 @@ COMMANDS:
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
protected-branches, pb 受保护的分支 API,中文文档:https://docs.gitlab.cn/jh/api/protected_branches.html
mix-archive 归档(混合命令,多接口命令)
mix-create-project-level-variables 创建项目级别 CI/CD 变量(混合命令,多接口命令)
mix-delete, mix-rm 删除(混合命令,多接口命令)
...
...
@@ -553,6 +554,32 @@ COPYRIGHT:
--help, -h show help
```
-
[
protected-branches 受保护的分支 API
](
https://docs.gitlab.cn/jh/api/protected_branches.html
)
```shell
$ go run main.go protected-branches --help
NAME:
gitlab-go protected-branches - 受保护的分支 API,中文文档:https://docs.gitlab.cn/jh/api/protected_branches.html
USAGE:
gitlab-go protected-branches 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 编码的路径
--search value 要搜索的受保护分支的名称或部分名称
--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
```
-
mix-archive 归档(混合命令,多接口命令)
```shell
...
...
constant/common.go
浏览文件 @
67202104
...
...
@@ -41,4 +41,5 @@ const (
VariableMasked
=
"masked"
VariableRaw
=
"raw"
VariableEnvironmentScope
=
"environment-scope"
ProtectedBranchesSearch
=
"search"
)
flag/flag.go
浏览文件 @
67202104
...
...
@@ -327,3 +327,10 @@ func VariableEnvironmentScope() cli.Flag {
Value
:
"*"
,
}
}
func
ProtectedBranchesSearch
()
cli
.
Flag
{
return
&
cli
.
StringFlag
{
Name
:
constant
.
ProtectedBranchesSearch
,
Usage
:
"要搜索的受保护分支的名称或部分名称"
,
}
}
main.go
浏览文件 @
67202104
...
...
@@ -15,6 +15,7 @@ import (
"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"
"github.com/xuxiaowei-com-cn/gitlab-go/protected_branches"
"gopkg.in/yaml.v3"
"log"
"os"
...
...
@@ -80,6 +81,7 @@ func main() {
pipelines
.
Pipelines
(),
project_level_variables
.
ProjectLevelVariables
(),
projects
.
Projects
(),
protected_branches
.
ProtectedBranches
(),
mix
.
Archive
(),
mix
.
CreateProjectLevelVariables
(),
mix
.
Delete
(),
...
...
protected_branches/list.go
0 → 100644
浏览文件 @
67202104
package
protected_branches
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/protected_branches.html#%E5%88%97%E5%87%BA%E5%8F%97%E4%BF%9D%E6%8A%A4%E7%9A%84%E5%88%86%E6%94%AF
func
List
()
*
cli
.
Command
{
return
&
cli
.
Command
{
Name
:
"list"
,
Usage
:
"列出受保护的分支"
,
Flags
:
append
(
flag
.
CommonTokenRequired
(),
flag
.
Id
(
true
),
flag
.
ProtectedBranchesSearch
(),
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
search
=
context
.
String
(
constant
.
Search
)
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
.
ListProtectedBranchesOptions
{
Search
:
&
search
,
ListOptions
:
gitlab
.
ListOptions
{
Page
:
page
,
PerPage
:
perPage
,
},
}
protectedBranchs
,
response
,
err
:=
gitClient
.
ProtectedBranches
.
ListProtectedBranches
(
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
_
,
protectedBranch
:=
range
protectedBranchs
{
jsonData
,
err
:=
json
.
Marshal
(
protectedBranch
)
if
err
!=
nil
{
panic
(
err
)
}
log
.
Printf
(
"
\n
%s
\n
"
,
string
(
jsonData
))
fmt
.
Println
(
""
)
}
}
else
{
for
_
,
protectedBranch
:=
range
protectedBranchs
{
jsonData
,
err
:=
json
.
Marshal
(
protectedBranch
)
if
err
!=
nil
{
panic
(
err
)
}
fmt
.
Printf
(
"%s
\n
"
,
string
(
jsonData
))
fmt
.
Println
(
""
)
}
}
}
else
{
if
printTime
{
for
_
,
protectedBranch
:=
range
protectedBranchs
{
log
.
Printf
(
"ID: %d
\n
"
,
protectedBranch
.
ID
)
log
.
Printf
(
"Name: %s
\n
"
,
protectedBranch
.
Name
)
log
.
Printf
(
"AllowForcePush: %t
\n
"
,
protectedBranch
.
AllowForcePush
)
log
.
Printf
(
"CodeOwnerApprovalRequired: %t
\n
"
,
protectedBranch
.
CodeOwnerApprovalRequired
)
log
.
Printf
(
"PushAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
PushAccessLevels
{
log
.
Printf
(
"PushAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
log
.
Printf
(
"PushAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
log
.
Printf
(
"PushAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
log
.
Printf
(
"PushAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
log
.
Printf
(
"PushAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
log
.
Printf
(
"MergeAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
MergeAccessLevels
{
log
.
Printf
(
"MergeAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
log
.
Printf
(
"MergeAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
log
.
Printf
(
"MergeAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
log
.
Printf
(
"MergeAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
log
.
Printf
(
"MergeAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
log
.
Printf
(
"UnprotectAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
UnprotectAccessLevels
{
log
.
Printf
(
"UnprotectAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
log
.
Printf
(
"UnprotectAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
log
.
Printf
(
"UnprotectAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
log
.
Printf
(
"UnprotectAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
log
.
Printf
(
"UnprotectAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
fmt
.
Println
(
""
)
}
}
else
{
for
_
,
protectedBranch
:=
range
protectedBranchs
{
fmt
.
Printf
(
"ID: %d
\n
"
,
protectedBranch
.
ID
)
fmt
.
Printf
(
"Name: %s
\n
"
,
protectedBranch
.
Name
)
fmt
.
Printf
(
"AllowForcePush: %t
\n
"
,
protectedBranch
.
AllowForcePush
)
fmt
.
Printf
(
"CodeOwnerApprovalRequired: %t
\n
"
,
protectedBranch
.
CodeOwnerApprovalRequired
)
fmt
.
Printf
(
"PushAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
PushAccessLevels
{
fmt
.
Printf
(
"PushAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
fmt
.
Printf
(
"PushAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
fmt
.
Printf
(
"PushAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
fmt
.
Printf
(
"PushAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
fmt
.
Printf
(
"PushAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
fmt
.
Printf
(
"MergeAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
MergeAccessLevels
{
fmt
.
Printf
(
"MergeAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
fmt
.
Printf
(
"MergeAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
fmt
.
Printf
(
"MergeAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
fmt
.
Printf
(
"MergeAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
fmt
.
Printf
(
"MergeAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
fmt
.
Printf
(
"UnprotectAccessLevels:
\n
"
)
for
index
,
branchAccessDescription
:=
range
protectedBranch
.
UnprotectAccessLevels
{
fmt
.
Printf
(
"UnprotectAccessLevels[%d] ID: %d
\n
"
,
index
,
branchAccessDescription
.
ID
)
fmt
.
Printf
(
"UnprotectAccessLevels[%d] AccessLevel: %d
\n
"
,
index
,
branchAccessDescription
.
AccessLevel
)
fmt
.
Printf
(
"UnprotectAccessLevels[%d] AccessLevelDescription: %s
\n
"
,
index
,
branchAccessDescription
.
AccessLevelDescription
)
fmt
.
Printf
(
"UnprotectAccessLevels[%d] UserID: %d
\n
"
,
index
,
branchAccessDescription
.
UserID
)
fmt
.
Printf
(
"UnprotectAccessLevels[%d] GroupID: %d
\n
"
,
index
,
branchAccessDescription
.
GroupID
)
}
fmt
.
Println
(
""
)
}
}
}
return
nil
},
}
}
protected_branches/protected_branches.go
0 → 100644
浏览文件 @
67202104
package
protected_branches
import
(
"github.com/urfave/cli/v2"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
)
// ProtectedBranches 受保护的分支 API https://docs.gitlab.cn/jh/api/protected_branches.html
func
ProtectedBranches
()
*
cli
.
Command
{
return
&
cli
.
Command
{
Name
:
"protected-branches"
,
Aliases
:
[]
string
{
"pb"
},
Usage
:
"受保护的分支 API,中文文档:https://docs.gitlab.cn/jh/api/protected_branches.html"
,
Flags
:
append
(
flag
.
Common
(),
flag
.
Id
(
false
),
flag
.
ProtectedBranchesSearch
(),
flag
.
Page
(),
flag
.
PerPage
(),
flag
.
PrintJson
(),
flag
.
PrintTime
()),
Subcommands
:
[]
*
cli
.
Command
{
List
(),
},
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录