Skip to content

Commit

Permalink
Fix PRs made from forks. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg authored Feb 18, 2024
1 parent 7bbe4f4 commit 79d9c58
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
24 changes: 20 additions & 4 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,17 @@ func (p errpkg) Error() string {
return fmt.Sprintf("error(s) loading package %s: %s", p.pkg.PkgPath, strings.Join(strs, "; "))
}

// CompareGit compares the Go packages in two revisions of a Git repo at the given URL.
// CompareGit compares the Go packages in two revisions of a Git repository at the given URL.
func CompareGit(ctx context.Context, repoURL, olderRev, newerRev string) (Result, error) {
return CompareGitWith(ctx, repoURL, olderRev, newerRev, CompareDirs)
}

// CompareGitWith compares the Go packages in two revisions of a Git repo at the given URL.
// CompareGit2 compares the Go packages in one revision each of two Git repositories.
func CompareGit2(ctx context.Context, olderRepoURL, olderRev, newerRepoURL, newerRev string) (Result, error) {
return CompareGit2With(ctx, olderRepoURL, olderRev, newerRepoURL, newerRev, CompareDirs)
}

// CompareGitWith compares the Go packages in two revisions of a Git repository at the given URL.
// It uses the given callback function to perform the comparison.
//
// The callback function receives the paths to two directories,
Expand All @@ -266,6 +271,17 @@ func CompareGit(ctx context.Context, repoURL, olderRev, newerRev string) (Result
//
// Note that CompareGit(...) is simply CompareGitWith(..., CompareDirs).
func CompareGitWith(ctx context.Context, repoURL, olderRev, newerRev string, f func(older, newer string) (Result, error)) (Result, error) {
return CompareGit2With(ctx, repoURL, olderRev, repoURL, newerRev, f)
}

// CompareGit2With compares the Go packages in one revision each of two Git repositories.
// It uses the given callback function to perform the comparison.
//
// The callback function receives the paths to two directories,
// each containing a clone of one of the repositories at its selected revision.
//
// Note that CompareGit2(...) is simply CompareGit2With(..., CompareDirs).
func CompareGit2With(ctx context.Context, olderRepoURL, olderRev, newerRepoURL, newerRev string, f func(older, newer string) (Result, error)) (Result, error) {
parent, err := os.MkdirTemp("", "modver")
if err != nil {
return None, fmt.Errorf("creating tmpdir: %w", err)
Expand All @@ -275,12 +291,12 @@ func CompareGitWith(ctx context.Context, repoURL, olderRev, newerRev string, f f
olderDir := filepath.Join(parent, "older")
newerDir := filepath.Join(parent, "newer")

err = gitSetup(ctx, repoURL, olderDir, olderRev)
err = gitSetup(ctx, olderRepoURL, olderDir, olderRev)
if err != nil {
return None, fmt.Errorf("setting up older clone: %w", err)
}

err = gitSetup(ctx, repoURL, newerDir, newerRev)
err = gitSetup(ctx, newerRepoURL, newerDir, newerRev)
if err != nil {
return None, fmt.Errorf("setting up newer clone: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// PR performs modver analysis on a GitHub pull request.
func PR(ctx context.Context, gh *github.Client, owner, reponame string, prnum int) (modver.Result, error) {
return prHelper(ctx, gh.Repositories, gh.PullRequests, gh.Issues, modver.CompareGit, owner, reponame, prnum)
return prHelper(ctx, gh.Repositories, gh.PullRequests, gh.Issues, modver.CompareGit2, owner, reponame, prnum)
}

type reposIntf interface {
Expand All @@ -35,7 +35,7 @@ type issuesIntf interface {
ListComments(ctx context.Context, owner, reponame string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error)
}

func prHelper(ctx context.Context, repos reposIntf, prs prsIntf, issues issuesIntf, comparer func(ctx context.Context, cloneURL, baseSHA, headSHA string) (modver.Result, error), owner, reponame string, prnum int) (modver.Result, error) {
func prHelper(ctx context.Context, repos reposIntf, prs prsIntf, issues issuesIntf, comparer func(ctx context.Context, baseURL, baseSHA, headURL, headSHA string) (modver.Result, error), owner, reponame string, prnum int) (modver.Result, error) {
repo, _, err := repos.Get(ctx, owner, reponame)
if err != nil {
return modver.None, errors.Wrap(err, "getting repository")
Expand All @@ -44,7 +44,7 @@ func prHelper(ctx context.Context, repos reposIntf, prs prsIntf, issues issuesIn
if err != nil {
return modver.None, errors.Wrap(err, "getting pull request")
}
result, err := comparer(ctx, *repo.CloneURL, *pr.Base.SHA, *pr.Head.SHA)
result, err := comparer(ctx, *pr.Base.Repo.CloneURL, *pr.Base.SHA, *pr.Head.Repo.CloneURL, *pr.Head.SHA)
if err != nil {
return modver.None, errors.Wrap(err, "comparing versions")
}
Expand Down
18 changes: 14 additions & 4 deletions internal/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,18 @@ type mockPRsService struct{}

func (mockPRsService) Get(ctx context.Context, owner, reponame string, number int) (*github.PullRequest, *github.Response, error) {
return &github.PullRequest{
Base: &github.PullRequestBranch{SHA: ptr("baseSHA")},
Head: &github.PullRequestBranch{SHA: ptr("headSHA")},
Base: &github.PullRequestBranch{
Repo: &github.Repository{
CloneURL: ptr("baseURL"),
},
SHA: ptr("baseSHA"),
},
Head: &github.PullRequestBranch{
Repo: &github.Repository{
CloneURL: ptr("headURL"),
},
SHA: ptr("headSHA"),
},
Number: ptr(17),
}, nil, nil
}
Expand Down Expand Up @@ -113,8 +123,8 @@ func (m *mockIssuesService) ListComments(ctx context.Context, owner, reponame st
return result, nil, nil
}

func mockComparer(result modver.Result) func(ctx context.Context, cloneURL, baseSHA, headSHA string) (modver.Result, error) {
return func(ctx context.Context, cloneURL, baseSHA, headSHA string) (modver.Result, error) {
func mockComparer(result modver.Result) func(_ context.Context, _, _, _, _ string) (modver.Result, error) {
return func(_ context.Context, _, _, _, _ string) (modver.Result, error) {
return result, nil
}
}
Expand Down

0 comments on commit 79d9c58

Please sign in to comment.