2015-11-18 09:13:34 +00:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Message
|
|
|
|
class RepositoryPush
|
|
|
|
attr_accessor :recipient
|
2015-11-20 14:29:47 +00:00
|
|
|
attr_reader :author_id, :ref, :action
|
2015-11-18 09:13:34 +00:00
|
|
|
|
2015-11-21 19:54:19 +00:00
|
|
|
delegate :namespace, :name_with_namespace, to: :project, prefix: :project
|
|
|
|
delegate :name, to: :author, prefix: :author
|
|
|
|
|
2015-11-18 09:13:34 +00:00
|
|
|
def initialize(notify, project_id, recipient, opts = {})
|
2015-11-20 14:29:47 +00:00
|
|
|
raise ArgumentError, 'Missing options: author_id, ref, action' unless
|
2015-11-18 09:13:34 +00:00
|
|
|
opts[:author_id] && opts[:ref] && opts[:action]
|
|
|
|
|
|
|
|
@notify = notify
|
|
|
|
@project_id = project_id
|
|
|
|
@recipient = recipient
|
2015-12-04 12:55:19 +00:00
|
|
|
@opts = opts.dup
|
2015-11-22 19:59:31 +00:00
|
|
|
@urls = Gitlab::Application.routes.url_helpers
|
2015-11-18 09:13:34 +00:00
|
|
|
|
2015-12-04 12:55:19 +00:00
|
|
|
@author_id = @opts.delete(:author_id)
|
|
|
|
@ref = @opts.delete(:ref)
|
|
|
|
@action = @opts.delete(:action)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
2015-11-20 14:29:47 +00:00
|
|
|
@project ||= Project.find(@project_id)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
2015-11-20 14:29:47 +00:00
|
|
|
@author ||= User.find(@author_id)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits
|
2015-11-20 14:29:47 +00:00
|
|
|
@commits ||= (Commit.decorate(compare.commits, project) if compare)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def diffs
|
2015-11-20 14:29:47 +00:00
|
|
|
@diffs ||= (compare.diffs if compare)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
2015-11-21 19:54:19 +00:00
|
|
|
def diffs_count
|
|
|
|
diffs.count if diffs
|
|
|
|
end
|
|
|
|
|
2015-11-20 14:29:47 +00:00
|
|
|
def compare
|
|
|
|
@opts[:compare]
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
2015-11-21 19:54:19 +00:00
|
|
|
def compare_timeout
|
|
|
|
compare.timeout if compare
|
|
|
|
end
|
|
|
|
|
2015-11-20 14:29:47 +00:00
|
|
|
def reverse_compare?
|
|
|
|
@opts[:reverse_compare] || false
|
|
|
|
end
|
2015-11-18 09:13:34 +00:00
|
|
|
|
2015-11-20 14:29:47 +00:00
|
|
|
def disable_diffs?
|
|
|
|
@opts[:disable_diffs] || false
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_from_committer_email?
|
|
|
|
@opts[:send_from_committer_email] || false
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_name
|
|
|
|
@action_name ||=
|
|
|
|
case @action
|
|
|
|
when :create
|
|
|
|
'pushed new'
|
|
|
|
when :delete
|
|
|
|
'deleted'
|
2015-11-18 09:13:34 +00:00
|
|
|
else
|
2015-11-20 14:29:47 +00:00
|
|
|
'pushed to'
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref_name
|
2015-11-20 14:29:47 +00:00
|
|
|
@ref_name ||= Gitlab::Git.ref_name(@ref)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref_type
|
2015-11-20 14:29:47 +00:00
|
|
|
@ref_type ||= Gitlab::Git.tag_ref?(@ref) ? 'tag' : 'branch'
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_url
|
|
|
|
if @action == :push
|
2015-11-20 14:29:47 +00:00
|
|
|
if commits.length > 1 && compare
|
2015-11-22 19:59:31 +00:00
|
|
|
@urls.namespace_project_compare_url(project_namespace,
|
2015-12-03 13:16:16 +00:00
|
|
|
project,
|
|
|
|
from: Commit.new(compare.base, project),
|
|
|
|
to: Commit.new(compare.head, project))
|
2015-11-18 09:13:34 +00:00
|
|
|
else
|
2015-11-22 19:59:31 +00:00
|
|
|
@urls.namespace_project_commit_url(project_namespace,
|
2015-12-03 13:16:16 +00:00
|
|
|
project, commits.first)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
unless @action == :delete
|
2015-11-22 19:59:31 +00:00
|
|
|
@urls.namespace_project_tree_url(project_namespace,
|
2015-12-03 13:16:16 +00:00
|
|
|
project, ref_name)
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to
|
2015-11-20 14:29:47 +00:00
|
|
|
if send_from_committer_email? && @notify.can_send_from_user_email?(author)
|
|
|
|
author.email
|
2015-11-18 09:13:34 +00:00
|
|
|
else
|
|
|
|
Gitlab.config.gitlab.email_reply_to
|
|
|
|
end
|
|
|
|
end
|
2015-11-20 14:29:47 +00:00
|
|
|
|
|
|
|
def subject
|
|
|
|
subject_text = '[Git]'
|
|
|
|
subject_text << "[#{project.path_with_namespace}]"
|
|
|
|
subject_text << "[#{ref_name}]" if @action == :push
|
|
|
|
subject_text << ' '
|
|
|
|
|
|
|
|
if @action == :push
|
|
|
|
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
|
2015-11-18 09:13:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|