BigW Consortium Gitlab

repository_push.rb 3.65 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
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
          return unless compare

38
          @commits ||= compare.commits
39 40 41
        end

        def diffs
42
          return unless compare
43

44
          # This diff is more moderated in number of files and lines
45
          @diffs ||= compare.diffs(max_files: 30, max_lines: 5000, expanded: true).diff_files
46 47
        end

48
        def diffs_count
49
          diffs&.size
50 51
        end

52
        def compare
53
          @opts[:compare] if @opts[:compare]
54 55
        end

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

60
        def compare_timeout
61
          diffs&.overflow?
62 63
        end

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

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

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

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

        def target_url
97 98
          if @action == :push && commits
            if commits.length > 1
99
              project_compare_url(project, from: compare.start_commit, to: compare.head_commit)
100
            else
101
              project_commit_url(project, commits.first)
102 103 104
            end
          else
            unless @action == :delete
105
              project_tree_url(project, ref_name)
106 107 108 109 110
            end
          end
        end

        def reply_to
111 112
          if send_from_committer_email? && @notify.can_send_from_user_email?(author)
            author.email
113 114 115 116
          else
            Gitlab.config.gitlab.email_reply_to
          end
        end
117 118 119

        def subject
          subject_text = '[Git]'
120
          subject_text << "[#{project.full_path}]"
121 122 123
          subject_text << "[#{ref_name}]" if @action == :push
          subject_text << ' '

124
          if @action == :push && commits
125 126 127 128 129 130 131 132 133 134 135 136 137
            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
138 139 140 141
      end
    end
  end
end