From 27d5e50819294f64a18097e314487fd4ddb62bb7 Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Sat, 8 Dec 2018 12:48:03 -0800 Subject: [PATCH] add default behavior to search for user if no org is found (#33) --- CHANGELOG.md | 1 + README.md | 14 +++++++++---- cmd/clone.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ac13a0cbe..d7662d6033 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) ## [Unreleased] - DATE ### Added - changelog +- when no org is found default to search for username instead - clone protocol to .ghorg to allow for https or ssh cloning ### Changed - readme diff --git a/README.md b/README.md index 039375900b..69d4a7bc6e 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,12 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/gabrie30/ghorg)](https://goreportcard.com/report/github.com/gabrie30/ghorg) GoDoc [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -ghorg allows you to quickly clone all of an orgs repos into a single directory. This can be useful in many situations including +ghorg allows you to quickly clone all of an orgs, or users repos into a single directory. This can be useful in many situations including -1. Searching your orgs codebase with ack, silver searcher, grep etc.. +1. Searching an orgs/users codebase with ack, silver searcher, grep etc.. 2. Bash scripting 3. Creating backups -4. Onboarding new teammates +4. Onboarding 5. Performing Audits > When running ghorg a second time, all local changes in your *_ghorg directory will be overwritten by whats on GitHub. If you are working out of this directory, make sure you rename it before running a second time otherwise all of you changes will be lost. @@ -47,7 +47,13 @@ $ go install $ ghorg org-you-want-to-clone ``` -> ghorg defaults to master however, for gitflows you can run on develop by setting GHORG_BRANCH=develop or similar +or + +```bash +$ ghorg user-you-want-to-clone +``` + +> ghorg defaults to master branch however, for gitflows you can run on develop by setting GHORG_BRANCH=develop or similar ## Configuration diff --git a/cmd/clone.go b/cmd/clone.go index 54565428b2..b773cdcfd2 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -57,6 +57,49 @@ func getAllOrgCloneUrls() ([]string, error) { var allRepos []*github.Repository for { repos, resp, err := client.Repositories.ListByOrg(context.Background(), os.Args[1], opt) + + if err != nil { + return nil, err + } + allRepos = append(allRepos, repos...) + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + cloneUrls := []string{} + + for _, repo := range allRepos { + if config.GhorgCloneProtocol == "https" { + cloneUrls = append(cloneUrls, *repo.CloneURL) + } else { + cloneUrls = append(cloneUrls, *repo.SSHURL) + } + } + + return cloneUrls, nil +} + +// TODO: refactor with getAllOrgCloneUrls +func getAllUserCloneUrls() ([]string, error) { + ctx := context.Background() + + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: getToken()}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + opt := &github.RepositoryListOptions{ + Type: "all", + ListOptions: github.ListOptions{PerPage: 100, Page: 0}, + } + + // get all pages of results + var allRepos []*github.Repository + for { + repos, resp, err := client.Repositories.List(context.Background(), os.Args[1], opt) + if err != nil { return nil, err } @@ -141,8 +184,15 @@ func CloneAllReposByOrg() { cloneTargets, err := getAllOrgCloneUrls() + if err != nil { + colorlog.PrintSubtleInfo("Change of Plans! Did not find GitHub Org " + os.Args[1] + " -- Looking instead for a GitHub User: " + os.Args[1]) + fmt.Println() + cloneTargets, err = getAllUserCloneUrls() + } + if err != nil { colorlog.PrintError(err) + os.Exit(1) } else { colorlog.PrintInfo(strconv.Itoa(len(cloneTargets)) + " repos found in " + os.Args[1]) fmt.Println() @@ -218,8 +268,3 @@ func CloneAllReposByOrg() { colorlog.PrintSuccess(fmt.Sprintf("Finished! %s%s_ghorg", config.AbsolutePathToCloneTo, os.Args[1])) } - -// TODO: Clone via http or ssh flag - -// Could clone all repos on a user -// orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil)