forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_branch.go
240 lines (217 loc) · 7.16 KB
/
delete_branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
var (
deleteBranchLong = templates.LongDesc(`
Deletes one or more branches in repositories.
Note that command will ask for confirmation before doing anything!
`)
deleteBranchExample = templates.Examples(`
# Selects the repositories to delete from the given GitHub organisation
jx delete branch --org myname --name myrepo -f updatebot- -a
`)
)
// DeleteBranchOptions the options for the create spring command
type DeleteBranchOptions struct {
CreateOptions
Organisation string
Repositories []string
GitHost string
GitHub bool
SelectAll bool
SelectFilter string
SelectAllRepos bool
SelectFilterRepos string
}
// NewCmdDeleteBranch creates a command object for the "delete repo" command
func NewCmdDeleteBranch(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteBranchOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "branch",
Short: "Deletes one or more branches in git repositories",
Aliases: []string{"repository"},
Long: deleteBranchLong,
Example: deleteBranchExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
//addDeleteFlags(cmd, &options.CreateOptions)
cmd.Flags().StringVarP(&options.Organisation, "org", "o", "", "Specify the Git provider organisation that includes the repository to delete")
cmd.Flags().StringArrayVarP(&options.Repositories, "name", "n", []string{}, "Specify the Git repository names to delete")
cmd.Flags().StringVarP(&options.GitHost, "git-host", "g", "", "The Git server host if not using GitHub")
cmd.Flags().BoolVarP(&options.GitHub, "github", "", false, "If you wish to pick the repositories from GitHub to import")
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "If selecting branches to remove this defaults to selecting them all")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "If selecting branches to remove this filters the list of repositories")
cmd.Flags().BoolVarP(&options.SelectAllRepos, "all-repos", "", false, "If selecting projects to remove branches this defaults to selecting them all")
cmd.Flags().StringVarP(&options.SelectFilterRepos, "filter-repos", "", "", "If selecting projects to remove brancehs this filters the list of repositories")
cmd.Flags().BoolVarP(&options.BatchMode, "batch-mode", "b", false, "Run without being prompted. WARNING! You will not be asked to confirm deletions if you use this flag.")
return cmd
}
// Run implements the command
func (o *DeleteBranchOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
var server *auth.AuthServer
config := authConfigSvc.Config()
if o.GitHub {
server = config.GetOrCreateServer(gits.GitHubURL)
} else {
if o.GitHost != "" {
server = config.GetOrCreateServer(o.GitHost)
} else {
server, err = config.PickServer("Pick the Git server to search for repositories", o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return err
}
}
}
if server == nil {
return fmt.Errorf("No Git server provided")
}
userAuth, err := config.PickServerUserAuth(server, "Git user name", o.BatchMode, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
provider, err := gits.CreateProvider(server, userAuth, o.Git())
if err != nil {
return err
}
username := userAuth.Username
org := o.Organisation
if org == "" {
org, err = gits.PickOrganisation(provider, username, o.In, o.Out, o.Err)
if err != nil {
return err
}
}
if org == "" {
org = username
}
names := o.Repositories
if len(names) == 0 {
repos, err := gits.PickRepositories(provider, org, "Which repositories do you want to remove branches from:", o.SelectAllRepos, o.SelectFilterRepos, o.In, o.Out, o.Err)
if err != nil {
return err
}
for _, r := range repos {
names = append(names, r.Name)
}
}
for _, name := range names {
repo, err := provider.GetRepository(org, name)
if err != nil {
return errors.Wrapf(err, "Failed to find repository for %s/%s", org, name)
}
dir, err := o.cloneOrPullRepository(org, name, repo.SSHURL)
if err != nil {
return errors.Wrapf(err, "Failed to clone/pull repository for %s/%s", org, name)
}
err = o.Git().PullRemoteBranches(dir)
if err != nil {
return errors.Wrapf(err, "Failed to pull remote branches for %s/%s", org, name)
}
branchNames, err := o.Git().RemoteBranchNames(dir, "remotes/origin/")
if err != nil {
return errors.Wrapf(err, "Failed to get remote branches for %s/%s", org, name)
}
branches, err := util.SelectNamesWithFilter(branchNames, "Which remote branches do you to to delete: ", o.SelectAll, o.SelectFilter, o.In, o.Out, o.Err)
if err != nil {
return err
}
if len(branches) == 0 {
return fmt.Errorf("No branches selected!")
}
if !o.BatchMode {
log.Warnf("You are about to delete these branches '%s' on the Git provider. This operation CANNOT be undone!",
strings.Join(branches, ","))
flag := false
prompt := &survey.Confirm{
Message: "Are you sure you want to delete these all these branches?",
Default: false,
}
err = survey.AskOne(prompt, &flag, nil, surveyOpts)
if err != nil {
return err
}
if !flag {
return nil
}
}
info := util.ColorInfo
for _, branch := range branches {
err := o.Git().DeleteRemoteBranch(dir, "origin", branch)
if err != nil {
return errors.Wrapf(err, "Failed to delete remote branche %s from %s/%s", branch, org, name)
}
log.Infof("Deleted branch in repo %s/%s branch: %s\n", info(org), info(name), info(branch))
}
}
return nil
}
func (o *CommonOptions) cloneOrPullRepository(org string, repo string, gitURL string) (string, error) {
environmentsDir, err := util.EnvironmentsDir()
if err != nil {
return "", err
}
dir := filepath.Join(environmentsDir, org, repo)
// now lets clone the fork and push it...
exists, err := util.FileExists(dir)
if err != nil {
return dir, err
}
if exists {
// lets check the git remote URL is setup correctly
err = o.Git().SetRemoteURL(dir, "origin", gitURL)
if err != nil {
return dir, err
}
err = o.Git().Stash(dir)
return dir, err
} else {
err := os.MkdirAll(dir, DefaultWritePermissions)
if err != nil {
return dir, fmt.Errorf("Failed to create directory %s due to %s", dir, err)
}
info := util.ColorInfo
log.Infof("Cloning repository %s/%s to %s\n", info(org), info(repo), info(dir))
err = o.Git().Clone(gitURL, dir)
if err != nil {
return dir, err
}
err = o.Git().SetRemoteURL(dir, "origin", gitURL)
if err != nil {
return dir, err
}
return dir, err
}
}