Refactor from script to cli tool #9
45
main.go
45
main.go
@ -7,6 +7,8 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"code.obermui.de/6543/GitLab_MergeDevel2Default/utils"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
@ -92,23 +94,23 @@ func main() {
|
||||
// Compile glob regex
|
||||
excludeRule, err := glob.Compile(values.repoExclude)
|
||||
if err != nil {
|
||||
error(2, "not able to compile regex '%s'", values.repoExclude)
|
||||
utils.Error(2, "not able to compile regex '%s'", values.repoExclude)
|
||||
}
|
||||
baseBranchRule, err := glob.Compile(values.pullBaseBranch)
|
||||
if err != nil {
|
||||
error(3, "not able to compile regex '%s'", values.pullBaseBranch)
|
||||
utils.Error(3, "not able to compile regex '%s'", values.pullBaseBranch)
|
||||
}
|
||||
|
||||
// connect to gitlab
|
||||
client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL))
|
||||
if err != nil {
|
||||
error(4, "Could not create Client: %v\n", err)
|
||||
utils.Error(4, "Could not create Client: %v\n", err)
|
||||
}
|
||||
|
||||
// discover GroupID by name
|
||||
org, _, err := client.Groups.GetGroup(values.orgName)
|
||||
if err != nil {
|
||||
error(5, "Error cant get information for Organisation \"%s\": %v", values.orgName, err)
|
||||
utils.Error(5, "Error cant get information for Organisation \"%s\": %v", values.orgName, err)
|
||||
}
|
||||
fmt.Printf("Found \"%s\"\n", org.WebURL)
|
||||
|
||||
@ -119,11 +121,11 @@ func main() {
|
||||
fmt.Printf("%d", page)
|
||||
repos, _, err := client.Groups.ListGroupProjects(org.ID, &gitlab.ListGroupProjectsOptions{
|
||||
ListOptions: gitlab.ListOptions{PerPage: 10, Page: page},
|
||||
Archived: optBool(false),
|
||||
IncludeSubgroups: optBool(false),
|
||||
Archived: utils.OptBool(false),
|
||||
IncludeSubgroups: utils.OptBool(false),
|
||||
})
|
||||
if err != nil {
|
||||
error(5, "Could not obtain repo list: %v\n", err)
|
||||
utils.Error(5, "Could not obtain repo list: %v\n", err)
|
||||
}
|
||||
|
||||
repoList = append(repoList, repos...)
|
||||
@ -164,7 +166,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
error(10, "Could not obtain branch list from '%s': %v\n", repo.Name, err)
|
||||
utils.Error(10, "Could not obtain branch list from '%s': %v\n", repo.Name, err)
|
||||
}
|
||||
|
||||
branchList = append(branchList, bL...)
|
||||
@ -191,7 +193,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
|
||||
To: &baseBranch.Name,
|
||||
})
|
||||
if err != nil {
|
||||
error(10, "Could not compare '%s'...'%s' at '%s': %v\n", repo.DefaultBranch, baseBranch.Name, repo.Name, err)
|
||||
utils.Error(10, "Could not compare '%s'...'%s' at '%s': %v\n", repo.DefaultBranch, baseBranch.Name, repo.Name, err)
|
||||
}
|
||||
|
||||
if len(diff.Commits) == 0 {
|
||||
@ -203,21 +205,21 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
|
||||
pullList, _, err := client.MergeRequests.ListProjectMergeRequests(repo.ID, &gitlab.ListProjectMergeRequestsOptions{
|
||||
SourceBranch: &baseBranch.Name,
|
||||
TargetBranch: &repo.DefaultBranch,
|
||||
State: optString("opened"),
|
||||
State: utils.OptString("opened"),
|
||||
})
|
||||
if err != nil {
|
||||
error(11, "Could not obtain merge request list from '%s': %v\n", repo.Name, err)
|
||||
utils.Error(11, "Could not obtain merge request list from '%s': %v\n", repo.Name, err)
|
||||
}
|
||||
|
||||
// if not create one
|
||||
if len(pullList) == 0 {
|
||||
fmt.Printf(" no existing pull for %s, creating one ...\n", baseBranch.Name)
|
||||
pull, _, err := client.MergeRequests.CreateMergeRequest(repo.ID, &gitlab.CreateMergeRequestOptions{
|
||||
Title: optString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)),
|
||||
Description: optString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
|
||||
Title: utils.OptString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)),
|
||||
Description: utils.OptString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
|
||||
SourceBranch: &baseBranch.Name,
|
||||
TargetBranch: &repo.DefaultBranch,
|
||||
AllowCollaboration: optBool(true),
|
||||
AllowCollaboration: utils.OptBool(true),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf(" could not create merge request '%s <- %s', skiping '%s'\n", repo.DefaultBranch, baseBranch.Name, repo.Name)
|
||||
@ -264,18 +266,3 @@ func mergePulls(c *gitlab.Client, pulls []*pullCache) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// optBool return ref of bool - dont ask it is go
|
||||
func optBool(val bool) *bool {
|
||||
return &val
|
||||
}
|
||||
|
||||
// optString return ref of string - dont ask it is go
|
||||
func optString(val string) *string {
|
||||
return &val
|
||||
}
|
||||
|
||||
func error(id int, format string, a ...interface{}) {
|
||||
fmt.Printf(format, a...)
|
||||
os.Exit(id)
|
||||
}
|
||||
|
2
merge/merge.go
Normal file
2
merge/merge.go
Normal file
@ -0,0 +1,2 @@
|
||||
package merge
|
||||
|
22
utils/utils.go
Normal file
22
utils/utils.go
Normal file
@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// OptBool return ref of bool - dont ask it is go
|
||||
func OptBool(val bool) *bool {
|
||||
return &val
|
||||
}
|
||||
|
||||
// OptString return ref of string - dont ask it is go
|
||||
func OptString(val string) *string {
|
||||
return &val
|
||||
}
|
||||
|
||||
// Error format the error and exec the program with given status
|
||||
func Error(status int, format string, a ...interface{}) {
|
||||
fmt.Printf(format, a...)
|
||||
os.Exit(status)
|
||||
}
|
Loading…
Reference in New Issue
Block a user