diff --git a/README.md b/README.md index e2269fbf07e2c331a4e71f1bc9301b95fd5b3c7e..f7b2be60c03f1c864612a8ab3c563119d43280fe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 8c0c791006cd4b32a72a6456efd62556ffc16f1a..b64c6cd887d4f7eebd1386348b693b6e2540586f 100644 --- a/main.go +++ b/main.go @@ -87,6 +87,7 @@ func main() { mix.Delete(), mix.Environments(), mix.Export(), + mix.ProtectBranches(), mix.Transfer(), mix.Unarchive(), }, diff --git a/mix/mix_protect_branches.go b/mix/mix_protect_branches.go new file mode 100644 index 0000000000000000000000000000000000000000..1f3381801538674d1ae26aff2af8ad3aed51a3db --- /dev/null +++ b/mix/mix_protect_branches.go @@ -0,0 +1,21 @@ +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(), + }, + } +} diff --git a/mix/protect_branches_all.go b/mix/protect_branches_all.go new file mode 100644 index 0000000000000000000000000000000000000000..92ee4f18470fea983f9fac7766161bbc5b44e44b --- /dev/null +++ b/mix/protect_branches_all.go @@ -0,0 +1,58 @@ +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 + }, + } +}