Skip to content

Commit

Permalink
add default behavior to search for user if no org is found (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 authored Dec 8, 2018
1 parent 6fa13ca commit 27d5e50
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/gabrie30/ghorg)](https://goreportcard.com/report/github.com/gabrie30/ghorg) <a href="https://godoc.org/github.com/gabrie30/ghorg"><img src="https://godoc.org/github.com/gabrie30/ghorg?status.svg" alt="GoDoc"></a> [![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.
Expand Down Expand Up @@ -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

Expand Down
55 changes: 50 additions & 5 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit 27d5e50

Please sign in to comment.