Refactor from script to cli tool #9

Merged
6543 merged 4 commits from refactor into master 2021-05-22 11:14:52 +00:00
4 changed files with 67 additions and 26 deletions
Showing only changes of commit 9bffd76a6d - Show all commits

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.16
require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/gobwas/glob v0.2.3
github.com/urfave/cli/v2 v2.3.0
github.com/xanzy/go-gitlab v0.48.0
)

10
go.sum
View File

@ -1,7 +1,10 @@
github.com/AlecAivazis/survey/v2 v2.2.12 h1:5a07y93zA6SZ09gOa9wLVLznF5zTJMQ+pJ3cZK4IuO8=
github.com/AlecAivazis/survey/v2 v2.2.12/go.mod h1:6d4saEvBsfSHXeN1a5OA5m2+HJ2LuVokllnC77pAIKI=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -31,11 +34,17 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1f
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/xanzy/go-gitlab v0.48.0 h1:RP9r4pMDIwE2fbtc+QYiC1euDsPGHcAjPkhje4X3QPU=
github.com/xanzy/go-gitlab v0.48.0/go.mod h1:UW8JJbyBbqtOyBYNHRo261IRdHUFJr2m0y0z1xUiu+E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@ -65,3 +74,4 @@ google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

21
main.go
View File

@ -1,7 +1,12 @@
package main

import (
"os"

"code.obermui.de/6543/GitLab_MergeDevel2Default/merge"
"code.obermui.de/6543/GitLab_MergeDevel2Default/utils"

"github.com/urfave/cli/v2"
)

var (
@ -9,5 +14,19 @@ var (
)

func main() {
merge.RunMerge()

app := cli.NewApp()
app.Name = "vv-tool"
app.Usage = "VV command line tool to interact with Gitlab"
app.Description = "cli tool for VV to manage things"
app.Version = Version
app.Commands = []*cli.Command{
&merge.CmdMerge,
}

app.EnableBashCompletion = true
err := app.Run(os.Args)
if err != nil {
utils.Error(1, "Error: %v\n", err)
}
}

View File

@ -4,13 +4,13 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"

"code.obermui.de/6543/GitLab_MergeDevel2Default/utils"

"github.com/AlecAivazis/survey/v2"
"github.com/gobwas/glob"
"github.com/urfave/cli/v2"
"github.com/xanzy/go-gitlab"
)

@ -69,25 +69,32 @@ func saveCache(cache *Cache) {
}
}

// RunMerge run script for merge subcommand
func RunMerge() {
var CmdMerge = cli.Command{
Name: "merge",
Usage: "run script for merge subcommand",
Description: `run script for merge subcommand`,
Action: runMerge,
}

// runMerge run script for merge subcommand
func runMerge(ctx *cli.Context) error {
values := loadCache()

// ask for infos
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.baseURL}, &values.baseURL); err != nil {
os.Exit(1)
return err
}
if err := survey.AskOne(&survey.Input{Message: "GitLab Token:", Default: values.token}, &values.token); err != nil {
os.Exit(1)
return err
}
if err := survey.AskOne(&survey.Input{Message: "Group Name:", Default: values.orgName}, &values.orgName); err != nil {
os.Exit(1)
return err
}
if err := survey.AskOne(&survey.Input{Message: "Ignore Repo with patter:", Default: values.repoExclude}, &values.repoExclude); err != nil {
os.Exit(1)
return err
}
if err := survey.AskOne(&survey.Input{Message: "Merge Branches with patter:", Default: values.pullBaseBranch}, &values.pullBaseBranch); err != nil {
os.Exit(1)
return err
}

saveCache(values)
@ -95,23 +102,23 @@ func RunMerge() {
// Compile glob regex
excludeRule, err := glob.Compile(values.repoExclude)
if err != nil {
utils.Error(2, "not able to compile regex '%s'", values.repoExclude)
return fmt.Errorf("not able to compile regex '%s'", values.repoExclude)
}
baseBranchRule, err := glob.Compile(values.pullBaseBranch)
if err != nil {
utils.Error(3, "not able to compile regex '%s'", values.pullBaseBranch)
return fmt.Errorf("not able to compile regex '%s'", values.pullBaseBranch)
}

// connect to gitlab
client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL))
if err != nil {
utils.Error(4, "Could not create Client: %v\n", err)
return fmt.Errorf("Could not create Client: %v\n", err)
}

