提交 02d1e0e4 编写于 作者: 徐晓伟's avatar 徐晓伟

保护仓库分支:保护所有仓库分支

上级 2143aa29
......@@ -207,6 +207,7 @@ COMMANDS:
mix-delete, mix-rm 删除(混合命令,多接口命令)
mix-create-environments, mix-create-environment, mix-create-env 创建新环境(混合命令,多接口命令)
mix-export 导出(混合命令,多接口命令)
mix-protect-branches 保护仓库分支(混合命令,多接口命令)
mix-transfer 转移(混合命令,多接口命令)
mix-unarchive 取消归档(混合命令,多接口命令)
help, h Shows a list of commands or help for one command
......@@ -752,6 +753,62 @@ COPYRIGHT:
--help, -h show help
```
- 保护仓库分支(混合命令,多接口命令)
```shell
$ go run main.go mix-protect-branches --help
NAME:
gitlab-go mix-protect-branches - 保护仓库分支(混合命令,多接口命令)
USAGE:
gitlab-go mix-protect-branches command [command options]
COMMANDS:
all, a 保护所有仓库分支
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
--owned 当前用户明确拥有的项目。 (default: false)
--name value 分支或通配符的名称
--push-access-level value 允许推送的访问级别(默认值:40,维护者角色),合法值:
0:NoPermissions
5:MinimalAccessPermissions
10:GuestPermissions
20:ReporterPermissions
30:DeveloperPermissions
40:MaintainerPermissions
50:OwnerPermissions
60:AdminPermissions
(default: 40)
--merge-access-level value 允许合并的访问级别(默认值:40,维护者角色),合法值:
0:NoPermissions
5:MinimalAccessPermissions
10:GuestPermissions
20:ReporterPermissions
30:DeveloperPermissions
40:MaintainerPermissions
50:OwnerPermissions
60:AdminPermissions
(default: 40)
--unprotect-access-level value 允许取消保护的访问级别(默认值:40,维护者角色),合法值:
0:NoPermissions
5:MinimalAccessPermissions
10:GuestPermissions
20:ReporterPermissions
30:DeveloperPermissions
40:MaintainerPermissions
50:OwnerPermissions
60:AdminPermissions
(default: 40)
--allow-force-push 启用后,可以推送到该分支的成员也可以强制推送 (default: false)
--code-owner-approval-required 如果分支在 CODEOWNERS https://docs.gitlab.cn/jh/user/project/codeowners/index.html 文件中,则阻止推送到此分支。(默认值:false) (default: false)
--print-json 打印 JSON (default: false)
--print-time 打印时间 (default: false)
--help, -h show help
```
- 转移(混合命令,多接口命令)
```shell
......
......@@ -87,6 +87,7 @@ func main() {
mix.Delete(),
mix.Environments(),
mix.Export(),
mix.ProtectBranches(),
mix.Transfer(),
mix.Unarchive(),
},
......
package mix
import (
"github.com/urfave/cli/v2"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
)
// ProtectBranches 保护仓库分支
func ProtectBranches() *cli.Command {
return &cli.Command{
Name: "mix-protect-branches",
Usage: "保护仓库分支(混合命令,多接口命令)",
Flags: append(flag.Common(), flag.Owned(false),
flag.BranchName(false), flag.PushAccessLevel(), flag.MergeAccessLevel(),
flag.UnprotectAccessLevel(), flag.AllowForcePush(), flag.CodeOwnerApprovalRequired(),
flag.PrintJson(), flag.PrintTime()),
Subcommands: []*cli.Command{
ProtectBranchesAll(),
},
}
}
package mix
import (
"github.com/urfave/cli/v2"
"github.com/xuxiaowei-com-cn/gitlab-go/constant"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
"github.com/xuxiaowei-com-cn/gitlab-go/projects"
"github.com/xuxiaowei-com-cn/gitlab-go/protected_branches"
"log"
)
// ProtectBranchesAll 保护所有仓库分支
func ProtectBranchesAll() *cli.Command {
return &cli.Command{
Name: "all",
Aliases: []string{"a"},
Usage: "保护所有仓库分支",
Flags: append(flag.CommonTokenRequired(), flag.Owned(true),
flag.BranchName(true), flag.PushAccessLevel(), flag.MergeAccessLevel(),
flag.UnprotectAccessLevel(), flag.AllowForcePush(), flag.CodeOwnerApprovalRequired(),
flag.AllowFailure(),
flag.PrintJson(), flag.PrintTime()),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
var token = context.String(constant.Token)
var owned = context.Bool(constant.Owned)
var name = context.String(constant.BranchName)
var pushAccessLevel = context.Int(constant.PushAccessLevel)
var mergeAccessLevel = context.Int(constant.MergeAccessLevel)
var unprotectAccessLevel = context.Int(constant.UnprotectAccessLevel)
var allowForcePush = context.Bool(constant.AllowForcePush)
var codeOwnerApprovalRequired = context.Bool(constant.CodeOwnerApprovalRequired)
var printJson = context.Bool(constant.PrintJson)
var printTime = context.Bool(constant.PrintTime)
var allowFailure = context.Bool(constant.AllowFailure)
projectList, err := projects.ListProjects(owned, token, baseUrl, 1, 100)
if err != nil {
return err
}
for index, project := range projectList {
log.Printf("Project Index: %d, WebURL: %s", index, project.WebURL)
err = protected_branches.ProtectRepositoryBranches(baseUrl, token, project.ID, name,
pushAccessLevel, mergeAccessLevel, unprotectAccessLevel, allowForcePush, codeOwnerApprovalRequired,
printJson, printTime, allowFailure)
if err != nil {
return err
}
}
return nil
},
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册