GitLab_MergeDevel2Default/main.go

282 lines
7.7 KiB
Go
Raw Normal View History

2021-04-28 18:10:55 +00:00
package main
import (
2021-05-05 09:38:49 +00:00
"encoding/json"
2021-04-28 18:10:55 +00:00
"fmt"
2021-05-05 09:38:49 +00:00
"io/ioutil"
2021-04-28 18:10:55 +00:00
"os"
2021-05-19 21:26:42 +00:00
"time"
2021-04-28 18:10:55 +00:00
"github.com/AlecAivazis/survey/v2"
"github.com/gobwas/glob"
"github.com/xanzy/go-gitlab"
)
var (
Version = "dev"
)
2021-05-05 09:26:10 +00:00
type Cache struct {
2021-05-05 09:53:27 +00:00
token string `json:"token"`
baseURL string `json:"baseURL"`
orgName string `json:"orgName"`
repoExclude string `json:"repoExclude"`
pullBaseBranch string `json:"pullBaseBranch"`
2021-05-05 09:24:54 +00:00
}
2021-05-19 21:26:42 +00:00
type pullCache struct {
repoID int
iid int
webURL string
}
2021-05-19 21:53:29 +00:00
const (
perPage = 10
waitSecForMerge = 3
)
2021-05-19 21:09:29 +00:00
2021-05-05 09:38:49 +00:00
func loadCache() (cache *Cache) {
2021-04-28 18:10:55 +00:00
// default values
2021-05-05 09:38:49 +00:00
cache = &Cache{
2021-05-05 09:26:10 +00:00
token: "XXXXXXXXXXXXXXXXXX",
2021-05-05 09:38:49 +00:00
baseURL: "https://gitlab.com",
orgName: "organisation",
2021-05-05 09:26:10 +00:00
repoExclude: "exclude*",
2021-05-05 09:24:54 +00:00
pullBaseBranch: "devel*",
}
2021-04-28 18:10:55 +00:00
2021-05-05 09:38:49 +00:00
data, err := ioutil.ReadFile(".cache.json")
if err != nil {
return
}
var c Cache
if err := json.Unmarshal(data, &c); err != nil {
return
}
return &c
}
func saveCache(cache *Cache) {
data, err := json.Marshal(cache)
if err != nil {
fmt.Printf("ERROR: marshal cache: %v\n", err)
}
2021-05-05 09:53:27 +00:00
err = ioutil.WriteFile(".cache.json", data, 0600)
2021-05-05 09:38:49 +00:00
if err != nil {
fmt.Printf("ERROR: save cache: %v\n", err)
}
}
func main() {
values := loadCache()
2021-04-28 18:10:55 +00:00
// ask for infos
2021-05-05 09:24:54 +00:00
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.baseURL}, &values.baseURL); err != nil {
2021-04-28 18:10:55 +00:00
os.Exit(1)
}
2021-05-05 09:24:54 +00:00
if err := survey.AskOne(&survey.Input{Message: "GitLab Token:", Default: values.token}, &values.token); err != nil {
2021-04-28 18:10:55 +00:00
os.Exit(1)
}
2021-05-05 09:24:54 +00:00
if err := survey.AskOne(&survey.Input{Message: "Group Name:", Default: values.orgName}, &values.orgName); err != nil {
2021-04-28 18:10:55 +00:00
os.Exit(1)
}
2021-05-05 09:24:54 +00:00
if err := survey.AskOne(&survey.Input{Message: "Ignore Repo with patter:", Default: values.repoExclude}, &values.repoExclude); err != nil {
2021-04-28 18:10:55 +00:00
os.Exit(1)
}
2021-05-05 09:24:54 +00:00
if err := survey.AskOne(&survey.Input{Message: "Merge Branches with patter:", Default: values.pullBaseBranch}, &values.pullBaseBranch); err != nil {
2021-04-28 18:10:55 +00:00
os.Exit(1)
}
2021-05-05 09:38:49 +00:00
saveCache(values)
2021-04-28 18:10:55 +00:00
// Compile glob regex
2021-05-05 09:24:54 +00:00
excludeRule, err := glob.Compile(values.repoExclude)
2021-04-28 18:10:55 +00:00
if err != nil {
2021-05-05 09:24:54 +00:00
error(2, "not able to compile regex '%s'", values.repoExclude)
2021-04-28 18:10:55 +00:00
}
2021-05-05 09:24:54 +00:00
baseBranchRule, err := glob.Compile(values.pullBaseBranch)
2021-04-28 18:10:55 +00:00
if err != nil {
2021-05-05 09:24:54 +00:00
error(3, "not able to compile regex '%s'", values.pullBaseBranch)
2021-04-28 18:10:55 +00:00
}
// connect to gitlab
2021-05-05 09:24:54 +00:00
client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL))
2021-04-28 18:10:55 +00:00
if err != nil {
error(4, "Could not create Client: %v\n", err)
}
// discover GroupID by name
2021-05-05 09:24:54 +00:00
org, _, err := client.Groups.GetGroup(values.orgName)
2021-04-28 18:10:55 +00:00
if err != nil {
2021-05-05 09:24:54 +00:00
error(5, "Error cant get information for Organisation \"%s\": %v", values.orgName, err)
2021-04-28 18:10:55 +00:00
}
fmt.Printf("Found \"%s\"\n", org.WebURL)
2021-05-05 09:00:22 +00:00
var repoList []*gitlab.Project
var page = 1
2021-05-05 09:53:27 +00:00
fmt.Printf("Retrieving repository list...\n")
2021-05-05 09:00:22 +00:00
for {
2021-05-05 09:53:27 +00:00
fmt.Printf("%d", page)
2021-05-05 09:00:22 +00:00
repos, _, err := client.Groups.ListGroupProjects(org.ID, &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{PerPage: 10, Page: page},
Archived: optBool(false),
IncludeSubgroups: optBool(false),
})
if err != nil {
error(5, "Could not obtain repo list: %v\n", err)
}
repoList = append(repoList, repos...)
2021-05-05 09:05:12 +00:00
if len(repos) < 10 {
2021-05-05 09:00:22 +00:00
break
}
2021-05-05 09:03:11 +00:00
page++
2021-04-28 18:10:55 +00:00
}
2021-05-05 09:53:27 +00:00
fmt.Println()
2021-04-28 18:10:55 +00:00
2021-05-19 21:26:42 +00:00
var pullsToMergeCache []*pullCache
2021-04-28 18:10:55 +00:00
for i := range repoList {
if !excludeRule.Match(repoList[i].Name) {
// for each NOT excluded repo ...
2021-05-19 21:26:42 +00:00
pullsToMergeCache = append(pullsToMergeCache,
threatRepo(baseBranchRule, client, repoList[i]),
)
2021-04-28 18:10:55 +00:00
}
}
2021-05-19 21:26:42 +00:00
fmt.Printf("\n\nwait 3sec for gitlab before merge created pulls\n")
2021-05-19 21:53:29 +00:00
time.Sleep(waitSecForMerge * time.Second)
2021-05-19 21:26:42 +00:00
mergePulls(client, pullsToMergeCache)
2021-04-28 18:10:55 +00:00
}
// threatRepo create (if needed) & merge "develop[ment]" branch in DefaultBranch
2021-05-19 21:26:42 +00:00
// and return pulls to merge
func threatRepo(baseBranchRule glob.Glob, client *gitlab.Client, repo *gitlab.Project) *pullCache {
2021-04-28 18:10:55 +00:00
fmt.Printf("Handle Repository '%s'\n", repo.Name)
2021-05-19 21:09:29 +00:00
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++
2021-04-28 18:10:55 +00:00
}
2021-05-05 09:53:27 +00:00
fmt.Printf(" found %d branches:", len(branchList))
fmt.Printf(" ")
for _, b := range branchList {
fmt.Printf("'%s' ", b.Name)
}
fmt.Println()
2021-05-05 09:11:21 +00:00
2021-04-28 18:10:55 +00:00
for _, baseBranch := range branchList {
if baseBranchRule.Match(baseBranch.Name) && !baseBranch.Default {
fmt.Printf(" found branch '%s'\n", baseBranch.Name)
// check if source is ahead target branch
diff, _, err := client.Repositories.Compare(repo.ID, &gitlab.CompareOptions{
From: &repo.DefaultBranch,
To: &baseBranch.Name,
})
if err != nil {
error(10, "Could not compare '%s'...'%s' at '%s': %v\n", repo.DefaultBranch, baseBranch.Name, repo.Name, err)
}
if len(diff.Commits) == 0 {
fmt.Printf(" branch '%s' is not ahead of '%s', skiping\n", baseBranch.Name, repo.DefaultBranch)
2021-05-19 21:26:42 +00:00
return nil
2021-04-28 18:10:55 +00:00
}
// check if pull already exist
pullList, _, err := client.MergeRequests.ListProjectMergeRequests(repo.ID, &gitlab.ListProjectMergeRequestsOptions{
SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch,
State: optString("opened"),
})
if err != nil {
error(11, "Could not obtain merge request list from '%s': %v\n", repo.Name, err)
}
// if not create one
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{
2021-05-19 21:09:29 +00:00
Title: optString(fmt.Sprintf("%s <- %s", repo.DefaultBranch, baseBranch.Name)),
2021-04-28 18:10:55 +00:00
Description: optString("Auto created by https://code.obermui.de/6543/GitLab_MergeDevel2Default"),
SourceBranch: &baseBranch.Name,
TargetBranch: &repo.DefaultBranch,
AllowCollaboration: optBool(true),
})
if err != nil {
fmt.Printf(" could not create merge request '%s <- %s', skiping '%s'\n", repo.DefaultBranch, baseBranch.Name, repo.Name)
2021-05-19 21:26:42 +00:00
return nil
2021-04-28 18:10:55 +00:00
}
pullList = []*gitlab.MergeRequest{pull}
} else {
// check if changes exist for pull
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)
2021-05-19 21:26:42 +00:00
return nil
2021-04-28 18:10:55 +00:00
}
if len(changes.Changes) == 0 {
fmt.Printf(" pull '%s' does not conatin changes, skiping\n", changes.WebURL)
2021-05-19 21:26:42 +00:00
return nil
2021-04-28 18:10:55 +00:00
}
}
2021-05-19 21:26:42 +00:00
// 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
}
2021-04-28 18:10:55 +00:00
2021-05-19 21:26:42 +00:00
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)
2021-05-05 07:34:51 +00:00
2021-05-19 21:26:42 +00:00
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", pull.SourceBranch)
2021-04-28 18:10:55 +00:00
}
}
}
}
2021-04-28 21:07:52 +00:00
// optBool return ref of bool - dont ask it is go
2021-04-28 18:10:55 +00:00
func optBool(val bool) *bool {
return &val
}
2021-04-28 21:07:52 +00:00
// optString return ref of string - dont ask it is go
2021-04-28 18:10:55 +00:00
func optString(val string) *string {
return &val
}
func error(id int, format string, a ...interface{}) {
fmt.Printf(format, a...)
os.Exit(id)
}