BigW Consortium Gitlab

emails_on_push_worker.rb 1.94 KB
Newer Older
1 2 3
class EmailsOnPushWorker
  include Sidekiq::Worker

Douwe Maan committed
4 5 6
  def perform(project_id, recipients, push_data, options = {})
    options.symbolize_keys!
    options.reverse_merge!(
7
      send_from_committer_email:  false,
Douwe Maan committed
8 9 10 11 12
      disable_diffs:              false
    )
    send_from_committer_email = options[:send_from_committer_email]
    disable_diffs = options[:disable_diffs]

13 14 15
    project = Project.find(project_id)
    before_sha = push_data["before"]
    after_sha = push_data["after"]
16
    ref = push_data["ref"]
17 18
    author_id = push_data["user_id"]

19
    action =
20
      if Gitlab::Git.blank_ref?(before_sha)
21
        :create
22 23 24 25 26
      elsif Gitlab::Git.blank_ref?(after_sha)
        :delete
      else
        :push
      end
27

28 29 30 31
    compare = nil
    reverse_compare = false
    if action == :push
      compare = Gitlab::Git::Compare.new(project.repository.raw_repository, before_sha, after_sha)
32

33
      return false if compare.same
34

35 36
      if compare.commits.empty?
        compare = Gitlab::Git::Compare.new(project.repository.raw_repository, after_sha, before_sha)
37

38
        reverse_compare = true
39

40 41
        return false if compare.commits.empty?
      end
42
    end
43 44

    recipients.split(" ").each do |recipient|
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
      begin
        Notify.repository_push_email(
          project_id,
          recipient,
          author_id:                  author_id,
          ref:                        ref,
          action:                     action,
          compare:                    compare,
          reverse_compare:            reverse_compare,
          send_from_committer_email:  send_from_committer_email,
          disable_diffs:              disable_diffs
        ).deliver
      # These are input errors and won't be corrected even if Sidekiq retries
      rescue Net::SMTPFatalError, Net::SMTPSyntaxError => e
        logger.info("Failed to send e-mail for project '#{project.name_with_namespace}' to #{recipient}: #{e}")
      end
61
    end
62 63 64
  ensure
    compare = nil
    GC.start
65 66
  end
end