Class: EmailsOnPushWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker
Defined in:
app/workers/emails_on_push_worker.rb

Overview

rubocop:disable Scalability/IdempotentWorker

Constant Summary

Constants included from ApplicationWorker

ApplicationWorker::LOGGING_EXTRA_KEY, ApplicationWorker::SAFE_PUSH_BULK_LIMIT

Constants included from Gitlab::Loggable

Gitlab::Loggable::ANONYMOUS

Constants included from WorkerAttributes

WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::DEFAULT_DEFER_DELAY, WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_DATA_CONSISTENCIES, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Loggable

#build_structured_payload

Methods included from Gitlab::SidekiqVersioning::Worker

#job_version

Methods included from WorkerContext

#with_context

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



10
11
12
# File 'app/workers/emails_on_push_worker.rb', line 10

def email
  @email
end

#skip_premailerObject (readonly)

Returns the value of attribute skip_premailer.



10
11
12
# File 'app/workers/emails_on_push_worker.rb', line 10

def skip_premailer
  @skip_premailer
end

Instance Method Details

#perform(project_id, recipients, push_data, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/workers/emails_on_push_worker.rb', line 17

def perform(project_id, recipients, push_data, options = {})
  options.symbolize_keys!
  options.reverse_merge!(
    send_from_committer_email: false,
    disable_diffs: false
  )
  send_from_committer_email = options[:send_from_committer_email]
  disable_diffs = options[:disable_diffs]

  project = Project.find(project_id)
  before_sha = push_data["before"]
  after_sha = push_data["after"]
  ref = push_data["ref"]
  author_id = push_data["user_id"]

  action =
    if Gitlab::Git.blank_ref?(before_sha)
      :create
    elsif Gitlab::Git.blank_ref?(after_sha)
      :delete
    else
      :push
    end

  diff_refs = nil
  compare = nil
  reverse_compare = false

  if action == :push
    compare = CompareService.new(project, after_sha)
      .execute(project, before_sha)
    diff_refs = compare.diff_refs

    return false if compare.same

    if compare.commits.empty?
      compare = CompareService.new(project, before_sha)
        .execute(project, after_sha)
      diff_refs = compare.diff_refs

      reverse_compare = true

      return false if compare.commits.empty?
    end
  end

  Integrations::EmailsOnPush.valid_recipients(recipients).each do |recipient|
    send_email(
      recipient,
      project_id,
      author_id: author_id,
      ref: ref,
      action: action,
      compare: compare,
      reverse_compare: reverse_compare,
      diff_refs: diff_refs,
      send_from_committer_email: send_from_committer_email,
      disable_diffs: disable_diffs
    )

  # 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.full_name}' to #{recipient}: #{e}")
  end
ensure
  @email = nil
  compare = nil
  GC.start
end