From f9808073fc6a91faec3713e755257578b1105eda Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Thu, 29 Mar 2018 07:05:53 -0700 Subject: [PATCH] wire up GitHub calls --- .env-sample | 1 + .gitignore | 1 + README.md | 4 +++- cmd/clone.go | 35 +++++++++++++++++++++++++++++++++-- main.go | 12 ++++++++++-- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 .env-sample diff --git a/.env-sample b/.env-sample new file mode 100644 index 0000000..3b926cd --- /dev/null +++ b/.env-sample @@ -0,0 +1 @@ +GITHUB_TOKEN= diff --git a/.gitignore b/.gitignore index 6e1d622..8374200 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ ghorg +.env diff --git a/README.md b/README.md index 26c6396..2920820 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -## Ghorg +# Ghorg +### Access +- Create a personal access token, if the org is behind SSO add it to the token https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/ diff --git a/cmd/clone.go b/cmd/clone.go index 5c89cbd..f9d1af4 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -1,9 +1,40 @@ package cmd import ( + "context" "fmt" + "os" + + "github.com/google/go-github/github" + "golang.org/x/oauth2" ) -func Clone(arg string) { - fmt.Println("Cloning Repos", arg) +func authSetup() (*github.Client, *github.RepositoryListByOrgOptions) { + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + opt := &github.RepositoryListByOrgOptions{ + Type: "all", + ListOptions: github.ListOptions{PerPage: 1000}, + } + + return client, opt } + +// CloneAllReposByOrg clones all repos for a given org +func CloneAllReposByOrg() { + client, opt := authSetup() + repos, _, err := client.Repositories.ListByOrg(context.Background(), os.Args[1], opt) + if err != nil { + fmt.Print("Oh no error city: ", err) + } + + fmt.Println(repos) +} + +// Could clone all repos on a user +// orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil) diff --git a/main.go b/main.go index d520e03..e2f2388 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,20 @@ package main import ( "fmt" - "os" + "log" "github.com/ghorg/cmd" + "github.com/joho/godotenv" ) func main() { fmt.Println("Hello, Ghorg") - cmd.Clone(os.Args[1]) + cmd.CloneAllReposByOrg() +} + +func init() { + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } }