Merge branch 'trunk' into multi-devcontainer
This commit is contained in:
commit
ce3d0791c3
171 changed files with 7582 additions and 1540 deletions
|
|
@ -11,12 +11,10 @@ func NewCmdActions(f *cmdutil.Factory) *cobra.Command {
|
|||
cs := f.IOStreams.ColorScheme()
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "actions",
|
||||
Short: "Learn about working with GitHub Actions",
|
||||
Long: actionsExplainer(cs),
|
||||
Annotations: map[string]string{
|
||||
"IsActions": "true",
|
||||
},
|
||||
Use: "actions",
|
||||
Short: "Learn about working with GitHub Actions",
|
||||
Long: actionsExplainer(cs),
|
||||
Hidden: true,
|
||||
}
|
||||
|
||||
cmdutil.DisableAuthCheck(cmd)
|
||||
|
|
|
|||
|
|
@ -24,8 +24,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List your aliases",
|
||||
Use: "list",
|
||||
Short: "List your aliases",
|
||||
Aliases: []string{"ls"},
|
||||
Long: heredoc.Doc(`
|
||||
This command prints out all of the aliases gh is configured to use.
|
||||
`),
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package api
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
|
@ -13,7 +12,6 @@ import (
|
|||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
|
|
@ -216,7 +214,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
|
|||
cmd.Flags().StringArrayVarP(&opts.MagicFields, "field", "F", nil, "Add a typed parameter in `key=value` format")
|
||||
cmd.Flags().StringArrayVarP(&opts.RawFields, "raw-field", "f", nil, "Add a string parameter in `key=value` format")
|
||||
cmd.Flags().StringArrayVarP(&opts.RequestHeaders, "header", "H", nil, "Add a HTTP request header in `key:value` format")
|
||||
cmd.Flags().StringSliceVarP(&opts.Previews, "preview", "p", nil, "Opt into GitHub API previews")
|
||||
cmd.Flags().StringSliceVarP(&opts.Previews, "preview", "p", nil, "GitHub API preview `names` to request (without the \"-preview\" suffix)")
|
||||
cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output")
|
||||
cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results")
|
||||
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The `file` to use as body for the HTTP request (use \"-\" to read from standard input)")
|
||||
|
|
@ -279,11 +277,11 @@ func apiRun(opts *ApiOptions) error {
|
|||
if opts.Silent {
|
||||
opts.IO.Out = ioutil.Discard
|
||||
} else {
|
||||
err := opts.IO.StartPager()
|
||||
if err != nil {
|
||||
return err
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
defer opts.IO.StopPager()
|
||||
}
|
||||
|
||||
cfg, err := opts.Config()
|
||||
|
|
@ -366,13 +364,13 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream
|
|||
responseBody = io.TeeReader(responseBody, bodyCopy)
|
||||
}
|
||||
|
||||
if opts.FilterOutput != "" {
|
||||
if opts.FilterOutput != "" && serverError == "" {
|
||||
// TODO: reuse parsed query across pagination invocations
|
||||
err = export.FilterJSON(opts.IO.Out, responseBody, opts.FilterOutput)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else if opts.Template != "" {
|
||||
} else if opts.Template != "" && serverError == "" {
|
||||
// TODO: reuse parsed template across pagination invocations
|
||||
err = template.Execute(responseBody)
|
||||
if err != nil {
|
||||
|
|
@ -384,11 +382,7 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream
|
|||
_, err = io.Copy(opts.IO.Out, responseBody)
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, syscall.EPIPE) {
|
||||
err = nil
|
||||
} else {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if serverError == "" && resp.StatusCode > 299 {
|
||||
|
|
|
|||
|
|
@ -490,6 +490,20 @@ func Test_apiRun(t *testing.T) {
|
|||
stdout: "not a cat",
|
||||
stderr: ``,
|
||||
},
|
||||
{
|
||||
name: "output template when REST error",
|
||||
options: ApiOptions{
|
||||
Template: `{{.status}}`,
|
||||
},
|
||||
httpResponse: &http.Response{
|
||||
StatusCode: 400,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
||||
},
|
||||
err: cmdutil.SilentError,
|
||||
stdout: `{"message": "THIS IS FINE"}`,
|
||||
stderr: "gh: THIS IS FINE (HTTP 400)\n",
|
||||
},
|
||||
{
|
||||
name: "jq filter",
|
||||
options: ApiOptions{
|
||||
|
|
@ -504,6 +518,20 @@ func Test_apiRun(t *testing.T) {
|
|||
stdout: "Mona\nHubot\n",
|
||||
stderr: ``,
|
||||
},
|
||||
{
|
||||
name: "jq filter when REST error",
|
||||
options: ApiOptions{
|
||||
FilterOutput: `.[].name`,
|
||||
},
|
||||
httpResponse: &http.Response{
|
||||
StatusCode: 400,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
||||
},
|
||||
err: cmdutil.SilentError,
|
||||
stdout: `{"message": "THIS IS FINE"}`,
|
||||
stderr: "gh: THIS IS FINE (HTTP 400)\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -14,8 +14,10 @@ import (
|
|||
func NewCmdAuth(f *cmdutil.Factory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "auth <command>",
|
||||
Short: "Login, logout, and refresh your authentication",
|
||||
Long: `Manage gh's authentication state.`,
|
||||
Short: "Authenticate gh and git with GitHub",
|
||||
Annotations: map[string]string{
|
||||
"IsCore": "true",
|
||||
},
|
||||
}
|
||||
|
||||
cmdutil.DisableAuthCheck(cmd)
|
||||
|
|
|
|||
|
|
@ -112,6 +112,9 @@ func helperRun(opts *CredentialOptions) error {
|
|||
gotUser = tokenUser
|
||||
} else {
|
||||
gotUser, _, _ = cfg.GetWithSource(lookupHost, "user")
|
||||
if gotUser == "" {
|
||||
gotUser = tokenUser
|
||||
}
|
||||
}
|
||||
|
||||
if gotUser == "" || gotToken == "" {
|
||||
|
|
|
|||
|
|
@ -15,11 +15,6 @@ func (c tinyConfig) GetWithSource(host, key string) (string, string, error) {
|
|||
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
|
||||
}
|
||||
|
||||
func (c tinyConfig) Get(host, key string) (val string, err error) {
|
||||
val, _, err = c.GetWithSource(host, key)
|
||||
return
|
||||
}
|
||||
|
||||
func Test_helperRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -170,6 +165,30 @@ func Test_helperRun(t *testing.T) {
|
|||
wantStdout: "",
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
name: "no username configured",
|
||||
opts: CredentialOptions{
|
||||
Operation: "get",
|
||||
Config: func() (config, error) {
|
||||
return tinyConfig{
|
||||
"_source": "/Users/monalisa/.config/gh/hosts.yml",
|
||||
"example.com:oauth_token": "OTOKEN",
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
input: heredoc.Doc(`
|
||||
protocol=https
|
||||
host=example.com
|
||||
`),
|
||||
wantErr: false,
|
||||
wantStdout: heredoc.Doc(`
|
||||
protocol=https
|
||||
host=example.com
|
||||
username=x-access-token
|
||||
password=OTOKEN
|
||||
`),
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
name: "token from env",
|
||||
opts: CredentialOptions{
|
||||
|
|
|
|||
|
|
@ -27,10 +27,11 @@ type LoginOptions struct {
|
|||
|
||||
Interactive bool
|
||||
|
||||
Hostname string
|
||||
Scopes []string
|
||||
Token string
|
||||
Web bool
|
||||
Hostname string
|
||||
Scopes []string
|
||||
Token string
|
||||
Web bool
|
||||
GitProtocol string
|
||||
}
|
||||
|
||||
func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Command {
|
||||
|
|
@ -49,13 +50,17 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
|
|||
Long: heredoc.Docf(`
|
||||
Authenticate with a GitHub host.
|
||||
|
||||
The default authentication mode is a web-based browser flow.
|
||||
The default authentication mode is a web-based browser flow. After completion, an
|
||||
authentication token will be stored internally.
|
||||
|
||||
Alternatively, pass in a token on standard input by using %[1]s--with-token%[1]s.
|
||||
Alternatively, use %[1]s--with-token%[1]s to pass in a token on standard input.
|
||||
The minimum required scopes for the token are: "repo", "read:org".
|
||||
|
||||
The --scopes flag accepts a comma separated list of scopes you want your gh credentials to have. If
|
||||
absent, this command ensures that gh has access to a minimum set of scopes.
|
||||
Alternatively, gh will use the authentication token found in environment variables.
|
||||
This method is most suitable for "headless" use of gh such as in automation. See
|
||||
%[1]sgh help environment%[1]s for more info.
|
||||
|
||||
To use gh in GitHub Actions, add %[1]sGH_TOKEN: ${{secrets.GITHUB_TOKEN}}%[1]s to "env".
|
||||
`, "`"),
|
||||
Example: heredoc.Doc(`
|
||||
# start interactive setup
|
||||
|
|
@ -64,7 +69,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
|
|||
# authenticate against github.com by reading the token from a file
|
||||
$ gh auth login --with-token < mytoken.txt
|
||||
|
||||
# authenticate with a specific GitHub Enterprise Server instance
|
||||
# authenticate with a specific GitHub instance
|
||||
$ gh auth login --hostname enterprise.internal
|
||||
`),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -108,9 +113,10 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
|
|||
}
|
||||
|
||||
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance to authenticate with")
|
||||
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have")
|
||||
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes to request")
|
||||
cmd.Flags().BoolVar(&tokenStdin, "with-token", false, "Read token from standard input")
|
||||
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open a browser to authenticate")
|
||||
cmdutil.StringEnumFlag(cmd, &opts.GitProtocol, "git-protocol", "p", "", []string{"ssh", "https"}, "The protocol to use for git operations")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -186,6 +192,7 @@ func loginRun(opts *LoginOptions) error {
|
|||
Web: opts.Web,
|
||||
Scopes: opts.Scopes,
|
||||
Executable: opts.MainExecutable,
|
||||
GitProtocol: opts.GitProtocol,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
|
|||
Use: "refresh",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Short: "Refresh stored authentication credentials",
|
||||
Long: heredoc.Doc(`Expand or fix the permission scopes for stored credentials
|
||||
Long: heredoc.Doc(`Expand or fix the permission scopes for stored credentials.
|
||||
|
||||
The --scopes flag accepts a comma separated list of scopes you want your gh credentials to have. If
|
||||
absent, this command ensures that gh has access to a minimum set of scopes.
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ type LoginOptions struct {
|
|||
Web bool
|
||||
Scopes []string
|
||||
Executable string
|
||||
GitProtocol string
|
||||
|
||||
sshContext sshContext
|
||||
}
|
||||
|
|
@ -39,8 +40,8 @@ func Login(opts *LoginOptions) error {
|
|||
httpClient := opts.HTTPClient
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
var gitProtocol string
|
||||
if opts.Interactive {
|
||||
gitProtocol := strings.ToLower(opts.GitProtocol)
|
||||
if opts.Interactive && gitProtocol == "" {
|
||||
var proto string
|
||||
err := prompt.SurveyAskOne(&survey.Select{
|
||||
Message: "What is your preferred protocol for Git operations?",
|
||||
|
|
|
|||
|
|
@ -3,17 +3,11 @@ package codespace
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type browser interface {
|
||||
Browse(string) error
|
||||
}
|
||||
|
||||
func newCodeCmd(app *App) *cobra.Command {
|
||||
var (
|
||||
codespace string
|
||||
|
|
@ -25,8 +19,7 @@ func newCodeCmd(app *App) *cobra.Command {
|
|||
Short: "Open a codespace in Visual Studio Code",
|
||||
Args: noArgsConstraint,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
b := cmdutil.NewBrowser("", ioutil.Discard, app.io.ErrOut)
|
||||
return app.VSCode(cmd.Context(), b, codespace, useInsiders)
|
||||
return app.VSCode(cmd.Context(), codespace, useInsiders)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -37,20 +30,14 @@ func newCodeCmd(app *App) *cobra.Command {
|
|||
}
|
||||
|
||||
// VSCode opens a codespace in the local VS VSCode application.
|
||||
func (a *App) VSCode(ctx context.Context, browser browser, codespaceName string, useInsiders bool) error {
|
||||
if codespaceName == "" {
|
||||
codespace, err := chooseCodespace(ctx, a.apiClient)
|
||||
if err != nil {
|
||||
if err == errNoCodespaces {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("error choosing codespace: %w", err)
|
||||
}
|
||||
codespaceName = codespace.Name
|
||||
func (a *App) VSCode(ctx context.Context, codespaceName string, useInsiders bool) error {
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url := vscodeProtocolURL(codespaceName, useInsiders)
|
||||
if err := browser.Browse(url); err != nil {
|
||||
url := vscodeProtocolURL(codespace.Name, useInsiders)
|
||||
if err := a.browser.Browse(url); err != nil {
|
||||
return fmt.Errorf("error opening Visual Studio Code: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func TestApp_VSCode(t *testing.T) {
|
||||
|
|
@ -40,11 +42,57 @@ func TestApp_VSCode(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b := &cmdutil.TestBrowser{}
|
||||
a := &App{}
|
||||
if err := a.VSCode(context.Background(), b, tt.args.codespaceName, tt.args.useInsiders); (err != nil) != tt.wantErr {
|
||||
a := &App{
|
||||
browser: b,
|
||||
apiClient: testCodeApiMock(),
|
||||
}
|
||||
if err := a.VSCode(context.Background(), tt.args.codespaceName, tt.args.useInsiders); (err != nil) != tt.wantErr {
|
||||
t.Errorf("App.VSCode() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
b.Verify(t, tt.wantURL)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingOperationDisallowsCode(t *testing.T) {
|
||||
app := testingCodeApp()
|
||||
|
||||
if err := app.VSCode(context.Background(), "disabledCodespace", false); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testingCodeApp() *App {
|
||||
io, _, _, _ := iostreams.Test()
|
||||
return NewApp(io, nil, testCodeApiMock(), nil)
|
||||
}
|
||||
|
||||
func testCodeApiMock() *apiClientMock {
|
||||
user := &api.User{Login: "monalisa"}
|
||||
testingCodespace := &api.Codespace{
|
||||
Name: "monalisa-cli-cli-abcdef",
|
||||
}
|
||||
disabledCodespace := &api.Codespace{
|
||||
Name: "disabledCodespace",
|
||||
PendingOperation: true,
|
||||
PendingOperationDisabledReason: "Some pending operation",
|
||||
}
|
||||
return &apiClientMock{
|
||||
GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) {
|
||||
if name == "disabledCodespace" {
|
||||
return disabledCodespace, nil
|
||||
}
|
||||
return testingCodespace, nil
|
||||
},
|
||||
GetUserFunc: func(_ context.Context) (*api.User, error) {
|
||||
return user, nil
|
||||
},
|
||||
AuthorizedKeysFunc: func(_ context.Context, _ string) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ import (
|
|||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type browser interface {
|
||||
Browse(string) error
|
||||
}
|
||||
|
||||
type executable interface {
|
||||
Executable() string
|
||||
}
|
||||
|
|
@ -30,9 +34,10 @@ type App struct {
|
|||
apiClient apiClient
|
||||
errLogger *log.Logger
|
||||
executable executable
|
||||
browser browser
|
||||
}
|
||||
|
||||
func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient) *App {
|
||||
func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient, browser browser) *App {
|
||||
errLogger := log.New(io.ErrOut, "", 0)
|
||||
|
||||
return &App{
|
||||
|
|
@ -40,6 +45,7 @@ func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient) *App {
|
|||
apiClient: apiClient,
|
||||
errLogger: errLogger,
|
||||
executable: exe,
|
||||
browser: browser,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,12 +68,14 @@ type apiClient interface {
|
|||
StartCodespace(ctx context.Context, name string) error
|
||||
StopCodespace(ctx context.Context, name string) error
|
||||
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
|
||||
EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
|
||||
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
|
||||
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
|
||||
GetCodespaceRegionLocation(ctx context.Context) (string, error)
|
||||
GetCodespacesMachines(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error)
|
||||
GetCodespaceRepositoryContents(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error)
|
||||
ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []string, err error)
|
||||
GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error)
|
||||
}
|
||||
|
||||
var errNoCodespaces = errors.New("you have no codespaces")
|
||||
|
|
@ -176,6 +184,13 @@ func getOrChooseCodespace(ctx context.Context, apiClient apiClient, codespaceNam
|
|||
}
|
||||
}
|
||||
|
||||
if codespace.PendingOperation {
|
||||
return nil, fmt.Errorf(
|
||||
"codespace is disabled while it has a pending operation: %s",
|
||||
codespace.PendingOperationDisabledReason,
|
||||
)
|
||||
}
|
||||
|
||||
return codespace, nil
|
||||
}
|
||||
|
||||
|
|
@ -250,7 +265,7 @@ type codespace struct {
|
|||
}
|
||||
|
||||
// displayName returns the repository nwo and branch.
|
||||
// If includeName is true, the name of the codespace is included.
|
||||
// If includeName is true, the name of the codespace (including displayName) is included.
|
||||
// If includeGitStatus is true, the branch will include a star if
|
||||
// the codespace has unsaved changes.
|
||||
func (c codespace) displayName(includeName, includeGitStatus bool) string {
|
||||
|
|
@ -260,11 +275,18 @@ func (c codespace) displayName(includeName, includeGitStatus bool) string {
|
|||
}
|
||||
|
||||
if includeName {
|
||||
var displayName = c.Name
|
||||
if c.DisplayName != "" {
|
||||
displayName = c.DisplayName
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"%s: %s [%s]", c.Repository.FullName, branch, c.Name,
|
||||
"%s: %s (%s)", c.Repository.FullName, displayName, branch,
|
||||
)
|
||||
}
|
||||
return c.Repository.FullName + ": " + branch
|
||||
return fmt.Sprintf(
|
||||
"%s: %s", c.Repository.FullName, branch,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
// gitStatusDirty represents an unsaved changes status.
|
||||
|
|
|
|||
132
pkg/cmd/codespace/common_test.go
Normal file
132
pkg/cmd/codespace/common_test.go
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
package codespace
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
)
|
||||
|
||||
func Test_codespace_displayName(t *testing.T) {
|
||||
type fields struct {
|
||||
Codespace *api.Codespace
|
||||
}
|
||||
type args struct {
|
||||
includeName bool
|
||||
includeGitStatus bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "No included name or gitstatus",
|
||||
fields: fields{
|
||||
Codespace: &api.Codespace{
|
||||
GitStatus: api.CodespaceGitStatus{
|
||||
Ref: "trunk",
|
||||
},
|
||||
Repository: api.Repository{
|
||||
FullName: "cli/cli",
|
||||
},
|
||||
DisplayName: "scuba steve",
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
includeName: false,
|
||||
includeGitStatus: false,
|
||||
},
|
||||
want: "cli/cli: trunk",
|
||||
},
|
||||
{
|
||||
name: "No included name - included gitstatus - no unsaved changes",
|
||||
fields: fields{
|
||||
Codespace: &api.Codespace{
|
||||
GitStatus: api.CodespaceGitStatus{
|
||||
Ref: "trunk",
|
||||
},
|
||||
Repository: api.Repository{
|
||||
FullName: "cli/cli",
|
||||
},
|
||||
DisplayName: "scuba steve",
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
includeName: false,
|
||||
includeGitStatus: true,
|
||||
},
|
||||
want: "cli/cli: trunk",
|
||||
},
|
||||
{
|
||||
name: "No included name - included gitstatus - unsaved changes",
|
||||
fields: fields{
|
||||
Codespace: &api.Codespace{
|
||||
GitStatus: api.CodespaceGitStatus{
|
||||
Ref: "trunk",
|
||||
HasUncommitedChanges: true,
|
||||
},
|
||||
Repository: api.Repository{
|
||||
FullName: "cli/cli",
|
||||
},
|
||||
DisplayName: "scuba steve",
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
includeName: false,
|
||||
includeGitStatus: true,
|
||||
},
|
||||
want: "cli/cli: trunk*",
|
||||
},
|
||||
{
|
||||
name: "Included name - included gitstatus - unsaved changes",
|
||||
fields: fields{
|
||||
Codespace: &api.Codespace{
|
||||
GitStatus: api.CodespaceGitStatus{
|
||||
Ref: "trunk",
|
||||
HasUncommitedChanges: true,
|
||||
},
|
||||
Repository: api.Repository{
|
||||
FullName: "cli/cli",
|
||||
},
|
||||
DisplayName: "scuba steve",
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
includeName: true,
|
||||
includeGitStatus: true,
|
||||
},
|
||||
want: "cli/cli: scuba steve (trunk*)",
|
||||
},
|
||||
{
|
||||
name: "Included name - included gitstatus - no unsaved changes",
|
||||
fields: fields{
|
||||
Codespace: &api.Codespace{
|
||||
GitStatus: api.CodespaceGitStatus{
|
||||
Ref: "trunk",
|
||||
HasUncommitedChanges: false,
|
||||
},
|
||||
Repository: api.Repository{
|
||||
FullName: "cli/cli",
|
||||
},
|
||||
DisplayName: "scuba steve",
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
includeName: true,
|
||||
includeGitStatus: true,
|
||||
},
|
||||
want: "cli/cli: scuba steve (trunk)",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := codespace{
|
||||
Codespace: tt.fields.Codespace,
|
||||
}
|
||||
if got := c.displayName(tt.args.includeName, tt.args.includeGitStatus); got != tt.want {
|
||||
t.Errorf("codespace.displayName() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -4,21 +4,25 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/cli/cli/v2/internal/codespaces"
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type createOptions struct {
|
||||
repo string
|
||||
branch string
|
||||
machine string
|
||||
showStatus bool
|
||||
idleTimeout time.Duration
|
||||
devContainerPath string
|
||||
repo string
|
||||
branch string
|
||||
machine string
|
||||
showStatus bool
|
||||
permissionsOptOut bool
|
||||
devContainerPath string
|
||||
idleTimeout time.Duration
|
||||
}
|
||||
|
||||
func newCreateCmd(app *App) *cobra.Command {
|
||||
|
|
@ -36,6 +40,7 @@ func newCreateCmd(app *App) *cobra.Command {
|
|||
createCmd.Flags().StringVarP(&opts.repo, "repo", "r", "", "repository name with owner: user/repo")
|
||||
createCmd.Flags().StringVarP(&opts.branch, "branch", "b", "", "repository branch")
|
||||
createCmd.Flags().StringVarP(&opts.machine, "machine", "m", "", "hardware specifications for the VM")
|
||||
createCmd.Flags().BoolVarP(&opts.permissionsOptOut, "default-permissions", "", false, "do not prompt to accept additional permissions requested by the codespace")
|
||||
createCmd.Flags().BoolVarP(&opts.showStatus, "status", "s", false, "show status of post-create command and dotfiles")
|
||||
createCmd.Flags().DurationVar(&opts.idleTimeout, "idle-timeout", 0, "allowed inactivity before codespace is stopped, e.g. \"10m\", \"1h\"")
|
||||
createCmd.Flags().StringVar(&opts.devContainerPath, "devcontainer-path", "", "path to the devcontainer.json file to use when creating codespace")
|
||||
|
|
@ -45,7 +50,13 @@ func newCreateCmd(app *App) *cobra.Command {
|
|||
|
||||
// Create creates a new Codespace
|
||||
func (a *App) Create(ctx context.Context, opts createOptions) error {
|
||||
locationCh := getLocation(ctx, a.apiClient)
|
||||
|
||||
// Overrides for Codespace developers to target test environments
|
||||
vscsLocation := os.Getenv("VSCS_LOCATION")
|
||||
vscsTarget := os.Getenv("VSCS_TARGET")
|
||||
vscsTargetUrl := os.Getenv("VSCS_TARGET_URL")
|
||||
|
||||
locationCh := getLocation(ctx, vscsLocation, a.apiClient)
|
||||
|
||||
userInputs := struct {
|
||||
Repository string
|
||||
|
|
@ -62,8 +73,14 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
|
|||
}
|
||||
questions := []*survey.Question{
|
||||
{
|
||||
Name: "repository",
|
||||
Prompt: &survey.Input{Message: "Repository:"},
|
||||
Name: "repository",
|
||||
Prompt: &survey.Input{
|
||||
Message: "Repository:",
|
||||
Help: "Search for repos by name. To search within an org or user, or to see private repos, enter at least ':user/'.",
|
||||
Suggest: func(toComplete string) []string {
|
||||
return getRepoSuggestions(ctx, a.apiClient, toComplete)
|
||||
},
|
||||
},
|
||||
Validate: survey.Required,
|
||||
},
|
||||
{
|
||||
|
|
@ -135,18 +152,34 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
|
|||
return errors.New("there are no available machine types for this repository")
|
||||
}
|
||||
|
||||
a.StartProgressIndicatorWithLabel("Creating codespace")
|
||||
codespace, err := a.apiClient.CreateCodespace(ctx, &api.CreateCodespaceParams{
|
||||
createParams := &api.CreateCodespaceParams{
|
||||
RepositoryID: repository.ID,
|
||||
Branch: branch,
|
||||
Machine: machine,
|
||||
Location: locationResult.Location,
|
||||
VSCSTarget: vscsTarget,
|
||||
VSCSTargetURL: vscsTargetUrl,
|
||||
IdleTimeoutMinutes: int(opts.idleTimeout.Minutes()),
|
||||
DevContainerPath: devContainerPath,
|
||||
})
|
||||
PermissionsOptOut: opts.permissionsOptOut,
|
||||
}
|
||||
|
||||
a.StartProgressIndicatorWithLabel("Creating codespace")
|
||||
codespace, err := a.apiClient.CreateCodespace(ctx, createParams)
|
||||
|
||||
a.StopProgressIndicator()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating codespace: %w", err)
|
||||
var aerr api.AcceptPermissionsRequiredError
|
||||
if !errors.As(err, &aerr) || aerr.AllowPermissionsURL == "" {
|
||||
return fmt.Errorf("error creating codespace: %w", err)
|
||||
}
|
||||
|
||||
codespace, err = a.handleAdditionalPermissions(ctx, createParams, aerr.AllowPermissionsURL)
|
||||
if err != nil {
|
||||
// this error could be a cmdutil.SilentError (in the case that the user opened the browser) so we don't want to wrap it
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.showStatus {
|
||||
|
|
@ -159,6 +192,72 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (a *App) handleAdditionalPermissions(ctx context.Context, createParams *api.CreateCodespaceParams, allowPermissionsURL string) (*api.Codespace, error) {
|
||||
var (
|
||||
isInteractive = a.io.CanPrompt()
|
||||
cs = a.io.ColorScheme()
|
||||
displayURL = utils.DisplayURL(allowPermissionsURL)
|
||||
)
|
||||
|
||||
fmt.Fprintf(a.io.ErrOut, "You must authorize or deny additional permissions requested by this codespace before continuing.\n")
|
||||
|
||||
if !isInteractive {
|
||||
fmt.Fprintf(a.io.ErrOut, "%s in your browser to review and authorize additional permissions: %s\n", cs.Bold("Open this URL"), displayURL)
|
||||
fmt.Fprintf(a.io.ErrOut, "Alternatively, you can run %q with the %q option to continue without authorizing additional permissions.\n", a.io.ColorScheme().Bold("create"), cs.Bold("--default-permissions"))
|
||||
return nil, cmdutil.SilentError
|
||||
}
|
||||
|
||||
choices := []string{
|
||||
"Continue in browser to review and authorize additional permissions (Recommended)",
|
||||
"Continue without authorizing additional permissions",
|
||||
}
|
||||
|
||||
permsSurvey := []*survey.Question{
|
||||
{
|
||||
Name: "accept",
|
||||
Prompt: &survey.Select{
|
||||
Message: "What would you like to do?",
|
||||
Options: choices,
|
||||
Default: choices[0],
|
||||
},
|
||||
Validate: survey.Required,
|
||||
},
|
||||
}
|
||||
|
||||
var answers struct {
|
||||
Accept string
|
||||
}
|
||||
|
||||
if err := ask(permsSurvey, &answers); err != nil {
|
||||
return nil, fmt.Errorf("error getting answers: %w", err)
|
||||
}
|
||||
|
||||
// if the user chose to continue in the browser, open the URL
|
||||
if answers.Accept == choices[0] {
|
||||
fmt.Fprintln(a.io.ErrOut, "Please re-run the create request after accepting permissions in the browser.")
|
||||
if err := a.browser.Browse(allowPermissionsURL); err != nil {
|
||||
return nil, fmt.Errorf("error opening browser: %w", err)
|
||||
}
|
||||
// browser opened successfully but we do not know if they accepted the permissions
|
||||
// so we must exit and wait for the user to attempt the create again
|
||||
return nil, cmdutil.SilentError
|
||||
}
|
||||
|
||||
// if the user chose to create the codespace without the permissions,
|
||||
// we can continue with the create opting out of the additional permissions
|
||||
createParams.PermissionsOptOut = true
|
||||
|
||||
a.StartProgressIndicatorWithLabel("Creating codespace")
|
||||
codespace, err := a.apiClient.CreateCodespace(ctx, createParams)
|
||||
a.StopProgressIndicator()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating codespace: %w", err)
|
||||
}
|
||||
|
||||
return codespace, nil
|
||||
}
|
||||
|
||||
// showStatus polls the codespace for a list of post create states and their status. It will keep polling
|
||||
// until all states have finished. Once all states have finished, we poll once more to check if any new
|
||||
// states have been introduced and stop polling otherwise.
|
||||
|
|
@ -228,9 +327,18 @@ type locationResult struct {
|
|||
Err error
|
||||
}
|
||||
|
||||
// getLocation fetches the closest Codespace datacenter region/location to the user.
|
||||
func getLocation(ctx context.Context, apiClient apiClient) <-chan locationResult {
|
||||
// getLocation fetches the closest Codespace datacenter
|
||||
// region/location to the user, unless the 'vscsLocationOverride' override is set
|
||||
func getLocation(ctx context.Context, vscsLocationOverride string, apiClient apiClient) <-chan locationResult {
|
||||
ch := make(chan locationResult, 1)
|
||||
|
||||
// Developer override is set, return the override
|
||||
if vscsLocationOverride != "" {
|
||||
ch <- locationResult{vscsLocationOverride, nil}
|
||||
return ch
|
||||
}
|
||||
|
||||
// Dynamically fetch the region location
|
||||
go func() {
|
||||
location, err := apiClient.GetCodespaceRegionLocation(ctx)
|
||||
ch <- locationResult{location, err}
|
||||
|
|
@ -299,6 +407,21 @@ func getMachineName(ctx context.Context, apiClient apiClient, repoID int, machin
|
|||
return selectedMachine.Name, nil
|
||||
}
|
||||
|
||||
func getRepoSuggestions(ctx context.Context, apiClient apiClient, partialSearch string) []string {
|
||||
searchParams := api.RepoSearchParameters{
|
||||
// The prompt shows 7 items so 7 effectively turns off scrolling which is similar behavior to other clients
|
||||
MaxRepos: 7,
|
||||
Sort: "repo",
|
||||
}
|
||||
|
||||
repos, err := apiClient.GetCodespaceRepoSuggestions(ctx, partialSearch, searchParams)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return repos
|
||||
}
|
||||
|
||||
// buildDisplayName returns display name to be used in the machine survey prompt.
|
||||
func buildDisplayName(displayName string, prebuildAvailability string) string {
|
||||
prebuildText := ""
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
|
|
@ -110,6 +111,9 @@ func TestApp_Create(t *testing.T) {
|
|||
Name: "monalisa-dotfiles-abcd1234",
|
||||
}, nil
|
||||
},
|
||||
GetCodespaceRepoSuggestionsFunc: func(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error) {
|
||||
return nil, nil // We can't ask for suggestions without a terminal.
|
||||
},
|
||||
},
|
||||
},
|
||||
opts: createOptions{
|
||||
|
|
@ -150,6 +154,57 @@ func TestApp_Create(t *testing.T) {
|
|||
wantErr: true,
|
||||
wantErrMsg: "error getting devcontainer.json paths: some error",
|
||||
},
|
||||
{
|
||||
name: "create codespace that requires accepting additional permissions",
|
||||
fields: fields{
|
||||
apiClient: &apiClientMock{
|
||||
GetCodespaceRegionLocationFunc: func(ctx context.Context) (string, error) {
|
||||
return "EUROPE", nil
|
||||
},
|
||||
GetRepositoryFunc: func(ctx context.Context, nwo string) (*api.Repository, error) {
|
||||
return &api.Repository{
|
||||
ID: 1234,
|
||||
FullName: nwo,
|
||||
DefaultBranch: "main",
|
||||
}, nil
|
||||
},
|
||||
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error) {
|
||||
return []*api.Machine{
|
||||
{
|
||||
Name: "GIGA",
|
||||
DisplayName: "Gigabits of a machine",
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) {
|
||||
if params.Branch != "main" {
|
||||
return nil, fmt.Errorf("got branch %q, want %q", params.Branch, "main")
|
||||
}
|
||||
if params.IdleTimeoutMinutes != 30 {
|
||||
return nil, fmt.Errorf("idle timeout minutes was %v", params.IdleTimeoutMinutes)
|
||||
}
|
||||
return &api.Codespace{}, api.AcceptPermissionsRequiredError{
|
||||
AllowPermissionsURL: "https://example.com/permissions",
|
||||
}
|
||||
},
|
||||
GetCodespaceRepoSuggestionsFunc: func(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error) {
|
||||
return nil, nil // We can't ask for suggestions without a terminal.
|
||||
},
|
||||
},
|
||||
},
|
||||
opts: createOptions{
|
||||
repo: "monalisa/dotfiles",
|
||||
branch: "",
|
||||
machine: "GIGA",
|
||||
showStatus: false,
|
||||
idleTimeout: 30 * time.Minute,
|
||||
},
|
||||
wantErr: cmdutil.SilentError,
|
||||
wantStderr: `You must authorize or deny additional permissions requested by this codespace before continuing.
|
||||
Open this URL in your browser to review and authorize additional permissions: example.com/permissions
|
||||
Alternatively, you can run "create" with the "--default-permissions" option to continue without authorizing additional permissions.
|
||||
`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
@ -161,8 +216,8 @@ func TestApp_Create(t *testing.T) {
|
|||
io: io,
|
||||
apiClient: tt.fields.apiClient,
|
||||
}
|
||||
err := a.Create(context.Background(), tt.opts)
|
||||
if (err != nil) != tt.wantErr {
|
||||
|
||||
if err := a.Create(context.Background(), tt.opts); err != tt.wantErr {
|
||||
t.Errorf("App.Create() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if tt.wantErrMsg != "" && err.Error() != tt.wantErrMsg {
|
||||
|
|
|
|||
|
|
@ -190,7 +190,7 @@ func TestDelete(t *testing.T) {
|
|||
io, _, stdout, stderr := iostreams.Test()
|
||||
io.SetStdinTTY(true)
|
||||
io.SetStdoutTTY(true)
|
||||
app := NewApp(io, nil, apiMock)
|
||||
app := NewApp(io, nil, apiMock, nil)
|
||||
err := app.Delete(context.Background(), opts)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("delete() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
64
pkg/cmd/codespace/edit.go
Normal file
64
pkg/cmd/codespace/edit.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package codespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type editOptions struct {
|
||||
codespaceName string
|
||||
displayName string
|
||||
idleTimeout time.Duration
|
||||
machine string
|
||||
}
|
||||
|
||||
func newEditCmd(app *App) *cobra.Command {
|
||||
opts := editOptions{}
|
||||
|
||||
editCmd := &cobra.Command{
|
||||
Use: "edit",
|
||||
Short: "Edit a codespace",
|
||||
Args: noArgsConstraint,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return app.Edit(cmd.Context(), opts)
|
||||
},
|
||||
}
|
||||
|
||||
editCmd.Flags().StringVarP(&opts.codespaceName, "codespace", "c", "", "Name of the codespace")
|
||||
editCmd.Flags().StringVarP(&opts.displayName, "displayName", "d", "", "display name")
|
||||
editCmd.Flags().DurationVar(&opts.idleTimeout, "idle-timeout", 0, "allowed inactivity before codespace is stopped, e.g. \"10m\", \"1h\"")
|
||||
editCmd.Flags().StringVarP(&opts.machine, "machine", "m", "", "hardware specifications for the VM")
|
||||
|
||||
return editCmd
|
||||
}
|
||||
|
||||
// Edits a codespace
|
||||
func (a *App) Edit(ctx context.Context, opts editOptions) error {
|
||||
userInputs := struct {
|
||||
CodespaceName string
|
||||
DisplayName string
|
||||
IdleTimeout time.Duration
|
||||
SKU string
|
||||
}{
|
||||
CodespaceName: opts.codespaceName,
|
||||
DisplayName: opts.displayName,
|
||||
IdleTimeout: opts.idleTimeout,
|
||||
SKU: opts.machine,
|
||||
}
|
||||
a.StartProgressIndicatorWithLabel("Editing codespace")
|
||||
_, err := a.apiClient.EditCodespace(ctx, userInputs.CodespaceName, &api.EditCodespaceParams{
|
||||
DisplayName: userInputs.DisplayName,
|
||||
IdleTimeoutMinutes: int(userInputs.IdleTimeout.Minutes()),
|
||||
Machine: userInputs.SKU,
|
||||
})
|
||||
a.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error editing codespace: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -16,9 +16,10 @@ func newListCmd(app *App) *cobra.Command {
|
|||
var exporter cmdutil.Exporter
|
||||
|
||||
listCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List your codespaces",
|
||||
Args: noArgsConstraint,
|
||||
Use: "list",
|
||||
Short: "List your codespaces",
|
||||
Aliases: []string{"ls"},
|
||||
Args: noArgsConstraint,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if limit < 1 {
|
||||
return cmdutil.FlagErrorf("invalid limit: %v", limit)
|
||||
|
|
@ -42,6 +43,14 @@ func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) er
|
|||
return fmt.Errorf("error getting codespaces: %w", err)
|
||||
}
|
||||
|
||||
hasNonProdVSCSTarget := false
|
||||
for _, apiCodespace := range codespaces {
|
||||
if apiCodespace.VSCSTarget != "" && apiCodespace.VSCSTarget != api.VSCSTargetProduction {
|
||||
hasNonProdVSCSTarget = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := a.io.StartPager(); err != nil {
|
||||
a.errLogger.Printf("error starting pager: %v", err)
|
||||
}
|
||||
|
|
@ -58,6 +67,11 @@ func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) er
|
|||
tp.AddField("BRANCH", nil, nil)
|
||||
tp.AddField("STATE", nil, nil)
|
||||
tp.AddField("CREATED AT", nil, nil)
|
||||
|
||||
if hasNonProdVSCSTarget {
|
||||
tp.AddField("VSCS TARGET", nil, nil)
|
||||
}
|
||||
|
||||
tp.EndRow()
|
||||
}
|
||||
|
||||
|
|
@ -73,10 +87,24 @@ func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) er
|
|||
stateColor = cs.Green
|
||||
}
|
||||
|
||||
tp.AddField(c.Name, nil, cs.Yellow)
|
||||
formattedName := formatNameForVSCSTarget(c.Name, c.VSCSTarget)
|
||||
|
||||
var nameColor func(string) string
|
||||
switch c.PendingOperation {
|
||||
case false:
|
||||
nameColor = cs.Yellow
|
||||
case true:
|
||||
nameColor = cs.Gray
|
||||
}
|
||||
|
||||
tp.AddField(formattedName, nil, nameColor)
|
||||
tp.AddField(c.Repository.FullName, nil, nil)
|
||||
tp.AddField(c.branchWithGitStatus(), nil, cs.Cyan)
|
||||
tp.AddField(c.State, nil, stateColor)
|
||||
if c.PendingOperation {
|
||||
tp.AddField(c.PendingOperationDisabledReason, nil, nameColor)
|
||||
} else {
|
||||
tp.AddField(c.State, nil, stateColor)
|
||||
}
|
||||
|
||||
if tp.IsTTY() {
|
||||
ct, err := time.Parse(time.RFC3339, c.CreatedAt)
|
||||
|
|
@ -87,8 +115,25 @@ func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) er
|
|||
} else {
|
||||
tp.AddField(c.CreatedAt, nil, nil)
|
||||
}
|
||||
|
||||
if hasNonProdVSCSTarget {
|
||||
tp.AddField(c.VSCSTarget, nil, nil)
|
||||
}
|
||||
|
||||
tp.EndRow()
|
||||
}
|
||||
|
||||
return tp.Render()
|
||||
}
|
||||
|
||||
func formatNameForVSCSTarget(name, vscsTarget string) string {
|
||||
if vscsTarget == api.VSCSTargetDevelopment || vscsTarget == api.VSCSTargetLocal {
|
||||
return fmt.Sprintf("%s 🚧", name)
|
||||
}
|
||||
|
||||
if vscsTarget == api.VSCSTargetPPE {
|
||||
return fmt.Sprintf("%s ✨", name)
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ func (a *App) Logs(ctx context.Context, codespaceName string, follow bool) (err
|
|||
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get or choose codespace: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
authkeys := make(chan error, 1)
|
||||
|
|
|
|||
47
pkg/cmd/codespace/logs_test.go
Normal file
47
pkg/cmd/codespace/logs_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package codespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func TestPendingOperationDisallowsLogs(t *testing.T) {
|
||||
app := testingLogsApp()
|
||||
|
||||
if err := app.Logs(context.Background(), "disabledCodespace", false); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testingLogsApp() *App {
|
||||
user := &api.User{Login: "monalisa"}
|
||||
disabledCodespace := &api.Codespace{
|
||||
Name: "disabledCodespace",
|
||||
PendingOperation: true,
|
||||
PendingOperationDisabledReason: "Some pending operation",
|
||||
}
|
||||
apiMock := &apiClientMock{
|
||||
GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) {
|
||||
if name == "disabledCodespace" {
|
||||
return disabledCodespace, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
GetUserFunc: func(_ context.Context) (*api.User, error) {
|
||||
return user, nil
|
||||
},
|
||||
AuthorizedKeysFunc: func(_ context.Context, _ string) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
io, _, _, _ := iostreams.Test()
|
||||
return NewApp(io, nil, apiMock, nil)
|
||||
}
|
||||
|
|
@ -25,12 +25,18 @@ import (
|
|||
// DeleteCodespaceFunc: func(ctx context.Context, name string) error {
|
||||
// panic("mock out the DeleteCodespace method")
|
||||
// },
|
||||
// EditCodespaceFunc: func(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error) {
|
||||
// panic("mock out the EditCodespace method")
|
||||
// },
|
||||
// GetCodespaceFunc: func(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error) {
|
||||
// panic("mock out the GetCodespace method")
|
||||
// },
|
||||
// GetCodespaceRegionLocationFunc: func(ctx context.Context) (string, error) {
|
||||
// panic("mock out the GetCodespaceRegionLocation method")
|
||||
// },
|
||||
// GetCodespaceRepoSuggestionsFunc: func(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error) {
|
||||
// panic("mock out the GetCodespaceRepoSuggestions method")
|
||||
// },
|
||||
// GetCodespaceRepositoryContentsFunc: func(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error) {
|
||||
// panic("mock out the GetCodespaceRepositoryContents method")
|
||||
// },
|
||||
|
|
@ -71,12 +77,18 @@ type apiClientMock struct {
|
|||
// DeleteCodespaceFunc mocks the DeleteCodespace method.
|
||||
DeleteCodespaceFunc func(ctx context.Context, name string) error
|
||||
|
||||
// EditCodespaceFunc mocks the EditCodespace method.
|
||||
EditCodespaceFunc func(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
|
||||
|
||||
// GetCodespaceFunc mocks the GetCodespace method.
|
||||
GetCodespaceFunc func(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error)
|
||||
|
||||
// GetCodespaceRegionLocationFunc mocks the GetCodespaceRegionLocation method.
|
||||
GetCodespaceRegionLocationFunc func(ctx context.Context) (string, error)
|
||||
|
||||
// GetCodespaceRepoSuggestionsFunc mocks the GetCodespaceRepoSuggestions method.
|
||||
GetCodespaceRepoSuggestionsFunc func(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error)
|
||||
|
||||
// GetCodespaceRepositoryContentsFunc mocks the GetCodespaceRepositoryContents method.
|
||||
GetCodespaceRepositoryContentsFunc func(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error)
|
||||
|
||||
|
|
@ -124,6 +136,15 @@ type apiClientMock struct {
|
|||
// Name is the name argument value.
|
||||
Name string
|
||||
}
|
||||
// EditCodespace holds details about calls to the EditCodespace method.
|
||||
EditCodespace []struct {
|
||||
// Ctx is the ctx argument value.
|
||||
Ctx context.Context
|
||||
// CodespaceName is the codespaceName argument value.
|
||||
CodespaceName string
|
||||
// Params is the params argument value.
|
||||
Params *api.EditCodespaceParams
|
||||
}
|
||||
// GetCodespace holds details about calls to the GetCodespace method.
|
||||
GetCodespace []struct {
|
||||
// Ctx is the ctx argument value.
|
||||
|
|
@ -138,6 +159,15 @@ type apiClientMock struct {
|
|||
// Ctx is the ctx argument value.
|
||||
Ctx context.Context
|
||||
}
|
||||
// GetCodespaceRepoSuggestions holds details about calls to the GetCodespaceRepoSuggestions method.
|
||||
GetCodespaceRepoSuggestions []struct {
|
||||
// Ctx is the ctx argument value.
|
||||
Ctx context.Context
|
||||
// PartialSearch is the partialSearch argument value.
|
||||
PartialSearch string
|
||||
// Params is the params argument value.
|
||||
Params api.RepoSearchParameters
|
||||
}
|
||||
// GetCodespaceRepositoryContents holds details about calls to the GetCodespaceRepositoryContents method.
|
||||
GetCodespaceRepositoryContents []struct {
|
||||
// Ctx is the ctx argument value.
|
||||
|
|
@ -206,8 +236,10 @@ type apiClientMock struct {
|
|||
lockAuthorizedKeys sync.RWMutex
|
||||
lockCreateCodespace sync.RWMutex
|
||||
lockDeleteCodespace sync.RWMutex
|
||||
lockEditCodespace sync.RWMutex
|
||||
lockGetCodespace sync.RWMutex
|
||||
lockGetCodespaceRegionLocation sync.RWMutex
|
||||
lockGetCodespaceRepoSuggestions sync.RWMutex
|
||||
lockGetCodespaceRepositoryContents sync.RWMutex
|
||||
lockGetCodespacesMachines sync.RWMutex
|
||||
lockGetRepository sync.RWMutex
|
||||
|
|
@ -323,6 +355,45 @@ func (mock *apiClientMock) DeleteCodespaceCalls() []struct {
|
|||
return calls
|
||||
}
|
||||
|
||||
// EditCodespace calls EditCodespaceFunc.
|
||||
func (mock *apiClientMock) EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error) {
|
||||
if mock.EditCodespaceFunc == nil {
|
||||
panic("apiClientMock.EditCodespaceFunc: method is nil but apiClient.EditCodespace was just called")
|
||||
}
|
||||
callInfo := struct {
|
||||
Ctx context.Context
|
||||
CodespaceName string
|
||||
Params *api.EditCodespaceParams
|
||||
}{
|
||||
Ctx: ctx,
|
||||
CodespaceName: codespaceName,
|
||||
Params: params,
|
||||
}
|
||||
mock.lockEditCodespace.Lock()
|
||||
mock.calls.EditCodespace = append(mock.calls.EditCodespace, callInfo)
|
||||
mock.lockEditCodespace.Unlock()
|
||||
return mock.EditCodespaceFunc(ctx, codespaceName, params)
|
||||
}
|
||||
|
||||
// EditCodespaceCalls gets all the calls that were made to EditCodespace.
|
||||
// Check the length with:
|
||||
// len(mockedapiClient.EditCodespaceCalls())
|
||||
func (mock *apiClientMock) EditCodespaceCalls() []struct {
|
||||
Ctx context.Context
|
||||
CodespaceName string
|
||||
Params *api.EditCodespaceParams
|
||||
} {
|
||||
var calls []struct {
|
||||
Ctx context.Context
|
||||
CodespaceName string
|
||||
Params *api.EditCodespaceParams
|
||||
}
|
||||
mock.lockEditCodespace.RLock()
|
||||
calls = mock.calls.EditCodespace
|
||||
mock.lockEditCodespace.RUnlock()
|
||||
return calls
|
||||
}
|
||||
|
||||
// GetCodespace calls GetCodespaceFunc.
|
||||
func (mock *apiClientMock) GetCodespace(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error) {
|
||||
if mock.GetCodespaceFunc == nil {
|
||||
|
|
@ -393,6 +464,45 @@ func (mock *apiClientMock) GetCodespaceRegionLocationCalls() []struct {
|
|||
return calls
|
||||
}
|
||||
|
||||
// GetCodespaceRepoSuggestions calls GetCodespaceRepoSuggestionsFunc.
|
||||
func (mock *apiClientMock) GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error) {
|
||||
if mock.GetCodespaceRepoSuggestionsFunc == nil {
|
||||
panic("apiClientMock.GetCodespaceRepoSuggestionsFunc: method is nil but apiClient.GetCodespaceRepoSuggestions was just called")
|
||||
}
|
||||
callInfo := struct {
|
||||
Ctx context.Context
|
||||
PartialSearch string
|
||||
Params api.RepoSearchParameters
|
||||
}{
|
||||
Ctx: ctx,
|
||||
PartialSearch: partialSearch,
|
||||
Params: params,
|
||||
}
|
||||
mock.lockGetCodespaceRepoSuggestions.Lock()
|
||||
mock.calls.GetCodespaceRepoSuggestions = append(mock.calls.GetCodespaceRepoSuggestions, callInfo)
|
||||
mock.lockGetCodespaceRepoSuggestions.Unlock()
|
||||
return mock.GetCodespaceRepoSuggestionsFunc(ctx, partialSearch, params)
|
||||
}
|
||||
|
||||
// GetCodespaceRepoSuggestionsCalls gets all the calls that were made to GetCodespaceRepoSuggestions.
|
||||
// Check the length with:
|
||||
// len(mockedapiClient.GetCodespaceRepoSuggestionsCalls())
|
||||
func (mock *apiClientMock) GetCodespaceRepoSuggestionsCalls() []struct {
|
||||
Ctx context.Context
|
||||
PartialSearch string
|
||||
Params api.RepoSearchParameters
|
||||
} {
|
||||
var calls []struct {
|
||||
Ctx context.Context
|
||||
PartialSearch string
|
||||
Params api.RepoSearchParameters
|
||||
}
|
||||
mock.lockGetCodespaceRepoSuggestions.RLock()
|
||||
calls = mock.calls.GetCodespaceRepoSuggestions
|
||||
mock.lockGetCodespaceRepoSuggestions.RUnlock()
|
||||
return calls
|
||||
}
|
||||
|
||||
// GetCodespaceRepositoryContents calls GetCodespaceRepositoryContentsFunc.
|
||||
func (mock *apiClientMock) GetCodespaceRepositoryContents(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error) {
|
||||
if mock.GetCodespaceRepositoryContentsFunc == nil {
|
||||
|
|
|
|||
|
|
@ -48,11 +48,7 @@ func newPortsCmd(app *App) *cobra.Command {
|
|||
func (a *App) ListPorts(ctx context.Context, codespaceName string, exporter cmdutil.Exporter) (err error) {
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
|
||||
if err != nil {
|
||||
// TODO(josebalius): remove special handling of this error here and it other places
|
||||
if err == errNoCodespaces {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("error choosing codespace: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
devContainerCh := getDevContainer(ctx, a.apiClient, codespace)
|
||||
|
|
@ -237,10 +233,7 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
|
|||
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
|
||||
if err != nil {
|
||||
if err == errNoCodespaces {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("error getting codespace: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
session, err := codespaces.ConnectToLiveshare(ctx, a, noopLogger(), a.apiClient, codespace)
|
||||
|
|
@ -313,10 +306,7 @@ func (a *App) ForwardPorts(ctx context.Context, codespaceName string, ports []st
|
|||
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
|
||||
if err != nil {
|
||||
if err == errNoCodespaces {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("error getting codespace: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
session, err := codespaces.ConnectToLiveshare(ctx, a, noopLogger(), a.apiClient, codespace)
|
||||
|
|
|
|||
71
pkg/cmd/codespace/ports_test.go
Normal file
71
pkg/cmd/codespace/ports_test.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package codespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func TestPendingOperationDisallowsListPorts(t *testing.T) {
|
||||
app := testingPortsApp()
|
||||
|
||||
if err := app.ListPorts(context.Background(), "disabledCodespace", nil); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingOperationDisallowsUpdatePortVisability(t *testing.T) {
|
||||
app := testingPortsApp()
|
||||
|
||||
if err := app.UpdatePortVisibility(context.Background(), "disabledCodespace", nil); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingOperationDisallowsForwardPorts(t *testing.T) {
|
||||
app := testingPortsApp()
|
||||
|
||||
if err := app.ForwardPorts(context.Background(), "disabledCodespace", nil); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testingPortsApp() *App {
|
||||
user := &api.User{Login: "monalisa"}
|
||||
disabledCodespace := &api.Codespace{
|
||||
Name: "disabledCodespace",
|
||||
PendingOperation: true,
|
||||
PendingOperationDisabledReason: "Some pending operation",
|
||||
}
|
||||
apiMock := &apiClientMock{
|
||||
GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) {
|
||||
if name == "disabledCodespace" {
|
||||
return disabledCodespace, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
GetUserFunc: func(_ context.Context) (*api.User, error) {
|
||||
return user, nil
|
||||
},
|
||||
AuthorizedKeysFunc: func(_ context.Context, _ string) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
io, _, _, _ := iostreams.Test()
|
||||
return NewApp(io, nil, apiMock, nil)
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ func NewRootCmd(app *App) *cobra.Command {
|
|||
|
||||
root.AddCommand(newCodeCmd(app))
|
||||
root.AddCommand(newCreateCmd(app))
|
||||
root.AddCommand(newEditCmd(app))
|
||||
root.AddCommand(newDeleteCmd(app))
|
||||
root.AddCommand(newListCmd(app))
|
||||
root.AddCommand(newLogsCmd(app))
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
|
|||
|
||||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, opts.codespace)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get or choose codespace: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
liveshareLogger := noopLogger()
|
||||
|
|
@ -379,6 +379,7 @@ func newCpCmd(app *App) *cobra.Command {
|
|||
cpCmd.Flags().BoolVarP(&opts.recursive, "recursive", "r", false, "Recursively copy directories")
|
||||
cpCmd.Flags().BoolVarP(&opts.expand, "expand", "e", false, "Expand remote file names on remote shell")
|
||||
cpCmd.Flags().StringVarP(&opts.codespace, "codespace", "c", "", "Name of the codespace")
|
||||
cpCmd.Flags().StringVarP(&opts.profile, "profile", "p", "", "Name of the SSH profile to use")
|
||||
return cpCmd
|
||||
}
|
||||
|
||||
|
|
|
|||
47
pkg/cmd/codespace/ssh_test.go
Normal file
47
pkg/cmd/codespace/ssh_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package codespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/codespaces/api"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func TestPendingOperationDisallowsSSH(t *testing.T) {
|
||||
app := testingSSHApp()
|
||||
|
||||
if err := app.SSH(context.Background(), []string{}, sshOptions{codespace: "disabledCodespace"}); err != nil {
|
||||
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
|
||||
t.Errorf("expected pending operation error, but got: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Error("expected pending operation error, but got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func testingSSHApp() *App {
|
||||
user := &api.User{Login: "monalisa"}
|
||||
disabledCodespace := &api.Codespace{
|
||||
Name: "disabledCodespace",
|
||||
PendingOperation: true,
|
||||
PendingOperationDisabledReason: "Some pending operation",
|
||||
}
|
||||
apiMock := &apiClientMock{
|
||||
GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) {
|
||||
if name == "disabledCodespace" {
|
||||
return disabledCodespace, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
GetUserFunc: func(_ context.Context) (*api.User, error) {
|
||||
return user, nil
|
||||
},
|
||||
AuthorizedKeysFunc: func(_ context.Context, _ string) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
io, _, _, _ := iostreams.Test()
|
||||
return NewApp(io, nil, apiMock, nil)
|
||||
}
|
||||
|
|
@ -92,8 +92,7 @@ func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
|
|||
}
|
||||
|
||||
cmdutil.DisableAuthCheck(cmd)
|
||||
|
||||
cmd.Flags().StringVarP(&shellType, "shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
|
||||
cmdutil.StringEnumFlag(cmd, &shellType, "shell", "s", "", []string{"bash", "zsh", "fish", "powershell"}, "Shell type")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ func TestNewCmdCompletion(t *testing.T) {
|
|||
{
|
||||
name: "unsupported shell",
|
||||
args: "completion -s csh",
|
||||
wantErr: "unsupported shell type \"csh\"",
|
||||
wantErr: "invalid argument \"csh\" for \"-s, --shell\" flag: valid values are {bash|zsh|fish|powershell}",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -23,9 +23,10 @@ func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Print a list of configuration keys and values",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Use: "list",
|
||||
Short: "Print a list of configuration keys and values",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
|
|
|
|||
|
|
@ -33,16 +33,17 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
|
|||
|
||||
An extension cannot override any of the core gh commands.
|
||||
|
||||
See the list of available extensions at <https://github.com/topics/gh-extension>
|
||||
See the list of available extensions at <https://github.com/topics/gh-extension>.
|
||||
`, "`"),
|
||||
Aliases: []string{"extensions"},
|
||||
}
|
||||
|
||||
extCmd.AddCommand(
|
||||
&cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List installed extension commands",
|
||||
Args: cobra.NoArgs,
|
||||
Use: "list",
|
||||
Short: "List installed extension commands",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cmds := m.List(true)
|
||||
if len(cmds) == 0 {
|
||||
|
|
@ -60,6 +61,11 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
|
|||
|
||||
t.AddField(fmt.Sprintf("gh %s", c.Name()), nil, nil)
|
||||
t.AddField(repo, nil, nil)
|
||||
version := c.CurrentVersion()
|
||||
if !c.IsBinary() && len(version) > 8 {
|
||||
version = version[:8]
|
||||
}
|
||||
t.AddField(version, nil, nil)
|
||||
var updateAvailable string
|
||||
if c.UpdateAvailable() {
|
||||
updateAvailable = "Upgrade available"
|
||||
|
|
@ -82,7 +88,7 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
|
|||
To install an extension in development from the current directory, use "." as the
|
||||
value of the repository argument.
|
||||
|
||||
See the list of available extensions at <https://github.com/topics/gh-extension>
|
||||
See the list of available extensions at <https://github.com/topics/gh-extension>.
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
$ gh extension install owner/gh-extension
|
||||
|
|
|
|||
|
|
@ -299,7 +299,7 @@ func TestNewCmdExtension(t *testing.T) {
|
|||
assert.Equal(t, 1, len(em.ListCalls()))
|
||||
}
|
||||
},
|
||||
wantStdout: "gh test\tcli/gh-test\t\ngh test2\tcli/gh-test2\tUpgrade available\n",
|
||||
wantStdout: "gh test\tcli/gh-test\t1\t\ngh test2\tcli/gh-test2\t1\tUpgrade available\n",
|
||||
},
|
||||
{
|
||||
name: "create extension interactive",
|
||||
|
|
|
|||
|
|
@ -39,6 +39,10 @@ func (e *Extension) IsLocal() bool {
|
|||
return e.isLocal
|
||||
}
|
||||
|
||||
func (e *Extension) CurrentVersion() string {
|
||||
return e.currentVersion
|
||||
}
|
||||
|
||||
func (e *Extension) UpdateAvailable() bool {
|
||||
if e.isLocal ||
|
||||
e.currentVersion == "" ||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
|
@ -215,7 +215,7 @@ func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []stri
|
|||
return fs, fmt.Errorf("failed to read file %s: %w", f, err)
|
||||
}
|
||||
|
||||
filename = path.Base(f)
|
||||
filename = filepath.Base(f)
|
||||
}
|
||||
|
||||
fs[filename] = &shared.GistFile{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -163,9 +163,9 @@ func TestNewCmdCreate(t *testing.T) {
|
|||
|
||||
func Test_createRun(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
fixtureFile := path.Join(tempDir, "fixture.txt")
|
||||
fixtureFile := filepath.Join(tempDir, "fixture.txt")
|
||||
assert.NoError(t, ioutil.WriteFile(fixtureFile, []byte("{}"), 0644))
|
||||
emptyFile := path.Join(tempDir, "empty.txt")
|
||||
emptyFile := filepath.Join(tempDir, "empty.txt")
|
||||
assert.NoError(t, ioutil.WriteFile(emptyFile, []byte(" \t\n"), 0644))
|
||||
|
||||
tests := []struct {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
|
@ -80,7 +80,7 @@ func deleteRun(opts *DeleteOptions) error {
|
|||
}
|
||||
|
||||
if username != gist.Owner.Login {
|
||||
return fmt.Errorf("You do not own this gist.")
|
||||
return errors.New("you do not own this gist")
|
||||
}
|
||||
|
||||
err = deleteGist(apiClient, host, gistID)
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ func Test_deleteRun(t *testing.T) {
|
|||
Owner: &shared.GistOwner{Login: "octocat2"},
|
||||
},
|
||||
wantErr: true,
|
||||
wantStderr: "You do not own this gist.",
|
||||
wantStderr: "you do not own this gist",
|
||||
}, {
|
||||
name: "successfully delete",
|
||||
gist: &shared.Gist{
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
|
@ -32,6 +33,7 @@ type EditOptions struct {
|
|||
Selector string
|
||||
EditFilename string
|
||||
AddFilename string
|
||||
SourceFile string
|
||||
Description string
|
||||
}
|
||||
|
||||
|
|
@ -50,11 +52,22 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit {<id> | <url>}",
|
||||
Use: "edit {<id> | <url>} [<filename>]",
|
||||
Short: "Edit one of your gists",
|
||||
Args: cmdutil.ExactArgs(1, "cannot edit: gist argument required"),
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return cmdutil.FlagErrorf("cannot edit: gist argument required")
|
||||
}
|
||||
if len(args) > 2 {
|
||||
return cmdutil.FlagErrorf("too many arguments")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
opts.Selector = args[0]
|
||||
if len(args) > 1 {
|
||||
opts.SourceFile = args[1]
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
return runF(&opts)
|
||||
|
|
@ -113,7 +126,7 @@ func editRun(opts *EditOptions) error {
|
|||
}
|
||||
|
||||
if username != gist.Owner.Login {
|
||||
return fmt.Errorf("You do not own this gist.")
|
||||
return errors.New("you do not own this gist")
|
||||
}
|
||||
|
||||
shouldUpdate := false
|
||||
|
|
@ -123,7 +136,36 @@ func editRun(opts *EditOptions) error {
|
|||
}
|
||||
|
||||
if opts.AddFilename != "" {
|
||||
files, err := getFilesToAdd(opts.AddFilename)
|
||||
var input io.Reader
|
||||
switch src := opts.SourceFile; {
|
||||
case src == "-":
|
||||
input = opts.IO.In
|
||||
case src != "":
|
||||
f, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
input = f
|
||||
default:
|
||||
f, err := os.Open(opts.AddFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
input = f
|
||||
}
|
||||
|
||||
content, err := io.ReadAll(input)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read content: %w", err)
|
||||
}
|
||||
|
||||
files, err := getFilesToAdd(opts.AddFilename, content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -169,13 +211,32 @@ func editRun(opts *EditOptions) error {
|
|||
return fmt.Errorf("editing binary files not supported")
|
||||
}
|
||||
|
||||
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
|
||||
if err != nil {
|
||||
return err
|
||||
var text string
|
||||
if src := opts.SourceFile; src != "" {
|
||||
if src == "-" {
|
||||
data, err := io.ReadAll(opts.IO.In)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read from stdin: %w", err)
|
||||
}
|
||||
text = string(data)
|
||||
} else {
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read %s: %w", src, err)
|
||||
}
|
||||
text = string(data)
|
||||
}
|
||||
} else {
|
||||
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := opts.Edit(editorCommand, filename, gistFile.Content, opts.IO)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text = data
|
||||
}
|
||||
|
||||
if text != gistFile.Content {
|
||||
|
|
@ -259,20 +320,11 @@ func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func getFilesToAdd(file string) (map[string]*shared.GistFile, error) {
|
||||
isBinary, err := shared.IsBinaryFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file %s: %w", file, err)
|
||||
}
|
||||
if isBinary {
|
||||
func getFilesToAdd(file string, content []byte) (map[string]*shared.GistFile, error) {
|
||||
if shared.IsBinaryContents(content) {
|
||||
return nil, fmt.Errorf("failed to upload %s: binary file not supported", file)
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file %s: %w", file, err)
|
||||
}
|
||||
|
||||
if len(content) == 0 {
|
||||
return nil, errors.New("file contents cannot be empty")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,14 +20,11 @@ import (
|
|||
)
|
||||
|
||||
func Test_getFilesToAdd(t *testing.T) {
|
||||
fileToAdd := filepath.Join(t.TempDir(), "gist-test.txt")
|
||||
err := ioutil.WriteFile(fileToAdd, []byte("hello"), 0600)
|
||||
filename := "gist-test.txt"
|
||||
|
||||
gf, err := getFilesToAdd(filename, []byte("hello"))
|
||||
require.NoError(t, err)
|
||||
|
||||
gf, err := getFilesToAdd(fileToAdd)
|
||||
require.NoError(t, err)
|
||||
|
||||
filename := filepath.Base(fileToAdd)
|
||||
assert.Equal(t, map[string]*shared.GistFile{
|
||||
filename: {
|
||||
Filename: filename,
|
||||
|
|
@ -65,6 +62,15 @@ func TestNewCmdEdit(t *testing.T) {
|
|||
AddFilename: "cool.md",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add with source",
|
||||
cli: "123 --add cool.md -",
|
||||
wants: EditOptions{
|
||||
Selector: "123",
|
||||
AddFilename: "cool.md",
|
||||
SourceFile: "-",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "description",
|
||||
cli: `123 --desc "my new description"`,
|
||||
|
|
@ -114,6 +120,7 @@ func Test_editRun(t *testing.T) {
|
|||
httpStubs func(*httpmock.Registry)
|
||||
askStubs func(*prompt.AskStubber)
|
||||
nontty bool
|
||||
stdin string
|
||||
wantErr string
|
||||
wantParams map[string]interface{}
|
||||
}{
|
||||
|
|
@ -247,7 +254,7 @@ func Test_editRun(t *testing.T) {
|
|||
},
|
||||
Owner: &shared.GistOwner{Login: "octocat2"},
|
||||
},
|
||||
wantErr: "You do not own this gist.",
|
||||
wantErr: "you do not own this gist",
|
||||
},
|
||||
{
|
||||
name: "add file to existing gist",
|
||||
|
|
@ -272,6 +279,9 @@ func Test_editRun(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "change description",
|
||||
opts: &EditOptions{
|
||||
Description: "my new description",
|
||||
},
|
||||
gist: &shared.Gist{
|
||||
ID: "1234",
|
||||
Description: "my old description",
|
||||
|
|
@ -299,8 +309,139 @@ func Test_editRun(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add file to existing gist from source parameter",
|
||||
gist: &shared.Gist{
|
||||
ID: "1234",
|
||||
Files: map[string]*shared.GistFile{
|
||||
"sample.txt": {
|
||||
Filename: "sample.txt",
|
||||
Content: "bwhiizzzbwhuiiizzzz",
|
||||
Type: "text/plain",
|
||||
},
|
||||
},
|
||||
Owner: &shared.GistOwner{Login: "octocat"},
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "gists/1234"),
|
||||
httpmock.StatusStringResponse(201, "{}"))
|
||||
},
|
||||
opts: &EditOptions{
|
||||
Description: "my new description",
|
||||
AddFilename: "from_source.txt",
|
||||
SourceFile: fileToAdd,
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"description": "",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"public": false,
|
||||
"files": map[string]interface{}{
|
||||
"from_source.txt": map[string]interface{}{
|
||||
"content": "hello",
|
||||
"filename": "from_source.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add file to existing gist from stdin",
|
||||
gist: &shared.Gist{
|
||||
ID: "1234",
|
||||
Files: map[string]*shared.GistFile{
|
||||
"sample.txt": {
|
||||
Filename: "sample.txt",
|
||||
Content: "bwhiizzzbwhuiiizzzz",
|
||||
Type: "text/plain",
|
||||
},
|
||||
},
|
||||
Owner: &shared.GistOwner{Login: "octocat"},
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "gists/1234"),
|
||||
httpmock.StatusStringResponse(201, "{}"))
|
||||
},
|
||||
opts: &EditOptions{
|
||||
AddFilename: "from_source.txt",
|
||||
SourceFile: "-",
|
||||
},
|
||||
stdin: "data from stdin",
|
||||
wantParams: map[string]interface{}{
|
||||
"description": "",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"public": false,
|
||||
"files": map[string]interface{}{
|
||||
"from_source.txt": map[string]interface{}{
|
||||
"content": "data from stdin",
|
||||
"filename": "from_source.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "edit gist using file from source parameter",
|
||||
gist: &shared.Gist{
|
||||
ID: "1234",
|
||||
Files: map[string]*shared.GistFile{
|
||||
"sample.txt": {
|
||||
Filename: "sample.txt",
|
||||
Content: "bwhiizzzbwhuiiizzzz",
|
||||
Type: "text/plain",
|
||||
},
|
||||
},
|
||||
Owner: &shared.GistOwner{Login: "octocat"},
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "gists/1234"),
|
||||
httpmock.StatusStringResponse(201, "{}"))
|
||||
},
|
||||
opts: &EditOptions{
|
||||
SourceFile: fileToAdd,
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"description": "",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"public": false,
|
||||
"files": map[string]interface{}{
|
||||
"sample.txt": map[string]interface{}{
|
||||
"content": "hello",
|
||||
"filename": "sample.txt",
|
||||
"type": "text/plain",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "edit gist using stdin",
|
||||
gist: &shared.Gist{
|
||||
ID: "1234",
|
||||
Files: map[string]*shared.GistFile{
|
||||
"sample.txt": {
|
||||
Filename: "sample.txt",
|
||||
Content: "bwhiizzzbwhuiiizzzz",
|
||||
Type: "text/plain",
|
||||
},
|
||||
},
|
||||
Owner: &shared.GistOwner{Login: "octocat"},
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "gists/1234"),
|
||||
httpmock.StatusStringResponse(201, "{}"))
|
||||
},
|
||||
opts: &EditOptions{
|
||||
SourceFile: "-",
|
||||
},
|
||||
stdin: "data from stdin",
|
||||
wantParams: map[string]interface{}{
|
||||
"description": "",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"public": false,
|
||||
"files": map[string]interface{}{
|
||||
"sample.txt": map[string]interface{}{
|
||||
"content": "data from stdin",
|
||||
"filename": "sample.txt",
|
||||
"type": "text/plain",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -332,7 +473,8 @@ func Test_editRun(t *testing.T) {
|
|||
tt.opts.HttpClient = func() (*http.Client, error) {
|
||||
return &http.Client{Transport: reg}, nil
|
||||
}
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
io, stdin, stdout, stderr := iostreams.Test()
|
||||
stdin.WriteString(tt.stdin)
|
||||
io.SetStdoutTTY(!tt.nontty)
|
||||
io.SetStdinTTY(!tt.nontty)
|
||||
tt.opts.IO = io
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -34,9 +35,10 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
var flagSecret bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List your gists",
|
||||
Args: cobra.NoArgs,
|
||||
Use: "list",
|
||||
Short: "List your gists",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if opts.Limit < 1 {
|
||||
return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit)
|
||||
|
|
@ -84,6 +86,12 @@ func listRun(opts *ListOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
tp := utils.NewTablePrinter(opts.IO)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ func NewCmdGPGKey(f *cmdutil.Factory) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "gpg-key <command>",
|
||||
Short: "Manage GPG keys",
|
||||
Long: "Manage GPG keys registered with your GitHub account",
|
||||
Long: "Manage GPG keys registered with your GitHub account.",
|
||||
}
|
||||
|
||||
cmd.AddCommand(cmdList.NewCmdList(f, nil))
|
||||
|
|
|
|||
|
|
@ -27,9 +27,10 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Lists GPG keys in your GitHub account",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Use: "list",
|
||||
Short: "Lists GPG keys in your GitHub account",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
|
|
|
|||
|
|
@ -23,9 +23,15 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) e
|
|||
|
||||
cmd := &cobra.Command{
|
||||
Use: "comment {<number> | <url>}",
|
||||
Short: "Create a new issue comment",
|
||||
Short: "Add a comment to an issue",
|
||||
Long: heredoc.Doc(`
|
||||
Add a comment to a GitHub issue.
|
||||
|
||||
Without the body text supplied through flags, the command will interactively
|
||||
prompt for the comment text.
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
$ gh issue comment 22 --body "I was able to reproduce this issue, lets fix it."
|
||||
$ gh issue comment 12 --body "Hi from GitHub CLI"
|
||||
`),
|
||||
Args: cobra.ExactArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -54,10 +60,10 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) e
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Supply a body. Will prompt for one otherwise.")
|
||||
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The comment body `text`")
|
||||
cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)")
|
||||
cmd.Flags().BoolP("editor", "e", false, "Add body using editor")
|
||||
cmd.Flags().BoolP("web", "w", false, "Add body in browser")
|
||||
cmd.Flags().BoolP("editor", "e", false, "Skip prompts and open the text editor to write the body in")
|
||||
cmd.Flags().BoolP("web", "w", false, "Open the web browser to write the comment")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ func NewCmdIssue(f *cmdutil.Factory) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "issue <command>",
|
||||
Short: "Manage issues",
|
||||
Long: `Work with GitHub issues`,
|
||||
Long: `Work with GitHub issues.`,
|
||||
Example: heredoc.Doc(`
|
||||
$ gh issue list
|
||||
$ gh issue create --label bug
|
||||
|
|
|
|||
|
|
@ -57,16 +57,22 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List and filter issues in this repository",
|
||||
Short: "List issues in a repository",
|
||||
Long: heredoc.Doc(`
|
||||
List issues in a GitHub repository.
|
||||
|
||||
The search query syntax is documented here:
|
||||
<https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests>
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
$ gh issue list -l "bug" -l "help wanted"
|
||||
$ gh issue list -A monalisa
|
||||
$ gh issue list -a "@me"
|
||||
$ gh issue list --web
|
||||
$ gh issue list --label "bug" --label "help wanted"
|
||||
$ gh issue list --author monalisa
|
||||
$ gh issue list --assignee "@me"
|
||||
$ gh issue list --milestone "The big 1.0"
|
||||
$ gh issue list --search "error no:assignee sort:created-asc"
|
||||
`),
|
||||
Args: cmdutil.NoArgsQuoteReminder,
|
||||
Aliases: []string{"ls"},
|
||||
Args: cmdutil.NoArgsQuoteReminder,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
|
@ -82,17 +88,14 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the issue(s)")
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List issues in the web browser")
|
||||
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
|
||||
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
|
||||
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|all}")
|
||||
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoFileComp
|
||||
})
|
||||
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by label")
|
||||
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "all"}, "Filter by state")
|
||||
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of issues to fetch")
|
||||
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
|
||||
cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention")
|
||||
cmd.Flags().StringVarP(&opts.Milestone, "milestone", "m", "", "Filter by milestone `number` or `title`")
|
||||
cmd.Flags().StringVarP(&opts.Milestone, "milestone", "m", "", "Filter by milestone number or title")
|
||||
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search issues with `query`")
|
||||
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
|
||||
|
||||
|
|
@ -160,11 +163,11 @@ func listRun(opts *ListOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = opts.IO.StartPager()
|
||||
if err != nil {
|
||||
return err
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
defer opts.IO.StopPager()
|
||||
|
||||
if opts.Exporter != nil {
|
||||
return opts.Exporter.Write(opts.IO, listResult.Issues)
|
||||
|
|
|
|||
110
pkg/cmd/pr/checks/aggregate.go
Normal file
110
pkg/cmd/pr/checks/aggregate.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package checks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
)
|
||||
|
||||
type check struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
StartedAt time.Time `json:"startedAt"`
|
||||
CompletedAt time.Time `json:"completedAt"`
|
||||
Link string `json:"link"`
|
||||
Bucket string `json:"bucket"`
|
||||
}
|
||||
|
||||
type checkCounts struct {
|
||||
Failed int
|
||||
Passed int
|
||||
Pending int
|
||||
Skipping int
|
||||
}
|
||||
|
||||
func aggregateChecks(pr *api.PullRequest) ([]check, checkCounts, error) {
|
||||
checks := []check{}
|
||||
counts := checkCounts{}
|
||||
|
||||
if len(pr.StatusCheckRollup.Nodes) == 0 {
|
||||
return checks, counts, fmt.Errorf("no commit found on the pull request")
|
||||
}
|
||||
|
||||
rollup := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
|
||||
if len(rollup) == 0 {
|
||||
return checks, counts, fmt.Errorf("no checks reported on the '%s' branch", pr.HeadRefName)
|
||||
}
|
||||
|
||||
checkContexts := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
|
||||
for _, c := range eliminateDuplicates(checkContexts) {
|
||||
state := c.State
|
||||
if state == "" {
|
||||
if c.Status == "COMPLETED" {
|
||||
state = c.Conclusion
|
||||
} else {
|
||||
state = c.Status
|
||||
}
|
||||
}
|
||||
|
||||
link := c.DetailsURL
|
||||
if link == "" {
|
||||
link = c.TargetURL
|
||||
}
|
||||
|
||||
name := c.Name
|
||||
if name == "" {
|
||||
name = c.Context
|
||||
}
|
||||
|
||||
item := check{
|
||||
Name: name,
|
||||
State: state,
|
||||
StartedAt: c.StartedAt,
|
||||
CompletedAt: c.CompletedAt,
|
||||
Link: link,
|
||||
}
|
||||
switch state {
|
||||
case "SUCCESS":
|
||||
item.Bucket = "pass"
|
||||
counts.Passed++
|
||||
case "SKIPPED", "NEUTRAL":
|
||||
item.Bucket = "skipping"
|
||||
counts.Skipping++
|
||||
case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED":
|
||||
item.Bucket = "fail"
|
||||
counts.Failed++
|
||||
default: // "EXPECTED", "REQUESTED", "WAITING", "QUEUED", "PENDING", "IN_PROGRESS", "STALE"
|
||||
item.Bucket = "pending"
|
||||
counts.Pending++
|
||||
}
|
||||
|
||||
checks = append(checks, item)
|
||||
}
|
||||
|
||||
return checks, counts, nil
|
||||
}
|
||||
|
||||
func eliminateDuplicates(checkContexts []api.CheckContext) []api.CheckContext {
|
||||
// To return the most recent check, sort in descending order by StartedAt.
|
||||
sort.Slice(checkContexts, func(i, j int) bool { return checkContexts[i].StartedAt.After(checkContexts[j].StartedAt) })
|
||||
|
||||
m := make(map[string]struct{})
|
||||
unique := make([]api.CheckContext, 0, len(checkContexts))
|
||||
|
||||
// Eliminate duplicates using Name or Context.
|
||||
for _, ctx := range checkContexts {
|
||||
key := ctx.Name
|
||||
if key == "" {
|
||||
key = ctx.Context
|
||||
}
|
||||
if _, ok := m[key]; ok {
|
||||
continue
|
||||
}
|
||||
unique = append(unique, ctx)
|
||||
m[key] = struct{}{}
|
||||
}
|
||||
|
||||
return unique
|
||||
}
|
||||
|
|
@ -2,7 +2,8 @@ package checks
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"io"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
|
|
@ -14,6 +15,8 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const defaultInterval time.Duration = 10 * time.Second
|
||||
|
||||
type browser interface {
|
||||
Browse(string) error
|
||||
}
|
||||
|
|
@ -26,12 +29,16 @@ type ChecksOptions struct {
|
|||
|
||||
SelectorArg string
|
||||
WebMode bool
|
||||
Interval time.Duration
|
||||
Watch bool
|
||||
}
|
||||
|
||||
func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Command {
|
||||
var interval int
|
||||
opts := &ChecksOptions{
|
||||
IO: f.IOStreams,
|
||||
Browser: f.Browser,
|
||||
IO: f.IOStreams,
|
||||
Browser: f.Browser,
|
||||
Interval: defaultInterval,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
|
|
@ -41,14 +48,27 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co
|
|||
Show CI status for a single pull request.
|
||||
|
||||
Without an argument, the pull request that belongs to the current branch
|
||||
is selected.
|
||||
is selected.
|
||||
`),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.Finder = shared.NewFinder(f)
|
||||
|
||||
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
|
||||
return cmdutil.FlagErrorf("argument required when using the --repo flag")
|
||||
return cmdutil.FlagErrorf("argument required when using the `--repo` flag")
|
||||
}
|
||||
|
||||
intervalChanged := cmd.Flags().Changed("interval")
|
||||
if !opts.Watch && intervalChanged {
|
||||
return cmdutil.FlagErrorf("cannot use `--interval` flag without `--watch` flag")
|
||||
}
|
||||
|
||||
if intervalChanged {
|
||||
var err error
|
||||
opts.Interval, err = time.ParseDuration(fmt.Sprintf("%ds", interval))
|
||||
if err != nil {
|
||||
return cmdutil.FlagErrorf("could not parse `--interval` flag: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
|
|
@ -64,17 +84,16 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co
|
|||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser to show details about checks")
|
||||
cmd.Flags().BoolVarP(&opts.Watch, "watch", "", false, "Watch checks until they finish")
|
||||
cmd.Flags().IntVarP(&interval, "interval", "i", 10, "Refresh interval in seconds when using `--watch` flag")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func checksRun(opts *ChecksOptions) error {
|
||||
func checksRunWebMode(opts *ChecksOptions) error {
|
||||
findOptions := shared.FindOptions{
|
||||
Selector: opts.SelectorArg,
|
||||
Fields: []string{"number", "baseRefName", "statusCheckRollup"},
|
||||
}
|
||||
if opts.WebMode {
|
||||
findOptions.Fields = []string{"number"}
|
||||
Fields: []string{"number"},
|
||||
}
|
||||
pr, baseRepo, err := opts.Finder.Find(findOptions)
|
||||
if err != nil {
|
||||
|
|
@ -82,166 +101,85 @@ func checksRun(opts *ChecksOptions) error {
|
|||
}
|
||||
|
||||
isTerminal := opts.IO.IsStdoutTTY()
|
||||
|
||||
if opts.WebMode {
|
||||
openURL := ghrepo.GenerateRepoURL(baseRepo, "pull/%d/checks", pr.Number)
|
||||
if isTerminal {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
|
||||
}
|
||||
return opts.Browser.Browse(openURL)
|
||||
}
|
||||
|
||||
if len(pr.StatusCheckRollup.Nodes) == 0 {
|
||||
return fmt.Errorf("no commit found on the pull request")
|
||||
}
|
||||
|
||||
rollup := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
|
||||
if len(rollup) == 0 {
|
||||
return fmt.Errorf("no checks reported on the '%s' branch", pr.BaseRefName)
|
||||
}
|
||||
|
||||
passing := 0
|
||||
failing := 0
|
||||
skipping := 0
|
||||
pending := 0
|
||||
|
||||
type output struct {
|
||||
mark string
|
||||
bucket string
|
||||
name string
|
||||
elapsed string
|
||||
link string
|
||||
markColor func(string) string
|
||||
}
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
outputs := []output{}
|
||||
|
||||
for _, c := range pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes {
|
||||
mark := "✓"
|
||||
bucket := "pass"
|
||||
state := c.State
|
||||
markColor := cs.Green
|
||||
if state == "" {
|
||||
if c.Status == "COMPLETED" {
|
||||
state = c.Conclusion
|
||||
} else {
|
||||
state = c.Status
|
||||
}
|
||||
}
|
||||
switch state {
|
||||
case "SUCCESS":
|
||||
passing++
|
||||
case "SKIPPED", "NEUTRAL":
|
||||
mark = "-"
|
||||
markColor = cs.Gray
|
||||
skipping++
|
||||
bucket = "skipping"
|
||||
case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED":
|
||||
mark = "X"
|
||||
markColor = cs.Red
|
||||
failing++
|
||||
bucket = "fail"
|
||||
default: // "EXPECTED", "REQUESTED", "WAITING", "QUEUED", "PENDING", "IN_PROGRESS", "STALE"
|
||||
mark = "*"
|
||||
markColor = cs.Yellow
|
||||
pending++
|
||||
bucket = "pending"
|
||||
}
|
||||
|
||||
elapsed := ""
|
||||
zeroTime := time.Time{}
|
||||
|
||||
if c.StartedAt != zeroTime && c.CompletedAt != zeroTime {
|
||||
e := c.CompletedAt.Sub(c.StartedAt)
|
||||
if e > 0 {
|
||||
elapsed = e.String()
|
||||
}
|
||||
}
|
||||
|
||||
link := c.DetailsURL
|
||||
if link == "" {
|
||||
link = c.TargetURL
|
||||
}
|
||||
|
||||
name := c.Name
|
||||
if name == "" {
|
||||
name = c.Context
|
||||
}
|
||||
|
||||
outputs = append(outputs, output{mark, bucket, name, elapsed, link, markColor})
|
||||
}
|
||||
|
||||
sort.Slice(outputs, func(i, j int) bool {
|
||||
b0 := outputs[i].bucket
|
||||
n0 := outputs[i].name
|
||||
l0 := outputs[i].link
|
||||
b1 := outputs[j].bucket
|
||||
n1 := outputs[j].name
|
||||
l1 := outputs[j].link
|
||||
|
||||
if b0 == b1 {
|
||||
if n0 == n1 {
|
||||
return l0 < l1
|
||||
}
|
||||
return n0 < n1
|
||||
}
|
||||
|
||||
return (b0 == "fail") || (b0 == "pending" && b1 == "success")
|
||||
})
|
||||
|
||||
tp := utils.NewTablePrinter(opts.IO)
|
||||
|
||||
for _, o := range outputs {
|
||||
if isTerminal {
|
||||
tp.AddField(o.mark, nil, o.markColor)
|
||||
tp.AddField(o.name, nil, nil)
|
||||
tp.AddField(o.elapsed, nil, nil)
|
||||
tp.AddField(o.link, nil, nil)
|
||||
} else {
|
||||
tp.AddField(o.name, nil, nil)
|
||||
tp.AddField(o.bucket, nil, nil)
|
||||
if o.elapsed == "" {
|
||||
tp.AddField("0", nil, nil)
|
||||
} else {
|
||||
tp.AddField(o.elapsed, nil, nil)
|
||||
}
|
||||
tp.AddField(o.link, nil, nil)
|
||||
}
|
||||
|
||||
tp.EndRow()
|
||||
}
|
||||
|
||||
summary := ""
|
||||
if failing+passing+skipping+pending > 0 {
|
||||
if failing > 0 {
|
||||
summary = "Some checks were not successful"
|
||||
} else if pending > 0 {
|
||||
summary = "Some checks are still pending"
|
||||
} else {
|
||||
summary = "All checks were successful"
|
||||
}
|
||||
|
||||
tallies := fmt.Sprintf("%d failing, %d successful, %d skipped, and %d pending checks",
|
||||
failing, passing, skipping, pending)
|
||||
|
||||
summary = fmt.Sprintf("%s\n%s", cs.Bold(summary), tallies)
|
||||
}
|
||||
openURL := ghrepo.GenerateRepoURL(baseRepo, "pull/%d/checks", pr.Number)
|
||||
|
||||
if isTerminal {
|
||||
fmt.Fprintln(opts.IO.Out, summary)
|
||||
fmt.Fprintln(opts.IO.Out)
|
||||
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
|
||||
}
|
||||
|
||||
err = tp.Render()
|
||||
if err != nil {
|
||||
return err
|
||||
return opts.Browser.Browse(openURL)
|
||||
}
|
||||
|
||||
func checksRun(opts *ChecksOptions) error {
|
||||
if opts.WebMode {
|
||||
return checksRunWebMode(opts)
|
||||
}
|
||||
|
||||
if failing+pending > 0 {
|
||||
if opts.Watch {
|
||||
if err := opts.IO.EnableVirtualTerminalProcessing(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Only start pager in non-watch mode
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
var checks []check
|
||||
var counts checkCounts
|
||||
|
||||
for {
|
||||
findOptions := shared.FindOptions{
|
||||
Selector: opts.SelectorArg,
|
||||
Fields: []string{"number", "headRefName", "statusCheckRollup"},
|
||||
}
|
||||
pr, _, err := opts.Finder.Find(findOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
checks, counts, err = aggregateChecks(pr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if counts.Pending != 0 && opts.Watch {
|
||||
refreshScreen(opts.IO.Out)
|
||||
cs := opts.IO.ColorScheme()
|
||||
fmt.Fprintln(opts.IO.Out, cs.Boldf("Refreshing checks status every %v seconds. Press Ctrl+C to quit.\n", opts.Interval.Seconds()))
|
||||
}
|
||||
|
||||
printSummary(opts.IO, counts)
|
||||
err = printTable(opts.IO, checks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if counts.Pending == 0 || !opts.Watch {
|
||||
break
|
||||
}
|
||||
|
||||
time.Sleep(opts.Interval)
|
||||
}
|
||||
|
||||
if counts.Failed+counts.Pending > 0 {
|
||||
return cmdutil.SilentError
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func refreshScreen(w io.Writer) {
|
||||
if runtime.GOOS == "windows" {
|
||||
// Just clear whole screen; I wasn't able to get the nicer cursor movement thing working
|
||||
fmt.Fprintf(w, "\x1b[2J")
|
||||
} else {
|
||||
// Move cursor to 0,0
|
||||
fmt.Fprint(w, "\x1b[0;0H")
|
||||
// Clear from cursor to bottom of screen
|
||||
fmt.Fprint(w, "\x1b[J")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ import (
|
|||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
|
|
@ -20,22 +22,47 @@ import (
|
|||
|
||||
func TestNewCmdChecks(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cli string
|
||||
wants ChecksOptions
|
||||
name string
|
||||
cli string
|
||||
wants ChecksOptions
|
||||
wantsError string
|
||||
}{
|
||||
{
|
||||
name: "no arguments",
|
||||
cli: "",
|
||||
wants: ChecksOptions{},
|
||||
name: "no arguments",
|
||||
cli: "",
|
||||
wants: ChecksOptions{
|
||||
Interval: time.Duration(10000000000),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "pr argument",
|
||||
cli: "1234",
|
||||
wants: ChecksOptions{
|
||||
SelectorArg: "1234",
|
||||
Interval: time.Duration(10000000000),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "watch flag",
|
||||
cli: "--watch",
|
||||
wants: ChecksOptions{
|
||||
Watch: true,
|
||||
Interval: time.Duration(10000000000),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "watch flag and interval flag",
|
||||
cli: "--watch --interval 5",
|
||||
wants: ChecksOptions{
|
||||
Watch: true,
|
||||
Interval: time.Duration(5000000000),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "interval flag without watch flag",
|
||||
cli: "--interval 5",
|
||||
wantsError: "cannot use `--interval` flag without `--watch` flag",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -59,9 +86,14 @@ func TestNewCmdChecks(t *testing.T) {
|
|||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
_, err = cmd.ExecuteC()
|
||||
if tt.wantsError != "" {
|
||||
assert.EqualError(t, err, tt.wantsError)
|
||||
return
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tt.wants.SelectorArg, gotOpts.SelectorArg)
|
||||
assert.Equal(t, tt.wants.Watch, gotOpts.Watch)
|
||||
assert.Equal(t, tt.wants.Interval, gotOpts.Interval)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -83,7 +115,7 @@ func Test_checksRun(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "no checks",
|
||||
prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }`,
|
||||
prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "headRefName": "master" }`,
|
||||
wantOut: "",
|
||||
wantErr: "no checks reported on the 'master' branch",
|
||||
},
|
||||
|
|
@ -114,7 +146,7 @@ func Test_checksRun(t *testing.T) {
|
|||
{
|
||||
name: "no checks",
|
||||
nontty: true,
|
||||
prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }`,
|
||||
prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "headRefName": "master" }`,
|
||||
wantOut: "",
|
||||
wantErr: "no checks reported on the 'master' branch",
|
||||
},
|
||||
|
|
@ -225,7 +257,7 @@ func TestChecksRun_web(t *testing.T) {
|
|||
_, teardown := run.Stub()
|
||||
defer teardown(t)
|
||||
|
||||
err := checksRun(&ChecksOptions{
|
||||
err := checksRunWebMode(&ChecksOptions{
|
||||
IO: io,
|
||||
Browser: browser,
|
||||
WebMode: true,
|
||||
|
|
@ -239,3 +271,213 @@ func TestChecksRun_web(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEliminateDupulicates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
checkContexts []api.CheckContext
|
||||
want []api.CheckContext
|
||||
}{
|
||||
{
|
||||
name: "duplicate CheckRun (lint)",
|
||||
checkContexts: []api.CheckContext{
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "build (ubuntu-latest)",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/1",
|
||||
},
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "lint",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "FAILURE",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/2",
|
||||
},
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "lint",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/3",
|
||||
},
|
||||
},
|
||||
want: []api.CheckContext{
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "lint",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/3",
|
||||
},
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "build (ubuntu-latest)",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "duplicate StatusContext (Windows GPU)",
|
||||
checkContexts: []api.CheckContext{
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Windows GPU",
|
||||
State: "FAILURE",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/2",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Windows GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/3",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Linux GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/1",
|
||||
},
|
||||
},
|
||||
want: []api.CheckContext{
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Windows GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
CompletedAt: time.Date(2022, 2, 2, 2, 2, 2, 2, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/3",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Linux GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unique CheckContext",
|
||||
checkContexts: []api.CheckContext{
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "build (ubuntu-latest)",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/1",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Windows GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/2",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Linux GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/3",
|
||||
},
|
||||
},
|
||||
want: []api.CheckContext{
|
||||
{
|
||||
TypeName: "CheckRun",
|
||||
Name: "build (ubuntu-latest)",
|
||||
Status: "COMPLETED",
|
||||
Conclusion: "SUCCESS",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "https://github.com/cli/cli/runs/1",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Windows GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/2",
|
||||
},
|
||||
{
|
||||
TypeName: "StatusContext",
|
||||
Name: "",
|
||||
Context: "Linux GPU",
|
||||
State: "SUCCESS",
|
||||
Status: "",
|
||||
Conclusion: "",
|
||||
StartedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
CompletedAt: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
|
||||
DetailsURL: "",
|
||||
TargetURL: "https://github.com/cli/cli/3",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := eliminateDuplicates(tt.checkContexts)
|
||||
if !reflect.DeepEqual(tt.want, got) {
|
||||
t.Errorf("got eliminateDuplicates %+v, want %+v\n", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
111
pkg/cmd/pr/checks/output.go
Normal file
111
pkg/cmd/pr/checks/output.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
package checks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/cli/cli/v2/utils"
|
||||
)
|
||||
|
||||
func addRow(tp utils.TablePrinter, io *iostreams.IOStreams, o check) {
|
||||
cs := io.ColorScheme()
|
||||
elapsed := ""
|
||||
zeroTime := time.Time{}
|
||||
|
||||
if o.StartedAt != zeroTime && o.CompletedAt != zeroTime {
|
||||
e := o.CompletedAt.Sub(o.StartedAt)
|
||||
if e > 0 {
|
||||
elapsed = e.String()
|
||||
}
|
||||
}
|
||||
|
||||
mark := "✓"
|
||||
markColor := cs.Green
|
||||
switch o.Bucket {
|
||||
case "fail":
|
||||
mark = "X"
|
||||
markColor = cs.Red
|
||||
case "pending":
|
||||
mark = "*"
|
||||
markColor = cs.Yellow
|
||||
case "skipping":
|
||||
mark = "-"
|
||||
markColor = cs.Gray
|
||||
}
|
||||
|
||||
if io.IsStdoutTTY() {
|
||||
tp.AddField(mark, nil, markColor)
|
||||
tp.AddField(o.Name, nil, nil)
|
||||
tp.AddField(elapsed, nil, nil)
|
||||
tp.AddField(o.Link, nil, nil)
|
||||
} else {
|
||||
tp.AddField(o.Name, nil, nil)
|
||||
tp.AddField(o.Bucket, nil, nil)
|
||||
if elapsed == "" {
|
||||
tp.AddField("0", nil, nil)
|
||||
} else {
|
||||
tp.AddField(elapsed, nil, nil)
|
||||
}
|
||||
tp.AddField(o.Link, nil, nil)
|
||||
}
|
||||
|
||||
tp.EndRow()
|
||||
}
|
||||
|
||||
func printSummary(io *iostreams.IOStreams, counts checkCounts) {
|
||||
summary := ""
|
||||
if counts.Failed+counts.Passed+counts.Skipping+counts.Pending > 0 {
|
||||
if counts.Failed > 0 {
|
||||
summary = "Some checks were not successful"
|
||||
} else if counts.Pending > 0 {
|
||||
summary = "Some checks are still pending"
|
||||
} else {
|
||||
summary = "All checks were successful"
|
||||
}
|
||||
|
||||
tallies := fmt.Sprintf("%d failing, %d successful, %d skipped, and %d pending checks",
|
||||
counts.Failed, counts.Passed, counts.Skipping, counts.Pending)
|
||||
|
||||
summary = fmt.Sprintf("%s\n%s", io.ColorScheme().Bold(summary), tallies)
|
||||
}
|
||||
|
||||
if io.IsStdoutTTY() {
|
||||
fmt.Fprintln(io.Out, summary)
|
||||
fmt.Fprintln(io.Out)
|
||||
}
|
||||
}
|
||||
|
||||
func printTable(io *iostreams.IOStreams, checks []check) error {
|
||||
tp := utils.NewTablePrinter(io)
|
||||
|
||||
sort.Slice(checks, func(i, j int) bool {
|
||||
b0 := checks[i].Bucket
|
||||
n0 := checks[i].Name
|
||||
l0 := checks[i].Link
|
||||
b1 := checks[j].Bucket
|
||||
n1 := checks[j].Name
|
||||
l1 := checks[j].Link
|
||||
|
||||
if b0 == b1 {
|
||||
if n0 == n1 {
|
||||
return l0 < l1
|
||||
}
|
||||
return n0 < n1
|
||||
}
|
||||
|
||||
return (b0 == "fail") || (b0 == "pending" && b1 == "success")
|
||||
})
|
||||
|
||||
for _, o := range checks {
|
||||
addRow(tp, io, o)
|
||||
}
|
||||
|
||||
err := tp.Render()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -90,34 +90,36 @@ func closeRun(opts *CloseOptions) error {
|
|||
if opts.DeleteBranch {
|
||||
branchSwitchString := ""
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
|
||||
|
||||
if opts.DeleteLocalBranch {
|
||||
currentBranch, err := opts.Branch()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var branchToSwitchTo string
|
||||
if currentBranch == pr.HeadRefName {
|
||||
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = git.CheckoutBranch(branchToSwitchTo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
localBranchExists := git.HasLocalBranch(pr.HeadRefName)
|
||||
if localBranchExists {
|
||||
currentBranch, err := opts.Branch()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var branchToSwitchTo string
|
||||
if currentBranch == pr.HeadRefName {
|
||||
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = git.CheckoutBranch(branchToSwitchTo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := git.DeleteLocalBranch(pr.HeadRefName); err != nil {
|
||||
return fmt.Errorf("failed to delete local branch %s: %w", cs.Cyan(pr.HeadRefName), err)
|
||||
}
|
||||
}
|
||||
|
||||
if branchToSwitchTo != "" {
|
||||
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
|
||||
if branchToSwitchTo != "" {
|
||||
branchSwitchString = fmt.Sprintf(" and switched to branch %s", cs.Cyan(branchToSwitchTo))
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "%s Skipped deleting the local branch since current directory is not a git repository \n", cs.WarningIcon())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -230,3 +230,37 @@ func TestPrClose_deleteBranch_sameBranch(t *testing.T) {
|
|||
✓ Deleted branch trunk and switched to branch main
|
||||
`), output.Stderr())
|
||||
}
|
||||
|
||||
func TestPrClose_deleteBranch_notInGitRepo(t *testing.T) {
|
||||
http := &httpmock.Registry{}
|
||||
defer http.Verify(t)
|
||||
|
||||
baseRepo, pr := stubPR("OWNER/REPO:main", "OWNER/REPO:trunk")
|
||||
pr.Title = "The title of the PR"
|
||||
shared.RunCommandFinder("96", pr, baseRepo)
|
||||
|
||||
http.Register(
|
||||
httpmock.GraphQL(`mutation PullRequestClose\b`),
|
||||
httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
|
||||
func(inputs map[string]interface{}) {
|
||||
assert.Equal(t, inputs["pullRequestId"], "THE-ID")
|
||||
}),
|
||||
)
|
||||
http.Register(
|
||||
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/trunk"),
|
||||
httpmock.StringResponse(`{}`))
|
||||
|
||||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/trunk`, 128, "could not determine current branch: fatal: not a git repository (or any of the parent directories): .git")
|
||||
|
||||
output, err := runCommand(http, true, `96 --delete-branch`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "", output.String())
|
||||
assert.Equal(t, heredoc.Doc(`
|
||||
✓ Closed pull request #96 (The title of the PR)
|
||||
! Skipped deleting the local branch since current directory is not a git repository
|
||||
✓ Deleted branch trunk
|
||||
`), output.Stderr())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,17 +22,15 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) err
|
|||
|
||||
cmd := &cobra.Command{
|
||||
Use: "comment [<number> | <url> | <branch>]",
|
||||
Short: "Create a new pr comment",
|
||||
Short: "Add a comment to a pull request",
|
||||
Long: heredoc.Doc(`
|
||||
Create a new pr comment.
|
||||
Add a comment to a GitHub pull request.
|
||||
|
||||
Without an argument, the pull request that belongs to the current branch
|
||||
is selected.
|
||||
|
||||
With '--web', comment on the pull request in a web browser instead.
|
||||
Without the body text supplied through flags, the command will interactively
|
||||
prompt for the comment text.
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
$ gh pr comment 22 --body "This looks great, lets get it deployed."
|
||||
$ gh pr comment 13 --body "Hi from GitHub CLI"
|
||||
`),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -68,10 +66,10 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) err
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Supply a body. Will prompt for one otherwise.")
|
||||
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The comment body `text`")
|
||||
cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)")
|
||||
cmd.Flags().BoolP("editor", "e", false, "Add body using editor")
|
||||
cmd.Flags().BoolP("web", "w", false, "Add body in browser")
|
||||
cmd.Flags().BoolP("editor", "e", false, "Skip prompts and open the text editor to write the body in")
|
||||
cmd.Flags().BoolP("web", "w", false, "Open the web browser to write the comment")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -598,6 +598,16 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) {
|
|||
|
||||
http.StubRepoInfoResponse("OWNER", "REPO", "master")
|
||||
shared.RunCommandFinder("feature", nil, nil)
|
||||
http.Register(
|
||||
httpmock.GraphQL(`query PullRequestTemplates\b`),
|
||||
httpmock.StringResponse(`
|
||||
{ "data": { "repository": { "pullRequestTemplates": [
|
||||
{ "filename": "template1",
|
||||
"body": "this is a bug" },
|
||||
{ "filename": "template2",
|
||||
"body": "this is a enhancement" }
|
||||
] } } }`),
|
||||
)
|
||||
http.Register(
|
||||
httpmock.GraphQL(`mutation PullRequestCreate\b`),
|
||||
httpmock.GraphQLMutation(`
|
||||
|
|
@ -606,7 +616,7 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) {
|
|||
} } } }
|
||||
`, func(input map[string]interface{}) {
|
||||
assert.Equal(t, "my title", input["title"].(string))
|
||||
assert.Equal(t, "- commit 1\n- commit 0\n\nFixes a bug and Closes an issue", input["body"].(string))
|
||||
assert.Equal(t, "- commit 1\n- commit 0\n\nthis is a bug", input["body"].(string))
|
||||
}))
|
||||
|
||||
cs, cmdTeardown := run.Stub()
|
||||
|
|
@ -615,13 +625,11 @@ func TestPRCreate_nonLegacyTemplate(t *testing.T) {
|
|||
cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "1234567890,commit 0\n2345678901,commit 1")
|
||||
cs.Register(`git status --porcelain`, 0, "")
|
||||
|
||||
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
|
||||
as, teardown := prompt.InitAskStubber()
|
||||
defer teardown()
|
||||
as := prompt.NewAskStubber(t)
|
||||
|
||||
as.StubPrompt("Choose a template").
|
||||
AssertOptions([]string{"Bug fix", "Open a blank pull request"}).
|
||||
AnswerWith("Bug fix")
|
||||
AssertOptions([]string{"template1", "template2", "Open a blank pull request"}).
|
||||
AnswerWith("template1")
|
||||
as.StubPrompt("Body").AnswerDefault()
|
||||
as.StubPrompt("What's next?").
|
||||
AssertOptions([]string{"Submit", "Continue in browser", "Add metadata", "Cancel"}).
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/api"
|
||||
|
|
@ -67,7 +66,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
|
|||
case "never":
|
||||
opts.UseColor = false
|
||||
default:
|
||||
return cmdutil.FlagErrorf("the value for `--color` must be one of \"auto\", \"always\", or \"never\"")
|
||||
return fmt.Errorf("unsupported color %q", colorFlag)
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
|
|
@ -77,7 +76,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&colorFlag, "color", "auto", "Use color in diff output: {always|never|auto}")
|
||||
cmdutil.StringEnumFlag(cmd, &colorFlag, "color", "", "auto", []string{"always", "never", "auto"}, "Use color in diff output")
|
||||
cmd.Flags().BoolVar(&opts.Patch, "patch", false, "Display diff in patch format")
|
||||
|
||||
return cmd
|
||||
|
|
@ -104,17 +103,14 @@ func diffRun(opts *DiffOptions) error {
|
|||
}
|
||||
defer diff.Close()
|
||||
|
||||
err = opts.IO.StartPager()
|
||||
if err != nil {
|
||||
return err
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
defer opts.IO.StopPager()
|
||||
|
||||
if !opts.UseColor {
|
||||
_, err = io.Copy(opts.IO.Out, diff)
|
||||
if errors.Is(err, syscall.EPIPE) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ func Test_NewCmdDiff(t *testing.T) {
|
|||
name: "invalid --color argument",
|
||||
args: "--color doublerainbow",
|
||||
isTTY: true,
|
||||
wantErr: "the value for `--color` must be one of \"auto\", \"always\", or \"never\"",
|
||||
wantErr: "invalid argument \"doublerainbow\" for \"--color\" flag: valid values are {always|never|auto}",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ type ListOptions struct {
|
|||
HeadBranch string
|
||||
Labels []string
|
||||
Author string
|
||||
AppAuthor string
|
||||
Assignee string
|
||||
Search string
|
||||
Draft string
|
||||
|
|
@ -52,24 +53,28 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List and filter pull requests in this repository",
|
||||
Short: "List pull requests in a repository",
|
||||
Long: heredoc.Doc(`
|
||||
List pull requests in a GitHub repository.
|
||||
|
||||
The search query syntax is documented here:
|
||||
<https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests>
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
List PRs authored by you
|
||||
$ gh pr list --author "@me"
|
||||
|
||||
List PRs assigned to you
|
||||
$ gh pr list --assignee "@me"
|
||||
|
||||
List PRs by label, combining multiple labels with AND
|
||||
List only PRs with all of the given labels
|
||||
$ gh pr list --label bug --label "priority 1"
|
||||
|
||||
List PRs using search syntax
|
||||
Filter PRs using search syntax
|
||||
$ gh pr list --search "status:success review:required"
|
||||
|
||||
Open the list of PRs in a web browser
|
||||
$ gh pr list --web
|
||||
Find a PR that introduced a given commit
|
||||
$ gh pr list --search "<SHA>" --state merged
|
||||
`),
|
||||
Args: cmdutil.NoArgsQuoteReminder,
|
||||
Aliases: []string{"ls"},
|
||||
Args: cmdutil.NoArgsQuoteReminder,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
|
@ -82,6 +87,14 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
opts.Draft = strconv.FormatBool(draft)
|
||||
}
|
||||
|
||||
if err := cmdutil.MutuallyExclusive(
|
||||
"specify only `--author` or `--app`",
|
||||
opts.Author != "",
|
||||
opts.AppAuthor != "",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
|
|
@ -89,16 +102,14 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the pull requests")
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List pull requests in the web browser")
|
||||
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of items to fetch")
|
||||
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|merged|all}")
|
||||
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoFileComp
|
||||
})
|
||||
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "merged", "all"}, "Filter by state")
|
||||
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "B", "", "Filter by base branch")
|
||||
cmd.Flags().StringVarP(&opts.HeadBranch, "head", "H", "", "Filter by head branch")
|
||||
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
|
||||
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by label")
|
||||
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
|
||||
cmd.Flags().StringVar(&opts.AppAuthor, "app", "", "Filter by GitHub App author")
|
||||
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
|
||||
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search pull requests with `query`")
|
||||
cmd.Flags().BoolVarP(&draft, "draft", "d", false, "Filter by draft state")
|
||||
|
|
@ -163,6 +174,10 @@ func listRun(opts *ListOptions) error {
|
|||
return opts.Browser.Browse(openURL)
|
||||
}
|
||||
|
||||
if opts.AppAuthor != "" {
|
||||
filters.Author = fmt.Sprintf("app/%s", opts.AppAuthor)
|
||||
}
|
||||
|
||||
listResult, err := listPullRequests(httpClient, baseRepo, filters, opts.LimitResults)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -229,6 +229,53 @@ func TestPRList_filteringDraft(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPRList_filteringAuthor(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cli string
|
||||
expectedQuery string
|
||||
}{
|
||||
{
|
||||
name: "author @me",
|
||||
cli: `--author "@me"`,
|
||||
expectedQuery: `repo:OWNER/REPO is:pr is:open author:@me`,
|
||||
},
|
||||
{
|
||||
name: "author user",
|
||||
cli: `--author "monalisa"`,
|
||||
expectedQuery: `repo:OWNER/REPO is:pr is:open author:monalisa`,
|
||||
},
|
||||
{
|
||||
name: "app author",
|
||||
cli: `--author "app/dependabot"`,
|
||||
expectedQuery: `repo:OWNER/REPO is:pr is:open author:app/dependabot`,
|
||||
},
|
||||
{
|
||||
name: "app author with app option",
|
||||
cli: `--app "dependabot"`,
|
||||
expectedQuery: `repo:OWNER/REPO is:pr is:open author:app/dependabot`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
http := initFakeHTTP()
|
||||
defer http.Verify(t)
|
||||
|
||||
http.Register(
|
||||
httpmock.GraphQL(`query PullRequestSearch\b`),
|
||||
httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) {
|
||||
assert.Equal(t, test.expectedQuery, params["q"].(string))
|
||||
}))
|
||||
|
||||
_, err := runCommand(http, true, test.cli)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPRList_withInvalidLimitFlag(t *testing.T) {
|
||||
http := initFakeHTTP()
|
||||
defer http.Verify(t)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/cli/cli/v2/context"
|
||||
"github.com/cli/cli/v2/git"
|
||||
"github.com/cli/cli/v2/internal/config"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
|
|
@ -322,19 +321,36 @@ func mergeRun(opts *MergeOptions) error {
|
|||
|
||||
var branchToSwitchTo string
|
||||
if currentBranch == pr.HeadRefName {
|
||||
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
branchToSwitchTo = pr.BaseRefName
|
||||
if branchToSwitchTo == "" {
|
||||
branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
remotes, err := opts.Remotes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = git.CheckoutBranch(branchToSwitchTo)
|
||||
baseRemote, err := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := pullLatestChanges(opts, baseRepo, branchToSwitchTo)
|
||||
if err != nil {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "%s warning: not posible to fast-forward to: %q\n", cs.WarningIcon(), branchToSwitchTo)
|
||||
if git.HasLocalBranch(branchToSwitchTo) {
|
||||
if err := git.CheckoutBranch(branchToSwitchTo); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := git.CheckoutNewBranch(baseRemote.Name, branchToSwitchTo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := git.Pull(baseRemote.Name, branchToSwitchTo); err != nil {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", cs.WarningIcon(), branchToSwitchTo)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -364,25 +380,6 @@ func mergeRun(opts *MergeOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func pullLatestChanges(opts *MergeOptions, repo ghrepo.Interface, branch string) error {
|
||||
remotes, err := opts.Remotes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
baseRemote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = git.Pull(baseRemote.Name, branch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergeMethodSurvey(baseRepo *api.Repository) (PullRequestMergeMethod, error) {
|
||||
type mergeOption struct {
|
||||
title string
|
||||
|
|
|
|||
|
|
@ -480,6 +480,7 @@ func TestPrMerge_deleteBranch(t *testing.T) {
|
|||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/master`, 0, "")
|
||||
cs.Register(`git checkout master`, 0, "")
|
||||
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
|
||||
cs.Register(`git branch -D blueberries`, 0, "")
|
||||
|
|
@ -497,6 +498,106 @@ func TestPrMerge_deleteBranch(t *testing.T) {
|
|||
`), output.Stderr())
|
||||
}
|
||||
|
||||
func TestPrMerge_deleteBranch_nonDefault(t *testing.T) {
|
||||
http := initFakeHTTP()
|
||||
defer http.Verify(t)
|
||||
|
||||
shared.RunCommandFinder(
|
||||
"",
|
||||
&api.PullRequest{
|
||||
ID: "PR_10",
|
||||
Number: 10,
|
||||
State: "OPEN",
|
||||
Title: "Blueberries are a good fruit",
|
||||
HeadRefName: "blueberries",
|
||||
MergeStateStatus: "CLEAN",
|
||||
BaseRefName: "fruit",
|
||||
},
|
||||
baseRepo("OWNER", "REPO", "master"),
|
||||
)
|
||||
|
||||
http.Register(
|
||||
httpmock.GraphQL(`mutation PullRequestMerge\b`),
|
||||
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
|
||||
assert.Equal(t, "PR_10", input["pullRequestId"].(string))
|
||||
assert.Equal(t, "MERGE", input["mergeMethod"].(string))
|
||||
assert.NotContains(t, input, "commitHeadline")
|
||||
}))
|
||||
http.Register(
|
||||
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/blueberries"),
|
||||
httpmock.StringResponse(`{}`))
|
||||
|
||||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/fruit`, 0, "")
|
||||
cs.Register(`git checkout fruit`, 0, "")
|
||||
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
|
||||
cs.Register(`git branch -D blueberries`, 0, "")
|
||||
cs.Register(`git pull --ff-only`, 0, "")
|
||||
|
||||
output, err := runCommand(http, "blueberries", true, `pr merge --merge --delete-branch`)
|
||||
if err != nil {
|
||||
t.Fatalf("Got unexpected error running `pr merge` %s", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "", output.String())
|
||||
assert.Equal(t, heredoc.Doc(`
|
||||
✓ Merged pull request #10 (Blueberries are a good fruit)
|
||||
✓ Deleted branch blueberries and switched to branch fruit
|
||||
`), output.Stderr())
|
||||
}
|
||||
|
||||
func TestPrMerge_deleteBranch_checkoutNewBranch(t *testing.T) {
|
||||
http := initFakeHTTP()
|
||||
defer http.Verify(t)
|
||||
|
||||
shared.RunCommandFinder(
|
||||
"",
|
||||
&api.PullRequest{
|
||||
ID: "PR_10",
|
||||
Number: 10,
|
||||
State: "OPEN",
|
||||
Title: "Blueberries are a good fruit",
|
||||
HeadRefName: "blueberries",
|
||||
MergeStateStatus: "CLEAN",
|
||||
BaseRefName: "fruit",
|
||||
},
|
||||
baseRepo("OWNER", "REPO", "master"),
|
||||
)
|
||||
|
||||
http.Register(
|
||||
httpmock.GraphQL(`mutation PullRequestMerge\b`),
|
||||
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
|
||||
assert.Equal(t, "PR_10", input["pullRequestId"].(string))
|
||||
assert.Equal(t, "MERGE", input["mergeMethod"].(string))
|
||||
assert.NotContains(t, input, "commitHeadline")
|
||||
}))
|
||||
http.Register(
|
||||
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/blueberries"),
|
||||
httpmock.StringResponse(`{}`))
|
||||
|
||||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/fruit`, 1, "")
|
||||
cs.Register(`git checkout -b fruit --track origin/fruit`, 0, "")
|
||||
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
|
||||
cs.Register(`git branch -D blueberries`, 0, "")
|
||||
cs.Register(`git pull --ff-only`, 0, "")
|
||||
|
||||
output, err := runCommand(http, "blueberries", true, `pr merge --merge --delete-branch`)
|
||||
if err != nil {
|
||||
t.Fatalf("Got unexpected error running `pr merge` %s", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, "", output.String())
|
||||
assert.Equal(t, heredoc.Doc(`
|
||||
✓ Merged pull request #10 (Blueberries are a good fruit)
|
||||
✓ Deleted branch blueberries and switched to branch fruit
|
||||
`), output.Stderr())
|
||||
}
|
||||
|
||||
func TestPrMerge_deleteNonCurrentBranch(t *testing.T) {
|
||||
http := initFakeHTTP()
|
||||
defer http.Verify(t)
|
||||
|
|
@ -764,6 +865,7 @@ func TestPrMerge_alreadyMerged(t *testing.T) {
|
|||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/master`, 0, "")
|
||||
cs.Register(`git checkout master`, 0, "")
|
||||
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
|
||||
cs.Register(`git branch -D blueberries`, 0, "")
|
||||
|
|
@ -906,6 +1008,7 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
|
|||
cs, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
cs.Register(`git rev-parse --verify refs/heads/master`, 0, "")
|
||||
cs.Register(`git checkout master`, 0, "")
|
||||
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
|
||||
cs.Register(`git branch -D blueberries`, 0, "")
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ func NewCmdPR(f *cmdutil.Factory) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "pr <command>",
|
||||
Short: "Manage pull requests",
|
||||
Long: "Work with GitHub pull requests",
|
||||
Long: "Work with GitHub pull requests.",
|
||||
Example: heredoc.Doc(`
|
||||
$ gh pr checkout 353
|
||||
$ gh pr create --fill
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func NewCmdReady(f *cmdutil.Factory, runF func(*ReadyOptions) error) *cobra.Comm
|
|||
Use: "ready [<number> | <url> | <branch>]",
|
||||
Short: "Mark a pull request as ready for review",
|
||||
Long: heredoc.Doc(`
|
||||
Mark a pull request as ready for review
|
||||
Mark a pull request as ready for review.
|
||||
|
||||
Without an argument, the pull request that belongs to the current branch
|
||||
is marked as ready.
|
||||
|
|
|
|||
|
|
@ -65,13 +65,9 @@ func CommentablePreRun(cmd *cobra.Command, opts *CommentableOptions) error {
|
|||
|
||||
if inputFlags == 0 {
|
||||
if !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("`--body`, `--body-file` or `--web` required when not running interactively")
|
||||
return cmdutil.FlagErrorf("flags required when not running interactively")
|
||||
}
|
||||
opts.Interactive = true
|
||||
} else if inputFlags == 1 {
|
||||
if !opts.IO.CanPrompt() && opts.InputType == InputTypeEditor {
|
||||
return cmdutil.FlagErrorf("`--body`, `--body-file` or `--web` required when not running interactively")
|
||||
}
|
||||
} else if inputFlags > 1 {
|
||||
return cmdutil.FlagErrorf("specify only one of `--body`, `--body-file`, `--editor`, or `--web`")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,7 +110,13 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
|
|||
f.branchName = branch
|
||||
}
|
||||
} else if f.prNumber == 0 {
|
||||
if prNumber, err := strconv.Atoi(strings.TrimPrefix(opts.Selector, "#")); err == nil {
|
||||
// If opts.Selector is a valid number then assume it is the
|
||||
// PR number unless opts.BaseBranch is specified. This is a
|
||||
// special case for PR create command which will always want
|
||||
// to assume that a numerical selector is a branch name rather
|
||||
// than PR number.
|
||||
prNumber, err := strconv.Atoi(strings.TrimPrefix(opts.Selector, "#"))
|
||||
if opts.BaseBranch == "" && err == nil {
|
||||
f.prNumber = prNumber
|
||||
} else {
|
||||
f.branchName = opts.Selector
|
||||
|
|
|
|||
|
|
@ -49,6 +49,35 @@ func TestFind(t *testing.T) {
|
|||
wantPR: 13,
|
||||
wantRepo: "https://github.com/OWNER/REPO",
|
||||
},
|
||||
{
|
||||
name: "number argument with base branch",
|
||||
args: args{
|
||||
selector: "13",
|
||||
baseBranch: "main",
|
||||
fields: []string{"id", "number"},
|
||||
baseRepoFn: func() (ghrepo.Interface, error) {
|
||||
return ghrepo.FromFullName("OWNER/REPO")
|
||||
},
|
||||
},
|
||||
httpStub: func(r *httpmock.Registry) {
|
||||
r.Register(
|
||||
httpmock.GraphQL(`query PullRequestForBranch\b`),
|
||||
httpmock.StringResponse(`{"data":{"repository":{
|
||||
"pullRequests":{"nodes":[
|
||||
{
|
||||
"number": 123,
|
||||
"state": "OPEN",
|
||||
"baseRefName": "main",
|
||||
"headRefName": "13",
|
||||
"isCrossRepository": false,
|
||||
"headRepositoryOwner": {"login":"OWNER"}
|
||||
}
|
||||
]}
|
||||
}}}`))
|
||||
},
|
||||
wantPR: 123,
|
||||
wantRepo: "https://github.com/OWNER/REPO",
|
||||
},
|
||||
{
|
||||
name: "baseRepo is error",
|
||||
args: args{
|
||||
|
|
|
|||
|
|
@ -23,6 +23,12 @@ type issueTemplate struct {
|
|||
Gbody string `graphql:"body"`
|
||||
}
|
||||
|
||||
type pullRequestTemplate struct {
|
||||
// I would have un-exported these fields, except `cli/shurcool-graphql` then cannot unmarshal them :/
|
||||
Gname string `graphql:"filename"`
|
||||
Gbody string `graphql:"body"`
|
||||
}
|
||||
|
||||
func (t *issueTemplate) Name() string {
|
||||
return t.Gname
|
||||
}
|
||||
|
|
@ -35,7 +41,19 @@ func (t *issueTemplate) Body() []byte {
|
|||
return []byte(t.Gbody)
|
||||
}
|
||||
|
||||
func listIssueTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]issueTemplate, error) {
|
||||
func (t *pullRequestTemplate) Name() string {
|
||||
return t.Gname
|
||||
}
|
||||
|
||||
func (t *pullRequestTemplate) NameForSubmit() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *pullRequestTemplate) Body() []byte {
|
||||
return []byte(t.Gbody)
|
||||
}
|
||||
|
||||
func listIssueTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]Template, error) {
|
||||
var query struct {
|
||||
Repository struct {
|
||||
IssueTemplates []issueTemplate
|
||||
|
|
@ -54,10 +72,44 @@ func listIssueTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]issue
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return query.Repository.IssueTemplates, nil
|
||||
ts := query.Repository.IssueTemplates
|
||||
templates := make([]Template, len(ts))
|
||||
for i := range templates {
|
||||
templates[i] = &ts[i]
|
||||
}
|
||||
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
func hasIssueTemplateSupport(httpClient *http.Client, hostname string) (bool, error) {
|
||||
func listPullRequestTemplates(httpClient *http.Client, repo ghrepo.Interface) ([]Template, error) {
|
||||
var query struct {
|
||||
Repository struct {
|
||||
PullRequestTemplates []pullRequestTemplate
|
||||
} `graphql:"repository(owner: $owner, name: $name)"`
|
||||
}
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"owner": githubv4.String(repo.RepoOwner()),
|
||||
"name": githubv4.String(repo.RepoName()),
|
||||
}
|
||||
|
||||
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
|
||||
|
||||
err := gql.QueryNamed(context.Background(), "PullRequestTemplates", &query, variables)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ts := query.Repository.PullRequestTemplates
|
||||
templates := make([]Template, len(ts))
|
||||
for i := range templates {
|
||||
templates[i] = &ts[i]
|
||||
}
|
||||
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
func hasTemplateSupport(httpClient *http.Client, hostname string, isPR bool) (bool, error) {
|
||||
if !ghinstance.IsEnterprise(hostname) {
|
||||
return true, nil
|
||||
}
|
||||
|
|
@ -81,20 +133,29 @@ func hasIssueTemplateSupport(httpClient *http.Client, hostname string) (bool, er
|
|||
return false, err
|
||||
}
|
||||
|
||||
var hasQuerySupport bool
|
||||
var hasMutationSupport bool
|
||||
var hasIssueQuerySupport bool
|
||||
var hasIssueMutationSupport bool
|
||||
var hasPullRequestQuerySupport bool
|
||||
|
||||
for _, field := range featureDetection.Repository.Fields {
|
||||
if field.Name == "issueTemplates" {
|
||||
hasQuerySupport = true
|
||||
hasIssueQuerySupport = true
|
||||
}
|
||||
if field.Name == "pullRequestTemplates" {
|
||||
hasPullRequestQuerySupport = true
|
||||
}
|
||||
}
|
||||
for _, field := range featureDetection.CreateIssueInput.InputFields {
|
||||
if field.Name == "issueTemplate" {
|
||||
hasMutationSupport = true
|
||||
hasIssueMutationSupport = true
|
||||
}
|
||||
}
|
||||
|
||||
return hasQuerySupport && hasMutationSupport, nil
|
||||
if isPR {
|
||||
return hasPullRequestQuerySupport, nil
|
||||
} else {
|
||||
return hasIssueQuerySupport && hasIssueMutationSupport, nil
|
||||
}
|
||||
}
|
||||
|
||||
type Template interface {
|
||||
|
|
@ -129,13 +190,10 @@ func NewTemplateManager(httpClient *http.Client, repo ghrepo.Interface, dir stri
|
|||
}
|
||||
|
||||
func (m *templateManager) hasAPI() (bool, error) {
|
||||
if m.isPR {
|
||||
return false, nil
|
||||
}
|
||||
if m.cachedClient == nil {
|
||||
m.cachedClient = api.NewCachedClient(m.httpClient, time.Hour*24)
|
||||
}
|
||||
return hasIssueTemplateSupport(m.cachedClient, m.repo.RepoHost())
|
||||
return hasTemplateSupport(m.cachedClient, m.repo.RepoHost(), m.isPR)
|
||||
}
|
||||
|
||||
func (m *templateManager) HasTemplates() (bool, error) {
|
||||
|
|
@ -201,14 +259,15 @@ func (m *templateManager) fetch() error {
|
|||
}
|
||||
|
||||
if hasAPI {
|
||||
issueTemplates, err := listIssueTemplates(m.httpClient, m.repo)
|
||||
lister := listIssueTemplates
|
||||
if m.isPR {
|
||||
lister = listPullRequestTemplates
|
||||
}
|
||||
templates, err := lister(m.httpClient, m.repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.templates = make([]Template, len(issueTemplates))
|
||||
for i := range issueTemplates {
|
||||
m.templates[i] = &issueTemplates[i]
|
||||
}
|
||||
m.templates = templates
|
||||
}
|
||||
|
||||
if !m.allowFS {
|
||||
|
|
|
|||
|
|
@ -63,14 +63,70 @@ func TestTemplateManager_hasAPI(t *testing.T) {
|
|||
|
||||
assert.Equal(t, "LEGACY", string(m.LegacyBody()))
|
||||
|
||||
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
|
||||
as, askRestore := prompt.InitAskStubber()
|
||||
defer askRestore()
|
||||
as := prompt.NewAskStubber(t)
|
||||
as.StubPrompt("Choose a template").
|
||||
AssertOptions([]string{"Bug report", "Feature request", "Open a blank issue"}).
|
||||
AnswerWith("Feature request")
|
||||
|
||||
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
|
||||
as.StubOne(1) // choose "Feature Request"
|
||||
tpl, err := m.Choose()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Feature request", tpl.NameForSubmit())
|
||||
assert.Equal(t, "I need a feature", string(tpl.Body()))
|
||||
}
|
||||
|
||||
func TestTemplateManager_hasAPI_PullRequest(t *testing.T) {
|
||||
rootDir := t.TempDir()
|
||||
legacyTemplateFile := filepath.Join(rootDir, ".github", "PULL_REQUEST_TEMPLATE.md")
|
||||
_ = os.MkdirAll(filepath.Dir(legacyTemplateFile), 0755)
|
||||
_ = ioutil.WriteFile(legacyTemplateFile, []byte("LEGACY"), 0644)
|
||||
|
||||
tr := httpmock.Registry{}
|
||||
httpClient := &http.Client{Transport: &tr}
|
||||
defer tr.Verify(t)
|
||||
|
||||
tr.Register(
|
||||
httpmock.GraphQL(`query IssueTemplates_fields\b`),
|
||||
httpmock.StringResponse(`{"data":{
|
||||
"Repository": {
|
||||
"fields": [
|
||||
{"name": "foo"},
|
||||
{"name": "pullRequestTemplates"}
|
||||
]
|
||||
}
|
||||
}}`))
|
||||
tr.Register(
|
||||
httpmock.GraphQL(`query PullRequestTemplates\b`),
|
||||
httpmock.StringResponse(`{"data":{"repository":{
|
||||
"pullRequestTemplates": [
|
||||
{"filename": "bug_pr.md", "body": "I fixed a problem"},
|
||||
{"filename": "feature_pr.md", "body": "I added a feature"}
|
||||
]
|
||||
}}}`))
|
||||
|
||||
m := templateManager{
|
||||
repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"),
|
||||
rootDir: rootDir,
|
||||
allowFS: true,
|
||||
isPR: true,
|
||||
httpClient: httpClient,
|
||||
cachedClient: httpClient,
|
||||
}
|
||||
|
||||
hasTemplates, err := m.HasTemplates()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, hasTemplates)
|
||||
|
||||
assert.Equal(t, "LEGACY", string(m.LegacyBody()))
|
||||
|
||||
as := prompt.NewAskStubber(t)
|
||||
as.StubPrompt("Choose a template").
|
||||
AssertOptions([]string{"bug_pr.md", "feature_pr.md", "Open a blank pull request"}).
|
||||
AnswerWith("bug_pr.md")
|
||||
|
||||
tpl, err := m.Choose()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "", tpl.NameForSubmit())
|
||||
assert.Equal(t, "I fixed a problem", string(tpl.Body()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,12 +109,11 @@ func viewRun(opts *ViewOptions) error {
|
|||
}
|
||||
|
||||
opts.IO.DetectTerminalTheme()
|
||||
|
||||
err = opts.IO.StartPager()
|
||||
if err != nil {
|
||||
return err
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
defer opts.IO.StopPager()
|
||||
|
||||
if opts.Exporter != nil {
|
||||
return opts.Exporter.Write(opts.IO, pr)
|
||||
|
|
|
|||
|
|
@ -174,6 +174,7 @@ func createRun(opts *CreateOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
var existingTag bool
|
||||
if opts.TagName == "" {
|
||||
tags, err := getTags(httpClient, baseRepo, 5)
|
||||
if err != nil {
|
||||
|
|
@ -198,6 +199,7 @@ func createRun(opts *CreateOptions) error {
|
|||
return fmt.Errorf("could not prompt: %w", err)
|
||||
}
|
||||
if tag != createNewTagOption {
|
||||
existingTag = true
|
||||
opts.TagName = tag
|
||||
}
|
||||
}
|
||||
|
|
@ -213,6 +215,29 @@ func createRun(opts *CreateOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
var tagDescription string
|
||||
if opts.RepoOverride == "" {
|
||||
tagDescription, _ = gitTagInfo(opts.TagName)
|
||||
// If there is a local tag with the same name as specified
|
||||
// the user may not want to create a new tag on the remote
|
||||
// as the local one might be annotated or signed.
|
||||
// If the user specifies the target take that as explict instruction
|
||||
// to create the tag on the remote pointing to the target regardless
|
||||
// of local tag status.
|
||||
// If a remote tag with the same name as specified exists already
|
||||
// then a new tag will not be created so ignore local tag status.
|
||||
if tagDescription != "" && !existingTag && opts.Target == "" {
|
||||
remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !remoteExists {
|
||||
return fmt.Errorf("tag %s exists locally but has not been pushed to %s, please push it before continuing or specify the `--target` flag to create a new tag",
|
||||
opts.TagName, ghrepo.FullName(baseRepo))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !opts.BodyProvided && opts.IO.CanPrompt() {
|
||||
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
|
||||
if err != nil {
|
||||
|
|
@ -220,7 +245,6 @@ func createRun(opts *CreateOptions) error {
|
|||
}
|
||||
|
||||
var generatedNotes *releaseNotes
|
||||
var tagDescription string
|
||||
var generatedChangelog string
|
||||
|
||||
params := map[string]interface{}{
|
||||
|
|
@ -236,7 +260,6 @@ func createRun(opts *CreateOptions) error {
|
|||
|
||||
if opts.RepoOverride == "" {
|
||||
headRef := opts.TagName
|
||||
tagDescription, _ = gitTagInfo(opts.TagName)
|
||||
if tagDescription == "" {
|
||||
if opts.Target != "" {
|
||||
// TODO: use the remote-tracking version of the branch ref
|
||||
|
|
@ -366,13 +389,6 @@ func createRun(opts *CreateOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
if opts.Draft && len(opts.DiscussionCategory) > 0 {
|
||||
return fmt.Errorf(
|
||||
"%s Discussions not supported with draft releases",
|
||||
opts.IO.ColorScheme().FailureIcon(),
|
||||
)
|
||||
}
|
||||
|
||||
params := map[string]interface{}{
|
||||
"tag_name": opts.TagName,
|
||||
"draft": opts.Draft,
|
||||
|
|
@ -420,7 +436,7 @@ func createRun(opts *CreateOptions) error {
|
|||
}
|
||||
|
||||
if !opts.Draft {
|
||||
rel, err := publishRelease(httpClient, newRelease.APIURL)
|
||||
rel, err := publishRelease(httpClient, newRelease.APIURL, opts.DiscussionCategory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ func Test_createRun(t *testing.T) {
|
|||
name string
|
||||
isTTY bool
|
||||
opts CreateOptions
|
||||
wantParams interface{}
|
||||
httpStubs func(t *testing.T, reg *httpmock.Registry)
|
||||
wantErr string
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
|
|
@ -302,12 +302,20 @@ func Test_createRun(t *testing.T) {
|
|||
BodyProvided: true,
|
||||
Target: "",
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"name": "The Big 1.2",
|
||||
"body": "* Fixed bugs",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"name": "The Big 1.2",
|
||||
"body": "* Fixed bugs",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
wantStderr: ``,
|
||||
|
|
@ -323,13 +331,21 @@ func Test_createRun(t *testing.T) {
|
|||
Target: "",
|
||||
DiscussionCategory: "General",
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"name": "The Big 1.2",
|
||||
"body": "* Fixed bugs",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"discussion_category_name": "General",
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"name": "The Big 1.2",
|
||||
"body": "* Fixed bugs",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"discussion_category_name": "General",
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
wantStderr: ``,
|
||||
|
|
@ -344,11 +360,19 @@ func Test_createRun(t *testing.T) {
|
|||
BodyProvided: true,
|
||||
Target: "main",
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"target_commitish": "main",
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"target_commitish": "main",
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
wantStderr: ``,
|
||||
|
|
@ -364,35 +388,22 @@ func Test_createRun(t *testing.T) {
|
|||
Draft: true,
|
||||
Target: "",
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
wantStderr: ``,
|
||||
},
|
||||
{
|
||||
name: "discussion category for draft release",
|
||||
isTTY: true,
|
||||
opts: CreateOptions{
|
||||
TagName: "v1.2.3",
|
||||
Name: "",
|
||||
Body: "",
|
||||
BodyProvided: true,
|
||||
Draft: true,
|
||||
Target: "",
|
||||
DiscussionCategory: "general",
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
"discussion_category_name": "general",
|
||||
},
|
||||
wantErr: "X Discussions not supported with draft releases",
|
||||
wantStdout: "",
|
||||
},
|
||||
{
|
||||
name: "with generate notes",
|
||||
isTTY: true,
|
||||
|
|
@ -404,11 +415,19 @@ func Test_createRun(t *testing.T) {
|
|||
BodyProvided: true,
|
||||
GenerateNotes: true,
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"generate_release_notes": true,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"generate_release_notes": true,
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
wantErr: "",
|
||||
|
|
@ -433,10 +452,97 @@ func Test_createRun(t *testing.T) {
|
|||
},
|
||||
Concurrency: 1,
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
}, params)
|
||||
}))
|
||||
reg.Register(httpmock.REST("POST", "assets/upload"), func(req *http.Request) (*http.Response, error) {
|
||||
q := req.URL.Query()
|
||||
assert.Equal(t, "ball.tgz", q.Get("name"))
|
||||
assert.Equal(t, "", q.Get("label"))
|
||||
return &http.Response{
|
||||
StatusCode: 201,
|
||||
Request: req,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {"application/json"},
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
reg.Register(httpmock.REST("PATCH", "releases/123"), httpmock.RESTPayload(201, `{
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"draft": false,
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
|
||||
wantStderr: ``,
|
||||
},
|
||||
{
|
||||
name: "upload files and create discussion",
|
||||
isTTY: true,
|
||||
opts: CreateOptions{
|
||||
TagName: "v1.2.3",
|
||||
Name: "",
|
||||
Body: "",
|
||||
BodyProvided: true,
|
||||
Draft: false,
|
||||
Target: "",
|
||||
Assets: []*shared.AssetForUpload{
|
||||
{
|
||||
Name: "ball.tgz",
|
||||
Open: func() (io.ReadCloser, error) {
|
||||
return ioutil.NopCloser(bytes.NewBufferString(`TARBALL`)), nil
|
||||
},
|
||||
},
|
||||
},
|
||||
DiscussionCategory: "general",
|
||||
Concurrency: 1,
|
||||
},
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"tag_name": "v1.2.3",
|
||||
"draft": true,
|
||||
"prerelease": false,
|
||||
"discussion_category_name": "general",
|
||||
}, params)
|
||||
}))
|
||||
reg.Register(httpmock.REST("POST", "assets/upload"), func(req *http.Request) (*http.Response, error) {
|
||||
q := req.URL.Query()
|
||||
assert.Equal(t, "ball.tgz", q.Get("name"))
|
||||
assert.Equal(t, "", q.Get("label"))
|
||||
return &http.Response{
|
||||
StatusCode: 201,
|
||||
Request: req,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {"application/json"},
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
reg.Register(httpmock.REST("PATCH", "releases/123"), httpmock.RESTPayload(201, `{
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
|
||||
}`, func(params map[string]interface{}) {
|
||||
assert.Equal(t, map[string]interface{}{
|
||||
"draft": false,
|
||||
"discussion_category_name": "general",
|
||||
}, params)
|
||||
}))
|
||||
},
|
||||
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
|
||||
wantStderr: ``,
|
||||
|
|
@ -450,15 +556,10 @@ func Test_createRun(t *testing.T) {
|
|||
io.SetStderrTTY(tt.isTTY)
|
||||
|
||||
fakeHTTP := &httpmock.Registry{}
|
||||
fakeHTTP.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.StatusStringResponse(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`))
|
||||
fakeHTTP.Register(httpmock.REST("POST", "assets/upload"), httpmock.StatusStringResponse(201, `{}`))
|
||||
fakeHTTP.Register(httpmock.REST("PATCH", "releases/123"), httpmock.StatusStringResponse(201, `{
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
|
||||
}`))
|
||||
if tt.httpStubs != nil {
|
||||
tt.httpStubs(t, fakeHTTP)
|
||||
}
|
||||
defer fakeHTTP.Verify(t)
|
||||
|
||||
tt.opts.IO = io
|
||||
tt.opts.HttpClient = func() (*http.Client, error) {
|
||||
|
|
@ -476,26 +577,6 @@ func Test_createRun(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
bb, err := ioutil.ReadAll(fakeHTTP.Requests[0].Body)
|
||||
require.NoError(t, err)
|
||||
var params interface{}
|
||||
err = json.Unmarshal(bb, ¶ms)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantParams, params)
|
||||
|
||||
if len(tt.opts.Assets) > 0 {
|
||||
q := fakeHTTP.Requests[1].URL.Query()
|
||||
assert.Equal(t, tt.opts.Assets[0].Name, q.Get("name"))
|
||||
assert.Equal(t, tt.opts.Assets[0].Label, q.Get("label"))
|
||||
|
||||
bb, err := ioutil.ReadAll(fakeHTTP.Requests[2].Body)
|
||||
require.NoError(t, err)
|
||||
var updateParams interface{}
|
||||
err = json.Unmarshal(bb, &updateParams)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, map[string]interface{}{"draft": false}, updateParams)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.wantStdout, stdout.String())
|
||||
assert.Equal(t, tt.wantStderr, stderr.String())
|
||||
})
|
||||
|
|
@ -673,6 +754,8 @@ func Test_createRun_interactive(t *testing.T) {
|
|||
rs.Register(`git describe --tags --abbrev=0 v1\.2\.3\^`, 1, "")
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.GraphQL("RepositoryFindRef"),
|
||||
httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": "tag id"}}}}`))
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"),
|
||||
httpmock.StatusStringResponse(404, `{}`))
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"),
|
||||
|
|
@ -690,6 +773,57 @@ func Test_createRun_interactive(t *testing.T) {
|
|||
},
|
||||
wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
},
|
||||
{
|
||||
name: "error when unpublished local tag and target not specified",
|
||||
opts: &CreateOptions{
|
||||
TagName: "v1.2.3",
|
||||
},
|
||||
runStubs: func(rs *run.CommandStubber) {
|
||||
rs.Register(`git tag --list`, 0, "tag exists")
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.GraphQL("RepositoryFindRef"),
|
||||
httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": ""}}}}`))
|
||||
},
|
||||
wantErr: "tag v1.2.3 exists locally but has not been pushed to OWNER/REPO, please push it before continuing or specify the `--target` flag to create a new tag",
|
||||
},
|
||||
{
|
||||
name: "create a release when unpublished local tag and target specified",
|
||||
opts: &CreateOptions{
|
||||
TagName: "v1.2.3",
|
||||
Target: "main",
|
||||
},
|
||||
askStubs: func(as *prompt.AskStubber) {
|
||||
as.StubPrompt("Title (optional)").AnswerWith("")
|
||||
as.StubPrompt("Release notes").
|
||||
AssertOptions([]string{"Write my own", "Write using generated notes as template", "Write using git tag message as template", "Leave blank"}).
|
||||
AnswerWith("Leave blank")
|
||||
as.StubPrompt("Is this a prerelease?").AnswerWith(false)
|
||||
as.StubPrompt("Submit?").AnswerWith("Publish release")
|
||||
},
|
||||
runStubs: func(rs *run.CommandStubber) {
|
||||
rs.Register(`git tag --list`, 0, "tag exists")
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"),
|
||||
httpmock.StatusStringResponse(200, `{
|
||||
"name": "generated name",
|
||||
"body": "generated body"
|
||||
}`))
|
||||
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.StatusStringResponse(201, `{
|
||||
"url": "https://api.github.com/releases/123",
|
||||
"upload_url": "https://api.github.com/assets/upload",
|
||||
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
|
||||
}`))
|
||||
},
|
||||
wantParams: map[string]interface{}{
|
||||
"draft": false,
|
||||
"prerelease": false,
|
||||
"tag_name": "v1.2.3",
|
||||
"target_commitish": "main",
|
||||
},
|
||||
wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
ios, _, stdout, stderr := iostreams.Test()
|
||||
|
|
@ -739,7 +873,17 @@ func Test_createRun_interactive(t *testing.T) {
|
|||
}
|
||||
|
||||
if tt.wantParams != nil {
|
||||
bb, err := ioutil.ReadAll(reg.Requests[1].Body)
|
||||
var r *http.Request
|
||||
for _, req := range reg.Requests {
|
||||
if req.URL.Path == "/repos/OWNER/REPO/releases" {
|
||||
r = req
|
||||
break
|
||||
}
|
||||
}
|
||||
if r == nil {
|
||||
t.Fatalf("no http requests for creating a release found")
|
||||
}
|
||||
bb, err := ioutil.ReadAll(r.Body)
|
||||
assert.NoError(t, err)
|
||||
var params map[string]interface{}
|
||||
err = json.Unmarshal(bb, ¶ms)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package create
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -13,6 +14,7 @@ import (
|
|||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmd/release/shared"
|
||||
graphql "github.com/cli/shurcooL-graphql"
|
||||
)
|
||||
|
||||
type tag struct {
|
||||
|
|
@ -26,6 +28,25 @@ type releaseNotes struct {
|
|||
|
||||
var notImplementedError = errors.New("not implemented")
|
||||
|
||||
func remoteTagExists(httpClient *http.Client, repo ghrepo.Interface, tagName string) (bool, error) {
|
||||
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
|
||||
qualifiedTagName := fmt.Sprintf("refs/tags/%s", tagName)
|
||||
var query struct {
|
||||
Repository struct {
|
||||
Ref struct {
|
||||
ID string
|
||||
} `graphql:"ref(qualifiedName: $tagName)"`
|
||||
} `graphql:"repository(owner: $owner, name: $name)"`
|
||||
}
|
||||
variables := map[string]interface{}{
|
||||
"owner": graphql.String(repo.RepoOwner()),
|
||||
"name": graphql.String(repo.RepoName()),
|
||||
"tagName": graphql.String(qualifiedTagName),
|
||||
}
|
||||
err := gql.QueryNamed(context.Background(), "RepositoryFindRef", &query, variables)
|
||||
return query.Repository.Ref.ID != "", err
|
||||
}
|
||||
|
||||
func getTags(httpClient *http.Client, repo ghrepo.Interface, limit int) ([]tag, error) {
|
||||
path := fmt.Sprintf("repos/%s/%s/tags?per_page=%d", repo.RepoOwner(), repo.RepoName(), limit)
|
||||
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
|
||||
|
|
@ -134,8 +155,17 @@ func createRelease(httpClient *http.Client, repo ghrepo.Interface, params map[st
|
|||
return &newRelease, err
|
||||
}
|
||||
|
||||
func publishRelease(httpClient *http.Client, releaseURL string) (*shared.Release, error) {
|
||||
req, err := http.NewRequest("PATCH", releaseURL, bytes.NewBufferString(`{"draft":false}`))
|
||||
func publishRelease(httpClient *http.Client, releaseURL string, discussionCategory string) (*shared.Release, error) {
|
||||
params := map[string]interface{}{"draft": false}
|
||||
if discussionCategory != "" {
|
||||
params["discussion_category_name"] = discussionCategory
|
||||
}
|
||||
|
||||
bodyBytes, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("PATCH", releaseURL, bytes.NewBuffer(bodyBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,10 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List releases in a repository",
|
||||
Args: cobra.NoArgs,
|
||||
Use: "list",
|
||||
Short: "List releases in a repository",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
|
@ -63,6 +64,12 @@ func listRun(opts *ListOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
table := utils.NewTablePrinter(opts.IO)
|
||||
iofmt := opts.IO.ColorScheme()
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
func NewCmdRelease(f *cmdutil.Factory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "release <command>",
|
||||
Short: "Manage GitHub releases",
|
||||
Short: "Manage releases",
|
||||
Annotations: map[string]string{
|
||||
"IsCore": "true",
|
||||
},
|
||||
|
|
|
|||
99
pkg/cmd/repo/deploy-key/add/add.go
Normal file
99
pkg/cmd/repo/deploy-key/add/add.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package add
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type AddOptions struct {
|
||||
IO *iostreams.IOStreams
|
||||
HTTPClient func() (*http.Client, error)
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
|
||||
KeyFile string
|
||||
Title string
|
||||
AllowWrite bool
|
||||
}
|
||||
|
||||
func NewCmdAdd(f *cmdutil.Factory, runF func(*AddOptions) error) *cobra.Command {
|
||||
opts := &AddOptions{
|
||||
HTTPClient: f.HttpClient,
|
||||
IO: f.IOStreams,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "add <key-file>",
|
||||
Short: "Add a deploy key to a GitHub repository",
|
||||
Long: heredoc.Doc(`
|
||||
Add a deploy key to a GitHub repository.
|
||||
|
||||
Note that any key added by gh will be associated with the current authentication token.
|
||||
If you de-authorize the GitHub CLI app or authentication token from your account, any
|
||||
deploy keys added by GitHub CLI will be removed as well.
|
||||
`),
|
||||
Example: heredoc.Doc(`
|
||||
# generate a passwordless SSH key and add it as a deploy key to a repository
|
||||
$ ssh-keygen -t ed25519 -C "my description" -N "" -f ~/.ssh/gh-test
|
||||
$ gh repo deploy-key add ~/.ssh/gh-test.pub
|
||||
`),
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
opts.KeyFile = args[0]
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
return addRun(opts)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Title of the new key")
|
||||
cmd.Flags().BoolVarP(&opts.AllowWrite, "allow-write", "w", false, "Allow write access for the key")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func addRun(opts *AddOptions) error {
|
||||
httpClient, err := opts.HTTPClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var keyReader io.Reader
|
||||
if opts.KeyFile == "-" {
|
||||
keyReader = opts.IO.In
|
||||
defer opts.IO.In.Close()
|
||||
} else {
|
||||
f, err := os.Open(opts.KeyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
keyReader = f
|
||||
}
|
||||
|
||||
repo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := uploadDeployKey(httpClient, repo, keyReader, opts.Title, opts.AllowWrite); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !opts.IO.IsStdoutTTY() {
|
||||
return nil
|
||||
}
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
_, err = fmt.Fprintf(opts.IO.Out, "%s Deploy key added to %s\n", cs.SuccessIcon(), cs.Bold(ghrepo.FullName(repo)))
|
||||
return err
|
||||
}
|
||||
85
pkg/cmd/repo/deploy-key/add/add_test.go
Normal file
85
pkg/cmd/repo/deploy-key/add/add_test.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package add
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/httpmock"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func Test_addRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
opts AddOptions
|
||||
isTTY bool
|
||||
stdin string
|
||||
httpStubs func(t *testing.T, reg *httpmock.Registry)
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "add from stdin",
|
||||
isTTY: true,
|
||||
opts: AddOptions{
|
||||
KeyFile: "-",
|
||||
Title: "my sacred key",
|
||||
AllowWrite: false,
|
||||
},
|
||||
stdin: "PUBKEY\n",
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("POST", "repos/OWNER/REPO/keys"),
|
||||
httpmock.RESTPayload(200, `{}`, func(payload map[string]interface{}) {
|
||||
if title := payload["title"].(string); title != "my sacred key" {
|
||||
t.Errorf("POST title %q, want %q", title, "my sacred key")
|
||||
}
|
||||
if key := payload["key"].(string); key != "PUBKEY\n" {
|
||||
t.Errorf("POST key %q, want %q", key, "PUBKEY\n")
|
||||
}
|
||||
if isReadOnly := payload["read_only"].(bool); !isReadOnly {
|
||||
t.Errorf("POST read_only %v, want %v", isReadOnly, true)
|
||||
}
|
||||
}))
|
||||
},
|
||||
wantStdout: "✓ Deploy key added to OWNER/REPO\n",
|
||||
wantStderr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, stdin, stdout, stderr := iostreams.Test()
|
||||
stdin.WriteString(tt.stdin)
|
||||
io.SetStdinTTY(tt.isTTY)
|
||||
io.SetStdoutTTY(tt.isTTY)
|
||||
io.SetStderrTTY(tt.isTTY)
|
||||
|
||||
reg := &httpmock.Registry{}
|
||||
if tt.httpStubs != nil {
|
||||
tt.httpStubs(t, reg)
|
||||
}
|
||||
|
||||
opts := tt.opts
|
||||
opts.IO = io
|
||||
opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }
|
||||
opts.HTTPClient = func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }
|
||||
|
||||
err := addRun(&opts)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("addRun() return error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if stdout.String() != tt.wantStdout {
|
||||
t.Errorf("wants stdout %q, got %q", tt.wantStdout, stdout.String())
|
||||
}
|
||||
if stderr.String() != tt.wantStderr {
|
||||
t.Errorf("wants stderr %q, got %q", tt.wantStderr, stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
57
pkg/cmd/repo/deploy-key/add/http.go
Normal file
57
pkg/cmd/repo/deploy-key/add/http.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package add
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
func uploadDeployKey(httpClient *http.Client, repo ghrepo.Interface, keyFile io.Reader, title string, isWritable bool) error {
|
||||
path := fmt.Sprintf("repos/%s/%s/keys", repo.RepoOwner(), repo.RepoName())
|
||||
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
|
||||
|
||||
keyBytes, err := ioutil.ReadAll(keyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"title": title,
|
||||
"key": string(keyBytes),
|
||||
"read_only": !isWritable,
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode > 299 {
|
||||
return api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
_, err = io.Copy(ioutil.Discard, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
67
pkg/cmd/repo/deploy-key/delete/delete.go
Normal file
67
pkg/cmd/repo/deploy-key/delete/delete.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type DeleteOptions struct {
|
||||
IO *iostreams.IOStreams
|
||||
HTTPClient func() (*http.Client, error)
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
|
||||
KeyID string
|
||||
}
|
||||
|
||||
func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command {
|
||||
opts := &DeleteOptions{
|
||||
HTTPClient: f.HttpClient,
|
||||
IO: f.IOStreams,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "delete <key-id>",
|
||||
Short: "Delete a deploy key from a GitHub repository",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
opts.KeyID = args[0]
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
return deleteRun(opts)
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func deleteRun(opts *DeleteOptions) error {
|
||||
httpClient, err := opts.HTTPClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := deleteDeployKey(httpClient, repo, opts.KeyID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !opts.IO.IsStdoutTTY() {
|
||||
return nil
|
||||
}
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
_, err = fmt.Fprintf(opts.IO.Out, "%s Deploy key deleted from %s\n", cs.SuccessIconWithColor(cs.Red), cs.Bold(ghrepo.FullName(repo)))
|
||||
return err
|
||||
}
|
||||
40
pkg/cmd/repo/deploy-key/delete/delete_test.go
Normal file
40
pkg/cmd/repo/deploy-key/delete/delete_test.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/httpmock"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_deleteRun(t *testing.T) {
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
io.SetStdinTTY(false)
|
||||
io.SetStdoutTTY(true)
|
||||
io.SetStderrTTY(true)
|
||||
|
||||
tr := httpmock.Registry{}
|
||||
defer tr.Verify(t)
|
||||
|
||||
tr.Register(
|
||||
httpmock.REST("DELETE", "repos/OWNER/REPO/keys/1234"),
|
||||
httpmock.StringResponse(`{}`))
|
||||
|
||||
err := deleteRun(&DeleteOptions{
|
||||
IO: io,
|
||||
HTTPClient: func() (*http.Client, error) {
|
||||
return &http.Client{Transport: &tr}, nil
|
||||
},
|
||||
BaseRepo: func() (ghrepo.Interface, error) {
|
||||
return ghrepo.New("OWNER", "REPO"), nil
|
||||
},
|
||||
KeyID: "1234",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "", stderr.String())
|
||||
assert.Equal(t, "✓ Deploy key deleted from OWNER/REPO\n", stdout.String())
|
||||
}
|
||||
39
pkg/cmd/repo/deploy-key/delete/http.go
Normal file
39
pkg/cmd/repo/deploy-key/delete/http.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
func deleteDeployKey(httpClient *http.Client, repo ghrepo.Interface, id string) error {
|
||||
path := fmt.Sprintf("repos/%s/%s/keys/%s", repo.RepoOwner(), repo.RepoName(), id)
|
||||
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
|
||||
|
||||
req, err := http.NewRequest("DELETE", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode > 299 {
|
||||
return api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
_, err = io.Copy(ioutil.Discard, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
24
pkg/cmd/repo/deploy-key/deploy-key.go
Normal file
24
pkg/cmd/repo/deploy-key/deploy-key.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package deploykey
|
||||
|
||||
import (
|
||||
cmdAdd "github.com/cli/cli/v2/pkg/cmd/repo/deploy-key/add"
|
||||
cmdDelete "github.com/cli/cli/v2/pkg/cmd/repo/deploy-key/delete"
|
||||
cmdList "github.com/cli/cli/v2/pkg/cmd/repo/deploy-key/list"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCmdDeployKey(f *cmdutil.Factory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "deploy-key <command>",
|
||||
Short: "Manage deploy keys in a repository",
|
||||
}
|
||||
|
||||
cmdutil.EnableRepoOverride(cmd, f)
|
||||
|
||||
cmd.AddCommand(cmdList.NewCmdList(f, nil))
|
||||
cmd.AddCommand(cmdAdd.NewCmdAdd(f, nil))
|
||||
cmd.AddCommand(cmdDelete.NewCmdDelete(f, nil))
|
||||
|
||||
return cmd
|
||||
}
|
||||
53
pkg/cmd/repo/deploy-key/list/http.go
Normal file
53
pkg/cmd/repo/deploy-key/list/http.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
type deployKey struct {
|
||||
ID int
|
||||
Key string
|
||||
Title string
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ReadOnly bool `json:"read_only"`
|
||||
}
|
||||
|
||||
func repoKeys(httpClient *http.Client, repo ghrepo.Interface) ([]deployKey, error) {
|
||||
path := fmt.Sprintf("repos/%s/%s/keys?per_page=100", repo.RepoOwner(), repo.RepoName())
|
||||
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode > 299 {
|
||||
return nil, api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var keys []deployKey
|
||||
err = json.Unmarshal(b, &keys)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
107
pkg/cmd/repo/deploy-key/list/list.go
Normal file
107
pkg/cmd/repo/deploy-key/list/list.go
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/cli/cli/v2/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type ListOptions struct {
|
||||
IO *iostreams.IOStreams
|
||||
HTTPClient func() (*http.Client, error)
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
}
|
||||
|
||||
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
|
||||
opts := &ListOptions{
|
||||
IO: f.IOStreams,
|
||||
HTTPClient: f.HttpClient,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List deploy keys in a GitHub repository",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
return listRun(opts)
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func listRun(opts *ListOptions) error {
|
||||
apiClient, err := opts.HTTPClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deployKeys, err := repoKeys(apiClient, repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(deployKeys) == 0 {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "No deploy keys found in %s\n", ghrepo.FullName(repo))
|
||||
return cmdutil.SilentError
|
||||
}
|
||||
|
||||
t := utils.NewTablePrinter(opts.IO)
|
||||
cs := opts.IO.ColorScheme()
|
||||
now := time.Now()
|
||||
|
||||
for _, deployKey := range deployKeys {
|
||||
sshID := strconv.Itoa(deployKey.ID)
|
||||
t.AddField(sshID, nil, nil)
|
||||
t.AddField(deployKey.Title, nil, nil)
|
||||
|
||||
sshType := "read-only"
|
||||
if !deployKey.ReadOnly {
|
||||
sshType = "read-write"
|
||||
}
|
||||
t.AddField(sshType, nil, nil)
|
||||
t.AddField(deployKey.Key, truncateMiddle, nil)
|
||||
|
||||
createdAt := deployKey.CreatedAt.Format(time.RFC3339)
|
||||
if t.IsTTY() {
|
||||
createdAt = utils.FuzzyAgoAbbr(now, deployKey.CreatedAt)
|
||||
}
|
||||
t.AddField(createdAt, nil, cs.Gray)
|
||||
t.EndRow()
|
||||
}
|
||||
|
||||
return t.Render()
|
||||
}
|
||||
|
||||
func truncateMiddle(maxWidth int, t string) string {
|
||||
if len(t) <= maxWidth {
|
||||
return t
|
||||
}
|
||||
|
||||
ellipsis := "..."
|
||||
if maxWidth < len(ellipsis)+2 {
|
||||
return t[0:maxWidth]
|
||||
}
|
||||
|
||||
halfWidth := (maxWidth - len(ellipsis)) / 2
|
||||
remainder := (maxWidth - len(ellipsis)) % 2
|
||||
return t[0:halfWidth+remainder] + ellipsis + t[len(t)-halfWidth:]
|
||||
}
|
||||
132
pkg/cmd/repo/deploy-key/list/list_test.go
Normal file
132
pkg/cmd/repo/deploy-key/list/list_test.go
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/httpmock"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
)
|
||||
|
||||
func TestListRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
opts ListOptions
|
||||
isTTY bool
|
||||
httpStubs func(t *testing.T, reg *httpmock.Registry)
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "list tty",
|
||||
isTTY: true,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
createdAt := time.Now().Add(time.Duration(-24) * time.Hour)
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "repos/OWNER/REPO/keys"),
|
||||
httpmock.StringResponse(fmt.Sprintf(`[
|
||||
{
|
||||
"id": 1234,
|
||||
"key": "ssh-rsa AAAABbBB123",
|
||||
"title": "Mac",
|
||||
"created_at": "%[1]s",
|
||||
"read_only": true
|
||||
},
|
||||
{
|
||||
"id": 5678,
|
||||
"key": "ssh-rsa EEEEEEEK247",
|
||||
"title": "hubot@Windows",
|
||||
"created_at": "%[1]s",
|
||||
"read_only": false
|
||||
}
|
||||
]`, createdAt.Format(time.RFC3339))),
|
||||
)
|
||||
},
|
||||
wantStdout: heredoc.Doc(`
|
||||
1234 Mac read-only ssh-rsa AAAABbBB123 1d
|
||||
5678 hubot@Windows read-write ssh-rsa EEEEEEEK247 1d
|
||||
`),
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
name: "list non-tty",
|
||||
isTTY: false,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
createdAt, _ := time.Parse(time.RFC3339, "2020-08-31T15:44:24+02:00")
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "repos/OWNER/REPO/keys"),
|
||||
httpmock.StringResponse(fmt.Sprintf(`[
|
||||
{
|
||||
"id": 1234,
|
||||
"key": "ssh-rsa AAAABbBB123",
|
||||
"title": "Mac",
|
||||
"created_at": "%[1]s",
|
||||
"read_only": false
|
||||
},
|
||||
{
|
||||
"id": 5678,
|
||||
"key": "ssh-rsa EEEEEEEK247",
|
||||
"title": "hubot@Windows",
|
||||
"created_at": "%[1]s",
|
||||
"read_only": true
|
||||
}
|
||||
]`, createdAt.Format(time.RFC3339))),
|
||||
)
|
||||
},
|
||||
wantStdout: heredoc.Doc(`
|
||||
1234 Mac read-write ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00
|
||||
5678 hubot@Windows read-only ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00
|
||||
`),
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
name: "no keys",
|
||||
isTTY: false,
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "repos/OWNER/REPO/keys"),
|
||||
httpmock.StringResponse(`[]`))
|
||||
},
|
||||
wantStdout: "",
|
||||
wantStderr: "No deploy keys found in OWNER/REPO\n",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
io.SetStdoutTTY(tt.isTTY)
|
||||
io.SetStdinTTY(tt.isTTY)
|
||||
io.SetStderrTTY(tt.isTTY)
|
||||
|
||||
reg := &httpmock.Registry{}
|
||||
if tt.httpStubs != nil {
|
||||
tt.httpStubs(t, reg)
|
||||
}
|
||||
|
||||
opts := tt.opts
|
||||
opts.IO = io
|
||||
opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }
|
||||
opts.HTTPClient = func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }
|
||||
|
||||
err := listRun(&opts)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("listRun() return error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if stdout.String() != tt.wantStdout {
|
||||
t.Errorf("wants stdout %q, got %q", tt.wantStdout, stdout.String())
|
||||
}
|
||||
if stderr.String() != tt.wantStderr {
|
||||
t.Errorf("wants stderr %q, got %q", tt.wantStderr, stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -8,44 +8,73 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/cli/cli/v2/pkg/prompt"
|
||||
"github.com/cli/cli/v2/pkg/set"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
const (
|
||||
allowMergeCommits = "Allow Merge Commits"
|
||||
allowSquashMerge = "Allow Squash Merging"
|
||||
allowRebaseMerge = "Allow Rebase Merging"
|
||||
|
||||
optionAllowForking = "Allow Forking"
|
||||
optionDefaultBranchName = "Default Branch Name"
|
||||
optionDescription = "Description"
|
||||
optionHomePageURL = "Home Page URL"
|
||||
optionIssues = "Issues"
|
||||
optionMergeOptions = "Merge Options"
|
||||
optionProjects = "Projects"
|
||||
optionTemplateRepo = "Template Repository"
|
||||
optionTopics = "Topics"
|
||||
optionVisibility = "Visibility"
|
||||
optionWikis = "Wikis"
|
||||
)
|
||||
|
||||
type EditOptions struct {
|
||||
HTTPClient *http.Client
|
||||
Repository ghrepo.Interface
|
||||
Edits EditRepositoryInput
|
||||
AddTopics []string
|
||||
RemoveTopics []string
|
||||
HTTPClient *http.Client
|
||||
Repository ghrepo.Interface
|
||||
IO *iostreams.IOStreams
|
||||
Edits EditRepositoryInput
|
||||
AddTopics []string
|
||||
RemoveTopics []string
|
||||
InteractiveMode bool
|
||||
// Cache of current repo topics to avoid retrieving them
|
||||
// in multiple flows.
|
||||
topicsCache []string
|
||||
}
|
||||
|
||||
type EditRepositoryInput struct {
|
||||
Description *string `json:"description,omitempty"`
|
||||
Homepage *string `json:"homepage,omitempty"`
|
||||
Visibility *string `json:"visibility,omitempty"`
|
||||
EnableIssues *bool `json:"has_issues,omitempty"`
|
||||
EnableProjects *bool `json:"has_projects,omitempty"`
|
||||
EnableWiki *bool `json:"has_wiki,omitempty"`
|
||||
IsTemplate *bool `json:"is_template,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"`
|
||||
EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"`
|
||||
EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
|
||||
EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"`
|
||||
DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"`
|
||||
AllowForking *bool `json:"allow_forking,omitempty"`
|
||||
DefaultBranch *string `json:"default_branch,omitempty"`
|
||||
DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
EnableAutoMerge *bool `json:"allow_auto_merge,omitempty"`
|
||||
EnableIssues *bool `json:"has_issues,omitempty"`
|
||||
EnableMergeCommit *bool `json:"allow_merge_commit,omitempty"`
|
||||
EnableProjects *bool `json:"has_projects,omitempty"`
|
||||
EnableRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
|
||||
EnableSquashMerge *bool `json:"allow_squash_merge,omitempty"`
|
||||
EnableWiki *bool `json:"has_wiki,omitempty"`
|
||||
Homepage *string `json:"homepage,omitempty"`
|
||||
IsTemplate *bool `json:"is_template,omitempty"`
|
||||
Visibility *string `json:"visibility,omitempty"`
|
||||
}
|
||||
|
||||
func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobra.Command {
|
||||
opts := &EditOptions{}
|
||||
opts := &EditOptions{
|
||||
IO: f.IOStreams,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit [<repository>]",
|
||||
|
|
@ -57,12 +86,20 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr
|
|||
- by URL, e.g. "https://github.com/OWNER/REPO"
|
||||
`),
|
||||
},
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if cmd.Flags().NFlag() == 0 {
|
||||
return cmdutil.FlagErrorf("at least one flag is required")
|
||||
}
|
||||
Long: heredoc.Docf(`
|
||||
Edit repository settings.
|
||||
|
||||
To toggle a setting off, use the %[1]s--flag=false%[1]s syntax.
|
||||
`, "`"),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Example: heredoc.Doc(`
|
||||
# enable issues and wiki
|
||||
gh repo edit --enable-issues --enable-wiki
|
||||
|
||||
# disable projects
|
||||
gh repo edit --enable-projects=false
|
||||
`),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) > 0 {
|
||||
var err error
|
||||
opts.Repository, err = ghrepo.FromFullName(args[0])
|
||||
|
|
@ -83,6 +120,14 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr
|
|||
return err
|
||||
}
|
||||
|
||||
if cmd.Flags().NFlag() == 0 {
|
||||
opts.InteractiveMode = true
|
||||
}
|
||||
|
||||
if opts.InteractiveMode && !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("specify properties to edit when not running interactively")
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
|
|
@ -112,6 +157,38 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(options *EditOptions) error) *cobr
|
|||
|
||||
func editRun(ctx context.Context, opts *EditOptions) error {
|
||||
repo := opts.Repository
|
||||
|
||||
if opts.InteractiveMode {
|
||||
apiClient := api.NewClientFromHTTP(opts.HTTPClient)
|
||||
fieldsToRetrieve := []string{
|
||||
"autoMergeAllowed",
|
||||
"defaultBranchRef",
|
||||
"deleteBranchOnMerge",
|
||||
"description",
|
||||
"hasIssuesEnabled",
|
||||
"hasProjectsEnabled",
|
||||
"hasWikiEnabled",
|
||||
"homepageUrl",
|
||||
"isInOrganization",
|
||||
"isTemplate",
|
||||
"mergeCommitAllowed",
|
||||
"rebaseMergeAllowed",
|
||||
"repositoryTopics",
|
||||
"squashMergeAllowed",
|
||||
"visibility",
|
||||
}
|
||||
opts.IO.StartProgressIndicator()
|
||||
fetchedRepo, err := api.FetchRepository(apiClient, opts.Repository, fieldsToRetrieve)
|
||||
opts.IO.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = interactiveRepoEdit(opts, fetchedRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("repos/%s/%s", repo.RepoOwner(), repo.RepoName())
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
|
|
@ -132,16 +209,19 @@ func editRun(ctx context.Context, opts *EditOptions) error {
|
|||
|
||||
if len(opts.AddTopics) > 0 || len(opts.RemoveTopics) > 0 {
|
||||
g.Go(func() error {
|
||||
existingTopics, err := getTopics(ctx, opts.HTTPClient, repo)
|
||||
if err != nil {
|
||||
return err
|
||||
// opts.topicsCache gets populated in interactive mode
|
||||
if !opts.InteractiveMode {
|
||||
var err error
|
||||
opts.topicsCache, err = getTopics(ctx, opts.HTTPClient, repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
oldTopics := set.NewStringSet()
|
||||
oldTopics.AddValues(existingTopics)
|
||||
oldTopics.AddValues(opts.topicsCache)
|
||||
|
||||
newTopics := set.NewStringSet()
|
||||
newTopics.AddValues(existingTopics)
|
||||
newTopics.AddValues(opts.topicsCache)
|
||||
newTopics.AddValues(opts.AddTopics)
|
||||
newTopics.RemoveValues(opts.RemoveTopics)
|
||||
|
||||
|
|
@ -152,7 +232,209 @@ func editRun(ctx context.Context, opts *EditOptions) error {
|
|||
})
|
||||
}
|
||||
|
||||
return g.Wait()
|
||||
err := g.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
cs := opts.IO.ColorScheme()
|
||||
fmt.Fprintf(opts.IO.Out,
|
||||
"%s Edited repository %s\n",
|
||||
cs.SuccessIcon(),
|
||||
ghrepo.FullName(repo))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func interactiveChoice(r *api.Repository) ([]string, error) {
|
||||
options := []string{
|
||||
optionDefaultBranchName,
|
||||
optionDescription,
|
||||
optionHomePageURL,
|
||||
optionIssues,
|
||||
optionMergeOptions,
|
||||
optionProjects,
|
||||
optionTemplateRepo,
|
||||
optionTopics,
|
||||
optionVisibility,
|
||||
optionWikis,
|
||||
}
|
||||
if r.IsInOrganization {
|
||||
options = append(options, optionAllowForking)
|
||||
}
|
||||
var answers []string
|
||||
err := prompt.SurveyAskOne(&survey.MultiSelect{
|
||||
Message: "What do you want to edit?",
|
||||
Options: options,
|
||||
}, &answers, survey.WithPageSize(11))
|
||||
return answers, err
|
||||
}
|
||||
|
||||
func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
|
||||
for _, v := range r.RepositoryTopics.Nodes {
|
||||
opts.topicsCache = append(opts.topicsCache, v.Topic.Name)
|
||||
}
|
||||
choices, err := interactiveChoice(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range choices {
|
||||
switch c {
|
||||
case optionDescription:
|
||||
opts.Edits.Description = &r.Description
|
||||
err = prompt.SurveyAskOne(&survey.Input{
|
||||
Message: "Description of the repository",
|
||||
Default: r.Description,
|
||||
}, opts.Edits.Description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionHomePageURL:
|
||||
opts.Edits.Homepage = &r.HomepageURL
|
||||
err = prompt.SurveyAskOne(&survey.Input{
|
||||
Message: "Repository home page URL",
|
||||
Default: r.HomepageURL,
|
||||
}, opts.Edits.Homepage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionTopics:
|
||||
var addTopics string
|
||||
err = prompt.SurveyAskOne(&survey.Input{
|
||||
Message: "Add topics?(csv format)",
|
||||
}, &addTopics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(strings.TrimSpace(addTopics)) > 0 {
|
||||
opts.AddTopics = strings.Split(addTopics, ",")
|
||||
}
|
||||
|
||||
if len(opts.topicsCache) > 0 {
|
||||
err = prompt.SurveyAskOne(&survey.MultiSelect{
|
||||
Message: "Remove Topics",
|
||||
Options: opts.topicsCache,
|
||||
}, &opts.RemoveTopics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case optionDefaultBranchName:
|
||||
opts.Edits.DefaultBranch = &r.DefaultBranchRef.Name
|
||||
err = prompt.SurveyAskOne(&survey.Input{
|
||||
Message: "Default branch name",
|
||||
Default: r.DefaultBranchRef.Name,
|
||||
}, opts.Edits.DefaultBranch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionWikis:
|
||||
opts.Edits.EnableWiki = &r.HasWikiEnabled
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Enable Wikis?",
|
||||
Default: r.HasWikiEnabled,
|
||||
}, opts.Edits.EnableWiki)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionIssues:
|
||||
opts.Edits.EnableIssues = &r.HasIssuesEnabled
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Enable Issues?",
|
||||
Default: r.HasIssuesEnabled,
|
||||
}, opts.Edits.EnableIssues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionProjects:
|
||||
opts.Edits.EnableProjects = &r.HasProjectsEnabled
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Enable Projects?",
|
||||
Default: r.HasProjectsEnabled,
|
||||
}, opts.Edits.EnableProjects)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionVisibility:
|
||||
opts.Edits.Visibility = &r.Visibility
|
||||
err = prompt.SurveyAskOne(&survey.Select{
|
||||
Message: "Visibility",
|
||||
Options: []string{"public", "private", "internal"},
|
||||
Default: strings.ToLower(r.Visibility),
|
||||
}, opts.Edits.Visibility)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionMergeOptions:
|
||||
var defaultMergeOptions []string
|
||||
var selectedMergeOptions []string
|
||||
if r.MergeCommitAllowed {
|
||||
defaultMergeOptions = append(defaultMergeOptions, allowMergeCommits)
|
||||
}
|
||||
if r.SquashMergeAllowed {
|
||||
defaultMergeOptions = append(defaultMergeOptions, allowSquashMerge)
|
||||
}
|
||||
if r.RebaseMergeAllowed {
|
||||
defaultMergeOptions = append(defaultMergeOptions, allowRebaseMerge)
|
||||
}
|
||||
err = prompt.SurveyAskOne(&survey.MultiSelect{
|
||||
Message: "Allowed merge strategies",
|
||||
Default: defaultMergeOptions,
|
||||
Options: []string{allowMergeCommits, allowSquashMerge, allowRebaseMerge},
|
||||
}, &selectedMergeOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enableMergeCommit := isIncluded(allowMergeCommits, selectedMergeOptions)
|
||||
opts.Edits.EnableMergeCommit = &enableMergeCommit
|
||||
enableSquashMerge := isIncluded(allowSquashMerge, selectedMergeOptions)
|
||||
opts.Edits.EnableSquashMerge = &enableSquashMerge
|
||||
enableRebaseMerge := isIncluded(allowRebaseMerge, selectedMergeOptions)
|
||||
opts.Edits.EnableRebaseMerge = &enableRebaseMerge
|
||||
if !enableMergeCommit && !enableSquashMerge && !enableRebaseMerge {
|
||||
return fmt.Errorf("you need to allow at least one merge strategy")
|
||||
}
|
||||
|
||||
opts.Edits.EnableAutoMerge = &r.AutoMergeAllowed
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Enable Auto Merge?",
|
||||
Default: r.AutoMergeAllowed,
|
||||
}, opts.Edits.EnableAutoMerge)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.Edits.DeleteBranchOnMerge = &r.DeleteBranchOnMerge
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Automatically delete head branches after merging?",
|
||||
Default: r.DeleteBranchOnMerge,
|
||||
}, opts.Edits.DeleteBranchOnMerge)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionTemplateRepo:
|
||||
opts.Edits.IsTemplate = &r.IsTemplate
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Convert into a template repository?",
|
||||
Default: r.IsTemplate,
|
||||
}, opts.Edits.IsTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case optionAllowForking:
|
||||
opts.Edits.AllowForking = &r.ForkingAllowed
|
||||
err = prompt.SurveyAskOne(&survey.Confirm{
|
||||
Message: "Allow forking (of an organization repository)?",
|
||||
Default: r.ForkingAllowed,
|
||||
}, opts.Edits.AllowForking)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTopics(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface) ([]string, error) {
|
||||
|
|
@ -216,3 +498,12 @@ func setTopics(ctx context.Context, httpClient *http.Client, repo ghrepo.Interfa
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isIncluded(value string, opts []string) bool {
|
||||
for _, opt := range opts {
|
||||
if strings.EqualFold(opt, value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import (
|
|||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/prompt"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/httpmock"
|
||||
|
|
@ -23,11 +25,6 @@ func TestNewCmdEdit(t *testing.T) {
|
|||
wantOpts EditOptions
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "no argument",
|
||||
args: "",
|
||||
wantErr: "at least one flag is required",
|
||||
},
|
||||
{
|
||||
name: "change repo description",
|
||||
args: "--description hello",
|
||||
|
|
@ -135,6 +132,11 @@ func Test_editRun(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, _, _, _ := iostreams.Test()
|
||||
io.SetStdoutTTY(true)
|
||||
io.SetStdinTTY(true)
|
||||
io.SetStderrTTY(true)
|
||||
|
||||
httpReg := &httpmock.Registry{}
|
||||
defer httpReg.Verify(t)
|
||||
if tt.httpStubs != nil {
|
||||
|
|
@ -143,6 +145,186 @@ func Test_editRun(t *testing.T) {
|
|||
|
||||
opts := &tt.opts
|
||||
opts.HTTPClient = &http.Client{Transport: httpReg}
|
||||
opts.IO = io
|
||||
|
||||
err := editRun(context.Background(), opts)
|
||||
if tt.wantsErr == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
assert.EqualError(t, err, tt.wantsErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_editRun_interactive(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
opts EditOptions
|
||||
askStubs func(*prompt.AskStubber)
|
||||
httpStubs func(*testing.T, *httpmock.Registry)
|
||||
wantsStderr string
|
||||
wantsErr string
|
||||
}{
|
||||
{
|
||||
name: "updates repo description",
|
||||
opts: EditOptions{
|
||||
Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"),
|
||||
InteractiveMode: true,
|
||||
},
|
||||
askStubs: func(as *prompt.AskStubber) {
|
||||
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Description"})
|
||||
as.StubPrompt("Description of the repository").AnswerWith("awesome repo description")
|
||||
},
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`
|
||||
{
|
||||
"data": {
|
||||
"repository": {
|
||||
"description": "old description",
|
||||
"homePageUrl": "https://url.com",
|
||||
"defaultBranchRef": {
|
||||
"name": "main"
|
||||
},
|
||||
"isInOrganization": false,
|
||||
"repositoryTopics": {
|
||||
"nodes": [{
|
||||
"topic": {
|
||||
"name": "x"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`))
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.RESTPayload(200, `{}`, func(payload map[string]interface{}) {
|
||||
assert.Equal(t, "awesome repo description", payload["description"])
|
||||
}))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "updates repo topics",
|
||||
opts: EditOptions{
|
||||
Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"),
|
||||
InteractiveMode: true,
|
||||
},
|
||||
askStubs: func(as *prompt.AskStubber) {
|
||||
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Description", "Topics"})
|
||||
as.StubPrompt("Description of the repository").AnswerWith("awesome repo description")
|
||||
as.StubPrompt("Add topics?(csv format)").AnswerWith("a,b,c,d")
|
||||
as.StubPrompt("Remove Topics").AnswerWith([]string{"x"})
|
||||
},
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`
|
||||
{
|
||||
"data": {
|
||||
"repository": {
|
||||
"description": "old description",
|
||||
"homePageUrl": "https://url.com",
|
||||
"defaultBranchRef": {
|
||||
"name": "main"
|
||||
},
|
||||
"isInOrganization": false,
|
||||
"repositoryTopics": {
|
||||
"nodes": [{
|
||||
"topic": {
|
||||
"name": "x"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`))
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.RESTPayload(200, `{}`, func(payload map[string]interface{}) {
|
||||
assert.Equal(t, "awesome repo description", payload["description"])
|
||||
}))
|
||||
reg.Register(
|
||||
httpmock.REST("PUT", "repos/OWNER/REPO/topics"),
|
||||
httpmock.RESTPayload(200, `{}`, func(payload map[string]interface{}) {
|
||||
assert.Equal(t, []interface{}{"a", "b", "c", "d"}, payload["names"])
|
||||
}))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "updates repo merge options",
|
||||
opts: EditOptions{
|
||||
Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"),
|
||||
InteractiveMode: true,
|
||||
},
|
||||
askStubs: func(as *prompt.AskStubber) {
|
||||
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Merge Options"})
|
||||
as.StubPrompt("Allowed merge strategies").AnswerWith([]string{allowMergeCommits, allowRebaseMerge})
|
||||
as.StubPrompt("Enable Auto Merge?").AnswerWith(false)
|
||||
as.StubPrompt("Automatically delete head branches after merging?").AnswerWith(false)
|
||||
},
|
||||
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query RepositoryInfo\b`),
|
||||
httpmock.StringResponse(`
|
||||
{
|
||||
"data": {
|
||||
"repository": {
|
||||
"description": "old description",
|
||||
"homePageUrl": "https://url.com",
|
||||
"defaultBranchRef": {
|
||||
"name": "main"
|
||||
},
|
||||
"isInOrganization": false,
|
||||
"squashMergeAllowed": false,
|
||||
"rebaseMergeAllowed": true,
|
||||
"mergeCommitAllowed": true,
|
||||
"deleteBranchOnMerge": false,
|
||||
"repositoryTopics": {
|
||||
"nodes": [{
|
||||
"topic": {
|
||||
"name": "x"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`))
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.RESTPayload(200, `{}`, func(payload map[string]interface{}) {
|
||||
assert.Equal(t, true, payload["allow_merge_commit"])
|
||||
assert.Equal(t, false, payload["allow_squash_merge"])
|
||||
assert.Equal(t, true, payload["allow_rebase_merge"])
|
||||
}))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, _, _, _ := iostreams.Test()
|
||||
io.SetStdoutTTY(true)
|
||||
io.SetStdinTTY(true)
|
||||
io.SetStderrTTY(true)
|
||||
|
||||
httpReg := &httpmock.Registry{}
|
||||
defer httpReg.Verify(t)
|
||||
if tt.httpStubs != nil {
|
||||
tt.httpStubs(t, httpReg)
|
||||
}
|
||||
|
||||
opts := &tt.opts
|
||||
opts.HTTPClient = &http.Client{Transport: httpReg}
|
||||
opts.IO = io
|
||||
|
||||
as := prompt.NewAskStubber(t)
|
||||
if tt.askStubs != nil {
|
||||
tt.askStubs(as)
|
||||
}
|
||||
|
||||
err := editRun(context.Background(), opts)
|
||||
if tt.wantsErr == "" {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/context"
|
||||
"github.com/cli/cli/v2/git"
|
||||
|
|
@ -39,6 +40,7 @@ type ForkOptions struct {
|
|||
PromptRemote bool
|
||||
RemoteName string
|
||||
Organization string
|
||||
ForkName string
|
||||
Rename bool
|
||||
}
|
||||
|
||||
|
|
@ -60,21 +62,23 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman
|
|||
Use: "fork [<repository>] [-- <gitflags>...]",
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if cmd.ArgsLenAtDash() == 0 && len(args[1:]) > 0 {
|
||||
return cmdutil.FlagErrorf("repository argument required when passing 'git clone' flags")
|
||||
return cmdutil.FlagErrorf("repository argument required when passing git clone flags")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Short: "Create a fork of a repository",
|
||||
Long: `Create a fork of a repository.
|
||||
Long: heredoc.Docf(`
|
||||
Create a fork of a repository.
|
||||
|
||||
With no argument, creates a fork of the current repository. Otherwise, forks
|
||||
the specified repository.
|
||||
With no argument, creates a fork of the current repository. Otherwise, forks
|
||||
the specified repository.
|
||||
|
||||
By default, the new fork is set to be your 'origin' remote and any existing
|
||||
origin remote is renamed to 'upstream'. To alter this behavior, you can set
|
||||
a name for the new fork's remote with --remote-name.
|
||||
By default, the new fork is set to be your "origin" remote and any existing
|
||||
origin remote is renamed to "upstream". To alter this behavior, you can set
|
||||
a name for the new fork's remote with %[1]s--remote-name%[1]s.
|
||||
|
||||
Additional 'git clone' flags can be passed in by listing them after '--'.`,
|
||||
Additional git clone flags can be passed after %[1]s--%[1]s.
|
||||
`, "`"),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
promptOk := opts.IO.CanPrompt()
|
||||
if len(args) > 0 {
|
||||
|
|
@ -108,13 +112,14 @@ Additional 'git clone' flags can be passed in by listing them after '--'.`,
|
|||
if err == pflag.ErrHelp {
|
||||
return err
|
||||
}
|
||||
return cmdutil.FlagErrorf("%w\nSeparate git clone flags with '--'.", err)
|
||||
return cmdutil.FlagErrorf("%w\nSeparate git clone flags with `--`.", err)
|
||||
})
|
||||
|
||||
cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork {true|false}")
|
||||
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}")
|
||||
cmd.Flags().StringVar(&opts.RemoteName, "remote-name", defaultRemoteName, "Specify a name for a fork's new remote.")
|
||||
cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork")
|
||||
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add a git remote for the fork")
|
||||
cmd.Flags().StringVar(&opts.RemoteName, "remote-name", defaultRemoteName, "Specify the name for the new remote")
|
||||
cmd.Flags().StringVar(&opts.Organization, "org", "", "Create the fork in an organization")
|
||||
cmd.Flags().StringVar(&opts.ForkName, "fork-name", "", "Rename the forked repository")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -201,6 +206,17 @@ func forkRun(opts *ForkOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Rename the forked repo if ForkName is specified in opts.
|
||||
if opts.ForkName != "" {
|
||||
forkedRepo, err = api.RenameRepo(apiClient, forkedRepo, opts.ForkName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not rename fork: %w", err)
|
||||
}
|
||||
if connectedToTerminal {
|
||||
fmt.Fprintf(stderr, "%s Renamed fork to %s\n", cs.SuccessIconWithColor(cs.Green), cs.Bold(ghrepo.FullName(forkedRepo)))
|
||||
}
|
||||
}
|
||||
|
||||
if (inParent && (!opts.Remote && !opts.PromptRemote)) || (!inParent && (!opts.Clone && !opts.PromptClone)) {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -209,7 +225,7 @@ func forkRun(opts *ForkOptions) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
protocol, err := cfg.GetOrDefault(repoToFork.RepoHost(), "git_protocol")
|
||||
protocol, err := cfg.Get(repoToFork.RepoHost(), "git_protocol")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -220,17 +236,20 @@ func forkRun(opts *ForkOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if remote, err := remotes.FindByRepo(repoToFork.RepoOwner(), repoToFork.RepoName()); err == nil {
|
||||
|
||||
scheme := ""
|
||||
if remote.FetchURL != nil {
|
||||
scheme = remote.FetchURL.Scheme
|
||||
}
|
||||
if remote.PushURL != nil {
|
||||
scheme = remote.PushURL.Scheme
|
||||
}
|
||||
if scheme != "" {
|
||||
protocol = scheme
|
||||
if protocol == "" { // user has no set preference
|
||||
if remote, err := remotes.FindByRepo(repoToFork.RepoOwner(), repoToFork.RepoName()); err == nil {
|
||||
scheme := ""
|
||||
if remote.FetchURL != nil {
|
||||
scheme = remote.FetchURL.Scheme
|
||||
}
|
||||
if remote.PushURL != nil {
|
||||
scheme = remote.PushURL.Scheme
|
||||
}
|
||||
if scheme != "" {
|
||||
protocol = scheme
|
||||
} else {
|
||||
protocol = cfg.Default("git_protocol")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ func TestNewCmdFork(t *testing.T) {
|
|||
name: "git args without repo",
|
||||
cli: "-- --foo bar",
|
||||
wantErr: true,
|
||||
errMsg: "repository argument required when passing 'git clone' flags",
|
||||
errMsg: "repository argument required when passing git clone flags",
|
||||
},
|
||||
{
|
||||
name: "repo",
|
||||
|
|
@ -130,7 +130,17 @@ func TestNewCmdFork(t *testing.T) {
|
|||
name: "git flags in wrong place",
|
||||
cli: "--depth 1 OWNER/REPO",
|
||||
wantErr: true,
|
||||
errMsg: "unknown flag: --depth\nSeparate git clone flags with '--'.",
|
||||
errMsg: "unknown flag: --depth\nSeparate git clone flags with `--`.",
|
||||
},
|
||||
{
|
||||
name: "with fork name",
|
||||
cli: "--fork-name new-fork",
|
||||
wants: ForkOptions{
|
||||
Remote: false,
|
||||
RemoteName: "origin",
|
||||
ForkName: "new-fork",
|
||||
Rename: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -198,15 +208,15 @@ func TestRepoFork(t *testing.T) {
|
|||
httpStubs func(*httpmock.Registry)
|
||||
execStubs func(*run.CommandStubber)
|
||||
askStubs func(*prompt.AskStubber)
|
||||
cfg func(config.Config) config.Config
|
||||
remotes []*context.Remote
|
||||
wantOut string
|
||||
wantErrOut string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
}{
|
||||
// TODO implicit, override existing remote's protocol with configured protocol
|
||||
{
|
||||
name: "implicit match existing remote's protocol",
|
||||
name: "implicit match, configured protocol overrides provided",
|
||||
tty: true,
|
||||
opts: &ForkOptions{
|
||||
Remote: true,
|
||||
|
|
@ -221,6 +231,31 @@ func TestRepoFork(t *testing.T) {
|
|||
},
|
||||
},
|
||||
httpStubs: forkPost,
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote add -f fork https://github\.com/someone/REPO\.git`, 0, "")
|
||||
},
|
||||
wantErrOut: "✓ Created fork someone/REPO\n✓ Added remote fork\n",
|
||||
},
|
||||
{
|
||||
name: "implicit match, no configured protocol",
|
||||
tty: true,
|
||||
opts: &ForkOptions{
|
||||
Remote: true,
|
||||
RemoteName: "fork",
|
||||
},
|
||||
remotes: []*context.Remote{
|
||||
{
|
||||
Remote: &git.Remote{Name: "origin", PushURL: &url.URL{
|
||||
Scheme: "ssh",
|
||||
}},
|
||||
Repo: ghrepo.New("OWNER", "REPO"),
|
||||
},
|
||||
},
|
||||
cfg: func(c config.Config) config.Config {
|
||||
_ = c.Set("", "git_protocol", "")
|
||||
return c
|
||||
},
|
||||
httpStubs: forkPost,
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote add -f fork git@github\.com:someone/REPO\.git`, 0, "")
|
||||
},
|
||||
|
|
@ -534,6 +569,78 @@ func TestRepoFork(t *testing.T) {
|
|||
cs.Register(`git -C REPO remote add -f upstream https://github\.com/OWNER/REPO\.git`, 0, "")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "non tty repo arg with fork-name",
|
||||
opts: &ForkOptions{
|
||||
Repository: "someone/REPO",
|
||||
Clone: false,
|
||||
ForkName: "NEW_REPO",
|
||||
},
|
||||
tty: false,
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
forkResult := `{
|
||||
"node_id": "123",
|
||||
"name": "REPO",
|
||||
"clone_url": "https://github.com/OWNER/REPO.git",
|
||||
"created_at": "2011-01-26T19:01:12Z",
|
||||
"owner": {
|
||||
"login": "OWNER"
|
||||
}
|
||||
}`
|
||||
renameResult := `{
|
||||
"node_id": "1234",
|
||||
"name": "NEW_REPO",
|
||||
"clone_url": "https://github.com/OWNER/NEW_REPO.git",
|
||||
"created_at": "2012-01-26T19:01:12Z",
|
||||
"owner": {
|
||||
"login": "OWNER"
|
||||
}
|
||||
}`
|
||||
reg.Register(
|
||||
httpmock.REST("POST", "repos/someone/REPO/forks"),
|
||||
httpmock.StringResponse(forkResult))
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StringResponse(renameResult))
|
||||
},
|
||||
wantErrOut: "",
|
||||
},
|
||||
{
|
||||
name: "tty repo arg with fork-name",
|
||||
opts: &ForkOptions{
|
||||
Repository: "someone/REPO",
|
||||
Clone: false,
|
||||
ForkName: "NEW_REPO",
|
||||
},
|
||||
tty: true,
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
forkResult := `{
|
||||
"node_id": "123",
|
||||
"name": "REPO",
|
||||
"clone_url": "https://github.com/OWNER/REPO.git",
|
||||
"created_at": "2011-01-26T19:01:12Z",
|
||||
"owner": {
|
||||
"login": "OWNER"
|
||||
}
|
||||
}`
|
||||
renameResult := `{
|
||||
"node_id": "1234",
|
||||
"name": "NEW_REPO",
|
||||
"clone_url": "https://github.com/OWNER/NEW_REPO.git",
|
||||
"created_at": "2012-01-26T19:01:12Z",
|
||||
"owner": {
|
||||
"login": "OWNER"
|
||||
}
|
||||
}`
|
||||
reg.Register(
|
||||
httpmock.REST("POST", "repos/someone/REPO/forks"),
|
||||
httpmock.StringResponse(forkResult))
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StringResponse(renameResult))
|
||||
},
|
||||
wantErrOut: "✓ Created fork OWNER/REPO\n✓ Renamed fork to OWNER/NEW_REPO\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -556,6 +663,9 @@ func TestRepoFork(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := config.NewBlankConfig()
|
||||
if tt.cfg != nil {
|
||||
cfg = tt.cfg(cfg)
|
||||
}
|
||||
tt.opts.Config = func() (config.Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
package rename
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
func apiRename(client *http.Client, repo ghrepo.Interface, newRepoName string) (ghrepo.Interface, error) {
|
||||
input := map[string]string{"name": newRepoName}
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%srepos/%s",
|
||||
ghinstance.RESTPrefix(repo.RepoHost()),
|
||||
ghrepo.FullName(repo))
|
||||
|
||||
request, err := http.NewRequest("PATCH", path, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode > 299 {
|
||||
return nil, api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := struct {
|
||||
Name string
|
||||
Owner struct {
|
||||
Login string
|
||||
}
|
||||
}{}
|
||||
if err := json.Unmarshal(b, &result); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshaling response: %w", err)
|
||||
}
|
||||
|
||||
newRepo := ghrepo.NewWithHost(result.Owner.Login, result.Name, repo.RepoHost())
|
||||
|
||||
return newRepo, nil
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/context"
|
||||
"github.com/cli/cli/v2/git"
|
||||
"github.com/cli/cli/v2/internal/config"
|
||||
|
|
@ -40,7 +41,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
|
|||
cmd := &cobra.Command{
|
||||
Use: "rename [<new-name>]",
|
||||
Short: "Rename a repository",
|
||||
Long: heredoc.Doc(`Rename a GitHub repository
|
||||
Long: heredoc.Doc(`Rename a GitHub repository.
|
||||
|
||||
By default, this renames the current repository; otherwise renames the specified repository.`),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
|
|
@ -114,11 +115,15 @@ func renameRun(opts *RenameOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
newRepo, err := apiRename(httpClient, currRepo, newRepoName)
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
|
||||
newRepo, err := api.RenameRepo(apiClient, currRepo, newRepoName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
renamedRepo := ghrepo.New(newRepo.Owner.Login, newRepo.Name)
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "%s Renamed repository %s\n", cs.SuccessIcon(), ghrepo.FullName(newRepo))
|
||||
|
|
@ -128,7 +133,7 @@ func renameRun(opts *RenameOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
remote, err := updateRemote(currRepo, newRepo, opts)
|
||||
remote, err := updateRemote(currRepo, renamedRepo, opts)
|
||||
if err != nil {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "%s Warning: unable to update remote %q: %v\n", cs.WarningIcon(), remote.Name, err)
|
||||
} else if opts.IO.IsStdoutTTY() {
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ func TestRenameRun(t *testing.T) {
|
|||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
httpmock.StatusStringResponse(200, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
},
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
|
|
@ -143,7 +143,7 @@ func TestRenameRun(t *testing.T) {
|
|||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
httpmock.StatusStringResponse(200, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
},
|
||||
tty: true,
|
||||
},
|
||||
|
|
@ -156,7 +156,7 @@ func TestRenameRun(t *testing.T) {
|
|||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
httpmock.StatusStringResponse(200, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
},
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
|
|
@ -171,7 +171,7 @@ func TestRenameRun(t *testing.T) {
|
|||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
httpmock.StatusStringResponse(200, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
},
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
|
|
@ -192,7 +192,7 @@ func TestRenameRun(t *testing.T) {
|
|||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
httpmock.StatusStringResponse(200, `{"name":"NEW_REPO","owner":{"login":"OWNER"}}`))
|
||||
},
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
repoCreateCmd "github.com/cli/cli/v2/pkg/cmd/repo/create"
|
||||
creditsCmd "github.com/cli/cli/v2/pkg/cmd/repo/credits"
|
||||
repoDeleteCmd "github.com/cli/cli/v2/pkg/cmd/repo/delete"
|
||||
deployKeyCmd "github.com/cli/cli/v2/pkg/cmd/repo/deploy-key"
|
||||
repoEditCmd "github.com/cli/cli/v2/pkg/cmd/repo/edit"
|
||||
repoForkCmd "github.com/cli/cli/v2/pkg/cmd/repo/fork"
|
||||
gardenCmd "github.com/cli/cli/v2/pkg/cmd/repo/garden"
|
||||
|
|
@ -21,8 +22,8 @@ import (
|
|||
func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "repo <command>",
|
||||
Short: "Create, clone, fork, and view repositories",
|
||||
Long: `Work with GitHub repositories`,
|
||||
Short: "Manage repositories",
|
||||
Long: `Work with GitHub repositories.`,
|
||||
Example: heredoc.Doc(`
|
||||
$ gh repo create
|
||||
$ gh repo clone cli/cli
|
||||
|
|
@ -47,6 +48,7 @@ func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
|
|||
cmd.AddCommand(repoSyncCmd.NewCmdSync(f, nil))
|
||||
cmd.AddCommand(creditsCmd.NewCmdRepoCredits(f, nil))
|
||||
cmd.AddCommand(gardenCmd.NewCmdGarden(f, nil))
|
||||
cmd.AddCommand(deployKeyCmd.NewCmdDeployKey(f))
|
||||
cmd.AddCommand(repoRenameCmd.NewCmdRename(f, nil))
|
||||
cmd.AddCommand(repoDeleteCmd.NewCmdDelete(f, nil))
|
||||
cmd.AddCommand(repoArchiveCmd.NewCmdArchive(f, nil))
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ func NewCmdSync(f *cmdutil.Factory, runF func(*SyncOptions) error) *cobra.Comman
|
|||
Long: heredoc.Docf(`
|
||||
Sync destination repository from source repository. Syncing uses the main branch
|
||||
of the source repository to update the matching branch on the destination
|
||||
repository so they are equal. A fast forward update will be used execept when the
|
||||
repository so they are equal. A fast forward update will be used except when the
|
||||
%[1]s--force%[1]s flag is specified, then the two branches will
|
||||
by synced using a hard reset.
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"syscall"
|
||||
"text/template"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
|
|
@ -142,10 +141,11 @@ func viewRun(opts *ViewOptions) error {
|
|||
}
|
||||
|
||||
opts.IO.DetectTerminalTheme()
|
||||
if err := opts.IO.StartPager(); err != nil {
|
||||
return err
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
defer opts.IO.StopPager()
|
||||
|
||||
if opts.Exporter != nil {
|
||||
return opts.Exporter.Write(opts.IO, repo)
|
||||
|
|
@ -212,12 +212,7 @@ func viewRun(opts *ViewOptions) error {
|
|||
View: cs.Gray(fmt.Sprintf("View this repository on GitHub: %s", openURL)),
|
||||
}
|
||||
|
||||
err = tmpl.Execute(stdout, repoData)
|
||||
if err != nil && !errors.Is(err, syscall.EPIPE) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return tmpl.Execute(stdout, repoData)
|
||||
}
|
||||
|
||||
func isMarkdownFile(filename string) bool {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package root
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
|
|
@ -88,6 +89,7 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) {
|
|||
return
|
||||
}
|
||||
|
||||
namePadding := 12
|
||||
coreCommands := []string{}
|
||||
actionsCommands := []string{}
|
||||
additionalCommands := []string{}
|
||||
|
|
@ -99,7 +101,7 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) {
|
|||
continue
|
||||
}
|
||||
|
||||
s := rpad(c.Name()+":", c.NamePadding()) + c.Short
|
||||
s := rpad(c.Name()+":", namePadding) + c.Short
|
||||
if _, ok := c.Annotations["IsCore"]; ok {
|
||||
coreCommands = append(coreCommands, s)
|
||||
} else if _, ok := c.Annotations["IsActions"]; ok {
|
||||
|
|
@ -145,6 +147,16 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
if isRootCmd(command) {
|
||||
var helpTopics []string
|
||||
if c := findCommand(command, "actions"); c != nil {
|
||||
helpTopics = append(helpTopics, rpad(c.Name()+":", namePadding)+c.Short)
|
||||
}
|
||||
for topic, params := range HelpTopics {
|
||||
helpTopics = append(helpTopics, rpad(topic+":", namePadding)+params["short"])
|
||||
}
|
||||
sort.Strings(helpTopics)
|
||||
helpEntries = append(helpEntries, helpEntry{"HELP TOPICS", strings.Join(helpTopics, "\n")})
|
||||
|
||||
if exts := f.ExtensionManager.List(false); len(exts) > 0 {
|
||||
var names []string
|
||||
for _, ext := range exts {
|
||||
|
|
@ -192,6 +204,15 @@ Read the manual at https://cli.github.com/manual`})
|
|||
}
|
||||
}
|
||||
|
||||
func findCommand(cmd *cobra.Command, name string) *cobra.Command {
|
||||
for _, c := range cmd.Commands() {
|
||||
if c.Name() == name {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rpad adds padding to the right of a string.
|
||||
func rpad(s string, padding int) string {
|
||||
template := fmt.Sprintf("%%-%ds ", padding)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ var HelpTopics = map[string]map[string]string{
|
|||
"short": "Information about using gh with MinTTY",
|
||||
"long": heredoc.Doc(`
|
||||
MinTTY is the terminal emulator that comes by default with Git
|
||||
for Windows. It has known issues with gh's ability to prompt a
|
||||
for Windows. It has known issues with gh's ability to prompt a
|
||||
user for input.
|
||||
|
||||
There are a few workarounds to make gh work with MinTTY:
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
repoCmd "github.com/cli/cli/v2/pkg/cmd/repo"
|
||||
creditsCmd "github.com/cli/cli/v2/pkg/cmd/repo/credits"
|
||||
runCmd "github.com/cli/cli/v2/pkg/cmd/run"
|
||||
searchCmd "github.com/cli/cli/v2/pkg/cmd/search"
|
||||
secretCmd "github.com/cli/cli/v2/pkg/cmd/secret"
|
||||
sshKeyCmd "github.com/cli/cli/v2/pkg/cmd/ssh-key"
|
||||
versionCmd "github.com/cli/cli/v2/pkg/cmd/version"
|
||||
|
|
@ -50,9 +51,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
|
|||
"help:feedback": heredoc.Doc(`
|
||||
Open an issue using 'gh issue create -R github.com/cli/cli'
|
||||
`),
|
||||
"help:environment": heredoc.Doc(`
|
||||
See 'gh help environment' for the list of supported environment variables.
|
||||
`),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -82,6 +80,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
|
|||
cmd.AddCommand(gpgKeyCmd.NewCmdGPGKey(f))
|
||||
cmd.AddCommand(completionCmd.NewCmdCompletion(f.IOStreams))
|
||||
cmd.AddCommand(extensionCmd.NewCmdExtension(f))
|
||||
cmd.AddCommand(searchCmd.NewCmdSearch(f))
|
||||
cmd.AddCommand(secretCmd.NewCmdSecret(f))
|
||||
cmd.AddCommand(sshKeyCmd.NewCmdSSHKey(f))
|
||||
cmd.AddCommand(newCodespaceCmd(f))
|
||||
|
|
@ -142,6 +141,7 @@ func newCodespaceCmd(f *cmdutil.Factory) *cobra.Command {
|
|||
vscsURL,
|
||||
&lazyLoadedHTTPClient{factory: f},
|
||||
),
|
||||
f.Browser,
|
||||
)
|
||||
cmd := codespaceCmd.NewRootCmd(app)
|
||||
cmd.Use = "codespace"
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ func runCancel(opts *CancelOptions) error {
|
|||
var run *shared.Run
|
||||
|
||||
if opts.Prompt {
|
||||
runs, err := shared.GetRunsWithFilter(client, repo, 10, func(run shared.Run) bool {
|
||||
runs, err := shared.GetRunsWithFilter(client, repo, nil, 10, func(run shared.Run) bool {
|
||||
return run.Status != shared.Completed
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ type ListOptions struct {
|
|||
|
||||
Limit int
|
||||
WorkflowSelector string
|
||||
Branch string
|
||||
Actor string
|
||||
}
|
||||
|
||||
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
|
||||
|
|
@ -38,9 +40,10 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List recent workflow runs",
|
||||
Args: cobra.NoArgs,
|
||||
Use: "list",
|
||||
Short: "List recent workflow runs",
|
||||
Aliases: []string{"ls"},
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
|
@ -62,6 +65,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
|
|||
|
||||
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of runs to fetch")
|
||||
cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow")
|
||||
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Filter runs by branch")
|
||||
cmd.Flags().StringVarP(&opts.Actor, "user", "u", "", "Filter runs by user who triggered the run")
|
||||
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.RunFields)
|
||||
|
||||
return cmd
|
||||
|
|
@ -82,22 +87,33 @@ func listRun(opts *ListOptions) error {
|
|||
var runs []shared.Run
|
||||
var workflow *workflowShared.Workflow
|
||||
|
||||
filters := &shared.FilterOptions{
|
||||
Branch: opts.Branch,
|
||||
Actor: opts.Actor,
|
||||
}
|
||||
|
||||
opts.IO.StartProgressIndicator()
|
||||
if opts.WorkflowSelector != "" {
|
||||
states := []workflowShared.WorkflowState{workflowShared.Active}
|
||||
workflow, err = workflowShared.ResolveWorkflow(
|
||||
opts.IO, client, baseRepo, false, opts.WorkflowSelector, states)
|
||||
if err == nil {
|
||||
runs, err = shared.GetRunsByWorkflow(client, baseRepo, opts.Limit, workflow.ID)
|
||||
runs, err = shared.GetRunsByWorkflow(client, baseRepo, filters, opts.Limit, workflow.ID)
|
||||
}
|
||||
} else {
|
||||
runs, err = shared.GetRuns(client, baseRepo, opts.Limit)
|
||||
runs, err = shared.GetRuns(client, baseRepo, filters, opts.Limit)
|
||||
}
|
||||
opts.IO.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get runs: %w", err)
|
||||
}
|
||||
|
||||
if err := opts.IO.StartPager(); err == nil {
|
||||
defer opts.IO.StopPager()
|
||||
} else {
|
||||
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
|
||||
}
|
||||
|
||||
if opts.Exporter != nil {
|
||||
return opts.Exporter.Write(opts.IO, runs)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
|
|
@ -51,6 +52,22 @@ func TestNewCmdList(t *testing.T) {
|
|||
WorkflowSelector: "foo.yml",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "branch",
|
||||
cli: "--branch new-cool-stuff",
|
||||
wants: ListOptions{
|
||||
Limit: defaultLimit,
|
||||
Branch: "new-cool-stuff",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user",
|
||||
cli: "--user bak1an",
|
||||
wants: ListOptions{
|
||||
Limit: defaultLimit,
|
||||
Actor: "bak1an",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -83,6 +100,9 @@ func TestNewCmdList(t *testing.T) {
|
|||
}
|
||||
|
||||
assert.Equal(t, tt.wants.Limit, gotOpts.Limit)
|
||||
assert.Equal(t, tt.wants.WorkflowSelector, gotOpts.WorkflowSelector)
|
||||
assert.Equal(t, tt.wants.Branch, gotOpts.Branch)
|
||||
assert.Equal(t, tt.wants.Actor, gotOpts.Actor)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -198,6 +218,40 @@ func TestListRun(t *testing.T) {
|
|||
},
|
||||
wantOut: "STATUS NAME WORKFLOW BRANCH EVENT ID ELAPSED AGE\n* cool commit in progress trunk push 2 4m34s Feb 23, 2021\n✓ cool commit successful trunk push 3 4m34s Feb 23, 2021\nX cool commit failed trunk push 1234 4m34s Feb 23, 2021\n\nFor details on a run, try: gh run view <run-id>\n",
|
||||
},
|
||||
{
|
||||
name: "branch filter applied",
|
||||
opts: &ListOptions{
|
||||
Limit: defaultLimit,
|
||||
Branch: "the-branch",
|
||||
},
|
||||
stubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.QueryMatcher("GET", "repos/OWNER/REPO/actions/runs", url.Values{
|
||||
"branch": []string{"the-branch"},
|
||||
}),
|
||||
httpmock.JSONResponse(shared.RunsPayload{}),
|
||||
)
|
||||
},
|
||||
wantOut: "",
|
||||
wantErrOut: "No runs found\n",
|
||||
},
|
||||
{
|
||||
name: "actor filter applied",
|
||||
opts: &ListOptions{
|
||||
Limit: defaultLimit,
|
||||
Actor: "bak1an",
|
||||
},
|
||||
stubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.QueryMatcher("GET", "repos/OWNER/REPO/actions/runs", url.Values{
|
||||
"actor": []string{"bak1an"},
|
||||
}),
|
||||
httpmock.JSONResponse(shared.RunsPayload{}),
|
||||
)
|
||||
},
|
||||
wantOut: "",
|
||||
wantErrOut: "No runs found\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -18,7 +18,9 @@ type RerunOptions struct {
|
|||
IO *iostreams.IOStreams
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
|
||||
RunID string
|
||||
RunID string
|
||||
OnlyFailed bool
|
||||
JobID string
|
||||
|
||||
Prompt bool
|
||||
}
|
||||
|
|
@ -37,12 +39,18 @@ func NewCmdRerun(f *cmdutil.Factory, runF func(*RerunOptions) error) *cobra.Comm
|
|||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
||||
if len(args) > 0 {
|
||||
if len(args) == 0 && opts.JobID == "" {
|
||||
if !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("`<run-id>` or `--job` required when not running interactively")
|
||||
} else {
|
||||
opts.Prompt = true
|
||||
}
|
||||
} else if len(args) > 0 {
|
||||
opts.RunID = args[0]
|
||||
} else if !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("run ID required when not running interactively")
|
||||
} else {
|
||||
opts.Prompt = true
|
||||
}
|
||||
|
||||
if opts.RunID != "" && opts.JobID != "" {
|
||||
return cmdutil.FlagErrorf("specify only one of `<run-id>` or `--job`")
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
|
|
@ -52,6 +60,9 @@ func NewCmdRerun(f *cmdutil.Factory, runF func(*RerunOptions) error) *cobra.Comm
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&opts.OnlyFailed, "failed", false, "Rerun only failed jobs, including dependencies")
|
||||
cmd.Flags().StringVarP(&opts.JobID, "job", "j", "", "Rerun a specific job from a run, including dependencies")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
|
@ -67,11 +78,24 @@ func runRerun(opts *RerunOptions) error {
|
|||
return fmt.Errorf("failed to determine base repo: %w", err)
|
||||
}
|
||||
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
runID := opts.RunID
|
||||
jobID := opts.JobID
|
||||
var selectedJob *shared.Job
|
||||
|
||||
if jobID != "" {
|
||||
opts.IO.StartProgressIndicator()
|
||||
selectedJob, err = shared.GetJob(client, repo, jobID)
|
||||
opts.IO.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get job: %w", err)
|
||||
}
|
||||
runID = fmt.Sprintf("%d", selectedJob.RunID)
|
||||
}
|
||||
|
||||
if opts.Prompt {
|
||||
cs := opts.IO.ColorScheme()
|
||||
runs, err := shared.GetRunsWithFilter(client, repo, 10, func(run shared.Run) bool {
|
||||
runs, err := shared.GetRunsWithFilter(client, repo, nil, 10, func(run shared.Run) bool {
|
||||
if run.Status != shared.Completed {
|
||||
return false
|
||||
}
|
||||
|
|
@ -83,7 +107,7 @@ func runRerun(opts *RerunOptions) error {
|
|||
return fmt.Errorf("failed to get runs: %w", err)
|
||||
}
|
||||
if len(runs) == 0 {
|
||||
return errors.New("no recent runs have failed; please specify a specific run ID")
|
||||
return errors.New("no recent runs have failed; please specify a specific `<run-id>`")
|
||||
}
|
||||
runID, err = shared.PromptForRun(cs, runs)
|
||||
if err != nil {
|
||||
|
|
@ -91,30 +115,73 @@ func runRerun(opts *RerunOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
opts.IO.StartProgressIndicator()
|
||||
run, err := shared.GetRun(client, repo, runID)
|
||||
opts.IO.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get run: %w", err)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("repos/%s/actions/runs/%d/rerun", ghrepo.FullName(repo), run.ID)
|
||||
|
||||
err = client.REST(repo.RepoHost(), "POST", path, nil, nil)
|
||||
if err != nil {
|
||||
var httpError api.HTTPError
|
||||
if errors.As(err, &httpError) && httpError.StatusCode == 403 {
|
||||
return fmt.Errorf("run %d cannot be rerun; its workflow file may be broken.", run.ID)
|
||||
if opts.JobID != "" {
|
||||
err = rerunJob(client, repo, selectedJob)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "%s Requested rerun of job %s on run %s\n",
|
||||
cs.SuccessIcon(),
|
||||
cs.Cyanf("%d", selectedJob.ID),
|
||||
cs.Cyanf("%d", selectedJob.RunID))
|
||||
}
|
||||
} else {
|
||||
opts.IO.StartProgressIndicator()
|
||||
run, err := shared.GetRun(client, repo, runID)
|
||||
opts.IO.StopProgressIndicator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get run: %w", err)
|
||||
}
|
||||
return fmt.Errorf("failed to rerun: %w", err)
|
||||
}
|
||||
|
||||
if opts.IO.CanPrompt() {
|
||||
cs := opts.IO.ColorScheme()
|
||||
fmt.Fprintf(opts.IO.Out, "%s Requested rerun of run %s\n",
|
||||
cs.SuccessIcon(),
|
||||
cs.Cyanf("%d", run.ID))
|
||||
err = rerunRun(client, repo, run, opts.OnlyFailed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
onlyFailedMsg := ""
|
||||
if opts.OnlyFailed {
|
||||
onlyFailedMsg = "(failed jobs) "
|
||||
}
|
||||
fmt.Fprintf(opts.IO.Out, "%s Requested rerun %sof run %s\n",
|
||||
cs.SuccessIcon(),
|
||||
onlyFailedMsg,
|
||||
cs.Cyanf("%d", run.ID))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func rerunRun(client *api.Client, repo ghrepo.Interface, run *shared.Run, onlyFailed bool) error {
|
||||
runVerb := "rerun"
|
||||
if onlyFailed {
|
||||
runVerb = "rerun-failed-jobs"
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("repos/%s/actions/runs/%d/%s", ghrepo.FullName(repo), run.ID, runVerb)
|
||||
|
||||
err := client.REST(repo.RepoHost(), "POST", path, nil, nil)
|
||||
if err != nil {
|
||||
var httpError api.HTTPError
|
||||
if errors.As(err, &httpError) && httpError.StatusCode == 403 {
|
||||
return fmt.Errorf("run %d cannot be rerun; its workflow file may be broken", run.ID)
|
||||
}
|
||||
return fmt.Errorf("failed to rerun: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rerunJob(client *api.Client, repo ghrepo.Interface, job *shared.Job) error {
|
||||
path := fmt.Sprintf("repos/%s/actions/jobs/%d/rerun", ghrepo.FullName(repo), job.ID)
|
||||
|
||||
err := client.REST(repo.RepoHost(), "POST", path, nil, nil)
|
||||
if err != nil {
|
||||
var httpError api.HTTPError
|
||||
if errors.As(err, &httpError) && httpError.StatusCode == 403 {
|
||||
return fmt.Errorf("job %d cannot be rerun", job.ID)
|
||||
}
|
||||
return fmt.Errorf("failed to rerun: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,48 @@ func TestNewCmdRerun(t *testing.T) {
|
|||
RunID: "1234",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "failed arg nontty",
|
||||
cli: "4321 --failed",
|
||||
wants: RerunOptions{
|
||||
RunID: "4321",
|
||||
OnlyFailed: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "failed arg",
|
||||
tty: true,
|
||||
cli: "--failed",
|
||||
wants: RerunOptions{
|
||||
Prompt: true,
|
||||
OnlyFailed: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with arg job",
|
||||
tty: true,
|
||||
cli: "--job 1234",
|
||||
wants: RerunOptions{
|
||||
JobID: "1234",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with args jobID and runID fails",
|
||||
tty: true,
|
||||
cli: "1234 --job 5678",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "with arg job with no ID fails",
|
||||
tty: true,
|
||||
cli: "--job",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "with arg job with no ID no tty fails",
|
||||
cli: "--job",
|
||||
wantsErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -117,6 +159,39 @@ func TestRerun(t *testing.T) {
|
|||
},
|
||||
wantOut: "✓ Requested rerun of run 1234\n",
|
||||
},
|
||||
{
|
||||
name: "arg including onlyFailed",
|
||||
tty: true,
|
||||
opts: &RerunOptions{
|
||||
RunID: "1234",
|
||||
OnlyFailed: true,
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"),
|
||||
httpmock.JSONResponse(shared.FailedRun))
|
||||
reg.Register(
|
||||
httpmock.REST("POST", "repos/OWNER/REPO/actions/runs/1234/rerun-failed-jobs"),
|
||||
httpmock.StringResponse("{}"))
|
||||
},
|
||||
wantOut: "✓ Requested rerun (failed jobs) of run 1234\n",
|
||||
},
|
||||
{
|
||||
name: "arg including a specific job",
|
||||
tty: true,
|
||||
opts: &RerunOptions{
|
||||
JobID: "20", // 20 is shared.FailedJob.ID
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("GET", "repos/OWNER/REPO/actions/jobs/20"),
|
||||
httpmock.JSONResponse(shared.FailedJob))
|
||||
reg.Register(
|
||||
httpmock.REST("POST", "repos/OWNER/REPO/actions/jobs/20/rerun"),
|
||||
httpmock.StringResponse("{}"))
|
||||
},
|
||||
wantOut: "✓ Requested rerun of job 20 on run 1234\n",
|
||||
},
|
||||
{
|
||||
name: "prompt",
|
||||
tty: true,
|
||||
|
|
@ -158,7 +233,7 @@ func TestRerun(t *testing.T) {
|
|||
}}))
|
||||
},
|
||||
wantErr: true,
|
||||
errOut: "no recent runs have failed; please specify a specific run ID",
|
||||
errOut: "no recent runs have failed; please specify a specific `<run-id>`",
|
||||
},
|
||||
{
|
||||
name: "unrerunnable",
|
||||
|
|
@ -175,7 +250,7 @@ func TestRerun(t *testing.T) {
|
|||
httpmock.StatusStringResponse(403, "no"))
|
||||
},
|
||||
wantErr: true,
|
||||
errOut: "run 3 cannot be rerun; its workflow file may be broken.",
|
||||
errOut: "run 3 cannot be rerun; its workflow file may be broken",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue