update ghorg branch to first look for repos default branch (#113)

* update ghorg branch to first look for repos default branch
This commit is contained in:
Jay Gabriels
2020-11-29 09:14:13 -08:00
committed by GitHub
parent 0e63d0a339
commit d479395bdf
10 changed files with 73 additions and 18 deletions

View File

@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
### Added
### Changed
- error messages for ls command
- GHORG_BRANCH if not set, will first look for the repos default branch, if no default branch is found on repo will fall back to using master as default
### Deprecated
### Removed
### Fixed

View File

@@ -356,7 +356,7 @@ func CloneAllRepos() {
limit := limiter.NewConcurrencyLimiter(l)
for _, target := range cloneTargets {
appName := getAppNameFromURL(target.URL)
branch := os.Getenv("GHORG_BRANCH")
branch := target.CloneBranch
repo := target
limit.Execute(func() {
@@ -452,7 +452,7 @@ func CloneAllRepos() {
}
}
colorlog.PrintSuccess("Success " + repo.URL)
colorlog.PrintSuccess("Success cloning repo: " + repo.URL + " -> branch: " + branch)
})
}
@@ -484,10 +484,13 @@ func PrintConfigs() {
colorlog.PrintInfo("* SCM : " + os.Getenv("GHORG_SCM_TYPE"))
colorlog.PrintInfo("* Type : " + os.Getenv("GHORG_CLONE_TYPE"))
colorlog.PrintInfo("* Protocol : " + os.Getenv("GHORG_CLONE_PROTOCOL"))
colorlog.PrintInfo("* Branch : " + os.Getenv("GHORG_BRANCH"))
colorlog.PrintInfo("* Location : " + os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"))
colorlog.PrintInfo("* Concurrency : " + os.Getenv("GHORG_CONCURRENCY"))
if os.Getenv("GHORG_BRANCH") != "" {
colorlog.PrintInfo("* Branch : " + getGhorgBranch())
}
if os.Getenv("GHORG_SCM_BASE_URL") != "" {
colorlog.PrintInfo("* Base URL : " + os.Getenv("GHORG_SCM_BASE_URL"))
}
@@ -511,6 +514,14 @@ func PrintConfigs() {
fmt.Println("")
}
func getGhorgBranch() string {
if os.Getenv("GHORG_BRANCH") == "" {
return "default branch"
}
return os.Getenv("GHORG_BRANCH")
}
func ensureTrailingSlash(path string) string {
if string(path[len(path)-1]) == "/" {
return path

View File

@@ -121,8 +121,6 @@ func getOrSetDefaults(envVar string) {
switch envVar {
case "GHORG_ABSOLUTE_PATH_TO_CLONE_TO":
os.Setenv(envVar, HomeDir()+"/Desktop/ghorg/")
case "GHORG_BRANCH":
os.Setenv(envVar, "master")
case "GHORG_CLONE_PROTOCOL":
os.Setenv(envVar, "https")
case "GHORG_CLONE_TYPE":

View File

@@ -9,15 +9,10 @@ import (
func TestDefaultSettings(t *testing.T) {
branch := os.Getenv("GHORG_BRANCH")
protocol := os.Getenv("GHORG_CLONE_PROTOCOL")
scm := os.Getenv("GHORG_SCM_TYPE")
cloneType := os.Getenv("GHORG_CLONE_TYPE")
if branch != "master" {
t.Errorf("Default branch should be master, got: %v", branch)
}
if protocol != "https" {
t.Errorf("Default protocol should be https, got: %v", protocol)
}

View File

@@ -24,7 +24,6 @@ GHORG_GITLAB_TOKEN:
# flag (--preserve-dir)
GHORG_PRESERVE_DIRECTORY_STRUCTURE:
# +-+-+-+-+-+ +-+-+-+-+-+-+-+-+
# |G|I|T|E|A| |S|P|E|C|I|F|I|C|
# +-+-+-+-+-+ +-+-+-+-+-+-+-+-+
@@ -34,7 +33,6 @@ GHORG_PRESERVE_DIRECTORY_STRUCTURE:
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
GHORG_GITEA_TOKEN:
# +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
# |B|I|T|B|U|C|K|E|T| |S|P|E|C|I|F|I|C|
# +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
@@ -47,7 +45,6 @@ GHORG_BITBUCKET_APP_PASSWORD:
# flag (--bitbucket-username)
GHORG_BITBUCKET_USERNAME:
# +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+
# |G|E|N|E|R|A|L| |C|O|N|F|I|G|U|R|A|T|I|O|N|
# +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -73,7 +70,8 @@ GHORG_CLONE_PROTOCOL:
GHORG_ABSOLUTE_PATH_TO_CLONE_TO:
# Branch ghorg resets and leaves checked out
# default: master
# default: default branch
# NOTE: if no default branch is found on the repo, will fallback to using master
# flag (--branch, -b)
GHORG_BRANCH:

View File

@@ -78,6 +78,26 @@ func (_ Bitbucket) filter(resp interface{}) (repoData []Repo, err error) {
}
r := Repo{}
if os.Getenv("GHORG_BRANCH") == "" {
var defaultBranch string
if clone["mainbranch"] == nil {
defaultBranch = "master"
} else {
defaultBranch = clone["mainbranch"].(map[string]interface{})["name"].(string)
}
r.CloneBranch = defaultBranch
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
if os.Getenv("GHORG_BRANCH") == "" {
r.CloneBranch = "master"
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
if os.Getenv("GHORG_CLONE_PROTOCOL") == "ssh" && linkType == "ssh" {
r.URL = link.(string)
r.CloneURL = link.(string)

View File

@@ -174,6 +174,16 @@ func (c Gitea) filter(rps []*gitea.Repository) (repoData []Repo, err error) {
r := Repo{}
r.Path = rp.FullName
if os.Getenv("GHORG_BRANCH") == "" {
defaultBranch := rp.DefaultBranch
if defaultBranch == "" {
defaultBranch = "master"
}
r.CloneBranch = defaultBranch
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
cloneURL := rp.CloneURL
if rp.Private {

View File

@@ -161,6 +161,17 @@ func (c Github) filter(allRepos []*github.Repository, envTopics []string) []Repo
}
r := Repo{}
if os.Getenv("GHORG_BRANCH") == "" {
defaultBranch := ghRepo.GetDefaultBranch()
if defaultBranch == "" {
defaultBranch = "master"
}
r.CloneBranch = defaultBranch
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
r.CloneURL = c.addTokenToHTTPSCloneURL(*ghRepo.CloneURL, os.Getenv("GHORG_GITHUB_TOKEN"))
r.URL = *ghRepo.CloneURL

View File

@@ -152,6 +152,16 @@ func (c Gitlab) filter(ps []*gitlab.Project) []Repo {
r := Repo{}
if os.Getenv("GHORG_BRANCH") == "" {
defaultBranch := p.DefaultBranch
if defaultBranch == "" {
defaultBranch = "master"
}
r.CloneBranch = defaultBranch
} else {
r.CloneBranch = os.Getenv("GHORG_BRANCH")
}
r.Path = p.PathWithNamespace
if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
r.CloneURL = c.addTokenToHTTPSCloneURL(p.HTTPURLToRepo, os.Getenv("GHORG_GITLAB_TOKEN"))

View File

@@ -2,8 +2,9 @@ package scm
// Repo represents an SCM repo
type Repo struct {
Name string
Path string
URL string
CloneURL string
Name string
Path string
URL string
CloneURL string
CloneBranch string
}