diff --git a/CHANGELOG.md b/CHANGELOG.md index 9532c2f..d43616c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) ### Added - GHORG_GITHUB_TOPICS to filter cloning repos matching specified topics; thanks @ryanaross - GHORG_MATCH_PREFIX to filter cloning repos by prefix -- example commands +- example commands directory +- base-url to github for self hosted github instances ### Changed ### Deprecated ### Removed diff --git a/cmd/clone.go b/cmd/clone.go index d934eb1..b3964bb 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -58,7 +58,7 @@ func init() { cloneCmd.Flags().BoolVar(&skipArchived, "skip-archived", false, "GHORG_SKIP_ARCHIVED - skips archived repos, github/gitlab only") cloneCmd.Flags().BoolVar(&skipArchived, "preserve-dir", false, "GHORG_PRESERVE_DIRECTORY_STRUCTURE - clones repos in a directory structure that matches gitlab namespaces eg company/unit/subunit/app would clone into *_ghorg/unit/subunit/app, gitlab only") cloneCmd.Flags().BoolVar(&backup, "backup", false, "GHORG_BACKUP - backup mode, clone as mirror, no working copy (ignores branch parameter)") - cloneCmd.Flags().StringVarP(&baseURL, "base-url", "", "", "GHORG_SCM_BASE_URL - change SCM base url, for on self hosted instances (currently gitlab only, use format of https://git.mydomain.com/api/v3)") + cloneCmd.Flags().StringVarP(&baseURL, "base-url", "", "", "GHORG_SCM_BASE_URL - change SCM base url, for on self hosted instances (currently gitlab/github only, use format of https://git.mydomain.com/api/v3)") cloneCmd.Flags().StringVarP(&concurrency, "concurrency", "", "", "GHORG_CONCURRENCY - max goroutines to spin up while cloning (default 25)") cloneCmd.Flags().StringVarP(&topics, "topics", "", "", "GHORG_GITHUB_TOPICS - comma seperated list of github topics to filter for") cloneCmd.Flags().StringVarP(&outputDir, "output-dir", "", "", "GHORG_OUTPUT_DIR - name of directory repos will be cloned into, will force underscores and always append _ghorg (default {org/repo being cloned}_ghorg)") diff --git a/configs/configs.go b/configs/configs.go index 8aa8929..ad9c68c 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -137,15 +137,20 @@ func getOrSetDefaults(envVar string) { os.Setenv(envVar, "25") } } else { - // User forgot to put a / at the end of path, so we will add for them - if envVar == "GHORG_ABSOLUTE_PATH_TO_CLONE_TO" && !strings.HasSuffix(viper.GetString(envVar), "/") { - os.Setenv(envVar, viper.GetString(envVar)+"/") - } else { - os.Setenv(envVar, viper.GetString(envVar)) - } + s := viper.GetString(envVar) + os.Setenv(envVar, EnsureTrailingSlash(s)) } } +// EnsureTrailingSlash takes a string and ensures a single / is appened +func EnsureTrailingSlash(s string) string { + if !strings.HasSuffix(s, "/") { + s = s + "/" + } + + return s +} + // GhorgIgnoreLocation returns the path of users ghorgignore func GhorgIgnoreLocation() string { return GhorgDir() + "/ghorgignore" diff --git a/internal/github/github.go b/internal/github/github.go index 7aa85c1..d64ba96 100644 --- a/internal/github/github.go +++ b/internal/github/github.go @@ -2,9 +2,11 @@ package github import ( "context" + "net/url" "os" "strings" + "github.com/gabrie30/ghorg/configs" "github.com/gabrie30/ghorg/internal/repo" "github.com/google/go-github/v32/github" "golang.org/x/oauth2" @@ -20,6 +22,11 @@ func GetOrgRepos(targetOrg string) ([]repo.Data, error) { tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) + if os.Getenv("GHORG_SCM_BASE_URL") != "" { + u := configs.EnsureTrailingSlash(os.Getenv("GHORG_SCM_BASE_URL")) + client.BaseURL, _ = url.Parse(u) + } + opt := &github.RepositoryListByOrgOptions{ Type: "all", ListOptions: github.ListOptions{PerPage: 100, Page: 0}, @@ -30,6 +37,7 @@ func GetOrgRepos(targetOrg string) ([]repo.Data, error) { // get all pages of results var allRepos []*github.Repository for { + repos, resp, err := client.Repositories.ListByOrg(context.Background(), targetOrg, opt) if err != nil { @@ -107,6 +115,11 @@ func GetUserRepos(targetUser string) ([]repo.Data, error) { tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) + if os.Getenv("GHORG_SCM_BASE_URL") != "" { + u := configs.EnsureTrailingSlash(os.Getenv("GHORG_SCM_BASE_URL")) + client.BaseURL, _ = url.Parse(u) + } + opt := &github.RepositoryListOptions{ Type: "all", ListOptions: github.ListOptions{PerPage: 100, Page: 0}, diff --git a/sample-conf.yaml b/sample-conf.yaml index 21860b4..dcfc47e 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -19,11 +19,6 @@ GHORG_GITHUB_TOPICS: # |G|I|T|L|A|B| |S|P|E|C|I|F|I|C| # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -# Change SCM base url, for on self hosted instances (currently gitlab only, use format of https://git.mydomain.com/api/v3) -# default: gitlab.com/api/v3 -# flag (--base-url) -GHORG_SCM_BASE_URL: - # Add your GitLab token # Defaults to using key returned by # $ security find-internet-password -s gitlab.com | grep "acct" | awk -F\" '{ print $4 }' @@ -53,6 +48,11 @@ GHORG_BITBUCKET_USERNAME: # |G|E|N|E|R|A|L| |C|O|N|F|I|G|U|R|A|T|I|O|N| # +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+ +# Change SCM base url, for on self hosted instances (currently gitlab only, use format of https://git.mydomain.com/api/v3) +# default: uses github/gitlab public api +# flag (--base-url) +GHORG_SCM_BASE_URL: + # Which provider to clone from (github, gitlab, or bitbucket) # default: github # flag (--scm, -s)