move more stuff into context.go

This commit is contained in:
nate smith 2019-10-10 15:43:25 -05:00
parent df074046ac
commit 6e6b18c50a
2 changed files with 33 additions and 23 deletions

View file

@ -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
}

View file

@ -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
}