This commit is contained in:
Jingwen Owen Ou 2013-12-17 07:45:48 -08:00
Родитель e506ac1437
Коммит b480db1f7f
12 изменённых файлов: 41 добавлений и 42 удалений

Просмотреть файл

@ -56,8 +56,8 @@ func transformCheckoutArgs(args *Args) error {
}
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
gh := github.NewClient(url.Project)
pullRequest, err := gh.PullRequest(id)
gh := github.NewClient(url.Project.Host)
pullRequest, err := gh.PullRequest(url.Project, id)
if err != nil {
return err
}

Просмотреть файл

@ -75,8 +75,8 @@ func ciStatus(cmd *Command, args *Args) {
}
func fetchCiStatus(p *github.Project, sha string) (state, targetURL string, exitCode int, err error) {
gh := github.NewClient(p)
status, err := gh.CIStatus(sha)
gh := github.NewClient(p.Host)
status, err := gh.CIStatus(p, sha)
if err != nil {
return
}

Просмотреть файл

@ -81,7 +81,7 @@ func create(command *Command, args *Args) {
}
project := github.NewProject(owner, newRepoName, credentials.Host)
gh := github.NewClient(project)
gh := github.NewClient(project.Host)
var action string
if gh.IsRepositoryExist(project) {

Просмотреть файл

@ -50,7 +50,7 @@ func tranformFetchArgs(args *Args) error {
_, err := localRepo.RemotesByName(name)
if err != nil {
project := github.NewProject(name, "", "")
gh := github.NewClient(project)
gh := github.NewClient(project.Host)
repo, err := gh.Repository(project)
if err != nil {
continue

Просмотреть файл

@ -41,7 +41,7 @@ func fork(cmd *Command, args *Args) {
credentials := configs.PromptFor(project.Host)
forkProject := github.NewProject(credentials.User, project.Name, project.Host)
client := github.NewClient(project)
client := github.NewClient(project.Host)
existingRepo, err := client.Repository(forkProject)
if err == nil {
var parentURL *github.URL

Просмотреть файл

@ -22,12 +22,12 @@ func issue(cmd *Command, args *Args) {
project, err := localRepo.CurrentProject()
utils.Check(err)
gh := github.NewClient(project)
gh := github.NewClient(project.Host)
if args.Noop {
fmt.Printf("Would request list of issues for %s\n", gh.Project)
fmt.Printf("Would request list of issues for %s\n", project)
} else {
issues, err := gh.Issues()
issues, err := gh.Issues(project)
utils.Check(err)
for _, issue := range issues {
var url string

Просмотреть файл

@ -48,8 +48,8 @@ func transformMergeArgs(args *Args) error {
}
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
gh := github.NewClient(url.Project)
pullRequest, err := gh.PullRequest(id)
gh := github.NewClient(url.Project.Host)
pullRequest, err := gh.PullRequest(url.Project, id)
if err != nil {
return err
}

Просмотреть файл

@ -137,7 +137,7 @@ func pullRequest(cmd *Command, args *Args) {
}
}
client := github.NewClient(baseProject)
client := github.NewClient(baseProject.Host)
// when no tracking, assume remote branch is published under active user's fork
if trackedBranch == nil && !explicitOwner && client.Credentials.User != headProject.Owner {
@ -192,13 +192,13 @@ func pullRequest(cmd *Command, args *Args) {
pullRequestURL = "PULL_REQUEST_URL"
} else {
if title != "" {
pr, err := client.CreatePullRequest(base, fullHead, title, body)
pr, err := client.CreatePullRequest(baseProject, base, fullHead, title, body)
utils.Check(err)
pullRequestURL = pr.HTMLURL
}
if flagPullRequestIssue != "" {
pr, err := client.CreatePullRequestForIssue(base, fullHead, flagPullRequestIssue)
pr, err := client.CreatePullRequestForIssue(baseProject, base, fullHead, flagPullRequestIssue)
utils.Check(err)
pullRequestURL = pr.HTMLURL
}

Просмотреть файл

@ -22,11 +22,11 @@ func release(cmd *Command, args *Args) {
project, err := localRepo.CurrentProject()
utils.Check(err)
gh := github.NewClient(project)
gh := github.NewClient(project.Host)
if args.Noop {
fmt.Printf("Would request list of releases for %s\n", gh.Project)
fmt.Printf("Would request list of releases for %s\n", project)
} else {
releases, err := gh.Releases()
releases, err := gh.Releases(project)
utils.Check(err)
var outputs []string
for _, release := range releases {

Просмотреть файл

@ -34,8 +34,10 @@ func (c *Configs) PromptFor(host string) *Credentials {
user := c.PromptForUser()
pass := c.PromptForPassword(host, user)
client := &GitHub{Project: &Project{Host: host}}
// Create GitHub with a stub Credentials
client := &GitHub{Credentials: &Credentials{Host: host}}
token, err := client.FindOrCreateToken(user, pass, "")
// TODO: return a two-factor error
if err != nil {
re := regexp.MustCompile("two-factor authentication OTP code")
if re.MatchString(fmt.Sprintf("%s", err)) {

Просмотреть файл

@ -14,14 +14,11 @@ const (
)
type GitHub struct {
// TODO: Detach Project from GitHub
// It only cares about host & credentials
Project *Project
Credentials *Credentials
}
func (gh *GitHub) PullRequest(id string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name, "number": id})
func (gh *GitHub) PullRequest(project *Project, id string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name, "number": id})
if err != nil {
return
}
@ -35,8 +32,8 @@ func (gh *GitHub) PullRequest(id string) (pr *octokit.PullRequest, err error) {
return
}
func (gh *GitHub) CreatePullRequest(base, head, title, body string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name})
func (gh *GitHub) CreatePullRequest(project *Project, base, head, title, body string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
@ -51,8 +48,8 @@ func (gh *GitHub) CreatePullRequest(base, head, title, body string) (pr *octokit
return
}
func (gh *GitHub) CreatePullRequestForIssue(base, head, issue string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name})
func (gh *GitHub) CreatePullRequestForIssue(project *Project, base, head, issue string) (pr *octokit.PullRequest, err error) {
url, err := octokit.PullRequestsURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
@ -116,8 +113,8 @@ func (gh *GitHub) CreateRepository(project *Project, description, homepage strin
return
}
func (gh *GitHub) Releases() (releases []octokit.Release, err error) {
url, err := octokit.ReleasesURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name})
func (gh *GitHub) Releases(project *Project) (releases []octokit.Release, err error) {
url, err := octokit.ReleasesURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
@ -132,8 +129,8 @@ func (gh *GitHub) Releases() (releases []octokit.Release, err error) {
return
}
func (gh *GitHub) CIStatus(sha string) (status *octokit.Status, err error) {
url, err := octokit.StatusesURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name, "ref": sha})
func (gh *GitHub) CIStatus(project *Project, sha string) (status *octokit.Status, err error) {
url, err := octokit.StatusesURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name, "ref": sha})
if err != nil {
return
}
@ -167,8 +164,8 @@ func (gh *GitHub) ForkRepository(project *Project) (repo *octokit.Repository, er
return
}
func (gh *GitHub) Issues() (issues []octokit.Issue, err error) {
url, err := octokit.RepoIssuesURL.Expand(octokit.M{"owner": gh.Project.Owner, "repo": gh.Project.Name})
func (gh *GitHub) Issues(project *Project) (issues []octokit.Issue, err error) {
url, err := octokit.RepoIssuesURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
@ -233,7 +230,7 @@ func (gh *GitHub) octokit() (c *octokit.Client) {
func (gh *GitHub) requestURL(u *url.URL) (uu *url.URL) {
uu = u
if gh.Project.Host != GitHubHost {
if gh.Credentials.Host != GitHubHost {
uu, _ = url.Parse(fmt.Sprintf("/api/v3/%s", u.Path))
}
@ -243,7 +240,7 @@ func (gh *GitHub) requestURL(u *url.URL) (uu *url.URL) {
func (gh *GitHub) apiEndpoint() string {
host := os.Getenv("GH_API_HOST")
if host == "" {
host = gh.Project.Host
host = gh.Credentials.Host
}
if host == GitHubHost {
@ -262,7 +259,7 @@ func absolute(endpoint string) string {
return u.String()
}
func NewClient(p *Project) *GitHub {
c := CurrentConfigs().PromptFor(p.Host)
return &GitHub{Project: p, Credentials: c}
func NewClient(host string) *GitHub {
c := CurrentConfigs().PromptFor(host)
return &GitHub{Credentials: c}
}

Просмотреть файл

@ -6,12 +6,12 @@ import (
)
func TestApiEndpoint(t *testing.T) {
gh := &GitHub{Project: &Project{Name: "foo", Owner: "bar", Host: "github.com"}}
gh := &GitHub{Credentials: &Credentials{Host: "github.com"}}
assert.Equal(t, "https://api.github.com", gh.apiEndpoint())
gh = &GitHub{Project: &Project{Name: "foo", Owner: "bar", Host: "github.corporate.com"}}
gh = &GitHub{Credentials: &Credentials{Host: "github.corporate.com"}}
assert.Equal(t, "https://github.corporate.com", gh.apiEndpoint())
gh = &GitHub{Project: &Project{Name: "foo", Owner: "bar", Host: "http://github.corporate.com"}}
gh = &GitHub{Credentials: &Credentials{Host: "http://github.corporate.com"}}
assert.Equal(t, "http://github.corporate.com", gh.apiEndpoint())
}