Don't show timeout message for empty diffs (ie. blank file added)
[gitorious.git] / lib / push_event_logger.rb
blobf70123bfed6c9ef4b3d164abc69248239edb676f
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011 Gitorious AS
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
19 class PushEventLogger
20   PUSH_EVENT_DATA_SEPARATOR = "$"
22   def initialize(repository, spec, user, pushed_at = nil)
23     @repository = repository
24     @spec = spec
25     @user = user
26     @pushed_at = pushed_at
27   end
29   def create_meta_event?
30     !@spec.action_update?
31   end
33   def create_push_event?
34     (@spec.action_update? || @spec.action_create?) && @spec.head?
35   end
37   def build_meta_event
38     Event.new(:action => meta_event_type, :project => @repository.project,
39       :user => @user, :target => @repository, :data => @spec.ref_name,
40       :body => meta_event_body, :created_at => @pushed_at)
41   end
43   def create_meta_event
44     event = build_meta_event
45     event.save!
46     event
47   end
49   def build_push_event
50     Event.new(:user => @user, :project => @repository.project, :target => @repository,
51       :action => Action::PUSH_SUMMARY, :created_at => @pushed_at)
52   end
54   def create_push_event
55     event = build_push_event
56     event.data = push_event_data
57     event.save
58     event
59   end
61   def push_commit_extractor
62     @push_commit_extractor ||= PushCommitExtractor.new(@repository.full_repository_path, @spec)
63   end
65   def push_event_data
66     [calculate_first_commit(@spec).oid, @spec.to_sha.sha, @spec.ref_name, calculate_commit_count.to_s].join(PUSH_EVENT_DATA_SEPARATOR)
67   end
69   def calculate_first_commit(spec)
70     push_commit_extractor.newest_known_commit
71   end
73   def self.parse_event_data(data_string)
74     start_sha, end_sha, branch_name, commit_count = data_string.split(PUSH_EVENT_DATA_SEPARATOR)
75     {
76       :start_sha       => start_sha,
77       :start_sha_short => start_sha[0,7],
78       :end_sha         => end_sha,
79       :end_sha_short   => end_sha[0,7],
80       :branch          => branch_name,
81       :commit_count    => commit_count
82     }
83   end
85   def calculate_commit_count
86     push_commit_extractor.new_commits.count
87   end
89   private
90   def meta_event_type
91     return head_meta_event_type if @spec.head?
92     tag_meta_event_type
93   end
95   def head_meta_event_type
96     @spec.action_create? ? Action::CREATE_BRANCH : Action::DELETE_BRANCH
97   end
99   def tag_meta_event_type
100     @spec.action_create? ? Action::CREATE_TAG : Action::DELETE_TAG
101   end
103   def meta_event_body
104     meta_body(@spec.head? ? "branch" : "tag")
105   end
107   def meta_body(type)
108     @spec.action_create? ? "Created #{type} #{@spec.ref_name}" : "Deleted #{type} #{@spec.ref_name}"
109   end