Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getting pull requests that come from forks #563

Merged
merged 5 commits into from
Nov 12, 2024

Conversation

alankpatel
Copy link
Contributor

While switching the github api that we use to get pull requests given a commit sha we noticed it was not retrieving pull requests from forks. This PR includes a change to revert back to the old API that also lists PRs from forks. During investigation I also noticed the check_run event also did not list the pull requests if it came from a fork, so this PR also includes a change to use the list all commits given a commit like how it is done for the status event.

@alankpatel alankpatel self-assigned this Nov 8, 2024
@alankpatel alankpatel requested a review from a team November 8, 2024 00:17
@bluekeyes
Copy link
Member

Summarizing from our internal discussion, I think it would be good explore ways we could use this more expensive listing method only when required. That might be:

  • Only listing PRs if the commit association API returns no results
  • Trying to identify statuses on commits from forks via some other method, like fields of the status event payload
  • Something else clever (e.g. sometimes GraphQL enables things the REST API doesn't, but it does come with other tradeoffs)

@alankpatel alankpatel requested a review from bluekeyes November 8, 2024 20:21
Copy link
Member

@bluekeyes bluekeyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests for the new fallback behavior here!

return nil, errors.Wrapf(err, "failed to list pull requests for repository %s/%s", owner, repoName)
}
// GitHubClient is an interface that wraps the methods used from the github.Client.
type GitHubClient interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd call this GitHubPullRequestClient, since it is only satisfied by the PullRequestsService from go-github


if len(prs) == 0 {
logger.Debug().Msg("No pull requests associated with the check run, searching by SHA")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's not mention check runs here specifically, since this could be used for other things too


// GetAllPossibleOpenPullRequestsForSHA attempts to find all open pull requests
// associated with the given SHA using multiple methods in case we are dealing with a fork
func GetAllPossibleOpenPullRequestsForSHA(ctx context.Context, client GitHubClient, owner, repo, sha string) ([]*github.PullRequest, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the comments help, I'm wondering if we can make the differences between these functions more obvious by name. From the usage, it looks like GetAllPossibleOpenPullRequestsForSHA, ListOpenPullRequestsForSHA, and ListOpenPullRequestsForRef are the only functions that need to be public, so making the remaining ones private for now would help.

Beyond that, I think we should try to make it obvious which functions you should call by default and which are special cases:

  • I think GetAllPossibleOpenPullRequestsForSHA and ListOpenPullRequestsForRef are the two "by default" methods, so they should have similar names. Maybe use the Get pattern for both of these?
  • I think ListOpenPullRequestsForSHA is the special method that you only call directly when you know you are working with a fork and want to skip a request by not calling the "default" method. Maybe call this ListAllOpenPullRequestsFilteredBySHA? That might be enough to imply that there's something special about this compared to GetOpenPullRequestsForSHA. Open to other naming ideas too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with what you are saying about the function names being a bit confusing. Followed your advice to make the two main functions start with Get and changed the name of the List function to make it clear that it is filtering all pull requests

logger.Debug().Msg("Doing nothing since status change event affects no open pull requests")
return nil
logger.Debug().Msg("No pull requests associated with the check run, searching by SHA")
// if no PR's were attached with the event let's check with Github in case it is a fork
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to clarify here that check runs for fork PRs will always have an empty pull request list. I think the other case is check runs on branches that aren't part of any PR?

Comment on lines 109 to 112
if len(prs) == 0 {
logger.Debug().Msg("No open pull requests found for the given SHA")
return nil, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can probably drop this and return prs on L115 - it looks like the callers already log something when there are no PRs, so this feels redundant.

@alankpatel alankpatel requested a review from bluekeyes November 12, 2024 19:25
@alankpatel alankpatel merged commit 12e1e81 into develop Nov 12, 2024
8 checks passed
@alankpatel alankpatel deleted the alanp/fix-merging-fork-prs branch November 12, 2024 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants