Return HTTP errors properly for some commands (#8037)

* Return HTTP error for `run watch`

Partially fixes #8026

* Return HTTP error for `gpg-key delete`

Partially fixes #8026
This commit is contained in:
Heath Stewart 2023-09-24 09:22:55 -07:00 committed by GitHub
parent 60f2da2d1f
commit 073ec3426b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 2 deletions

View file

@ -91,7 +91,7 @@ func deleteRun(opts *DeleteOptions) error {
err = deleteGPGKey(httpClient, host, id)
if err != nil {
return nil
return err
}
if opts.IO.IsStdoutTTY() {

View file

@ -10,6 +10,7 @@ import (
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
@ -170,6 +171,19 @@ func Test_deleteRun(t *testing.T) {
wantErr: true,
wantErrMsg: "unable to delete GPG key ABC123: either the GPG key is not found or it is not owned by you",
},
{
name: "delete failed",
opts: DeleteOptions{KeyID: "ABC123", Confirmed: true},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "user/gpg_keys"), httpmock.StatusStringResponse(200, keysResp))
reg.Register(httpmock.REST("DELETE", "user/gpg_keys/123"), httpmock.StatusJSONResponse(404, api.HTTPError{
StatusCode: 404,
Message: "GPG key 123 not found",
}))
},
wantErr: true,
wantErrMsg: "HTTP 404: GPG key 123 not found (https://api.github.com/user/gpg_keys/123)",
},
}
for _, tt := range tests {

View file

@ -173,7 +173,7 @@ func watchRun(opts *WatchOptions) error {
opts.IO.StopAlternateScreenBuffer()
if err != nil {
return nil
return err
}
// Write the last temporary buffer one last time

View file

@ -14,6 +14,7 @@ import (
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
@ -298,6 +299,33 @@ func TestWatchRun(t *testing.T) {
wantErr: true,
errMsg: "SilentError",
},
{
name: "failed to get run status",
tty: true,
opts: &WatchOptions{
RunID: "1234",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"),
httpmock.JSONResponse(shared.TestRunWithCommit(1234, shared.InProgress, "", "commit2")),
)
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows/123"),
httpmock.JSONResponse(shared.TestWorkflow),
)
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"),
httpmock.StatusJSONResponse(404, api.HTTPError{
StatusCode: 404,
Message: "run 1234 not found",
}),
)
},
wantOut: "\x1b[?1049h\x1b[?1049l",
wantErr: true,
errMsg: "failed to get run: HTTP 404: run 1234 not found (https://api.github.com/repos/OWNER/REPO/actions/runs/1234?exclude_pull_requests=true)",
},
}
for _, tt := range tests {