repository_push.rb 4 KB
Newer Older
1 2 3 4
module Gitlab
  module Email
    module Message
      class RepositoryPush
5
        attr_reader :author_id, :ref, :action
6

7
        include Gitlab::Routing.url_helpers
8
        include DiffHelper
9

10 11
        delegate :namespace, :name_with_namespace, to: :project, prefix: :project
        delegate :name, to: :author, prefix: :author
12
        delegate :username, to: :author, prefix: :author
13

14
        def initialize(notify, project_id, opts = {})
15
          raise ArgumentError, 'Missing options: author_id, ref, action' unless
16 17 18 19
            opts[:author_id] && opts[:ref] && opts[:action]

          @notify = notify
          @project_id = project_id
20
          @opts = opts.dup
21

22 23 24
          @author_id = @opts.delete(:author_id)
          @ref = @opts.delete(:ref)
          @action = @opts.delete(:action)
25 26 27
        end

        def project
28
          @project ||= Project.find(@project_id)
29 30 31
        end

        def author
32
          @author ||= User.find(@author_id)
33 34 35
        end

        def commits
36 37 38
          return unless compare

          @commits ||= Commit.decorate(compare.commits, project)
39 40 41
        end

        def diffs
42 43
          return unless compare
          
44
          @diffs ||= SafeDiffs::Compare.new(compare, diff_options: { max_files: 30 }, project: project, diff_refs: diff_refs).diff_files
45 46
        end

47 48 49 50
        def diffs_count
          diffs.count if diffs
        end

51 52
        def compare
          @opts[:compare]
53 54
        end

55 56 57 58
        def diff_refs
          @opts[:diff_refs]
        end

59
        def compare_timeout
60
          diffs.overflow? if diffs
61 62
        end

63 64 65
        def reverse_compare?
          @opts[:reverse_compare] || false
        end
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        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'
82
            else
83
              'pushed to'
84 85 86 87
            end
        end

        def ref_name
88
          @ref_name ||= Gitlab::Git.ref_name(@ref)
89 90 91
        end

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

        def target_url
96 97
          if @action == :push && commits
            if commits.length > 1
98 99 100 101
              namespace_project_compare_url(project_namespace,
                                            project,
                                            from: Commit.new(compare.base, project),
                                            to:   Commit.new(compare.head, project))
102
            else
103 104
              namespace_project_commit_url(project_namespace,
                                           project, commits.first)
105 106 107
            end
          else
            unless @action == :delete
108 109
              namespace_project_tree_url(project_namespace,
                                         project, ref_name)
110 111 112 113 114
            end
          end
        end

        def reply_to
115 116
          if send_from_committer_email? && @notify.can_send_from_user_email?(author)
            author.email
117 118 119 120
          else
            Gitlab.config.gitlab.email_reply_to
          end
        end
121 122 123 124 125 126 127

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

128
          if @action == :push && commits
129 130 131 132 133 134 135 136 137 138 139 140 141
            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
142 143 144 145
      end
    end
  end
end