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/>.
20 PUSH_EVENT_DATA_SEPARATOR = "$"
22 def initialize(repository, spec, user, pushed_at = nil)
23 @repository = repository
26 @pushed_at = pushed_at
29 def create_meta_event?
33 def create_push_event?
34 (@spec.action_update? || @spec.action_create?) && @spec.head?
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)
44 event = build_meta_event
50 Event.new(:user => @user, :project => @repository.project, :target => @repository,
51 :action => Action::PUSH_SUMMARY, :created_at => @pushed_at)
55 event = build_push_event
56 event.data = push_event_data
61 def push_commit_extractor
62 @push_commit_extractor ||= PushCommitExtractor.new(@repository.full_repository_path, @spec)
66 [calculate_first_commit(@spec).oid, @spec.to_sha.sha, @spec.ref_name, calculate_commit_count.to_s].join(PUSH_EVENT_DATA_SEPARATOR)
69 def calculate_first_commit(spec)
70 push_commit_extractor.newest_known_commit
73 def self.parse_event_data(data_string)
74 start_sha, end_sha, branch_name, commit_count = data_string.split(PUSH_EVENT_DATA_SEPARATOR)
76 :start_sha => start_sha,
77 :start_sha_short => start_sha[0,7],
79 :end_sha_short => end_sha[0,7],
80 :branch => branch_name,
81 :commit_count => commit_count
85 def calculate_commit_count
86 push_commit_extractor.new_commits.count
91 return head_meta_event_type if @spec.head?
95 def head_meta_event_type
96 @spec.action_create? ? Action::CREATE_BRANCH : Action::DELETE_BRANCH
99 def tag_meta_event_type
100 @spec.action_create? ? Action::CREATE_TAG : Action::DELETE_TAG
104 meta_body(@spec.head? ? "branch" : "tag")
108 @spec.action_create? ? "Created #{type} #{@spec.ref_name}" : "Deleted #{type} #{@spec.ref_name}"