{"action":"created","comment":{"url":"https://api.github.com/repos/opf/openproject/pulls/comments/35222966","id":35222966,"diff_hunk":"@@ -0,0 +1,202 @@\n+#-- encoding: UTF-8\n+#-- copyright\n+# OpenProject is a project management system.\n+# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)\n+#\n+# This program is free software; you can redistribute it and/or\n+# modify it under the terms of the GNU General Public License version 3.\n+#\n+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:\n+# Copyright (C) 2006-2013 Jean-Philippe Lang\n+# Copyright (C) 2010-2013 the ChiliProject Team\n+#\n+# This program is free software; you can redistribute it and/or\n+# modify it under the terms of the GNU General Public License\n+# as published by the Free Software Foundation; either version 2\n+# of the License, or (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program; if not, write to the Free Software\n+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+#\n+# See doc/COPYRIGHT.rdoc for more details.\n+#++require 'rspec'\n+\n+require 'spec_helper'\n+\n+RSpec::Matchers.define :be_equivalent_to_journal do |expected|\n+  ignored_attributes = [:notes_id]\n+\n+  match do |actual|\n+    expected_attributes = get_normalized_attributes expected\n+    actual_attributes = get_normalized_attributes actual\n+\n+    expected_attributes.except(*ignored_attributes) == actual_attributes.except(*ignored_attributes)\n+  end\n+\n+  failure_message do |actual|\n+    expected_attributes = get_normalized_attributes expected\n+    actual_attributes = actual.attributes.symbolize_keys\n+    [\"expected attributes: #{display_sorted_hash(expected_attributes.except(*ignored_attributes))}\",\n+     \"actual attributes:   #{display_sorted_hash(actual_attributes.except(*ignored_attributes))}\"]\n+      .join($/)\n+  end\n+\n+  def get_normalized_attributes(journal)\n+    result = journal.attributes.symbolize_keys\n+    if result[:created_at]\n+      # µs are not stored in all DBMS\n+      result[:created_at] = result[:created_at].change(usec: 0)\n+    end\n+\n+    result\n+  end\n+\n+  def display_sorted_hash(hash)\n+    '{ ' + hash.sort.map { |k, v| \"#{k.inspect}=>#{v.inspect}\" }.join(', ') + ' }'\n+  end\n+end\n+\n+describe Journal::AggregatedJournal, type: :model do\n+  let(:work_package) {\n+    FactoryGirl.build(:work_package)\n+  }\n+  let(:user1) { FactoryGirl.create(:user) }\n+  let(:user2) { FactoryGirl.create(:user) }\n+  let(:initial_author) { user1 }\n+\n+  subject { described_class.order('id ASC').to_a }\n+\n+  before do\n+    allow(User).to receive(:current).and_return(initial_author)\n+    work_package.save!\n+  end\n+\n+  it 'returns the one and only journal' do\n+    expect(subject.count).to eql 1\n+    expect(subject.first).to be_equivalent_to_journal work_package.journals.first\n+  end\n+\n+  it 'also indicates its ID via notes_id' do\n+    expect(subject.first.notes_id).to eql work_package.journals.first.id\n+  end\n+\n+  it 'is the initial journal' do\n+    expect(subject.first.initial?).to be_truthy\n+  end\n+\n+  context 'WP updated immediately after uncommented change' do\n+    let(:notes) { nil }\n+\n+    before do\n+      changes = { status: FactoryGirl.build(:status) }\n+      changes[:notes] = notes if notes\n+\n+      expect(work_package.update_by!(new_author, changes)).to be_truthy\n+    end\n+\n+    context 'by author of last change' do\n+      let(:new_author) { initial_author }\n+\n+      it 'returns a single aggregated journal' do\n+        expect(subject.count).to eql 1\n+        expect(subject.first).to be_equivalent_to_journal work_package.journals.second\n+      end\n+\n+      it 'is the initial journal' do\n+        expect(subject.first.initial?).to be_truthy\n+      end\n+\n+      context 'with a comment' do\n+        let(:notes) { 'This is why I changed it.' }\n+\n+        it 'returns a single aggregated journal' do\n+          expect(subject.count).to eql 1\n+          expect(subject.first).to be_equivalent_to_journal work_package.journals.second\n+        end\n+\n+        context 'adding a second comment' do\n+          before do\n+            expect(work_package.update_by!(new_author, notes: notes)).to be_truthy\n+          end\n+\n+          it 'returns two journals' do\n+            expect(subject.count).to eql 2\n+            expect(subject.first).to be_equivalent_to_journal work_package.journals.second\n+            expect(subject.second).to be_equivalent_to_journal work_package.journals.last\n+          end\n+\n+          it 'has one initial journal and one non-initial journal' do\n+            expect(subject.first.initial?).to be_truthy\n+            expect(subject.second.initial?).to be_falsey\n+          end\n+        end\n+\n+        context 'adding another change without comment' do\n+          before do\n+            work_package.reload # need to update the lock_version, avoiding StaleObjectError\n+            expect(work_package.update_by!(new_author, subject: 'foo')).to be_truthy\n+          end\n+\n+          it 'returns a single journal' do\n+            expect(subject.count).to eql 1\n+          end\n+\n+          it 'combines the notes of the earlier journal with attributes of the later journal' do\n+            expected_journal = work_package.journals.last\n+            expected_journal.notes = work_package.journals.second.notes\n+\n+            expect(subject.first).to be_equivalent_to_journal expected_journal\n+          end\n+\n+          it 'indicates the ID of the earlier journal via notes_id' do\n+            expect(subject.first.notes_id).to eql work_package.journals.second.id\n+          end\n+        end\n+      end\n+    end\n+\n+    context 'by a different author' do\n+      let(:new_author) { user2 }\n+\n+      it 'returns both journals' do\n+        expect(subject.count).to eql 2\n+        expect(subject.first).to be_equivalent_to_journal work_package.journals.first\n+        expect(subject.second).to be_equivalent_to_journal work_package.journals.second\n+      end\n+    end\n+  end\n+\n+  context 'WP updated after aggregation timeout expired' do\n+    before do\n+      work_package.status = FactoryGirl.build(:status)\n+      work_package.save!\n+      work_package.journals.second.created_at += 1.day # one day delay should always be long enough","path":"spec/models/journal/aggregated_journal_spec.rb","position":179,"original_position":179,"commit_id":"a8ab1024b5cc474b47f9403c725b34d028fe35d3","original_commit_id":"a8ab1024b5cc474b47f9403c725b34d028fe35d3","user":{"login":"Azure7111","id":1461183,"avatar_url":"https://avatars.githubusercontent.com/u/1461183?v=3","gravatar_id":"","url":"https://api.github.com/users/Azure7111","html_url":"https://github.com/Azure7111","followers_url":"https://api.github.com/users/Azure7111/followers","following_url":"https://api.github.com/users/Azure7111/following{/other_user}","gists_url":"https://api.github.com/users/Azure7111/gists{/gist_id}","starred_url":"https://api.github.com/users/Azure7111/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Azure7111/subscriptions","organizations_url":"https://api.github.com/users/Azure7111/orgs","repos_url":"https://api.github.com/users/Azure7111/repos","events_url":"https://api.github.com/users/Azure7111/events{/privacy}","received_events_url":"https://api.github.com/users/Azure7111/received_events","type":"User","site_admin":false},"body":"Why don't you use something like ``Setting.journal_aggregation_time_minutes + 1``?","created_at":"2015-07-22T15:05:19Z","updated_at":"2015-07-22T15:05:19Z","html_url":"https://github.com/opf/openproject/pull/3230#discussion_r35222966","pull_request_url":"https://api.github.com/repos/opf/openproject/pulls/3230","_links":{"self":{"href":"https://api.github.com/repos/opf/openproject/pulls/comments/35222966"},"html":{"href":"https://github.com/opf/openproject/pull/3230#discussion_r35222966"},"pull_request":{"href":"https://api.github.com/repos/opf/openproject/pulls/3230"}}},"pull_request":{"url":"https://api.github.com/repos/opf/openproject/pulls/3230","id":39684618,"html_url":"https://github.com/opf/openproject/pull/3230","diff_url":"https://github.com/opf/openproject/pull/3230.diff","patch_url":"https://github.com/opf/openproject/pull/3230.patch","issue_url":"https://api.github.com/repos/opf/openproject/issues/3230","number":3230,"state":"open","locked":false,"title":"Journal aggregation (SQL based approach)","user":{"login":"NobodysNightmare","id":453584,"avatar_url":"https://avatars.githubusercontent.com/u/453584?v=3","gravatar_id":"","url":"https://api.github.com/users/NobodysNightmare","html_url":"https://github.com/NobodysNightmare","followers_url":"https://api.github.com/users/NobodysNightmare/followers","following_url":"https://api.github.com/users/NobodysNightmare/following{/other_user}","gists_url":"https://api.github.com/users/NobodysNightmare/gists{/gist_id}","starred_url":"https://api.github.com/users/NobodysNightmare/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NobodysNightmare/subscriptions","organizations_url":"https://api.github.com/users/NobodysNightmare/orgs","repos_url":"https://api.github.com/users/NobodysNightmare/repos","events_url":"https://api.github.com/users/NobodysNightmare/events{/privacy}","received_events_url":"https://api.github.com/users/NobodysNightmare/received_events","type":"User","site_admin":false},"body":"## OpenProject work package\r\n\r\nhttps://community.openproject.org/work_packages/20686\r\n\r\n## Dependencies\r\n\r\nThis PR depends on https://github.com/opf/openproject/pull/3247\r\n\r\n## Description\r\n\r\nThis PR adds the ability to obtain aggregated journals. Journals are aggregated if they succeed eachother on the same journalizable and were issued within a defined timespan.\r\n\r\nThis PR chooses a different implementation approach than https://github.com/opf/openproject/pull/3216, which I do not want to throw away (yet).\r\n\r\n## TODO\r\n\r\n* [x] lookup a journal for editing (by id, exposed via `notes_id`)\r\n* ~~determine earliest/latest `created_at`~~\r\n* [x] create index over (`journalized_type`, `journalized_id`, `version`)\r\n    * extracted into https://github.com/opf/openproject/pull/3247\r\n* [x] use the actual setting to determine the timespan\r\n* [ ] final performance test after rebasing onto https://github.com/opf/openproject/pull/3247","created_at":"2015-07-10T13:34:41Z","updated_at":"2015-07-22T15:05:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4de289f099b1fb7d6183e010aebf36b058ecd351","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/opf/openproject/pulls/3230/commits","review_comments_url":"https://api.github.com/repos/opf/openproject/pulls/3230/comments","review_comment_url":"https://api.github.com/repos/opf/openproject/pulls/comments{/number}","comments_url":"https://api.github.com/repos/opf/openproject/issues/3230/comments","statuses_url":"https://api.github.com/repos/opf/openproject/statuses/a8ab1024b5cc474b47f9403c725b34d028fe35d3","head":{"label":"NobodysNightmare:feature/sql_journal_aggregation","ref":"feature/sql_journal_aggregation","sha":"a8ab1024b5cc474b47f9403c725b34d028fe35d3","user":{"login":"NobodysNightmare","id":453584,"avatar_url":"https://avatars.githubusercontent.com/u/453584?v=3","gravatar_id":"","url":"https://api.github.com/users/NobodysNightmare","html_url":"https://github.com/NobodysNightmare","followers_url":"https://api.github.com/users/NobodysNightmare/followers","following_url":"https://api.github.com/users/NobodysNightmare/following{/other_user}","gists_url":"https://api.github.com/users/NobodysNightmare/gists{/gist_id}","starred_url":"https://api.github.com/users/NobodysNightmare/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NobodysNightmare/subscriptions","organizations_url":"https://api.github.com/users/NobodysNightmare/orgs","repos_url":"https://api.github.com/users/NobodysNightmare/repos","events_url":"https://api.github.com/users/NobodysNightmare/events{/privacy}","received_events_url":"https://api.github.com/users/NobodysNightmare/received_events","type":"User","site_admin":false},"repo":{"id":31598524,"name":"openproject","full_name":"NobodysNightmare/openproject","owner":{"login":"NobodysNightmare","id":453584,"avatar_url":"https://avatars.githubusercontent.com/u/453584?v=3","gravatar_id":"","url":"https://api.github.com/users/NobodysNightmare","html_url":"https://github.com/NobodysNightmare","followers_url":"https://api.github.com/users/NobodysNightmare/followers","following_url":"https://api.github.com/users/NobodysNightmare/following{/other_user}","gists_url":"https://api.github.com/users/NobodysNightmare/gists{/gist_id}","starred_url":"https://api.github.com/users/NobodysNightmare/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NobodysNightmare/subscriptions","organizations_url":"https://api.github.com/users/NobodysNightmare/orgs","repos_url":"https://api.github.com/users/NobodysNightmare/repos","events_url":"https://api.github.com/users/NobodysNightmare/events{/privacy}","received_events_url":"https://api.github.com/users/NobodysNightmare/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/NobodysNightmare/openproject","description":"OpenProject is a web-based project management system built on Ruby on Rails","fork":true,"url":"https://api.github.com/repos/NobodysNightmare/openproject","forks_url":"https://api.github.com/repos/NobodysNightmare/openproject/forks","keys_url":"https://api.github.com/repos/NobodysNightmare/openproject/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NobodysNightmare/openproject/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NobodysNightmare/openproject/teams","hooks_url":"https://api.github.com/repos/NobodysNightmare/openproject/hooks","issue_events_url":"https://api.github.com/repos/NobodysNightmare/openproject/issues/events{/number}","events_url":"https://api.github.com/repos/NobodysNightmare/openproject/events","assignees_url":"https://api.github.com/repos/NobodysNightmare/openproject/assignees{/user}","branches_url":"https://api.github.com/repos/NobodysNightmare/openproject/branches{/branch}","tags_url":"https://api.github.com/repos/NobodysNightmare/openproject/tags","blobs_url":"https://api.github.com/repos/NobodysNightmare/openproject/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NobodysNightmare/openproject/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NobodysNightmare/openproject/git/refs{/sha}","trees_url":"https://api.github.com/repos/NobodysNightmare/openproject/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NobodysNightmare/openproject/statuses/{sha}","languages_url":"https://api.github.com/repos/NobodysNightmare/openproject/languages","stargazers_url":"https://api.github.com/repos/NobodysNightmare/openproject/stargazers","contributors_url":"https://api.github.com/repos/NobodysNightmare/openproject/contributors","subscribers_url":"https://api.github.com/repos/NobodysNightmare/openproject/subscribers","subscription_url":"https://api.github.com/repos/NobodysNightmare/openproject/subscription","commits_url":"https://api.github.com/repos/NobodysNightmare/openproject/commits{/sha}","git_commits_url":"https://api.github.com/repos/NobodysNightmare/openproject/git/commits{/sha}","comments_url":"https://api.github.com/repos/NobodysNightmare/openproject/comments{/number}","issue_comment_url":"https://api.github.com/repos/NobodysNightmare/openproject/issues/comments{/number}","contents_url":"https://api.github.com/repos/NobodysNightmare/openproject/contents/{+path}","compare_url":"https://api.github.com/repos/NobodysNightmare/openproject/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NobodysNightmare/openproject/merges","archive_url":"https://api.github.com/repos/NobodysNightmare/openproject/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NobodysNightmare/openproject/downloads","issues_url":"https://api.github.com/repos/NobodysNightmare/openproject/issues{/number}","pulls_url":"https://api.github.com/repos/NobodysNightmare/openproject/pulls{/number}","milestones_url":"https://api.github.com/repos/NobodysNightmare/openproject/milestones{/number}","notifications_url":"https://api.github.com/repos/NobodysNightmare/openproject/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NobodysNightmare/openproject/labels{/name}","releases_url":"https://api.github.com/repos/NobodysNightmare/openproject/releases{/id}","created_at":"2015-03-03T13:00:18Z","updated_at":"2015-04-01T12:02:52Z","pushed_at":"2015-07-22T09:22:45Z","git_url":"git://github.com/NobodysNightmare/openproject.git","ssh_url":"git@github.com:NobodysNightmare/openproject.git","clone_url":"https://github.com/NobodysNightmare/openproject.git","svn_url":"https://github.com/NobodysNightmare/openproject","homepage":"https://www.openproject.org","size":65447,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"dev"}},"base":{"label":"opf:release/4.3","ref":"release/4.3","sha":"63539001f0f1e970a9ec7c721fdef49a3033db83","user":{"login":"opf","id":1756674,"avatar_url":"https://avatars.githubusercontent.com/u/1756674?v=3","gravatar_id":"","url":"https://api.github.com/users/opf","html_url":"https://github.com/opf","followers_url":"https://api.github.com/users/opf/followers","following_url":"https://api.github.com/users/opf/following{/other_user}","gists_url":"https://api.github.com/users/opf/gists{/gist_id}","starred_url":"https://api.github.com/users/opf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opf/subscriptions","organizations_url":"https://api.github.com/users/opf/orgs","repos_url":"https://api.github.com/users/opf/repos","events_url":"https://api.github.com/users/opf/events{/privacy}","received_events_url":"https://api.github.com/users/opf/received_events","type":"Organization","site_admin":false},"repo":{"id":6899875,"name":"openproject","full_name":"opf/openproject","owner":{"login":"opf","id":1756674,"avatar_url":"https://avatars.githubusercontent.com/u/1756674?v=3","gravatar_id":"","url":"https://api.github.com/users/opf","html_url":"https://github.com/opf","followers_url":"https://api.github.com/users/opf/followers","following_url":"https://api.github.com/users/opf/following{/other_user}","gists_url":"https://api.github.com/users/opf/gists{/gist_id}","starred_url":"https://api.github.com/users/opf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opf/subscriptions","organizations_url":"https://api.github.com/users/opf/orgs","repos_url":"https://api.github.com/users/opf/repos","events_url":"https://api.github.com/users/opf/events{/privacy}","received_events_url":"https://api.github.com/users/opf/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/opf/openproject","description":"OpenProject is a web-based project management system built on Ruby on Rails","fork":false,"url":"https://api.github.com/repos/opf/openproject","forks_url":"https://api.github.com/repos/opf/openproject/forks","keys_url":"https://api.github.com/repos/opf/openproject/keys{/key_id}","collaborators_url":"https://api.github.com/repos/opf/openproject/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/opf/openproject/teams","hooks_url":"https://api.github.com/repos/opf/openproject/hooks","issue_events_url":"https://api.github.com/repos/opf/openproject/issues/events{/number}","events_url":"https://api.github.com/repos/opf/openproject/events","assignees_url":"https://api.github.com/repos/opf/openproject/assignees{/user}","branches_url":"https://api.github.com/repos/opf/openproject/branches{/branch}","tags_url":"https://api.github.com/repos/opf/openproject/tags","blobs_url":"https://api.github.com/repos/opf/openproject/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/opf/openproject/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/opf/openproject/git/refs{/sha}","trees_url":"https://api.github.com/repos/opf/openproject/git/trees{/sha}","statuses_url":"https://api.github.com/repos/opf/openproject/statuses/{sha}","languages_url":"https://api.github.com/repos/opf/openproject/languages","stargazers_url":"https://api.github.com/repos/opf/openproject/stargazers","contributors_url":"https://api.github.com/repos/opf/openproject/contributors","subscribers_url":"https://api.github.com/repos/opf/openproject/subscribers","subscription_url":"https://api.github.com/repos/opf/openproject/subscription","commits_url":"https://api.github.com/repos/opf/openproject/commits{/sha}","git_commits_url":"https://api.github.com/repos/opf/openproject/git/commits{/sha}","comments_url":"https://api.github.com/repos/opf/openproject/comments{/number}","issue_comment_url":"https://api.github.com/repos/opf/openproject/issues/comments{/number}","contents_url":"https://api.github.com/repos/opf/openproject/contents/{+path}","compare_url":"https://api.github.com/repos/opf/openproject/compare/{base}...{head}","merges_url":"https://api.github.com/repos/opf/openproject/merges","archive_url":"https://api.github.com/repos/opf/openproject/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/opf/openproject/downloads","issues_url":"https://api.github.com/repos/opf/openproject/issues{/number}","pulls_url":"https://api.github.com/repos/opf/openproject/pulls{/number}","milestones_url":"https://api.github.com/repos/opf/openproject/milestones{/number}","notifications_url":"https://api.github.com/repos/opf/openproject/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/opf/openproject/labels{/name}","releases_url":"https://api.github.com/repos/opf/openproject/releases{/id}","created_at":"2012-11-28T09:59:11Z","updated_at":"2015-07-22T13:44:48Z","pushed_at":"2015-07-22T14:12:11Z","git_url":"git://github.com/opf/openproject.git","ssh_url":"git@github.com:opf/openproject.git","clone_url":"https://github.com/opf/openproject.git","svn_url":"https://github.com/opf/openproject","homepage":"https://www.openproject.org","size":204486,"stargazers_count":684,"watchers_count":684,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":291,"mirror_url":null,"open_issues_count":31,"forks":291,"open_issues":31,"watchers":684,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/opf/openproject/pulls/3230"},"html":{"href":"https://github.com/opf/openproject/pull/3230"},"issue":{"href":"https://api.github.com/repos/opf/openproject/issues/3230"},"comments":{"href":"https://api.github.com/repos/opf/openproject/issues/3230/comments"},"review_comments":{"href":"https://api.github.com/repos/opf/openproject/pulls/3230/comments"},"review_comment":{"href":"https://api.github.com/repos/opf/openproject/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/opf/openproject/pulls/3230/commits"},"statuses":{"href":"https://api.github.com/repos/opf/openproject/statuses/a8ab1024b5cc474b47f9403c725b34d028fe35d3"}}}}