From d8064e9610a571e1c7e26ee67dbf287fd2b72a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Moz=C5=ABras?= Date: Sun, 10 Aug 2014 20:32:54 +0300 Subject: [PATCH] Performance improvement - cache comments retrieved from GitHub --- CHANGELOG.md | 6 +++++ lib/pronto/github.rb | 10 +++++-- .../formatter/github_formattter_spec.rb | 2 +- spec/pronto/github_spec.rb | 27 +++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spec/pronto/github_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 77a380ad..ad1a2e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.3.0 +### New features + ### Changes * [#29](https://github.com/mmozuras/pronto/issues/29): Be compatible and depend on rugged '0.21.0'. +* Performance improvement - use Rugged::Blame instead of one provided by Grit. +* Performance improvement - cache comments retrieved from GitHub. + +### Bugs fixed diff --git a/lib/pronto/github.rb b/lib/pronto/github.rb index 2d2e06a0..fc8b58f5 100644 --- a/lib/pronto/github.rb +++ b/lib/pronto/github.rb @@ -1,8 +1,14 @@ module Pronto class Github + def initialize + @comment_cache = {} + end + def commit_comments(repo, sha) - client.commit_comments(repo, sha).map do |comment| - Comment.new(repo, sha, comment.body, comment.path, comment.body) + @comment_cache["#{repo}/#{sha}"] ||= begin + client.commit_comments(repo, sha).map do |comment| + Comment.new(repo, sha, comment.body, comment.path, comment.body) + end end end diff --git a/spec/pronto/formatter/github_formattter_spec.rb b/spec/pronto/formatter/github_formattter_spec.rb index 0980295f..b6515541 100644 --- a/spec/pronto/formatter/github_formattter_spec.rb +++ b/spec/pronto/formatter/github_formattter_spec.rb @@ -16,7 +16,7 @@ module Formatter specify do Octokit::Client.any_instance .should_receive(:commit_comments) - .twice + .once .and_return([]) Octokit::Client.any_instance diff --git a/spec/pronto/github_spec.rb b/spec/pronto/github_spec.rb new file mode 100644 index 00000000..208cfc85 --- /dev/null +++ b/spec/pronto/github_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +module Pronto + describe Github do + let(:github) { Github.new } + + describe '#commit_comments' do + subject { github.commit_comments(repo, sha) } + + context 'three requests for same comments' do + let(:repo) { 'mmozuras/pronto' } + let(:sha) { '61e4bef' } + + specify do + Octokit::Client.any_instance + .should_receive(:commit_comments) + .once + .and_return([]) + + subject + subject + subject + end + end + end + end +end