-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Clean-up code in GitHub module to reduce duplication & coupling w/ Github API #3054
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
Conversation
|
Two questions:
|
668ed4f to
aa211ae
Compare
aa211ae to
acf46fc
Compare
|
Nice work here!
I think mocking the JSON response would be enough. |
If you want to run single tests, you can specify e. g. |
MikeMcQuaid
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here, code is much cleaner 👏
| valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze | ||
| matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb") | ||
| [*matches].map do |match| | ||
| matches = GitHub.search_code(user: ["Homebrew", "caskroom"], filename: query, extension: "rb") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps should be users if it's taking an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd be kinda difficult to pull off, since #query_string would have to know how to singularize a word. I could just make it remove an s at the end of the word, or hard-code the users case, but that would make the method too specific in my opinion
| [*matches].map do |match| | ||
| matches = GitHub.search_code(user: "caskroom", path: "Casks", | ||
| filename: query, extension: "rb") | ||
| Array(matches).map do |match| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth instead ensuring search_code always returns an array?
|
|
||
| describe "::query_string" do | ||
| it "builds a query with the given hash parameters formatted as key:value" do | ||
| query = subject.query_string(user: "Homebrew", repo: "Brew") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lowercase brew here
Library/Homebrew/utils/github.rb
Outdated
| end | ||
|
|
||
| def url_to(*subroutes) | ||
| URI.parse(File.join(API_URL, *subroutes)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels a little weird using File.join in this way. I'd just use String#join
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URI::join
Edit: Only works with one argument. Maybe URL.join(API_URL, subroutes.join("/"))?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that this is kinda weird -- I did it so that url_to '/repos', '/Homebrew' would work, but I'm also fine if the weird-ness outweighs that benefit
In general it would be good to have at least one integration tests here that actually tests hitting the GitHub API. Use an old Homebrew/brew issue and we can lock it if necessary. I wouldn't worry about it changing/breaking: if it does, we can fix it then. Other tests beyond the one integration test: yeh, as @reitermarkus said I think just stubbing the JSON response is a good call.
Let us know where you expected this information to live. I'd love to make that more obviously documented. |
I was expecting something to be in a file like Edit: Also, that document could contain info about how to run brew from the source repo, took me a minute or two to realize that was why the tests were running without my changes |
a9c650b to
68cdb55
Compare
Library/Homebrew/utils/github.rb
Outdated
| end | ||
| end | ||
|
|
||
| def issues_matching(query, qualifiers = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qualifiers = {} -> **qualifiers, while we're at it.
Library/Homebrew/utils/github.rb
Outdated
| prs = open_or_closed_prs | ||
| else | ||
| return | ||
| return [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the return, and conditionally assign it to prs instead.
Library/Homebrew/utils/github.rb
Outdated
| search("code", **params) | ||
| end | ||
|
|
||
| def uri_escape(query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also remove the check for URI.respond_to?(:encode_www_form_component) here, since we're always using Ruby >= 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just replace uri_escape with URI.encode_www_form_component, since we're only using it once.
Library/Homebrew/utils/github.rb
Outdated
| params_list = main_params | ||
|
|
||
| qualifiers.each do |key, value| | ||
| if value.is_a? Array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of these separate checks, just use [*value], and loop through all values, even if it is only one.
3d6f97d to
987e588
Compare
Library/Homebrew/utils/github.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flat_map here, and append to params_list instead.
Library/Homebrew/utils/github.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline this again, since it is only used once.
Library/Homebrew/utils/github.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move prs = out of the if.
prs = if …
…
else
…
end
Library/Homebrew/utils/github.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline this, since it is only used once.
987e588 to
ce196e5
Compare
Fix duplication bug
ce196e5 to
5f8d212
Compare
|
This PR is all set from my perspective, can I get one last review? |
| describe "::search_issues", :needs_network do | ||
| it "queries GitHub issues with the passed parameters" do | ||
| results = subject.search_issues("brew search", repo: "Homebrew/brew", author: "avetamine", is: "closed") | ||
| expect(results.count).to eq(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will break if that user creates a new issue which will eventually be closed. Not sure if the line below should use #first or #last then.
| results = subject.search_issues("brew search", repo: "Homebrew/brew", author: "avetamine", is: "closed") | ||
| expect(results.count).to eq(1) | ||
| expect(results.first["title"]).to eq("brew search : 422 Unprocessable Entity") | ||
| expect(results.count).to be > 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(results).not_to be_empty
Library/Homebrew/utils/github.rb
Outdated
| end | ||
|
|
||
| def query_string(*main_params, **qualifiers) | ||
| params_list = main_params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just call this params.
reitermarkus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last comment, other than that 👍!
Everything done, except pluralising named arguments (which is unnecessary).
|
Thanks for the great cleanup, @BenMusch! |
|
No problem, thanks for the thorough reviews! |
brew testswith your changes locally?This PR cleans up some of the code in the GitHub module. The goals are to reduce duplication around things like building URLs, and to prevent external code from having to know about the "key:value" format of github qualifiers