From 4bf7b3da31574c5407f142036b7d6f2e3f26dc49 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 22 May 2021 12:37:59 +0200 Subject: [PATCH] move util functions into utils package --- main.go | 45 ++++++++++++++++----------------------------- merge/merge.go | 2 ++ utils/utils.go | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 merge/merge.go create mode 100644 utils/utils.go diff --git a/main.go b/main.go index a8484a4..7044c29 100644 --- a/main.go +++ b/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) -} diff --git a/merge/merge.go b/merge/merge.go new file mode 100644 index 0000000..74a7aad --- /dev/null +++ b/merge/merge.go @@ -0,0 +1,2 @@ +package merge + diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..536ae4e --- /dev/null +++ b/utils/utils.go @@ -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) +}