Class: Gitlab::Email::Message::RepositoryPush

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/email/message/repository_push.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notify, project_id, recipient, opts = {}) ⇒ RepositoryPush

Returns a new instance of RepositoryPush

Raises:

  • (ArgumentError)

14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/email/message/repository_push.rb', line 14

def initialize(notify, project_id, recipient, opts = {})
  raise ArgumentError, 'Missing options: author_id, ref, action' unless
    opts[:author_id] && opts[:ref] && opts[:action]

  @notify = notify
  @project_id = project_id
  @recipient = recipient
  @opts = opts.dup

  @author_id = @opts.delete(:author_id)
  @ref = @opts.delete(:ref)
  @action = @opts.delete(:action)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action


6
7
8
# File 'lib/gitlab/email/message/repository_push.rb', line 6

def action
  @action
end

#author_idObject (readonly)

Returns the value of attribute author_id


6
7
8
# File 'lib/gitlab/email/message/repository_push.rb', line 6

def author_id
  @author_id
end

#recipientObject

Returns the value of attribute recipient


5
6
7
# File 'lib/gitlab/email/message/repository_push.rb', line 5

def recipient
  @recipient
end

#refObject (readonly)

Returns the value of attribute ref


6
7
8
# File 'lib/gitlab/email/message/repository_push.rb', line 6

def ref
  @ref
end

Instance Method Details

#action_nameObject


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gitlab/email/message/repository_push.rb', line 68

def action_name
  @action_name ||=
    case @action
    when :create
      'pushed new'
    when :delete
      'deleted'
    else
      'pushed to'
    end
end

#authorObject


32
33
34
# File 'lib/gitlab/email/message/repository_push.rb', line 32

def author
  @author ||= User.find(@author_id)
end

#commitsObject


36
37
38
# File 'lib/gitlab/email/message/repository_push.rb', line 36

def commits
  @commits ||= (Commit.decorate(compare.commits, project) if compare)
end

#compareObject


48
49
50
# File 'lib/gitlab/email/message/repository_push.rb', line 48

def compare
  @opts[:compare]
end

#compare_timeoutObject


52
53
54
# File 'lib/gitlab/email/message/repository_push.rb', line 52

def compare_timeout
  diffs.overflow? if diffs
end

#diffsObject


40
41
42
# File 'lib/gitlab/email/message/repository_push.rb', line 40

def diffs
  @diffs ||= (compare.diffs if compare)
end

#diffs_countObject


44
45
46
# File 'lib/gitlab/email/message/repository_push.rb', line 44

def diffs_count
  diffs.count if diffs
end

#disable_diffs?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'lib/gitlab/email/message/repository_push.rb', line 60

def disable_diffs?
  @opts[:disable_diffs] || false
end

#projectObject


28
29
30
# File 'lib/gitlab/email/message/repository_push.rb', line 28

def project
  @project ||= Project.find(@project_id)
end

#ref_nameObject


80
81
82
# File 'lib/gitlab/email/message/repository_push.rb', line 80

def ref_name
  @ref_name ||= Gitlab::Git.ref_name(@ref)
end

#ref_typeObject


84
85
86
# File 'lib/gitlab/email/message/repository_push.rb', line 84

def ref_type
  @ref_type ||= Gitlab::Git.tag_ref?(@ref) ? 'tag' : 'branch'
end

#reply_toObject


107
108
109
110
111
112
113
# File 'lib/gitlab/email/message/repository_push.rb', line 107

def reply_to
  if send_from_committer_email? && @notify.can_send_from_user_email?(author)
    author.email
  else
    Gitlab.config.gitlab.email_reply_to
  end
end

#reverse_compare?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/gitlab/email/message/repository_push.rb', line 56

def reverse_compare?
  @opts[:reverse_compare] || false
end

#send_from_committer_email?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/gitlab/email/message/repository_push.rb', line 64

def send_from_committer_email?
  @opts[:send_from_committer_email] || false
end

#subjectObject


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gitlab/email/message/repository_push.rb', line 115

def subject
  subject_text = '[Git]'
  subject_text << "[#{project.path_with_namespace}]"
  subject_text << "[#{ref_name}]" if @action == :push
  subject_text << ' '

  if @action == :push && commits
    if commits.length > 1
      subject_text << "Deleted " if reverse_compare?
      subject_text << "#{commits.length} commits: #{commits.first.title}"
    else
      subject_text << "Deleted 1 commit: " if reverse_compare?
      subject_text << commits.first.title
    end
  else
    subject_action = action_name.dup
    subject_action[0] = subject_action[0].capitalize
    subject_text << "#{subject_action} #{ref_type} #{ref_name}"
  end
end

#target_urlObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab/email/message/repository_push.rb', line 88

def target_url
  if @action == :push && commits
    if commits.length > 1
      namespace_project_compare_url(project_namespace,
                                    project,
                                    from: Commit.new(compare.base, project),
                                    to:   Commit.new(compare.head, project))
    else
      namespace_project_commit_url(project_namespace,
                                   project, commits.first)
    end
  else
    unless @action == :delete
      namespace_project_tree_url(project_namespace,
                                 project, ref_name)
    end
  end
end