-
Notifications
You must be signed in to change notification settings - Fork 42
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
Some Improvements #32
Conversation
lib/data_source/client/github.rb
Outdated
return cache if cache | ||
ret = block.call | ||
write_cache(filename, JSON.dump(ret[:cache_content])) if ret[:write] | ||
ret[:cache_content] |
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.
purely deal with cache logic, move out the handle_response logic.
app.rb
Outdated
access_token: ENV['GITHUB_ACCESS_TOKEN'], | ||
cache_dir: ENV['alfred_workflow_cache'] | ||
) | ||
host: ENV['GITHUB_API_HOST'] || 'api.github.com', |
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.
change to GITHUB_API_HOST
, to differentiate it from the web UI host.
@@ -232,7 +232,7 @@ | |||
<key>spaces</key> | |||
<string></string> | |||
<key>url</key> | |||
<string>https://github.com/notifications</string> | |||
<string>https://{var:GITHUB_HOST}/notifications</string> |
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.
parameterise the url in the action
@@ -395,7 +355,100 @@ fi</string> | |||
<key>spaces</key> | |||
<string></string> | |||
<key>url</key> | |||
<string>https://github.com/settings/tokens/new?description=Github%20Repos%20Alfred%20workflow&scopes=repo</string> | |||
<string>https://{var:GITHUB_HOST}/settings/tokens/new?description=Github%20Repos%20Alfred%20workflow&scopes=repo</string> |
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.
parameterise the url in the action
<string>api.github.com</string> | ||
<key>GITHUB_HOST</key> | ||
<string>www.github.com</string> |
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.
web ui host.
@@ -13,7 +13,7 @@ def initialize(repositories:) | |||
end | |||
|
|||
def call(args) | |||
results = filter_repos(args[0]) | |||
results = filter_repos(args.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.
same
lib/data_source/client/github.rb
Outdated
request('/user/repos', params) | ||
with_cache(:user_repos) do | ||
|
||
page_count = get_total_page_for_request('/user/repos', 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.
get page count first.
lib/data_source/client/github.rb
Outdated
end | ||
{write: write, cache_content: all_user_repos} | ||
else | ||
res = request('/user/repos', 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.
fallback, just incase page count fetching failed.
lib/data_source/client/github.rb
Outdated
all_user_repos = all_user_repos+ deserialize_body(part_res.body) | ||
end | ||
end | ||
{write: write, cache_content: all_user_repos} |
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.
now the cache component interface is taking object as argument instead of string.
lib/data_source/client/github.rb
Outdated
raise res[:message] unless res.is_a?(Net::HTTPSuccess) | ||
|
||
begin | ||
res.header["Link"].split(",").map do |result| |
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.
fetch the total page number from the header "Link"
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.
Thanks for doing these changes @ileodo! I left a few comments, and also please add the corresponding unit tests.
lib/data_source/client/github.rb
Outdated
sort: 'pushed', | ||
direction: 'desc', | ||
per_page: 100 |
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.
I think this indentation got hit twice.
lib/data_source/client/github.rb
Outdated
params = search_params('', modifiers).merge( | ||
per_page: 100 | ||
per_page: 100 |
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.
Also indentation has 4 spaces here
lib/data_source/client/github.rb
Outdated
if page_count != nil | ||
all_user_pulls = Array.new | ||
write = true | ||
(1..page_count).step(1) do |n| | ||
params[:page] = n | ||
part_res = request('/search/issues', params) | ||
if not part_res.is_a?(Net::HTTPSuccess) | ||
write = false | ||
break | ||
else | ||
all_user_pulls = all_user_pulls+ deserialize_body(part_res.body) | ||
end | ||
end | ||
{write: write, cache_content: all_user_pulls} | ||
else | ||
res = request('/search/issues', params) | ||
if not res.is_a?(Net::HTTPSuccess) | ||
{ write: false, cache_content: nil } | ||
else | ||
{ write: true, cache_content: deserialize_body(res.body) } | ||
end |
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 block seems identical to the one in the other method (user_repos
). Do you think we can refactor it? I would suggest avoid using too many if
branches as well. You can use early returns to achieve that ;)
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.
Thanks for reviewing @edgarjs , I updated the PR with some refactoring and unit tests.
I'm not super familiar with the ruby language feature, open to any suggestion change. Thanks again.
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.
@ileodo I reviewed the changes and left some comments. I'm still have pending to review the rest of the files. Thank you for all your help!
lib/data_source/client/github.rb
Outdated
if @pr_all_involve_me | ||
modifiers = ['is:pr', 'state:open', "involves:#@me_account"] | ||
else | ||
modifiers = org_modifiers('is:pr', "user:#@me_account", 'state:open', "involves:#@me_account") | ||
end | ||
params = search_params('', modifiers).merge( | ||
per_page: 100 | ||
) |
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.
I would move the if
statement to another method called pulls_modifiers
and just call it directly in search_params
:
if @pr_all_involve_me | |
modifiers = ['is:pr', 'state:open', "involves:#@me_account"] | |
else | |
modifiers = org_modifiers('is:pr', "user:#@me_account", 'state:open', "involves:#@me_account") | |
end | |
params = search_params('', modifiers).merge( | |
per_page: 100 | |
) | |
params = search_params('', pulls_modifiers).merge( | |
per_page: 100 | |
) |
lib/data_source/client/github.rb
Outdated
if res.key?("Link") | ||
res["Link"].split(",").map do |result| | ||
page_num, rel = result.match(/&page=(\d+)>; .*?"(\w+)"/i).captures | ||
page_count = page_num.to_i if rel == "last" | ||
end | ||
end |
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 is a clever solution! Thank you. I'll just add my two cents: Instead of doing a map
, I would use find
, so that when the item you're looking is found, it stops the iteration. Although is possible that the rel="last"
item is the last one, in which case maybe it's worth doing a reverse: res['Link'].split(',').reverse.find do |result|
.
And also I would move this logic to another method called results_last_page(res)
lib/data_source/client/github.rb
Outdated
result = nil | ||
break | ||
else | ||
result.append deserialize_body(part_res.body) |
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 is good, but since you mentioned you're new to the Ruby language, another way to do it in Ruby is result << deserialize_body(part_res.body)
.
But you don't need to change it, just FYI.
lib/data_source/client/github.rb
Outdated
if not res.is_a?(Net::HTTPSuccess) | ||
nil | ||
else | ||
deserialize_body(res.body) | ||
end |
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.
We should return an empty array if response is not successful.
if not res.is_a?(Net::HTTPSuccess) | |
nil | |
else | |
deserialize_body(res.body) | |
end | |
res.is_a?(Net::HTTPSuccess) ? deserialize_body(res.body) : [] |
lib/data_source/client/github.rb
Outdated
@me_account = config[:me_account] | ||
@pr_all_involve_me = config[:pr_all_involve_me] | ||
|
||
@cache_name_hash = { | ||
user_repos: 'user_repos', | ||
user_orgs: 'user_orgs', | ||
user_pulls: 'user_pulls' | ||
} | ||
|
||
@cache_ttl_hash = { | ||
user_repos: config[:cache_ttl_sec_repo], | ||
user_orgs: config[:cache_ttl_sec_org], | ||
user_pulls: config[:cache_ttl_sec_pr] | ||
} |
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.
Now that we have more options in the configuration, let's only assign the config hash as an instance variable and access all the individual key from it.
def initialize(config)
@config = config
end
Another refactor for later could be moving all the configuration to its own class in DataSource::ClientConfiguration
lib/data_source/client/github.rb
Outdated
all_user_repos = Array.new | ||
responses.each do |response| | ||
all_user_repos = all_user_repos + response | ||
end | ||
all_user_repos |
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.
I think this is iterating unnecessarily. You can return responses
directly. And even remove it all together since with_cache
is the last call in the method (But also remove the assignation of the variable if it's not used (responses = ...
))
all_user_repos = Array.new | |
responses.each do |response| | |
all_user_repos = all_user_repos + response | |
end | |
all_user_repos |
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.
actually no, because responses is an array of response, each element is a response for a page.
The iterating here is to concatenate the elements inside each response together.
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.
But it's just copying them over another array isn't it? all_user_repos
is also an array, right?
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.
It’s more like flatting:
From [[a],[b],[c]] to [a, b ,c]
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.
I see. In that case you can use Ruby's Array.flat_map
method:
responses.flat_map
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.
yep, I have updated this in my previous commit. this comment is outdated.
lib/data_source/client/github.rb
Outdated
all_user_pulls = Array.new | ||
responses.each do |response| | ||
all_user_pulls = all_user_pulls + response[:items] | ||
end | ||
response[:items] | ||
all_user_pulls |
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 simplify this with flat_map
:
responses.flat_map { |r| r[:items] }
path = File.join(@cache_dir, filename) | ||
File.open(path, 'w') { |f| f.write(value) } | ||
path = File.join(@cache_dir, @cache_name_hash[filename]) | ||
File.open(path, 'w') { |f| f.write(JSON.dump(value)) } |
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.
I originally passed the response.body
directly to avoid parsing twice. Do you think there could be another way to keep that and at the same time add support for your pagination changes? That'd be neat!
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.
I did think about that, but since the cache is going to hold multiple responses, the neatest way is to leverage some structured data (i.e.JSON).
app.rb
Outdated
cache_ttl_sec_repo: (ENV['CACHE_TTL_SEC_REPO'] || (24 * 60 * 60)).to_i, | ||
cache_ttl_sec_org: (ENV['CACHE_TTL_SEC_ORG'] || (24 * 60 * 60)).to_i, | ||
cache_ttl_sec_pr: (ENV['CACHE_TTL_SEC_PR'] || (5 * 60)).to_i, | ||
) |
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.
See me other comment related to the configuration hash below and maybe we can move the defaults there as well.
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
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.
I think you may want to setup an editorconfig plugin for your editor. So it automatically configures to follow conventions for indentation spaces and such.
lib/data_source/client/github.rb
Outdated
all_user_repos = Array.new | ||
responses.each do |response| | ||
all_user_repos = all_user_repos + response | ||
end | ||
all_user_repos |
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.
But it's just copying them over another array isn't it? all_user_repos
is also an array, right?
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
@edgarjs any more comment I can help with? |
Thank you for your changes @ileodo. They have been merged and published in 3.1.0 |
gh
search to support Github Search syntax.