From 475357eb0bf11cc12408ead7417100b6956512a2 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 19:38:40 +0900 Subject: [PATCH 01/11] $ bundle update --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6420399..bdf63d2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,4 +27,4 @@ DEPENDENCIES rake (~> 10.0) BUNDLED WITH - 1.10.6 + 1.11.2 From 2e747c5f59e1ee445b8aa7b4fa4046559f7b188d Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 19:52:27 +0900 Subject: [PATCH 02/11] Refactor class method definition --- lib/github/nippou.rb | 85 ++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index a8c0ce9..728fd41 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -3,61 +3,61 @@ module Github module Nippou - def self.list - user = self.user - client = Octokit::Client.new(login: user, access_token: self.access_token) - events = client.user_events(user) + class << self + def list + client = Octokit::Client.new(login: user, access_token: access_token) + events = client.user_events(user) - url_to_detail = {} + url_to_detail = {} - events.each do |_| - break unless _.created_at.getlocal.to_date == Time.now.to_date - case _.type - when 'IssuesEvent', 'IssueCommentEvent' - title = _.payload.issue.title.gsub('`', '\\\`') - merged = client.pull_merged?(_.repo.name, _.payload.issue.number) - url_to_detail[_.payload.issue.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.issue.user.login, merged: merged} - when 'PullRequestEvent', 'PullRequestReviewCommentEvent' - title = _.payload.pull_request.title.gsub('`', '\\\`') - merged = client.pull_merged?(_.repo.name, _.payload.pull_request.number) - url_to_detail[_.payload.pull_request.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.pull_request.user.login, merged: merged} + events.each do |_| + break unless _.created_at.getlocal.to_date == Time.now.to_date + case _.type + when 'IssuesEvent', 'IssueCommentEvent' + title = _.payload.issue.title.gsub('`', '\\\`') + merged = client.pull_merged?(_.repo.name, _.payload.issue.number) + url_to_detail[_.payload.issue.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.issue.user.login, merged: merged} + when 'PullRequestEvent', 'PullRequestReviewCommentEvent' + title = _.payload.pull_request.title.gsub('`', '\\\`') + merged = client.pull_merged?(_.repo.name, _.payload.pull_request.number) + url_to_detail[_.payload.pull_request.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.pull_request.user.login, merged: merged} + end end - end - url_to_detail.each do |url, detail| - line = "* [#{detail[:title]} - #{detail[:repo_basename]}](#{url}) by #{detail[:username]}" - line << ' **merged!**' if detail[:merged] - puts line + url_to_detail.each do |url, detail| + line = "* [#{detail[:title]} - #{detail[:repo_basename]}](#{url}) by #{detail[:username]}" + line << ' **merged!**' if detail[:merged] + puts line + end end - end - private + private - def self.user - case - when ENV['GITHUB_NIPPOU_USER'] - ENV['GITHUB_NIPPOU_USER'] - when !`git config github-nippou.user`.chomp.empty? - `git config github-nippou.user`.chomp - else - puts < Date: Mon, 21 Mar 2016 19:53:39 +0900 Subject: [PATCH 03/11] Refactor using nil guard --- lib/github/nippou.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index 728fd41..f9829c6 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -34,7 +34,7 @@ def list private def user - case + @user ||= case when ENV['GITHUB_NIPPOU_USER'] ENV['GITHUB_NIPPOU_USER'] when !`git config github-nippou.user`.chomp.empty? @@ -51,7 +51,7 @@ def user end def access_token - case + @access_token ||= case when ENV['GITHUB_NIPPOU_ACCESS_TOKEN'] ENV['GITHUB_NIPPOU_ACCESS_TOKEN'] when !`git config github-nippou.token`.chomp.empty? From f3f91ced7748215fcb518d3a9e4d7cb5b7bf99e3 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 19:56:13 +0900 Subject: [PATCH 04/11] Fix bad variable name --- lib/github/nippou.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index f9829c6..c208dac 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -10,17 +10,17 @@ def list url_to_detail = {} - events.each do |_| - break unless _.created_at.getlocal.to_date == Time.now.to_date - case _.type + events.each do |e| + break unless e.created_at.getlocal.to_date == Time.now.to_date + case e.type when 'IssuesEvent', 'IssueCommentEvent' - title = _.payload.issue.title.gsub('`', '\\\`') - merged = client.pull_merged?(_.repo.name, _.payload.issue.number) - url_to_detail[_.payload.issue.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.issue.user.login, merged: merged} + title = e.payload.issue.title.gsub('`', '\\\`') + merged = client.pull_merged?(e.repo.name, e.payload.issue.number) + url_to_detail[e.payload.issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: e.payload.issue.user.login, merged: merged} when 'PullRequestEvent', 'PullRequestReviewCommentEvent' - title = _.payload.pull_request.title.gsub('`', '\\\`') - merged = client.pull_merged?(_.repo.name, _.payload.pull_request.number) - url_to_detail[_.payload.pull_request.html_url] ||= {title: title, repo_basename: _.repo.name, username: _.payload.pull_request.user.login, merged: merged} + title = e.payload.pull_request.title.gsub('`', '\\\`') + merged = client.pull_merged?(e.repo.name, e.payload.pull_request.number) + url_to_detail[e.payload.pull_request.html_url] ||= {title: title, repo_basename: e.repo.name, username: e.payload.pull_request.user.login, merged: merged} end end From 66dba6ac7e9d3bf23338420f6f9603fa1f177da6 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 19:57:18 +0900 Subject: [PATCH 05/11] Only once getting time --- lib/github/nippou.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index c208dac..ba742f8 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -9,9 +9,10 @@ def list events = client.user_events(user) url_to_detail = {} + now = Time.now events.each do |e| - break unless e.created_at.getlocal.to_date == Time.now.to_date + break unless e.created_at.getlocal.to_date == now.to_date case e.type when 'IssuesEvent', 'IssueCommentEvent' title = e.payload.issue.title.gsub('`', '\\\`') From 2cd7e6cea462b159ceff3f6bc0f7ad9cbc8bdc8c Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:01:47 +0900 Subject: [PATCH 06/11] Refactor using explanatory variables --- lib/github/nippou.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index ba742f8..d85feb9 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -15,13 +15,15 @@ def list break unless e.created_at.getlocal.to_date == now.to_date case e.type when 'IssuesEvent', 'IssueCommentEvent' - title = e.payload.issue.title.gsub('`', '\\\`') - merged = client.pull_merged?(e.repo.name, e.payload.issue.number) - url_to_detail[e.payload.issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: e.payload.issue.user.login, merged: merged} + issue = e.payload.issue + title = issue.title.gsub('`', '\\\`') + merged = client.pull_merged?(e.repo.name, issue.number) + url_to_detail[issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: issue.user.login, merged: merged} when 'PullRequestEvent', 'PullRequestReviewCommentEvent' - title = e.payload.pull_request.title.gsub('`', '\\\`') - merged = client.pull_merged?(e.repo.name, e.payload.pull_request.number) - url_to_detail[e.payload.pull_request.html_url] ||= {title: title, repo_basename: e.repo.name, username: e.payload.pull_request.user.login, merged: merged} + pr = e.payload.pull_request + title = pr.title.gsub('`', '\\\`') + merged = client.pull_merged?(e.repo.name, pr.number) + url_to_detail[pr.html_url] ||= {title: title, repo_basename: e.repo.name, username: pr.user.login, merged: merged} end end From 96feb1d3eb76b9a49ed610fc54ddba3c0da42d59 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:08:36 +0900 Subject: [PATCH 07/11] Refactor using refinements --- lib/github/nippou.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index d85feb9..79b2f27 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -1,9 +1,19 @@ require 'github/nippou/version' require 'octokit' +module StringExMarkdown + refine String do + def markdown_escape + self.gsub('`', '\\\`') + end + end +end + module Github module Nippou class << self + using StringExMarkdown + def list client = Octokit::Client.new(login: user, access_token: access_token) events = client.user_events(user) @@ -16,12 +26,12 @@ def list case e.type when 'IssuesEvent', 'IssueCommentEvent' issue = e.payload.issue - title = issue.title.gsub('`', '\\\`') + title = issue.title.markdown_escape merged = client.pull_merged?(e.repo.name, issue.number) url_to_detail[issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: issue.user.login, merged: merged} when 'PullRequestEvent', 'PullRequestReviewCommentEvent' pr = e.payload.pull_request - title = pr.title.gsub('`', '\\\`') + title = pr.title.markdown_escape merged = client.pull_merged?(e.repo.name, pr.number) url_to_detail[pr.html_url] ||= {title: title, repo_basename: e.repo.name, username: pr.user.login, merged: merged} end From aa04b26ea96281b77d7c1e9fcd61946ae06bce92 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:12:01 +0900 Subject: [PATCH 08/11] Improve variable name --- lib/github/nippou.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index 79b2f27..480de03 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -18,7 +18,7 @@ def list client = Octokit::Client.new(login: user, access_token: access_token) events = client.user_events(user) - url_to_detail = {} + nippous = {} now = Time.now events.each do |e| @@ -28,16 +28,16 @@ def list issue = e.payload.issue title = issue.title.markdown_escape merged = client.pull_merged?(e.repo.name, issue.number) - url_to_detail[issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: issue.user.login, merged: merged} + nippous[issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: issue.user.login, merged: merged} when 'PullRequestEvent', 'PullRequestReviewCommentEvent' pr = e.payload.pull_request title = pr.title.markdown_escape merged = client.pull_merged?(e.repo.name, pr.number) - url_to_detail[pr.html_url] ||= {title: title, repo_basename: e.repo.name, username: pr.user.login, merged: merged} + nippous[pr.html_url] ||= {title: title, repo_basename: e.repo.name, username: pr.user.login, merged: merged} end end - url_to_detail.each do |url, detail| + nippous.each do |url, detail| line = "* [#{detail[:title]} - #{detail[:repo_basename]}](#{url}) by #{detail[:username]}" line << ' **merged!**' if detail[:merged] puts line From 8a30b59eaad42833c1da970d65d672a3adc1507a Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:23:47 +0900 Subject: [PATCH 09/11] Refactor --- lib/github/nippou.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index 480de03..fb2f1b1 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -15,13 +15,10 @@ class << self using StringExMarkdown def list - client = Octokit::Client.new(login: user, access_token: access_token) - events = client.user_events(user) - nippous = {} now = Time.now - events.each do |e| + client.user_events(user).each do |e| break unless e.created_at.getlocal.to_date == now.to_date case e.type when 'IssuesEvent', 'IssueCommentEvent' @@ -46,6 +43,10 @@ def list private + def client + @client ||= Octokit::Client.new(login: user, access_token: access_token) + end + def user @user ||= case when ENV['GITHUB_NIPPOU_USER'] From 9533d1f106b07abe94e5e1157ea9e20ee0541b83 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:24:53 +0900 Subject: [PATCH 10/11] Fix variable name again --- lib/github/nippou.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index fb2f1b1..fbc7789 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -18,19 +18,19 @@ def list nippous = {} now = Time.now - client.user_events(user).each do |e| - break unless e.created_at.getlocal.to_date == now.to_date - case e.type + client.user_events(user).each do |event| + break unless event.created_at.getlocal.to_date == now.to_date + case event.type when 'IssuesEvent', 'IssueCommentEvent' - issue = e.payload.issue + issue = event.payload.issue title = issue.title.markdown_escape - merged = client.pull_merged?(e.repo.name, issue.number) - nippous[issue.html_url] ||= {title: title, repo_basename: e.repo.name, username: issue.user.login, merged: merged} + merged = client.pull_merged?(event.repo.name, issue.number) + nippous[issue.html_url] ||= {title: title, repo_basename: event.repo.name, username: issue.user.login, merged: merged} when 'PullRequestEvent', 'PullRequestReviewCommentEvent' - pr = e.payload.pull_request + pr = event.payload.pull_request title = pr.title.markdown_escape - merged = client.pull_merged?(e.repo.name, pr.number) - nippous[pr.html_url] ||= {title: title, repo_basename: e.repo.name, username: pr.user.login, merged: merged} + merged = client.pull_merged?(event.repo.name, pr.number) + nippous[pr.html_url] ||= {title: title, repo_basename: event.repo.name, username: pr.user.login, merged: merged} end end From 06fd7f57d807532a99fdfe04d5fd3a9d9df5e6bd Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Mon, 21 Mar 2016 20:28:39 +0900 Subject: [PATCH 11/11] Refactor --- lib/github/nippou.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index fbc7789..186cb17 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -23,14 +23,10 @@ def list case event.type when 'IssuesEvent', 'IssueCommentEvent' issue = event.payload.issue - title = issue.title.markdown_escape - merged = client.pull_merged?(event.repo.name, issue.number) - nippous[issue.html_url] ||= {title: title, repo_basename: event.repo.name, username: issue.user.login, merged: merged} + nippous[issue.html_url] ||= hash_for_issue(event.repo, issue) when 'PullRequestEvent', 'PullRequestReviewCommentEvent' pr = event.payload.pull_request - title = pr.title.markdown_escape - merged = client.pull_merged?(event.repo.name, pr.number) - nippous[pr.html_url] ||= {title: title, repo_basename: event.repo.name, username: pr.user.login, merged: merged} + nippous[pr.html_url] ||= hash_for_pr(event.repo, pr) end end @@ -84,6 +80,18 @@ def access_token exit! end end + + def hash_for_issue(repo, issue) + title = issue.title.markdown_escape + merged = client.pull_merged?(repo.name, issue.number) + {title: title, repo_basename: repo.name, username: issue.user.login, merged: merged} + end + + def hash_for_pr(repo, pr) + title = pr.title.markdown_escape + merged = client.pull_merged?(repo.name, pr.number) + {title: title, repo_basename: repo.name, username: pr.user.login, merged: merged} + end end end end