|
| 1 | +// Copyright © 2018 github.com/devopsctl authors |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "github.com/spf13/cobra" |
| 25 | + gitlab "github.com/xanzy/go-gitlab" |
| 26 | +) |
| 27 | + |
| 28 | +var editBranchCmd = &cobra.Command{ |
| 29 | + Use: "branch", |
| 30 | + Aliases: []string{"b"}, |
| 31 | + Short: "Protect or unprotect a repositort branch", |
| 32 | + Example: `# protect a branch |
| 33 | +gitlabctl edit branch master -p devopsctl/gitlabctl --protect |
| 34 | +
|
| 35 | +# unprotect a branch |
| 36 | +gitlabctl edit branch master -p devopsctl/gitlabctl --unprotect`, |
| 37 | + SilenceErrors: true, |
| 38 | + SilenceUsage: true, |
| 39 | + DisableAutoGenTag: true, |
| 40 | + Args: cobra.ExactArgs(1), |
| 41 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + if err := validateFlagCombination(cmd, "protect", "dev-can-merge", "dev-can-push"); err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + if cmd.Flag("protect").Changed && cmd.Flag("unprotect").Changed { |
| 46 | + return newUsedTooManyFlagError("protect", "unprotect") |
| 47 | + } |
| 48 | + return nil |
| 49 | + }, |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + if cmd.Flag("protect").Changed { |
| 52 | + return runProtectBranch(cmd, args[0]) |
| 53 | + } |
| 54 | + return runUnProtectBranch(args[0], getFlagString(cmd, "project"), getFlagString(cmd, "out")) |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +func init() { |
| 59 | + editCmd.AddCommand(editBranchCmd) |
| 60 | + addProjectFlag(editBranchCmd) |
| 61 | + verifyMarkFlagRequired(editBranchCmd, "project") |
| 62 | + editBranchCmd.Flags().Bool("unprotect", false, |
| 63 | + "Remove protection of a branch") |
| 64 | + editBranchCmd.Flags().Bool("protect", false, |
| 65 | + "Protect a branch") |
| 66 | + editBranchCmd.Flags().Bool("dev-can-push", false, |
| 67 | + "Used with '--protect'. Flag if developers can push to the branch") |
| 68 | + editBranchCmd.Flags().Bool("dev-can-merge", false, |
| 69 | + "Used with '--protect'. Flag if developers can merge to the branch") |
| 70 | +} |
| 71 | + |
| 72 | +func runProtectBranch(cmd *cobra.Command, branch string) error { |
| 73 | + opts := new(gitlab.ProtectBranchOptions) |
| 74 | + if cmd.Flag("dev-can-push").Changed { |
| 75 | + opts.DevelopersCanPush = gitlab.Bool(getFlagBool(cmd, "dev-can-push")) |
| 76 | + } |
| 77 | + if cmd.Flag("dev-can-merge").Changed { |
| 78 | + opts.DevelopersCanMerge = gitlab.Bool(getFlagBool(cmd, "dev-can-merge")) |
| 79 | + } |
| 80 | + branchInfo, err := protectBranch(branch, getFlagString(cmd, "project"), opts) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + printBranchOut(getFlagString(cmd, "out"), branchInfo) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func protectBranch(branch, project string, opts *gitlab.ProtectBranchOptions) (*gitlab.Branch, error) { |
| 89 | + git, err := newGitlabClient() |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + protectedBranch, _, err := git.Branches.ProtectBranch(project, branch, opts) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + return protectedBranch, nil |
| 98 | +} |
| 99 | + |
| 100 | +func runUnProtectBranch(branch, project, out string) error { |
| 101 | + branchInfo, err := unProtectBranch(branch, project) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + printBranchOut(out, branchInfo) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func unProtectBranch(branch, project string) (*gitlab.Branch, error) { |
| 110 | + git, err := newGitlabClient() |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + branchInfo, _, err := git.Branches.UnprotectBranch(project, branch) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + return branchInfo, nil |
| 119 | +} |
0 commit comments