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

Some Improvements #32

Merged
merged 12 commits into from
Feb 14, 2021
Merged

Some Improvements #32

merged 12 commits into from
Feb 14, 2021

Conversation

ileodo
Copy link
Contributor

@ileodo ileodo commented Jan 31, 2021

  1. Fixed the gh search to support Github Search syntax.
  2. Support different TTL for the cache of repo/org/pull
  3. Add an option that search all the PR you are involved (regardless the organisation)
  4. Parameterise the actions in the workflow.
  5. Support fetching multiple pages when caching user_repo, org, pull_request

return cache if cache
ret = block.call
write_cache(filename, JSON.dump(ret[:cache_content])) if ret[:write]
ret[:cache_content]
Copy link
Contributor Author

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',
Copy link
Contributor Author

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>
Copy link
Contributor Author

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&amp;scopes=repo</string>
<string>https://{var:GITHUB_HOST}/settings/tokens/new?description=Github%20Repos%20Alfred%20workflow&amp;scopes=repo</string>
Copy link
Contributor Author

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>
Copy link
Contributor Author

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(" "))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same

request('/user/repos', params)
with_cache(:user_repos) do

page_count = get_total_page_for_request('/user/repos', params)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

get page count first.

end
{write: write, cache_content: all_user_repos}
else
res = request('/user/repos', params)
Copy link
Contributor Author

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.

all_user_repos = all_user_repos+ deserialize_body(part_res.body)
end
end
{write: write, cache_content: all_user_repos}
Copy link
Contributor Author

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.

raise res[:message] unless res.is_a?(Net::HTTPSuccess)

begin
res.header["Link"].split(",").map do |result|
Copy link
Contributor Author

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"

Copy link
Owner

@edgarjs edgarjs 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 doing these changes @ileodo! I left a few comments, and also please add the corresponding unit tests.

Comment on lines 41 to 43
sort: 'pushed',
direction: 'desc',
per_page: 100
Copy link
Owner

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.

params = search_params('', modifiers).merge(
per_page: 100
per_page: 100
Copy link
Owner

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

Comment on lines 87 to 107
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
Copy link
Owner

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 ;)

Copy link
Contributor Author

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.

Copy link
Owner

@edgarjs edgarjs left a 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!

Comment on lines 58 to 65
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
)
Copy link
Owner

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:

Suggested change
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 Show resolved Hide resolved
Comment on lines 90 to 95
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
Copy link
Owner

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)

result = nil
break
else
result.append deserialize_body(part_res.body)
Copy link
Owner

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.

Comment on lines 119 to 123
if not res.is_a?(Net::HTTPSuccess)
nil
else
deserialize_body(res.body)
end
Copy link
Owner

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.

Suggested change
if not res.is_a?(Net::HTTPSuccess)
nil
else
deserialize_body(res.body)
end
res.is_a?(Net::HTTPSuccess) ? deserialize_body(res.body) : []

Comment on lines 15 to 28
@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]
}
Copy link
Owner

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

Comment on lines 50 to 54
all_user_repos = Array.new
responses.each do |response|
all_user_repos = all_user_repos + response
end
all_user_repos
Copy link
Owner

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 = ...))

Suggested change
all_user_repos = Array.new
responses.each do |response|
all_user_repos = all_user_repos + response
end
all_user_repos

Copy link
Contributor Author

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.

Copy link
Owner

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?

Copy link
Contributor Author

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]

Copy link
Owner

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

Copy link
Contributor Author

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.

Comment on lines 70 to 74
all_user_pulls = Array.new
responses.each do |response|
all_user_pulls = all_user_pulls + response[:items]
end
response[:items]
all_user_pulls
Copy link
Owner

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)) }
Copy link
Owner

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!

Copy link
Contributor Author

@ileodo ileodo Feb 4, 2021

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
Comment on lines 23 to 26
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,
)
Copy link
Owner

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.

ileodo and others added 3 commits February 4, 2021 01:35
Co-authored-by: Edgar <edgar.js@gmail.com>
Co-authored-by: Edgar <edgar.js@gmail.com>
Copy link
Owner

@edgarjs edgarjs left a 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.

app.rb Outdated Show resolved Hide resolved
app.rb Outdated Show resolved Hide resolved
lib/data_source/client/github.rb Outdated Show resolved Hide resolved
lib/data_source/client/github.rb Outdated Show resolved Hide resolved
lib/data_source/client/github.rb Outdated Show resolved Hide resolved
Comment on lines 50 to 54
all_user_repos = Array.new
responses.each do |response|
all_user_repos = all_user_repos + response
end
all_user_repos
Copy link
Owner

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?

ileodo and others added 5 commits February 11, 2021 16:31
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>
@ileodo
Copy link
Contributor Author

ileodo commented Feb 13, 2021

@edgarjs any more comment I can help with?

@edgarjs edgarjs merged commit ff9c0fc into edgarjs:master Feb 14, 2021
@edgarjs
Copy link
Owner

edgarjs commented Feb 14, 2021

Thank you for your changes @ileodo. They have been merged and published in 3.1.0

This pull request was closed.
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