Merge pull request 'Resolve TODOs' (#8) from resolve-todos into master

Reviewed-on: #8
This commit is contained in:
6543 2021-05-19 21:53:49 +00:00
commit aaef7d5749
1 changed files with 65 additions and 27 deletions

92
main.go
View File

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


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


type pullCache struct {
repoID int
iid int
webURL string
}

const (
perPage = 10
waitSecForMerge = 3
)

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


repoList = append(repoList, repos...) repoList = append(repoList, repos...)

if len(repos) < 10 { if len(repos) < 10 {
break break
} }

page++ page++
} }
fmt.Println() fmt.Println()


var pullsToMergeCache []*pullCache
for i := range repoList { for i := range repoList {
if !excludeRule.Match(repoList[i].Name) { if !excludeRule.Match(repoList[i].Name) {
// for each NOT excluded repo ... // 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 // 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) fmt.Printf("Handle Repository '%s'\n", repo.Name)


// TODO: handle pagination var branchList []*gitlab.Branch
branchList, _, err := client.Branches.ListBranches(repo.ID, nil) var page = 1
if err != nil { for {
error(10, "Could not obtain branch list from '%s': %v\n", repo.Name, err) 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)) 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 { if len(diff.Commits) == 0 {
fmt.Printf(" branch '%s' is not ahead of '%s', skiping\n", baseBranch.Name, repo.DefaultBranch) fmt.Printf(" branch '%s' is not ahead of '%s', skiping\n", baseBranch.Name, repo.DefaultBranch)
return return nil
} }


// check if pull already exist // check if pull already exist
@ -181,7 +213,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
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)), // 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"), Description: optString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
SourceBranch: &baseBranch.Name, SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch, TargetBranch: &repo.DefaultBranch,
@ -189,7 +221,7 @@ func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Pr
}) })
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)
return return nil
} }
pullList = []*gitlab.MergeRequest{pull} pullList = []*gitlab.MergeRequest{pull}
} else { } 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) changes, _, err := client.MergeRequests.GetMergeRequestChanges(repo.ID, pullList[0].IID, nil)
if err != nil { if err != nil {
fmt.Printf(" could not obtain changes of merge request '%s', skiping '%s'\n", pullList[0].WebURL, repo.Name) 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 { if len(changes.Changes) == 0 {
fmt.Printf(" pull '%s' does not conatin changes, skiping\n", changes.WebURL) 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 func mergePulls(c *gitlab.Client, pulls []*pullCache) {
if pull, _, err := client.MergeRequests.AcceptMergeRequest(repo.ID, pullList[0].IID, nil); err != nil { for i := range pulls {
fmt.Printf(" pull '%s' can not be merged: %v\n", pullList[0].WebURL, err) 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 { } else {
fmt.Printf(" pull '%s' got merged successfully\n", pull.WebURL) fmt.Printf(" branch '%s' successfully deleted\n", pull.SourceBranch)

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)
}
} }

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