From f657b48abb6d9c298743be5ad073c780dc60df3c Mon Sep 17 00:00:00 2001 From: steinfletcher Date: Thu, 20 Sep 2018 22:09:48 +0100 Subject: [PATCH] Support custom github api URL --- README.md | 4 ++++ github/github.go | 11 ++++++----- github/github_test.go | 4 ++-- main.go | 8 +++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ac5f451..b749027 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,10 @@ Clone organisation repos Override the default location github-org-clone -o MyOrg -d ~/projects/work + +Override the github api url + + github-org-clone -o MyOrg -a https://mycustomdomain.com View docs diff --git a/github/github.go b/github/github.go index 70c1390..ba7c057 100644 --- a/github/github.go +++ b/github/github.go @@ -10,13 +10,14 @@ import ( "time" ) -func NewGithub(username string, apiToken string) Github { - return &githubCli{username, apiToken} +func NewGithub(username string, apiToken string, apiUrl string) Github { + return &githubCli{username, apiToken, apiUrl} } type githubCli struct { username string apiToken string + apiUrl string } type Github interface { @@ -36,7 +37,7 @@ type Repo struct { } func (g *githubCli) Teams(org string) (error, []Team) { - err, resp := doGet(fmt.Sprintf("https://api.github.com/orgs/%s/teams", org), g.username, g.apiToken) + err, resp := doGet(fmt.Sprintf("%s/orgs/%s/teams", g.apiUrl, org), g.username, g.apiToken) if err != nil { return err, nil } @@ -52,7 +53,7 @@ func (g *githubCli) TeamRepos(teamId int) (error, []Repo) { var page = 1 var repos []Repo for { - err, res := doGet(fmt.Sprintf("https://api.github.com/teams/%d/repos?page=%d", teamId, page), g.username, g.apiToken) + err, res := doGet(fmt.Sprintf("%s/teams/%d/repos?page=%d", g.apiUrl, teamId, page), g.username, g.apiToken) if err != nil { return err, nil } @@ -78,7 +79,7 @@ func (g *githubCli) OrgRepos(org string) (error, []Repo) { var page = 1 var repos []Repo for { - err, res := doGet(fmt.Sprintf("https://api.github.com/orgs/%s/repos?page=%d", org, page), g.username, g.apiToken) + err, res := doGet(fmt.Sprintf("%s/orgs/%s/repos?page=%d", g.apiUrl, org, page), g.username, g.apiToken) if err != nil { return err, nil } diff --git a/github/github_test.go b/github/github_test.go index 45728b5..d7434ec 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -15,7 +15,7 @@ func TestFetchTeams(t *testing.T) { Reply(200). File("../testdata/teamsResponse.json") - githubCli := NewGithub("username", "password") + githubCli := NewGithub("username", "password", "https://api.github.com") _, teams := githubCli.Teams("MyOrg") @@ -30,7 +30,7 @@ func TestFetchTeamRepos(t *testing.T) { teamReposResp("2", "; rel=\"prev\", ; rel=\"next\", ; rel=\"first\", ; rel=\"last\"", "teamReposResponsePage2.json") teamReposResp("3", "; rel=\"prev\", ; rel=\"first\"", "teamReposResponsePage3.json") - githubCli := NewGithub("username", "password") + githubCli := NewGithub("username", "password", "https://api.github.com") _, teams := githubCli.TeamRepos(2285789) diff --git a/main.go b/main.go index 0eadb7e..c0a2c93 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,11 @@ func main() { Name: "dir, d", Usage: "directory to clone into. Defaults to the org name or org/team name if defined", }, + cli.StringFlag{ + Name: "api, a", + Value: "https://api.github.com", + Usage: "github api url", + }, } app.Action = func(c *cli.Context) error { @@ -62,6 +67,7 @@ func main() { team := c.String("team") org := c.String("org") dir := c.String("dir") + api := c.String("api") if len(username) == 0 { die("env var GITHUB_USERNAME or flag -u must be set", c) @@ -87,7 +93,7 @@ func main() { } sh := shell.NewShell() - githubCli := github.NewGithub(username, token) + githubCli := github.NewGithub(username, token, api) cl := cloner.NewCloner(githubCli, sh, dir) err := cl.Clone(org, team)