diff options
Diffstat (limited to 'bridge/github')
-rw-r--r-- | bridge/github/config.go (renamed from bridge/github/auth.go) | 92 | ||||
-rw-r--r-- | bridge/github/github.go | 28 |
2 files changed, 92 insertions, 28 deletions
diff --git a/bridge/github/auth.go b/bridge/github/config.go index b721df7f..3b12d3f9 100644 --- a/bridge/github/auth.go +++ b/bridge/github/config.go @@ -10,25 +10,36 @@ import ( "math/rand" "net/http" "os" + "regexp" "strings" "syscall" "time" + "github.com/MichaelMure/git-bug/bridge/core" + "github.com/MichaelMure/git-bug/repository" "golang.org/x/crypto/ssh/terminal" ) -const githubV3Url = "https://api.github.com" +const githubV3Url = "https://api.Github.com" +const keyUser = "user" +const keyProject = "project" +const keyToken = "token" + +func (*Github) Configure(repo repository.RepoCommon) (core.Configuration, error) { + conf := make(core.Configuration) -func Configure() (map[string]string, error) { fmt.Println("git-bug will generate an access token in your Github profile.") // fmt.Println("The token will have the \"repo\" permission, giving it read/write access to your repositories and issues. There is no narrower scope available, sorry :-|") fmt.Println() - tokenName, err := promptTokenName() + projectUser, projectName, err := promptURL() if err != nil { return nil, err } + conf[keyUser] = projectUser + conf[keyProject] = projectName + fmt.Println() username, err := promptUsername() @@ -47,12 +58,7 @@ func Configure() (map[string]string, error) { // Attempt to authenticate and create a token - var note string - if tokenName == "" { - note = "git-bug" - } else { - note = fmt.Sprintf("git-bug - %s", tokenName) - } + note := fmt.Sprintf("git-bug - %s/%s", projectUser, projectName) resp, err := requestToken(note, username, password) if err != nil { @@ -61,10 +67,6 @@ func Configure() (map[string]string, error) { defer resp.Body.Close() - if resp.StatusCode == http.StatusCreated { - return decodeBody(resp.Body) - } - // Handle 2FA is needed OTPHeader := resp.Header.Get("X-GitHub-OTP") if resp.StatusCode == http.StatusUnauthorized && OTPHeader != "" { @@ -79,10 +81,15 @@ func Configure() (map[string]string, error) { } defer resp.Body.Close() + } - if resp.StatusCode == http.StatusCreated { - return decodeBody(resp.Body) + if resp.StatusCode == http.StatusCreated { + token, err := decodeBody(resp.Body) + if err != nil { + return nil, err } + conf[keyToken] = token + return conf, nil } b, _ := ioutil.ReadAll(resp.Body) @@ -129,7 +136,7 @@ func requestTokenWith2FA(note, username, password, otpCode string) (*http.Respon return client.Do(req) } -func decodeBody(body io.ReadCloser) (map[string]string, error) { +func decodeBody(body io.ReadCloser) (string, error) { data, _ := ioutil.ReadAll(body) aux := struct { @@ -138,12 +145,14 @@ func decodeBody(body io.ReadCloser) (map[string]string, error) { err := json.Unmarshal(data, &aux) if err != nil { - return nil, err + return "", err + } + + if aux.Token == "" { + return "", fmt.Errorf("no token found in response: %s", string(data)) } - return map[string]string{ - "token": aux.Token, - }, nil + return aux.Token, nil } func randomFingerprint() string { @@ -180,17 +189,46 @@ func promptUsername() (string, error) { } } -func promptTokenName() (string, error) { - fmt.Println("To help distinguish the token, you can optionally provide a description") - fmt.Println("The token will be named \"git-bug - <description>\"") - fmt.Println("description:") +func promptURL() (string, string, error) { + for { + fmt.Println("Github project URL:") + + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return "", "", err + } + + line = strings.TrimRight(line, "\n") - line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if line == "" { + fmt.Println("URL is empty") + continue + } + + projectUser, projectName, err := splitURL(line) + + if err != nil { + fmt.Println(err) + continue + } + + return projectUser, projectName, nil + } +} + +func splitURL(url string) (string, string, error) { + re, err := regexp.Compile(`github\.com\/([^\/]*)\/([^\/]*)`) if err != nil { - return "", err + return "", "", err + } + + res := re.FindStringSubmatch(url) + + if res == nil { + return "", "", fmt.Errorf("bad github project url") } - return strings.TrimRight(line, "\n"), nil + return res[1], res[2], nil } func validateUsername(username string) (bool, error) { diff --git a/bridge/github/github.go b/bridge/github/github.go index 0238b4bf..45954e23 100644 --- a/bridge/github/github.go +++ b/bridge/github/github.go @@ -1,4 +1,30 @@ package github -type github struct { +import ( + "github.com/MichaelMure/git-bug/bridge/core" + "github.com/MichaelMure/git-bug/cache" +) + +type Github struct{} + +func (*Github) Name() string { + return "github" +} + +func (*Github) Importer() core.Importer { + return &githubImporter{} +} + +func (*Github) Exporter() core.Exporter { + return nil +} + +type githubImporter struct{} + +func (*githubImporter) ImportAll(repo *cache.RepoCache, conf core.Configuration) error { + panic("implement me") +} + +func (*githubImporter) Import(repo *cache.RepoCache, conf core.Configuration, id string) error { + panic("implement me") } |