Skip to content

Commit

Permalink
Support custom github api URL
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher committed Sep 20, 2018
1 parent fafebae commit f657b48
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -30,7 +30,7 @@ func TestFetchTeamRepos(t *testing.T) {
teamReposResp("2", "<https://api.github.com/teams/2285789/repos?page=1>; rel=\"prev\", <https://api.github.com/teams/2285789/repos?page=3>; rel=\"next\", <https://api.github.com/teams/2285789/repos?page=1>; rel=\"first\", <https://api.github.com/teams/2285789/repos?page=3>; rel=\"last\"", "teamReposResponsePage2.json")
teamReposResp("3", "<https://api.github.com/teams/2285789/repos?page=2>; rel=\"prev\", <https://api.github.com/teams/2285789/repos?page=1>; rel=\"first\"", "teamReposResponsePage3.json")

githubCli := NewGithub("username", "password")
githubCli := NewGithub("username", "password", "https://api.github.com")

_, teams := githubCli.TeamRepos(2285789)

Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f657b48

Please sign in to comment.