Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate gist create to new command format #1406

Merged
merged 11 commits into from Jul 22, 2020
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Expand Up @@ -29,7 +29,7 @@ jobs:
go mod verify
go mod download

LINT_VERSION=1.27.0
LINT_VERSION=1.29.0
curl -fsSL https://github.com/golangci/golangci-lint/releases/download/v${LINT_VERSION}/golangci-lint-${LINT_VERSION}-linux-amd64.tar.gz | \
tar xz --strip-components 1 --wildcards \*/golangci-lint
mkdir -p bin && mv golangci-lint bin/
Expand Down
18 changes: 13 additions & 5 deletions api/client.go
Expand Up @@ -33,6 +33,12 @@ func NewClient(opts ...ClientOption) *Client {
return client
}

// NewClientFromHTTP takes in an http.Client instance
func NewClientFromHTTP(httpClient *http.Client) *Client {
client := &Client{http: httpClient}
return client
}

// AddHeader turns a RoundTripper into one that adds a request header
func AddHeader(name, value string) ClientOption {
return func(tr http.RoundTripper) http.RoundTripper {
Expand Down Expand Up @@ -179,9 +185,10 @@ func (gr GraphQLErrorResponse) Error() string {

// HTTPError is an error returned by a failed API call
type HTTPError struct {
StatusCode int
RequestURL *url.URL
Message string
StatusCode int
RequestURL *url.URL
Message string
OAuthScopes string
}

func (err HTTPError) Error() string {
Expand Down Expand Up @@ -322,8 +329,9 @@ func handleResponse(resp *http.Response, data interface{}) error {

func handleHTTPError(resp *http.Response) error {
httpError := HTTPError{
StatusCode: resp.StatusCode,
RequestURL: resp.Request.URL,
StatusCode: resp.StatusCode,
RequestURL: resp.Request.URL,
OAuthScopes: resp.Header.Get("X-Oauth-Scopes"),
}

body, err := ioutil.ReadAll(resp.Body)
Expand Down
175 changes: 0 additions & 175 deletions command/gist.go

This file was deleted.

56 changes: 0 additions & 56 deletions command/gist_test.go

This file was deleted.

49 changes: 9 additions & 40 deletions command/root.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/internal/run"
apiCmd "github.com/cli/cli/pkg/cmd/api"
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/utils"
Expand Down Expand Up @@ -95,6 +96,14 @@ func init() {
},
}
RootCmd.AddCommand(apiCmd.NewCmdApi(cmdFactory, nil))

gistCmd := &cobra.Command{
Use: "gist",
Short: "Create gists",
Long: `Work with GitHub gists.`,
}
RootCmd.AddCommand(gistCmd)
gistCmd.AddCommand(gistCreateCmd.NewCmdCreate(cmdFactory, nil))
}

// RootCmd is the entry point of command-line execution
Expand Down Expand Up @@ -252,46 +261,6 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
return api.NewClient(opts...), nil
}

var ensureScopes = func(ctx context.Context, client *api.Client, wantedScopes ...string) (*api.Client, error) {
hasScopes, appID, err := client.HasScopes(wantedScopes...)
if err != nil {
return client, err
}

if hasScopes {
return client, nil
}

tokenFromEnv := len(os.Getenv("GITHUB_TOKEN")) > 0

if config.IsGitHubApp(appID) && !tokenFromEnv && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) {
cfg, err := ctx.Config()
if err != nil {
return nil, err
}
_, err = config.AuthFlowWithConfig(cfg, defaultHostname, "Notice: additional authorization required")
if err != nil {
return nil, err
}

reloadedClient, err := apiClientForContext(ctx)
if err != nil {
return client, err
}
return reloadedClient, nil
} else {
fmt.Fprintf(os.Stderr, "Warning: gh now requires %s OAuth scopes.\n", wantedScopes)
fmt.Fprintf(os.Stderr, "Visit https://github.com/settings/tokens and edit your token to enable %s\n", wantedScopes)
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")
}

}

func apiVerboseLog() api.ClientOption {
logTraffic := strings.Contains(os.Getenv("DEBUG"), "api")
colorize := utils.IsTerminal(os.Stderr)
Expand Down
3 changes: 0 additions & 3 deletions command/testing.go
Expand Up @@ -97,9 +97,6 @@ func initBlankContext(cfg, repo, branch string) {

func initFakeHTTP() *httpmock.Registry {
http := &httpmock.Registry{}
ensureScopes = func(ctx context.Context, client *api.Client, wantedScopes ...string) (*api.Client, error) {
return client, nil
}
apiClientForContext = func(context.Context) (*api.Client, error) {
return api.NewClient(api.ReplaceTripper(http)), nil
}
Expand Down