Skip to content

Consistent PR lookup interface#1020

Merged
probablycorey merged 10 commits intotrunkfrom
pr-lookup-v2
Jun 4, 2020
Merged

Consistent PR lookup interface#1020
probablycorey merged 10 commits intotrunkfrom
pr-lookup-v2

Conversation

@probablycorey
Copy link
Contributor

We've got a few ways to get a PR object right now, and they are all slightly different and require a lot of boilerplate code. This creates a consistent interface for that lookup. It might sound familiar because I tried it before in #926 but this time it will work!

The plan

This is going to be a two step process.

  1. Get the commands using the same pr lookup interface prFromArgs. This will look at the commands args to find the PR via a number, url, branch name or whatever the current branch of the existing git repo is. This is what I solved in this PR.

  2. Add an argument to prFromArgs that lets you pick what type of PR response you want back. This was mislav's magical reflection idea that we got working in https://github.com/cli/cli/compare/pr-get-query

Things I considered while writing this.

  • I pass the args string to prFromArgs because if passed the string itself like args[0] we'd get a panic when there are no args or we'd need a conditional before each call. This seemed annoying and passing the args array worked out well enough.
  • This does create an extra api in some commands because previously we only needed the PR number. But this is not common and I feel like it gives us the extra benefit of verifying that the PR exists.

@probablycorey probablycorey self-assigned this May 27, 2020
@probablycorey probablycorey requested a review from a team May 27, 2020 17:39
@vilmibm
Copy link
Contributor

vilmibm commented Jun 1, 2020

thank you for taking the time to do this 🙏

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Love how much cleanup this results in 👏

I've got one note about reading the repo info from the URL: it looks like we only respect the full URL in pr checkout but not in other pr commands. Now that there is a unified interface, I see it as a good opportunity to unify how URLs are handled across all commands.

Comment on lines +208 to +210
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, pr *PullRequest) (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/pulls/%d",
ghrepo.FullName(baseRepo), prNum)
ghrepo.FullName(baseRepo), pr.Number)
Copy link
Contributor

@mislav mislav Jun 2, 2020

Choose a reason for hiding this comment

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

Was there a strong reason to change the call signature of this method? The way I see it, accepting an int instead of a PullRequest makes it more versatile (knowing the number, the caller may retrieve PR diff without first having to query the PR data itself), and if the caller already has a PullRequest reference they can easily pass the pr.Number as argument themeselves

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vilmibm and I talked about this at one point. I think we thought it was more flexible to pass in the PR, but now that you mention that it sounds like it is LESS flexible now. I'll revert the change.

Comment on lines +32 to +35
r := regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`)
if m := r.FindStringSubmatch(prString); m != nil {
prString = m[3]
baseRepo = ghrepo.New(m[1], m[2])
Copy link
Contributor

@mislav mislav Jun 2, 2020

Choose a reason for hiding this comment

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

This respects the base repo as found in the URL. 👍 I see this as currently only scoped to pr checkout, but could we afford to extend this to all pr commands via pr_lookup.go?

Comment on lines +46 to +48
r := regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`)
if m := r.FindStringSubmatch(s); m != nil {
prNumberString := m[3]
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this ignores the information about the base repo from the URL. To address issues such as #1060, would we consider respecting it?

@probablycorey
Copy link
Contributor Author

@mislav check out 4c75c8b. prFromArgs now returns a repo that is used as the base branch for commands that use base branch. It feels a little weird to have the PR lookup also determine the baseBranch, but I couldn't think of a cleaner way around it besides adding another function called baseBranchFromArgs but that seemed to confusing...

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Love it! Thanks for your patience and hard work.

I've added one more note about how we could avoid a superfluous API request

@probablycorey probablycorey merged commit ee2b38d into trunk Jun 4, 2020
@mislav mislav deleted the pr-lookup-v2 branch June 25, 2020 14:36
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.

3 participants