This commit is contained in:
6543 2021-05-19 23:26:42 +02:00
parent e3ebcf47d3
commit 227a1dae55

61
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,12 @@ type Cache struct {
pullBaseBranch string `json:"pullBaseBranch"` pullBaseBranch string `json:"pullBaseBranch"`
} }


type pullCache struct {
repoID int
iid int
webURL string
}

const perPage = 10 const perPage = 10


func loadCache() (cache *Cache) { func loadCache() (cache *Cache) {
@ -124,16 +131,24 @@ func main() {
} }
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(3 * 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)


var branchList []*gitlab.Branch var branchList []*gitlab.Branch
@ -178,7 +193,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
@ -203,7 +218,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 {
@ -211,32 +226,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
} }
} }
} }