Resolve TODOs #8

Merged
6543 merged 3 commits from resolve-todos into master 2021-05-19 21:53:49 +00:00

92
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/gobwas/glob"
@ -23,6 +24,17 @@ type Cache struct {
pullBaseBranch string `json:"pullBaseBranch"`
}

type pullCache struct {
repoID int
iid int
webURL string
}

const (
perPage = 10
waitSecForMerge = 3
)

func loadCache() (cache *Cache) {
// default values
cache = &Cache{
@ -115,31 +127,51 @@ func main() {
}

repoList = append(repoList, repos...)

if len(repos) < 10 {
break
}

page++
}
fmt.Println()

var pullsToMergeCache []*pullCache
for i := range repoList {
if !excludeRule.Match(repoList[i].Name) {
// for each NOT excluded repo ...
threatRepo(baseBranchRule, client, repoList[i])
pullsToMergeCache = append(pullsToMergeCache,
threatRepo(baseBranchRule, client, repoList[i]),
)
}
}

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

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

// TODO: handle pagination
branchList, _, err := client.Branches.ListBranches(repo.ID, nil)
if err != nil {
error(10, "Could not obtain branch list from '%s': %v\n", repo.Name, err)
var branchList []*gitlab.Branch
var page = 1
for {
bL, _, err := client.Branches.ListBranches(repo.ID, &gitlab.ListBranchesOptions{
ListOptions: gitlab.ListOptions{
Page: page,
PerPage: perPage,
},
})
if err != nil {
error(10, "Could not obtain branch list from '%s': %v\n", repo.Name, err)
}

branchList = append(branchList, bL...)
if len(bL) < 10 {
break
}
page++
}

fmt.Printf(" found %d branches:", len(branchList))
@ -164,7 +196,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr

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

// check if pull already exist
@ -181,7 +213,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
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)), // TODO: better title (required)
Title: optString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)),
Description: optString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch,
@ -189,7 +221,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
return nil
}
pullList = []*gitlab.MergeRequest{pull}
} else {
@ -197,32 +229,38 @@ 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
return nil
}

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

// TODO: check and wait for pull to get ready
// since we had a match go to next repo ... and return pull
return &pullCache{
repoID: repo.ID,
iid: pullList[0].IID,
webURL: pullList[0].WebURL,
}
}
}
return nil
}

// merge pull
if pull, _, err := client.MergeRequests.AcceptMergeRequest(repo.ID, pullList[0].IID, nil); err != nil {
fmt.Printf(" pull '%s' can not be merged: %v\n", pullList[0].WebURL, err)
func mergePulls(c *gitlab.Client, pulls []*pullCache) {
for i := range pulls {
if pull, _, err := c.MergeRequests.AcceptMergeRequest(pulls[i].repoID, pulls[i].iid, nil); err != nil {
fmt.Printf(" pull '%s' can not be merged: %v\n", pulls[i].webURL, err)
} else {
fmt.Printf(" pull '%s' got merged successfully\n", pull.WebURL)

if err, _ := c.Branches.DeleteBranch(pulls[i].repoID, pull.SourceBranch); err != nil {
fmt.Printf(" branch '%s' can not be deleted: %v\n", pull.SourceBranch, err)
} else {
fmt.Printf(" pull '%s' got merged successfully\n", pull.WebURL)

if err, _ := client.Branches.DeleteBranch(repo.ID, baseBranch.Name); err != nil {
fmt.Printf(" branch '%s' can not be deleted: %v\n", baseBranch.Name, err)
} else {
fmt.Printf(" branch '%s' successfully deleted\n", baseBranch.Name)
}
fmt.Printf(" branch '%s' successfully deleted\n", pull.SourceBranch)
}

// since we had a match go to next repo ...
return
}
}
}