BigW Consortium Gitlab

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

8
        include Gitlab::Routing.url_helpers
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, recipient, opts = {})
15
          raise ArgumentError, 'Missing options: author_id, ref, action' unless
16 17 18 19 20
            opts[:author_id] && opts[:ref] && opts[:action]

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

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

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

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

        def commits
37
          @commits ||= (Commit.decorate(compare.commits, project) if compare)
38 39 40
        end

        def diffs
41
          @diffs ||= (compare.diffs if compare)
42 43
        end

44 45 46 47
        def diffs_count
          diffs.count if diffs
        end

48 49
        def compare
          @opts[:compare]
50 51
        end

52
        def compare_timeout
53
          diffs.overflow? if diffs
54 55
        end

56 57 58
        def reverse_compare?
          @opts[:reverse_compare] || false
        end
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        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'
75
            else
76
              'pushed to'
77 78 79 80
            end
        end

        def ref_name
81
          @ref_name ||= Gitlab::Git.ref_name(@ref)
82 83 84
        end

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

        def target_url
89 90
          if @action == :push && commits
            if commits.length > 1
91 92 93 94
              namespace_project_compare_url(project_namespace,
                                            project,
                                            from: Commit.new(compare.base, project),
                                            to:   Commit.new(compare.head, project))
95
            else
96 97
              namespace_project_commit_url(project_namespace,
                                           project, commits.first)
98 99 100
            end
          else
            unless @action == :delete
101 102
              namespace_project_tree_url(project_namespace,
                                         project, ref_name)
103 104 105 106 107
            end
          end
        end

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

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

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