Skip to content

Commit af68a74

Browse files
committed
Isolate pr diff command
1 parent 12637d0 commit af68a74

6 files changed

Lines changed: 323 additions & 224 deletions

File tree

api/queries_pr.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"strings"
1010

@@ -209,36 +209,28 @@ func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
209209
return
210210
}
211211

212-
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int) (string, error) {
212+
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int) (io.ReadCloser, error) {
213213
url := fmt.Sprintf("https://api.github.com/repos/%s/pulls/%d",
214214
ghrepo.FullName(baseRepo), prNumber)
215215
req, err := http.NewRequest("GET", url, nil)
216216
if err != nil {
217-
return "", err
217+
return nil, err
218218
}
219219

220220
req.Header.Set("Accept", "application/vnd.github.v3.diff; charset=utf-8")
221221

222222
resp, err := c.http.Do(req)
223223
if err != nil {
224-
return "", err
225-
}
226-
defer resp.Body.Close()
227-
228-
b, err := ioutil.ReadAll(resp.Body)
229-
if err != nil {
230-
return "", err
231-
}
232-
233-
if resp.StatusCode == 200 {
234-
return string(b), nil
224+
return nil, err
235225
}
236226

237227
if resp.StatusCode == 404 {
238-
return "", &NotFoundError{errors.New("pull request not found")}
228+
return nil, &NotFoundError{errors.New("pull request not found")}
229+
} else if resp.StatusCode != 200 {
230+
return nil, handleHTTPError(resp)
239231
}
240232

241-
return "", errors.New("pull request diff lookup failed")
233+
return resp.Body, nil
242234
}
243235

244236
func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, currentPRHeadRef, currentUsername string) (*PullRequestsPayload, error) {

command/pr_diff.go

Lines changed: 0 additions & 104 deletions
This file was deleted.

command/pr_diff_test.go

Lines changed: 0 additions & 104 deletions
This file was deleted.

command/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/cli/cli/internal/run"
2323
apiCmd "github.com/cli/cli/pkg/cmd/api"
2424
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
25+
prDiffCmd "github.com/cli/cli/pkg/cmd/pr/diff"
2526
prReviewCmd "github.com/cli/cli/pkg/cmd/pr/review"
2627
repoCmd "github.com/cli/cli/pkg/cmd/repo"
2728
repoCloneCmd "github.com/cli/cli/pkg/cmd/repo/clone"
@@ -170,6 +171,7 @@ func init() {
170171
repoCmd.Cmd.AddCommand(creditsCmd.NewCmdRepoCredits(&repoResolvingCmdFactory, nil))
171172

172173
prCmd.AddCommand(prReviewCmd.NewCmdReview(&repoResolvingCmdFactory, nil))
174+
prCmd.AddCommand(prDiffCmd.NewCmdDiff(&repoResolvingCmdFactory, nil))
173175

174176
RootCmd.AddCommand(creditsCmd.NewCmdCredits(cmdFactory, nil))
175177
}

0 commit comments

Comments
 (0)