Skip to content

Commit 6a65c43

Browse files
committed
fallback to normal git commands for large diffs
1 parent f03abba commit 6a65c43

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

main.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"exec"
56
"fmt"
67
"net/http"
78
"os"
@@ -165,14 +166,47 @@ func postGithubComment(ctx context.Context, flagsRef references.ReferenceSummary
165166

166167
func getDiffs(ctx context.Context, config *lcr.Config, prNumber int) ([]*diff.FileDiff, error) {
167168
gha.Debug("Getting pull request diff...")
168-
rawOpts := github.RawOptions{Type: github.Diff}
169-
raw, resp, err := config.GHClient.PullRequests.GetRaw(ctx, config.Owner, config.Repo, prNumber, rawOpts)
169+
170+
var (
171+
client = config.GHClient
172+
owner = config.Owner
173+
repo = config.Repo
174+
rawOpts = github.RawOptions{Type: github.Diff}
175+
)
176+
177+
raw, resp, err := client.PullRequests.GetRaw(ctx, owner, repo, prNumber, rawOpts)
170178
if err != nil {
171179
// TODO use this elsewhere
172180
if resp.StatusCode == http.StatusUnauthorized {
173181
return nil, e.UnauthorizedError
174182
}
175183

184+
// check that git is installed
185+
_, gitCmdErr := exec.Command("git", "-v").CombinedOutput()
186+
187+
// For very large diffs, the github api will return a 406, when this
188+
// happens, fallback to calling `git diff` directly
189+
if resp != nil && resp.StatusCode == http.StatusNotAcceptable && gitCmdErr == nil {
190+
gha.Debug("Diff too large, fallback to traditional git command")
191+
pr, _, err := client.PullRequests.Get(ctx, owner, repo, prNumber)
192+
if err != nil {
193+
return nil, err
194+
}
195+
196+
headSha := pr.GetHead().GetSHA()
197+
198+
commitsComparison, _, err := client.Repositories.CompareCommits(ctx, owner, repo, headSha, pr.GetBase().GetSHA(), nil)
199+
if err != nil {
200+
return nil, err
201+
}
202+
203+
mergeBaseSha := commitsComparison.GetMergeBaseCommit().GetSHA()
204+
raw, err = exec.Command("git", "diff", "--find-renames", mergeBaseSha, headSha).CombinedOutput()
205+
if err != nil {
206+
return nil, fmt.Errorf("failed to run git diff: %w", err)
207+
}
208+
}
209+
176210
return nil, err
177211
}
178212

0 commit comments

Comments
 (0)