1
# encoding: utf-8
2
#--
3
#   Copyright (C) 2011 Gitorious AS
4
#
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.
9
#
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.
14
#
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
#++
18
19
# This is required because ActiveMessaging actually forcefully loads
20
# all processors before initializers are run. Hopefully this can go away
21
# when the vendored ActiveMessaging plugin is removed.
22
require File.join(Rails.root, "config/initializers/messaging")
23
24
class PushProcessor
25
  include Gitorious::Messaging::Consumer
26
  consumes "/queue/GitoriousPush"
27
  attr_reader :user, :repository, :spec
28
29
  def on_message(message)
30
    load_message(message)
31
    log_message("Got payload: #{spec} from user #{user && user.login}")
32
33
    if spec.merge_request?
34
      log_message("Processing merge request")
35
      process_merge_request
36
    elsif repository.wiki?
37
      log_message("Processing wiki update")
38
      process_wiki_update
39
    else
40
      log_message("Processing regular push")
41
      process_push
42
    end
43
  end
44
45
  def process_merge_request
46
    return if spec.action_delete?
47
    merge_request.update_from_push!
48
  end
49
50
  def process_push
51
    ensure_user
52
    logger = PushEventLogger.new(repository, spec, user)
53
    logger.create_push_event if logger.create_push_event?
54
    logger.create_meta_event if logger.create_meta_event?
55
    repository.register_push
56
    repository.save
57
    trigger_hooks unless repository.hooks.blank?
58
  end
59
60
  def trigger_hooks
61
    generator = Gitorious::WebHookGenerator.new(repository, spec, user)
62
    generator.generate!
63
  end
64
65
  def process_wiki_update
66
    ensure_user
67
    logger = Gitorious::Wiki::UpdateEventLogger.new(repository, spec, user)
68
    logger.create_wiki_events
69
  end
70
71
  def load_message(message)
72
    @user = User.find_by_login(message["username"])
73
    @repository = Repository.find_by_hashed_path(message["gitdir"])
74
    @spec = PushSpecParser.new(*message["message"].split(" "))
75
  end
76
77
  private
78
  
79
  def merge_request
80
    @repository.merge_requests.find_by_sequence_number!(@spec.ref_name.to_i)
81
  end
82
83
  def ensure_user
84
    raise "Username was nil" if user.nil?
85
  end
86
87
  def log_message(message)
88
    logger.info("#{Time.now.to_s(:short)} #{message} to repository #{repository.gitdir}")
89
  end
90
end