done
This commit is contained in:
parent
e3ebcf47d3
commit
227a1dae55
55
main.go
55
main.go
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/gobwas/glob"
|
||||
@ -23,6 +24,12 @@ type Cache struct {
|
||||
pullBaseBranch string `json:"pullBaseBranch"`
|
||||
}
|
||||
|
||||
type pullCache struct {
|
||||
repoID int
|
||||
iid int
|
||||
webURL string
|
||||
}
|
||||
|
||||
const perPage = 10
|
||||
|
||||
func loadCache() (cache *Cache) {
|
||||
@ -124,16 +131,24 @@ func main() {
|
||||
}
|
||||
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(3 * 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)
|
||||
|
||||
var branchList []*gitlab.Branch
|
||||
@ -178,7 +193,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
|
||||
@ -203,7 +218,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 {
|
||||
@ -211,33 +226,39 @@ 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, _ := client.Branches.DeleteBranch(repo.ID, baseBranch.Name); err != nil {
|
||||
fmt.Printf(" branch '%s' can not be deleted: %v\n", baseBranch.Name, err)
|
||||
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(" 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user