Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import "errors"

var (
UnauthorizedError = errors.New("`repo-token` lacks required permissions")
NoGitError = errors.New("`git` not installed")
)
46 changes: 42 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"sort"
"strings"

Expand Down Expand Up @@ -165,18 +166,55 @@ func postGithubComment(ctx context.Context, flagsRef references.ReferenceSummary

func getDiffs(ctx context.Context, config *lcr.Config, prNumber int) ([]*diff.FileDiff, error) {
gha.Debug("Getting pull request diff...")
rawOpts := github.RawOptions{Type: github.Diff}
raw, resp, err := config.GHClient.PullRequests.GetRaw(ctx, config.Owner, config.Repo, prNumber, rawOpts)

var (
client = config.GHClient
owner = config.Owner
repo = config.Repo
rawOpts = github.RawOptions{Type: github.Diff}
rawDiff []byte
)

raw, resp, err := client.PullRequests.GetRaw(ctx, owner, repo, prNumber, rawOpts)
rawDiff = []byte(raw)
if err != nil {
// TODO use this elsewhere
if resp.StatusCode == http.StatusUnauthorized {
return nil, e.UnauthorizedError
}

return nil, err
// check that git is installed
if _, gitCmdErr := exec.Command("git", "-v").CombinedOutput(); gitCmdErr != nil {
return nil, e.NoGitError
}

// For very large diffs, the github api will return a 406, when this
// happens, fallback to calling `git diff` directly
if resp != nil && resp.StatusCode == http.StatusNotAcceptable {
gha.Debug("Diff too large, fallback to traditional git command")
pr, _, err := client.PullRequests.Get(ctx, owner, repo, prNumber)
if err != nil {
return nil, err
}

headSha := pr.GetHead().GetSHA()

commitsComparison, _, err := client.Repositories.CompareCommits(ctx, owner, repo, headSha, pr.GetBase().GetSHA(), nil)
if err != nil {
return nil, err
}

mergeBaseSha := commitsComparison.GetMergeBaseCommit().GetSHA()
rawDiff, err = exec.Command("git", "diff", "--find-renames", mergeBaseSha, headSha).CombinedOutput()
// git diff return exit status 1 if there is a diff, so that does
// not indicate an error
if err != nil && err.Error() != "exit status 1" {
return nil, fmt.Errorf("failed to run git diff: %w", err)
}
}
}

multi, err := diff.ParseMultiFileDiff([]byte(raw))
multi, err := diff.ParseMultiFileDiff(rawDiff)
if err != nil {
return nil, err
}
Expand Down