Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/queries_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

import (
"context"

"github.com/shurcooL/githubv4"
)

func CurrentLoginName(client *Client) (string, error) {
var query struct {
Viewer struct {
Login string
}
}
v4 := githubv4.NewClient(client.http)
err := v4.Query(context.Background(), &query, nil)
return query.Viewer.Login, err
}
2 changes: 1 addition & 1 deletion command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func issueStatus(cmd *cobra.Command, args []string) error {
return err
}

currentUser, err := ctx.AuthLogin()
currentUser, err := api.CurrentLoginName(apiClient)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions command/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestIssueStatus(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.Register(
httpmock.GraphQL(`\bviewer\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))

jsonFile, _ := os.Open("../test/fixtures/issueStatus.json")
defer jsonFile.Close()
Expand Down Expand Up @@ -49,6 +52,9 @@ func TestIssueStatus_blankSlate(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.Register(
httpmock.GraphQL(`\bviewer\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand Down Expand Up @@ -86,6 +92,9 @@ func TestIssueStatus_disabledIssues(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.Register(
httpmock.GraphQL(`\bviewer\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand Down
7 changes: 2 additions & 5 deletions command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ func prStatus(cmd *cobra.Command, args []string) error {
return err
}

currentUser, err := ctx.AuthLogin()
if err != nil {
return err
}

baseRepo, err := determineBaseRepo(apiClient, cmd, ctx)
if err != nil {
return err
Expand All @@ -125,6 +120,8 @@ func prStatus(cmd *cobra.Command, args []string) error {
return fmt.Errorf("could not query for pull request for current branch: %w", err)
}

// the `@me` macro is available because the API lookup is ElasticSearch-based
currentUser := "@me"
prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser)
if err != nil {
return err
Expand Down
37 changes: 29 additions & 8 deletions command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ var initContext = func() context.Context {
if repo := os.Getenv("GH_REPO"); repo != "" {
ctx.SetBaseRepo(repo)
}
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
ctx.SetAuthToken(token)
}
return ctx
}

Expand All @@ -113,11 +116,15 @@ func BasicClient() (*api.Client, error) {
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)))

if c, err := config.ParseDefaultConfig(); err == nil {
if token, _ := c.Get(defaultHostname, "oauth_token"); token != "" {
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
if c, err := config.ParseDefaultConfig(); err == nil {
token, _ = c.Get(defaultHostname, "oauth_token")
}
}
if token != "" {
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))
}
return api.NewClient(opts...), nil
}

Expand Down Expand Up @@ -145,8 +152,12 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
return fmt.Sprintf("token %s", token)
}

tokenFromEnv := func() bool {
return os.Getenv("GITHUB_TOKEN") == token
}

checkScopesFunc := func(appID string) error {
if config.IsGitHubApp(appID) && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
if config.IsGitHubApp(appID) && !tokenFromEnv() && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
newToken, loginHandle, err := config.AuthFlow("Notice: additional authorization required")
if err != nil {
return err
Expand All @@ -168,7 +179,11 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
} else {
fmt.Fprintln(os.Stderr, "Warning: gh now requires the `read:org` OAuth scope.")
fmt.Fprintln(os.Stderr, "Visit https://github.com/settings/tokens and edit your token to enable `read:org`")
fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`")
if tokenFromEnv() {
fmt.Fprintln(os.Stderr, "or generate a new token for the GITHUB_TOKEN environment variable")
} else {
fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
return nil
}
Expand All @@ -194,7 +209,9 @@ var ensureScopes = func(ctx context.Context, client *api.Client, wantedScopes ..
return client, nil
}

if config.IsGitHubApp(appID) && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
tokenFromEnv := len(os.Getenv("GITHUB_TOKEN")) > 0

if config.IsGitHubApp(appID) && !tokenFromEnv && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
newToken, loginHandle, err := config.AuthFlow("Notice: additional authorization required")
if err != nil {
return client, err
Expand All @@ -219,9 +236,13 @@ var ensureScopes = func(ctx context.Context, client *api.Client, wantedScopes ..

return reloadedClient, nil
} else {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Warning: gh now requires the `%s` OAuth scope(s).", wantedScopes))
fmt.Fprintln(os.Stderr, fmt.Sprintf("Warning: gh now requires %s OAuth scopes.", wantedScopes))
fmt.Fprintln(os.Stderr, fmt.Sprintf("Visit https://github.com/settings/tokens and edit your token to enable %s", wantedScopes))
fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`")
if tokenFromEnv {
fmt.Fprintln(os.Stderr, "or generate a new token for the GITHUB_TOKEN environment variable")
} else {
fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`")
}
return client, errors.New("Unable to reauthenticate")
}

Expand Down
9 changes: 0 additions & 9 deletions context/blank_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func NewBlank() *blankContext {
// A Context implementation that queries the filesystem
type blankContext struct {
authToken string
authLogin string
branch string
baseRepo ghrepo.Interface
remotes Remotes
Expand All @@ -39,14 +38,6 @@ func (c *blankContext) SetAuthToken(t string) {
c.authToken = t
}

func (c *blankContext) SetAuthLogin(login string) {
c.authLogin = login
}

func (c *blankContext) AuthLogin() (string, error) {
return c.authLogin, nil
}

func (c *blankContext) Branch() (string, error) {
if c.branch == "" {
return "", fmt.Errorf("branch was not initialized")
Expand Down
15 changes: 0 additions & 15 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const defaultHostname = "github.com"
type Context interface {
AuthToken() (string, error)
SetAuthToken(string)
AuthLogin() (string, error)
Branch() (string, error)
SetBranch(string)
Remotes() (Remotes, error)
Expand Down Expand Up @@ -195,20 +194,6 @@ func (c *fsContext) SetAuthToken(t string) {
c.authToken = t
}

func (c *fsContext) AuthLogin() (string, error) {
config, err := c.Config()
if err != nil {
return "", err
}

login, err := config.Get(defaultHostname, "user")
if login == "" || err != nil {
return "", err
}

return login, nil
}

func (c *fsContext) Branch() (string, error) {
if c.branch != "" {
return c.branch, nil
Expand Down
9 changes: 1 addition & 8 deletions internal/config/config_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ func setupConfigFile(filename string) (Config, error) {

func getViewer(token string) (string, error) {
http := api.NewClient(api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))

response := struct {
Viewer struct {
Login string
}
}{}
err := http.GraphQL("{ viewer { login } }", nil, &response)
return response.Viewer.Login, err
return api.CurrentLoginName(http)
}

func waitForEnter(r io.Reader) error {
Expand Down