move util functions into utils package

This commit is contained in:
6543 2021-05-22 12:37:59 +02:00
parent aaef7d5749
commit 4bf7b3da31
3 changed files with 40 additions and 29 deletions

45
main.go
View File

@ -7,6 +7,8 @@ import (
"os" "os"
"time" "time"


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

"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/xanzy/go-gitlab" "github.com/xanzy/go-gitlab"
@ -92,23 +94,23 @@ func main() {
// Compile glob regex // Compile glob regex
excludeRule, err := glob.Compile(values.repoExclude) excludeRule, err := glob.Compile(values.repoExclude)
if err != nil { 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) baseBranchRule, err := glob.Compile(values.pullBaseBranch)
if err != nil { 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 // connect to gitlab
client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL)) client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL))
if err != nil { 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 // discover GroupID by name
org, _, err := client.Groups.GetGroup(values.orgName) org, _, err := client.Groups.GetGroup(values.orgName)
if err != nil { 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) fmt.Printf("Found \"%s\"\n", org.WebURL)


@ -119,11 +121,11 @@ func main() {
fmt.Printf("%d", page) fmt.Printf("%d", page)
repos, _, err := client.Groups.ListGroupProjects(org.ID, &gitlab.ListGroupProjectsOptions{ repos, _, err := client.Groups.ListGroupProjects(org.ID, &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{PerPage: 10, Page: page}, ListOptions: gitlab.ListOptions{PerPage: 10, Page: page},
Archived: optBool(false), Archived: utils.OptBool(false),
IncludeSubgroups: optBool(false), IncludeSubgroups: utils.OptBool(false),
}) })
if err != nil { 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...) repoList = append(repoList, repos...)
@ -164,7 +166,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
}, },
}) })
if err != nil { 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...) branchList = append(branchList, bL...)
@ -191,7 +193,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
To: &baseBranch.Name, To: &baseBranch.Name,
}) })
if err != nil { 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 { 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{ pullList, _, err := client.MergeRequests.ListProjectMergeRequests(repo.ID, &gitlab.ListProjectMergeRequestsOptions{
SourceBranch: &baseBranch.Name, SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch, TargetBranch: &repo.DefaultBranch,
State: optString("opened"), State: utils.OptString("opened"),
}) })
if err != nil { 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 not create one
if len(pullList) == 0 { if len(pullList) == 0 {
fmt.Printf(" no existing pull for %s, creating one ...\n", baseBranch.Name) fmt.Printf(" no existing pull for %s, creating one ...\n", baseBranch.Name)
pull, _, err := client.MergeRequests.CreateMergeRequest(repo.ID, &gitlab.CreateMergeRequestOptions{ pull, _, err := client.MergeRequests.CreateMergeRequest(repo.ID, &gitlab.CreateMergeRequestOptions{
Title: optString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)), Title: utils.OptString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)),
Description: optString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"), Description: utils.OptString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
SourceBranch: &baseBranch.Name, SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch, TargetBranch: &repo.DefaultBranch,
AllowCollaboration: optBool(true), AllowCollaboration: utils.OptBool(true),
}) })
if err != nil { if err != nil {
fmt.Printf(" could not create merge request '%s <- %s', skiping '%s'\n", repo.DefaultBranch, baseBranch.Name, repo.Name) 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
View File

@ -0,0 +1,2 @@
package merge

22
utils/utils.go Normal file
View 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)
}