提交 0df403f1 编写于 作者: J Jingwen Owen Ou

Start basic auth implementation

上级 ba1fe663
......@@ -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
}
......@@ -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)
......
......@@ -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]
......
......@@ -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)
......
......@@ -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}
......
......@@ -45,7 +45,6 @@ var (
cmdPullRequest,
cmdHelp,
}
gh = NewGitHub(os.Getenv("HOME") + "/.config/gh")
repo = NewRepo()
)
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册