This commit is contained in:
Jingwen Owen Ou 2013-12-17 07:57:22 -08:00
Родитель b480db1f7f
Коммит 77900eac4b
3 изменённых файлов: 40 добавлений и 49 удалений

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

@ -13,18 +13,17 @@ const (
OAuthAppURL string = "http://owenou.com/gh"
)
type GitHub struct {
type Client struct {
Credentials *Credentials
}
func (gh *GitHub) PullRequest(project *Project, id string) (pr *octokit.PullRequest, err error) {
func (client *Client) 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
}
client := gh.octokit()
pr, result := client.PullRequests(gh.requestURL(url)).One()
pr, result := client.octokit().PullRequests(client.requestURL(url)).One()
if result.HasError() {
err = result.Err
}
@ -32,15 +31,14 @@ func (gh *GitHub) PullRequest(project *Project, id string) (pr *octokit.PullRequ
return
}
func (gh *GitHub) CreatePullRequest(project *Project, base, head, title, body string) (pr *octokit.PullRequest, err error) {
func (client *Client) 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
}
client := gh.octokit()
params := octokit.PullRequestParams{Base: base, Head: head, Title: title, Body: body}
pr, result := client.PullRequests(gh.requestURL(url)).Create(params)
pr, result := client.octokit().PullRequests(client.requestURL(url)).Create(params)
if result.HasError() {
err = result.Err
}
@ -48,15 +46,14 @@ func (gh *GitHub) CreatePullRequest(project *Project, base, head, title, body st
return
}
func (gh *GitHub) CreatePullRequestForIssue(project *Project, base, head, issue string) (pr *octokit.PullRequest, err error) {
func (client *Client) 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
}
client := gh.octokit()
params := octokit.PullRequestForIssueParams{Base: base, Head: head, Issue: issue}
pr, result := client.PullRequests(gh.requestURL(url)).Create(params)
pr, result := client.octokit().PullRequests(client.requestURL(url)).Create(params)
if result.HasError() {
err = result.Err
}
@ -64,14 +61,13 @@ func (gh *GitHub) CreatePullRequestForIssue(project *Project, base, head, issue
return
}
func (gh *GitHub) Repository(project *Project) (repo *octokit.Repository, err error) {
func (client *Client) Repository(project *Project) (repo *octokit.Repository, err error) {
url, err := octokit.RepositoryURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
client := gh.octokit()
repo, result := client.Repositories(gh.requestURL(url)).One()
repo, result := client.octokit().Repositories(client.requestURL(url)).One()
if result.HasError() {
err = result.Err
}
@ -79,15 +75,15 @@ func (gh *GitHub) Repository(project *Project) (repo *octokit.Repository, err er
return
}
func (gh *GitHub) IsRepositoryExist(project *Project) bool {
repo, err := gh.Repository(project)
func (client *Client) IsRepositoryExist(project *Project) bool {
repo, err := client.Repository(project)
return err == nil && repo != nil
}
func (gh *GitHub) CreateRepository(project *Project, description, homepage string, isPrivate bool) (repo *octokit.Repository, err error) {
func (client *Client) CreateRepository(project *Project, description, homepage string, isPrivate bool) (repo *octokit.Repository, err error) {
var repoURL octokit.Hyperlink
if project.Owner != gh.Credentials.User {
if project.Owner != client.Credentials.User {
repoURL = octokit.OrgRepositoriesURL
} else {
repoURL = octokit.UserRepositoriesURL
@ -98,14 +94,13 @@ func (gh *GitHub) CreateRepository(project *Project, description, homepage strin
return
}
client := gh.octokit()
params := octokit.Repository{
Name: project.Name,
Description: description,
Homepage: homepage,
Private: isPrivate,
}
repo, result := client.Repositories(gh.requestURL(url)).Create(params)
repo, result := client.octokit().Repositories(client.requestURL(url)).Create(params)
if result.HasError() {
err = result.Err
}
@ -113,14 +108,13 @@ func (gh *GitHub) CreateRepository(project *Project, description, homepage strin
return
}
func (gh *GitHub) Releases(project *Project) (releases []octokit.Release, err error) {
func (client *Client) 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
}
client := gh.octokit()
releases, result := client.Releases(gh.requestURL(url)).All()
releases, result := client.octokit().Releases(client.requestURL(url)).All()
if result.HasError() {
err = result.Err
return
@ -129,14 +123,13 @@ func (gh *GitHub) Releases(project *Project) (releases []octokit.Release, err er
return
}
func (gh *GitHub) CIStatus(project *Project, sha string) (status *octokit.Status, err error) {
func (client *Client) 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
}
client := gh.octokit()
statuses, result := client.Statuses(gh.requestURL(url)).All()
statuses, result := client.octokit().Statuses(client.requestURL(url)).All()
if result.HasError() {
err = result.Err
return
@ -149,14 +142,13 @@ func (gh *GitHub) CIStatus(project *Project, sha string) (status *octokit.Status
return
}
func (gh *GitHub) ForkRepository(project *Project) (repo *octokit.Repository, err error) {
func (client *Client) ForkRepository(project *Project) (repo *octokit.Repository, err error) {
url, err := octokit.ForksURL.Expand(octokit.M{"owner": project.Owner, "repo": project.Name})
if err != nil {
return
}
client := gh.octokit()
repo, result := client.Repositories(gh.requestURL(url)).Create(nil)
repo, result := client.octokit().Repositories(client.requestURL(url)).Create(nil)
if result.HasError() {
err = result.Err
}
@ -164,14 +156,13 @@ func (gh *GitHub) ForkRepository(project *Project) (repo *octokit.Repository, er
return
}
func (gh *GitHub) Issues(project *Project) (issues []octokit.Issue, err error) {
func (client *Client) 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
}
client := gh.octokit()
issues, result := client.Issues(gh.requestURL(url)).All()
issues, result := client.octokit().Issues(client.requestURL(url)).All()
if result.HasError() {
err = result.Err
return
@ -180,15 +171,15 @@ func (gh *GitHub) Issues(project *Project) (issues []octokit.Issue, err error) {
return
}
func (gh *GitHub) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) {
func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) {
url, err := octokit.AuthorizationsURL.Expand(nil)
if err != nil {
return
}
basicAuth := octokit.BasicAuth{Login: user, Password: password, OneTimePassword: twoFactorCode}
client := octokit.NewClientWith(gh.apiEndpoint(), nil, basicAuth)
authsService := client.Authorizations(gh.requestURL(url))
c := octokit.NewClientWith(client.apiEndpoint(), nil, basicAuth)
authsService := c.Authorizations(client.requestURL(url))
auths, result := authsService.All()
if result.HasError() {
@ -221,26 +212,26 @@ func (gh *GitHub) FindOrCreateToken(user, password, twoFactorCode string) (token
return
}
func (gh *GitHub) octokit() (c *octokit.Client) {
tokenAuth := octokit.TokenAuth{AccessToken: gh.Credentials.AccessToken}
c = octokit.NewClientWith(gh.apiEndpoint(), nil, tokenAuth)
func (client *Client) octokit() (c *octokit.Client) {
tokenAuth := octokit.TokenAuth{AccessToken: client.Credentials.AccessToken}
c = octokit.NewClientWith(client.apiEndpoint(), nil, tokenAuth)
return
}
func (gh *GitHub) requestURL(u *url.URL) (uu *url.URL) {
func (client *Client) requestURL(u *url.URL) (uu *url.URL) {
uu = u
if gh.Credentials.Host != GitHubHost {
if client.Credentials.Host != GitHubHost {
uu, _ = url.Parse(fmt.Sprintf("/api/v3/%s", u.Path))
}
return
}
func (gh *GitHub) apiEndpoint() string {
func (client *Client) apiEndpoint() string {
host := os.Getenv("GH_API_HOST")
if host == "" {
host = gh.Credentials.Host
host = client.Credentials.Host
}
if host == GitHubHost {
@ -259,7 +250,7 @@ func absolute(endpoint string) string {
return u.String()
}
func NewClient(host string) *GitHub {
func NewClient(host string) *Client {
c := CurrentConfigs().PromptFor(host)
return &GitHub{Credentials: c}
return &Client{Credentials: c}
}

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

@ -5,13 +5,13 @@ import (
"testing"
)
func TestApiEndpoint(t *testing.T) {
gh := &GitHub{Credentials: &Credentials{Host: "github.com"}}
func TestClient_ApiEndpoint(t *testing.T) {
gh := &Client{Credentials: &Credentials{Host: "github.com"}}
assert.Equal(t, "https://api.github.com", gh.apiEndpoint())
gh = &GitHub{Credentials: &Credentials{Host: "github.corporate.com"}}
gh = &Client{Credentials: &Credentials{Host: "github.corporate.com"}}
assert.Equal(t, "https://github.corporate.com", gh.apiEndpoint())
gh = &GitHub{Credentials: &Credentials{Host: "http://github.corporate.com"}}
gh = &Client{Credentials: &Credentials{Host: "http://github.corporate.com"}}
assert.Equal(t, "http://github.corporate.com", gh.apiEndpoint())
}

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

@ -34,8 +34,8 @@ func (c *Configs) PromptFor(host string) *Credentials {
user := c.PromptForUser()
pass := c.PromptForPassword(host, user)
// Create GitHub with a stub Credentials
client := &GitHub{Credentials: &Credentials{Host: host}}
// Create Client with a stub Credentials
client := &Client{Credentials: &Credentials{Host: host}}
token, err := client.FindOrCreateToken(user, pass, "")
// TODO: return a two-factor error
if err != nil {