// discover GroupID by name
org, _, err := client.Groups.GetGroup(values.orgName)
if err != nil {
utils.Error(5, "Error cant get information for Organisation \"%s\": %v", values.orgName, err)
return fmt.Errorf("Error cant get information for Organisation \"%s\": %v", values.orgName, err)
}
fmt.Printf("Found \"%s\"\n", org.WebURL)

@ -126,7 +133,7 @@ func RunMerge() {
IncludeSubgroups: utils.OptBool(false),
})
if err != nil {
utils.Error(5, "Could not obtain repo list: %v\n", err)
return fmt.Errorf("Could not obtain repo list: %v\n", err)
}

repoList = append(repoList, repos...)
@ -141,20 +148,24 @@ func RunMerge() {
for i := range repoList {
if !excludeRule.Match(repoList[i].Name) {
// for each NOT excluded repo ...
pullsToMergeCache = append(pullsToMergeCache,
threatRepo(baseBranchRule, client, repoList[i]),
)
pull, err := threatRepo(baseBranchRule, client, repoList[i])
if err != nil {
return err
}
pullsToMergeCache = append(pullsToMergeCache, pull)
}
}

fmt.Printf("\n\nwait 3sec for gitlab before merge created pulls\n")
time.Sleep(waitSecForMerge * time.Second)
mergePulls(client, pullsToMergeCache)

return nil
}

// threatRepo create (if needed) & merge "develop[ment]" branch in DefaultBranch
// and return pulls to merge
func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Project) *pullCache {
func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Project) (*pullCache, error) {
fmt.Printf("Handle Repository '%s'\n", repo.Name)

var branchList []*gitlab.Branch
@ -194,12 +205,12 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
To: &baseBranch.Name,
})
if err != nil {
utils.Error(10, "Could not compare '%s'...'%s' at '%s': %v\n", repo.DefaultBranch, baseBranch.Name, repo.Name, err)
return nil, fmt.Errorf("could not compare '%s'...'%s' at '%s': %v", repo.DefaultBranch, baseBranch.Name, repo.Name, err)
}

if len(diff.Commits) == 0 {
fmt.Printf(" branch '%s' is not ahead of '%s', skiping\n", baseBranch.Name, repo.DefaultBranch)
return nil
return nil, nil
}

// check if pull already exist
@ -209,7 +220,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
State: utils.OptString("opened"),
})
if err != nil {
utils.Error(11, "Could not obtain merge request list from '%s': %v\n", repo.Name, err)
return nil, fmt.Errorf("could not obtain merge request list from '%s': %v", repo.Name, err)
}

// if not create one
@ -224,7 +235,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
})
if err != nil {
fmt.Printf(" could not create merge request '%s <- %s', skiping '%s'\n", repo.DefaultBranch, baseBranch.Name, repo.Name)
return nil
return nil, nil
}
pullList = []*gitlab.MergeRequest{pull}
} else {
@ -232,12 +243,12 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
changes, _, err := client.MergeRequests.GetMergeRequestChanges(repo.ID, pullList[0].IID, nil)
if err != nil {
fmt.Printf(" could not obtain changes of merge request '%s', skiping '%s'\n", pullList[0].WebURL, repo.Name)
return nil
return nil, nil
}

if len(changes.Changes) == 0 {
fmt.Printf(" pull '%s' does not conatin changes, skiping\n", changes.WebURL)
return nil
return nil, nil
}
}

@ -246,10 +257,10 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
repoID: repo.ID,
iid: pullList[0].IID,
webURL: pullList[0].WebURL,
}, nil
}
}
}
return nil
return nil, nil
}

func mergePulls(c *gitlab.Client, pulls []*pullCache) {