From 6e6b18c50a3a1c8f806bcecc80a89833a8697705 Mon Sep 17 00:00:00 2001 From: nate smith Date: Thu, 10 Oct 2019 15:43:25 -0500 Subject: [PATCH] move more stuff into context.go --- api/queries.go | 35 ++++++++++++----------------------- context/context.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/api/queries.go b/api/queries.go index aa26c0be0..72b9056e0 100644 --- a/api/queries.go +++ b/api/queries.go @@ -2,9 +2,8 @@ package api import ( "fmt" - "strings" - "github.com/github/gh-cli/git" + "github.com/github/gh-cli/context" "github.com/github/gh-cli/github" ) @@ -84,10 +83,17 @@ func PullRequests() (PullRequestsPayload, error) { project := project() owner := project.Owner repo := project.Name - currentBranch := currentBranch() + currentBranch, cberr := context.CurrentBranch() + if cberr != nil { + return PullRequestsPayload{}, cberr + } + currentUsername, err := context.CurrentUsername() + if err != nil { + return PullRequestsPayload{}, err + } - viewerQuery := fmt.Sprintf("repo:%s/%s state:open is:pr author:%s", owner, repo, currentUsername()) - reviewerQuery := fmt.Sprintf("repo:%s/%s state:open review-requested:%s", owner, repo, currentUsername()) + viewerQuery := fmt.Sprintf("repo:%s/%s state:open is:pr author:%s", owner, repo, currentUsername) + reviewerQuery := fmt.Sprintf("repo:%s/%s state:open review-requested:%s", owner, repo, currentUsername) variables := map[string]string{ "viewerQuery": viewerQuery, @@ -98,7 +104,7 @@ func PullRequests() (PullRequestsPayload, error) { } var resp response - err := graphQL(query, variables, &resp) + err = graphQL(query, variables, &resp) if err != nil { return PullRequestsPayload{}, err } @@ -142,20 +148,3 @@ func project() github.Project { panic("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?") } - -func currentBranch() string { - currentBranch, err := git.Head() - if err != nil { - panic(err) - } - - return strings.Replace(currentBranch, "refs/heads/", "", 1) -} - -func currentUsername() string { - host, err := github.CurrentConfig().DefaultHost() - if err != nil { - panic(err) - } - return host.User -} diff --git a/context/context.go b/context/context.go index ab830deab..85af0d8f9 100644 --- a/context/context.go +++ b/context/context.go @@ -4,6 +4,10 @@ import ( "io/ioutil" "os/user" "regexp" + "strings" + + "github.com/github/gh-cli/git" + "github.com/github/gh-cli/github" ) // GetToken returns the authentication token as stored in the config file for the user running gh-cli @@ -22,3 +26,20 @@ func GetToken() (string, error) { token := r.FindStringSubmatch(string(content)) return token[1], nil } + +func CurrentUsername() (string, error) { + host, err := github.CurrentConfig().DefaultHost() + if err != nil { + return "", nil + } + return host.User, nil +} + +func CurrentBranch() (string, error) { + currentBranch, err := git.Head() + if err != nil { + return "", err + } + + return strings.Replace(currentBranch, "refs/heads/", "", 1), nil +}