From 66a4df14a6af020f78e398bfcc4519862326eb68 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 27 Mar 2016 16:33:43 +0900 Subject: [PATCH 1/2] Refactor data structure --- lib/github/nippou.rb | 2 + lib/github/nippou/commands.rb | 54 +++---------------- .../nippou/concerns/sawyer_resource_github.rb | 29 ++++++++++ lib/github/nippou/user_events.rb | 28 +++++++++- 4 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 lib/github/nippou/concerns/sawyer_resource_github.rb diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index a714cc8..83728d9 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -1,3 +1,5 @@ +require 'github/nippou/concerns/sawyer_resource_github' + require 'github/nippou/commands' require 'github/nippou/user_events' require 'github/nippou/version' diff --git a/lib/github/nippou/commands.rb b/lib/github/nippou/commands.rb index 9ad8172..ce5cd3c 100644 --- a/lib/github/nippou/commands.rb +++ b/lib/github/nippou/commands.rb @@ -11,6 +11,7 @@ def markdown_escape module Github module Nippou class Commands < Thor + using SawyerResourceGithub using StringExMarkdown default_task :list @@ -23,11 +24,13 @@ class Commands < Thor desc 'list', "Displays today's GitHub events formatted for Nippou" def list - nippous.each do |url, detail| - line = "* [#{detail[:title]} - #{detail[:repo_basename]}](#{url}) by #{detail[:username]}" - if detail[:merged] + user_events.each do |user_event| + issue = user_event.issue(client) + line = "* [%s - %s](%s) by %s" % + [issue.title.markdown_escape, user_event.repo.name, user_event.html_url, issue.user.login] + if issue.merged line << ' **merged!**' - elsif detail[:state] == 'closed' + elsif issue.state == 'closed' line << ' **closed!**' end puts line @@ -41,27 +44,10 @@ def version private - def nippous - result = {} - - user_events.each do |user_event| - case user_event.type - when 'IssuesEvent', 'IssueCommentEvent' - issue = user_event.payload.issue - result[issue.html_url] ||= hash_for_issue(user_event.repo.name, issue.number) - when 'PullRequestEvent', 'PullRequestReviewCommentEvent' - pr = user_event.payload.pull_request - result[pr.html_url] ||= hash_for_pr(user_event.repo.name, pr.number) - end - end - - result.sort - end - def user_events @user_events ||= UserEvents.new( client, user, options[:since_date], options[:until_date] - ).retrieve + ).collect end def client @@ -106,30 +92,6 @@ def access_token exit! end end - - def hash_for_issue(repo_name, issue_number) - issue = client.issue(repo_name, issue_number) - - { - title: issue.title.markdown_escape, - repo_basename: repo_name, - username: issue.user.login, - merged: issue.merged, - state: issue.state, - } - end - - def hash_for_pr(repo_name, pr_number) - pr = client.pull_request(repo_name, pr_number) - - { - title: pr.title.markdown_escape, - repo_basename: repo_name, - username: pr.user.login, - merged: pr.merged, - state: pr.state, - } - end end end end diff --git a/lib/github/nippou/concerns/sawyer_resource_github.rb b/lib/github/nippou/concerns/sawyer_resource_github.rb new file mode 100644 index 0000000..1c71cda --- /dev/null +++ b/lib/github/nippou/concerns/sawyer_resource_github.rb @@ -0,0 +1,29 @@ +require 'octokit' + +module SawyerResourceGithub + refine Sawyer::Resource do + def issue(client) + case + when self.issue? + client.issue(self.repo.name, self.payload.issue.number) + when self.pull_request? + client.pull_request(self.repo.name, self.payload.pull_request.number) + end + end + + def issue? + self.type == 'IssuesEvent' || + self.type == 'IssueCommentEvent' + end + + def pull_request? + self.type == 'PullRequestEvent' || + self.type == 'PullRequestReviewCommentEvent' + end + + def html_url + type = self.issue? ? :issue : :pull_request + self.payload.try!(type).html_url + end + end +end diff --git a/lib/github/nippou/user_events.rb b/lib/github/nippou/user_events.rb index c0c7c66..7da829e 100644 --- a/lib/github/nippou/user_events.rb +++ b/lib/github/nippou/user_events.rb @@ -4,6 +4,8 @@ module Github module Nippou class UserEvents + using SawyerResourceGithub + def initialize(client, user, since_date, until_date) @client = client @user = user @@ -12,6 +14,12 @@ def initialize(client, user, since_date, until_date) @range = @since_time..until_time end + def collect + uniq(sort(filter(retrieve))) + end + + private + def retrieve user_events = @client.user_events(@user, per_page: 100) last_response = @client.last_response @@ -24,8 +32,6 @@ def retrieve user_events.select { |user_event| in_range?(user_event) } end - private - def continue?(last_response, user_events) last_response.rels[:next] && user_events.last.created_at.getlocal >= @since_time @@ -34,6 +40,24 @@ def continue?(last_response, user_events) def in_range?(user_event) @range.include?(user_event.created_at.getlocal) end + + def filter(user_events) + user_events.select do |user_event| + user_event.issue? || user_event.pull_request? + end + end + + def sort(user_events) + user_events.sort do |a, b| + a.html_url <=> b.html_url + end + end + + def uniq(user_events) + user_events.uniq do |user_event| + user_event.html_url + end + end end end end From 7e51a6c4c314884a80f808001934ba81f64e5339 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 27 Mar 2016 16:36:01 +0900 Subject: [PATCH 2/2] Refactor refinement --- lib/github/nippou.rb | 1 + lib/github/nippou/commands.rb | 10 +--------- lib/github/nippou/concerns/string_markdown.rb | 7 +++++++ 3 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 lib/github/nippou/concerns/string_markdown.rb diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index 83728d9..af2654b 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -1,4 +1,5 @@ require 'github/nippou/concerns/sawyer_resource_github' +require 'github/nippou/concerns/string_markdown' require 'github/nippou/commands' require 'github/nippou/user_events' diff --git a/lib/github/nippou/commands.rb b/lib/github/nippou/commands.rb index ce5cd3c..ba557c5 100644 --- a/lib/github/nippou/commands.rb +++ b/lib/github/nippou/commands.rb @@ -1,18 +1,10 @@ require 'thor' -module StringExMarkdown - refine String do - def markdown_escape - self.gsub(/([`<>])/, '\\\\\1') - end - end -end - module Github module Nippou class Commands < Thor using SawyerResourceGithub - using StringExMarkdown + using StringMarkdown default_task :list class_option :since_date, type: :string, diff --git a/lib/github/nippou/concerns/string_markdown.rb b/lib/github/nippou/concerns/string_markdown.rb new file mode 100644 index 0000000..9ffe2f1 --- /dev/null +++ b/lib/github/nippou/concerns/string_markdown.rb @@ -0,0 +1,7 @@ +module StringMarkdown + refine String do + def markdown_escape + self.gsub(/([`<>])/, '\\\\\1') + end + end +end