diff --git a/config.go b/config.go index 290e9ce9c4d2fc76d88f2a4894bdcc612db82da1..fa743cac8f87e0a2f32a027f756263bd6c480864 100644 --- a/config.go +++ b/config.go @@ -12,10 +12,10 @@ type Config struct { Token string } -func loadConfig(filename string) Config { +func LoadConfig(filename string) (*Config, error) { f, err := os.Open(filename) if err != nil { - log.Fatal(err) + return &Config{}, err } defer f.Close() @@ -28,5 +28,5 @@ func loadConfig(filename string) Config { log.Fatal(err) } - return c + return &c, nil } diff --git a/config_test.go b/config_test.go index adfb9c5587424f80866e5b64cbf99f4b266b058a..9896bbe8542db07dc18aebef4b1ca0e21f1aaf71 100644 --- a/config_test.go +++ b/config_test.go @@ -2,13 +2,11 @@ package main import ( "github.com/bmizerany/assert" - "os" "testing" ) func TestLoadConfig(t *testing.T) { - home := os.Getenv("HOME") - config := loadConfig(home + "/.config/gh") + config, _ := LoadConfig("./test_support/gh") assert.Equal(t, "jingweno", config.User) assert.Equal(t, "02a66f3bdde949182bc0d629f1abef0d501e6a53", config.Token) diff --git a/git.go b/git.go index cf795e1046bcb5884daaffb5ab362fa6ecf56bc4..79b2507d016a21d3581856bff841d4025ef50c46 100644 --- a/git.go +++ b/git.go @@ -38,7 +38,7 @@ func FetchGitHead() string { // FIXME: only care about origin push remote now func FetchGitRemote() string { - r := regexp.MustCompile("origin\t(.+) \\(push\\)") + r := regexp.MustCompile("origin\t(.+github.com.+) \\(push\\)") for _, output := range execGitCmd([]string{"remote", "-v"}) { if r.MatchString(output) { return r.FindStringSubmatch(output)[1] diff --git a/git_hub.go b/git_hub.go index c5e9bf94b05ac04bce9b37a5eb289af657bf49e1..3911a864160d40c41a832901dc624fd546c7ae19 100644 --- a/git_hub.go +++ b/git_hub.go @@ -7,6 +7,9 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" + "os" + "path/filepath" ) const ( @@ -26,11 +29,11 @@ type unprocessableEntity struct { Errors []unprocessableEntityError `json:"errors"` } -func NewGitHub(configFile string) GitHub { - config := loadConfig(configFile) - client := &http.Client{} +func NewGitHub() *GitHub { + configFile := filepath.Join(os.Getenv("HOME"), ".config", "gh") + config, _ := LoadConfig(configFile) - return GitHub{client, config.Token} + return &GitHub{&http.Client{}, config.Token} } type GitHub struct { @@ -38,7 +41,15 @@ type GitHub struct { Authorization string } +func (gh *GitHub) performBasicAuth(url *url.URL) { + url.String() +} + func (gh *GitHub) call(request *http.Request) (*http.Response, error) { + if len(gh.Authorization) == 0 { + gh.performBasicAuth(request.URL) + } + request.Header.Set("Authorization", "token "+gh.Authorization) response, err := gh.httpClient.Do(request) diff --git a/git_hub_test.go b/git_hub_test.go index 181de201b276627b7831814e47db1a3ba2739c4c..8ddb3c1b1fddb2f1ab8c16b132b2a81b36f1ed94 100644 --- a/git_hub_test.go +++ b/git_hub_test.go @@ -3,13 +3,11 @@ package main import ( "github.com/bmizerany/assert" "net/http" - "os" "testing" ) func _TestCreatePullRequest(t *testing.T) { - home := os.Getenv("HOME") - config := loadConfig(home + "/.config/gh") + config, _ := LoadConfig("./test_support/gh") client := &http.Client{} gh := GitHub{client, config.Token} diff --git a/main.go b/main.go index 9d79ae0ef096948dabf7e3f6494cf7e2e9204281..7dd743612480d0901469e40e37e4f16196080c3f 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,6 @@ var ( cmdPullRequest, cmdHelp, } - gh = NewGitHub(os.Getenv("HOME") + "/.config/gh") repo = NewRepo() ) diff --git a/pull_request.go b/pull_request.go index ade179f53987a3ca126a4ba0d9c1b0103df9c176..6a1ac1c605bcb432306e221ab7bdc7fcdba059b1 100644 --- a/pull_request.go +++ b/pull_request.go @@ -65,6 +65,7 @@ func pullRequest(cmd *Command, args []string) { } params := PullRequestParams{title, body, flagPullRequestBase, flagPullRequestHead} + gh := NewGitHub() pullRequestResponse, err := gh.CreatePullRequest(repo.Owner, repo.Project, params) if err != nil { log.Fatal(err)