2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-18 04:13:34 -05:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Message
|
|
|
|
class RepositoryPush
|
2015-11-20 09:29:47 -05:00
|
|
|
attr_reader :author_id, :ref, :action
|
2015-11-18 04:13:34 -05:00
|
|
|
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2016-05-12 11:06:14 -04:00
|
|
|
include DiffHelper
|
2015-12-11 08:00:24 -05:00
|
|
|
|
2015-11-21 14:54:19 -05:00
|
|
|
delegate :namespace, :name_with_namespace, to: :project, prefix: :project
|
|
|
|
delegate :name, to: :author, prefix: :author
|
2015-12-19 14:04:40 -05:00
|
|
|
delegate :username, to: :author, prefix: :author
|
2015-11-21 14:54:19 -05:00
|
|
|
|
2016-05-06 08:16:53 -04:00
|
|
|
def initialize(notify, project_id, opts = {})
|
2015-11-20 09:29:47 -05:00
|
|
|
raise ArgumentError, 'Missing options: author_id, ref, action' unless
|
2015-11-18 04:13:34 -05:00
|
|
|
opts[:author_id] && opts[:ref] && opts[:action]
|
|
|
|
|
|
|
|
@notify = notify
|
|
|
|
@project_id = project_id
|
2015-12-04 07:55:19 -05:00
|
|
|
@opts = opts.dup
|
2015-11-18 04:13:34 -05:00
|
|
|
|
2015-12-04 07:55:19 -05:00
|
|
|
@author_id = @opts.delete(:author_id)
|
|
|
|
@ref = @opts.delete(:ref)
|
|
|
|
@action = @opts.delete(:action)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
2015-11-20 09:29:47 -05:00
|
|
|
@project ||= Project.find(@project_id)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
2015-11-20 09:29:47 -05:00
|
|
|
@author ||= User.find(@author_id)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits
|
2016-06-20 12:51:48 -04:00
|
|
|
return unless compare
|
|
|
|
|
2016-07-27 07:09:52 -04:00
|
|
|
@commits ||= compare.commits
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def diffs
|
2016-06-20 12:51:48 -04:00
|
|
|
return unless compare
|
2016-07-26 03:21:42 -04:00
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
# This diff is more moderated in number of files and lines
|
2017-05-26 19:27:30 -04:00
|
|
|
@diffs ||= compare.diffs(max_files: 30, max_lines: 5000, expanded: true).diff_files
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
2015-11-21 14:54:19 -05:00
|
|
|
def diffs_count
|
2017-02-07 09:16:46 -05:00
|
|
|
diffs&.size
|
2015-11-21 14:54:19 -05:00
|
|
|
end
|
|
|
|
|
2015-11-20 09:29:47 -05:00
|
|
|
def compare
|
2016-07-27 07:09:52 -04:00
|
|
|
@opts[:compare] if @opts[:compare]
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
2016-05-12 11:06:14 -04:00
|
|
|
def diff_refs
|
|
|
|
@opts[:diff_refs]
|
|
|
|
end
|
|
|
|
|
2015-11-21 14:54:19 -05:00
|
|
|
def compare_timeout
|
2017-02-07 09:16:46 -05:00
|
|
|
diffs&.overflow?
|
2015-11-21 14:54:19 -05:00
|
|
|
end
|
|
|
|
|
2015-11-20 09:29:47 -05:00
|
|
|
def reverse_compare?
|
|
|
|
@opts[:reverse_compare] || false
|
|
|
|
end
|
2015-11-18 04:13:34 -05:00
|
|
|
|
2015-11-20 09:29:47 -05: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 04:13:34 -05:00
|
|
|
else
|
2015-11-20 09:29:47 -05:00
|
|
|
'pushed to'
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref_name
|
2015-11-20 09:29:47 -05:00
|
|
|
@ref_name ||= Gitlab::Git.ref_name(@ref)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref_type
|
2015-11-20 09:29:47 -05:00
|
|
|
@ref_type ||= Gitlab::Git.tag_ref?(@ref) ? 'tag' : 'branch'
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_url
|
2015-12-04 08:11:17 -05:00
|
|
|
if @action == :push && commits
|
|
|
|
if commits.length > 1
|
2017-06-29 13:06:35 -04:00
|
|
|
project_compare_url(project, from: compare.start_commit, to: compare.head_commit)
|
2015-11-18 04:13:34 -05:00
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
project_commit_url(project, commits.first)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
unless @action == :delete
|
2017-06-29 13:06:35 -04:00
|
|
|
project_tree_url(project, ref_name)
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to
|
2015-11-20 09:29:47 -05:00
|
|
|
if send_from_committer_email? && @notify.can_send_from_user_email?(author)
|
|
|
|
author.email
|
2015-11-18 04:13:34 -05:00
|
|
|
else
|
|
|
|
Gitlab.config.gitlab.email_reply_to
|
|
|
|
end
|
|
|
|
end
|
2015-11-20 09:29:47 -05:00
|
|
|
|
|
|
|
def subject
|
2018-11-05 23:45:35 -05:00
|
|
|
subject_text = ['[Git]']
|
2017-07-20 05:34:09 -04:00
|
|
|
subject_text << "[#{project.full_path}]"
|
2015-11-20 09:29:47 -05:00
|
|
|
subject_text << "[#{ref_name}]" if @action == :push
|
|
|
|
subject_text << ' '
|
|
|
|
|
2015-12-04 08:11:17 -05:00
|
|
|
if @action == :push && commits
|
2015-11-20 09:29:47 -05:00
|
|
|
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
|
2018-11-05 23:45:35 -05:00
|
|
|
|
|
|
|
subject_text.join
|
2015-11-20 09:29:47 -05:00
|
|
|
end
|
2015-11-18 04:13:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|