summaryrefslogtreecommitdiff
path: root/app/mailers/emails/projects.rb
blob: 9cb7077e59d37d0317b6b995ca4bd347ede0b2d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
module Emails
  module Projects
    def project_access_granted_email(project_member_id)
      @project_member = ProjectMember.find project_member_id
      @project = @project_member.project

      @target_url = namespace_project_url(@project.namespace, @project)
      @current_user = @project_member.user

      mail(to: @project_member.user.notification_email,
           subject: subject("Access to project was granted"))
    end

    def project_member_invited_email(project_member_id, token)
      @project_member = ProjectMember.find project_member_id
      @project = @project_member.project
      @token = token

      @target_url = namespace_project_url(@project.namespace, @project)
      @current_user = @project_member.user

      mail(to: @project_member.invite_email,
           subject: "Invitation to join project #{@project.name_with_namespace}")
    end

    def project_invite_accepted_email(project_member_id)
      @project_member = ProjectMember.find project_member_id
      return if @project_member.created_by.nil?

      @project = @project_member.project

      @target_url = namespace_project_url(@project.namespace, @project)
      @current_user = @project_member.created_by

      mail(to: @project_member.created_by.notification_email,
           subject: subject("Invitation accepted"))
    end

    def project_invite_declined_email(project_id, invite_email, access_level, created_by_id)
      return if created_by_id.nil?

      @project = Project.find(project_id)
      @current_user = @created_by = User.find(created_by_id)
      @access_level = access_level
      @invite_email = invite_email
      
      @target_url = namespace_project_url(@project.namespace, @project)

      mail(to: @created_by.notification_email,
           subject: subject("Invitation declined"))
    end

    def project_was_moved_email(project_id, user_id)
      @current_user = @user = User.find user_id
      @project = Project.find project_id
      @target_url = namespace_project_url(@project.namespace, @project)
      mail(to: @user.notification_email,
           subject: subject("Project was moved"))
    end

    def repository_push_email(project_id, recipient,  author_id: nil, 
                                                      ref: nil, 
                                                      action: nil, 
                                                      compare: nil, 
                                                      reverse_compare: false, 
                                                      send_from_committer_email: false, 
                                                      disable_diffs: false)
      unless author_id && ref && action
        raise ArgumentError, "missing keywords: author_id, ref, action"
      end

      @project = Project.find(project_id)
      @current_user = @author  = User.find(author_id)
      @reverse_compare = reverse_compare
      @compare = compare
      @ref_name  = Gitlab::Git.ref_name(ref)
      @ref_type  = Gitlab::Git.tag_ref?(ref) ? "tag" : "branch"
      @action  = action
      @disable_diffs = disable_diffs

      if @compare
        @commits = Commit.decorate(compare.commits, @project)
        @diffs   = compare.diffs
      end

      @action_name = 
        case action
        when :create
          "pushed new"
        when :delete
          "deleted"
        else
          "pushed to"
        end

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

      if action == :push
        if @commits.length > 1
          @target_url = namespace_project_compare_url(@project.namespace,
                                                      @project,
                                                      from: Commit.new(@compare.base, @project),
                                                      to:   Commit.new(@compare.head, @project))
          @subject << "Deleted " if @reverse_compare
          @subject << "#{@commits.length} commits: #{@commits.first.title}"
        else
          @target_url = namespace_project_commit_url(@project.namespace,
                                                     @project, @commits.first)

          @subject << "Deleted 1 commit: " if @reverse_compare
          @subject << @commits.first.title
        end
      else
        unless action == :delete
          @target_url = namespace_project_tree_url(@project.namespace,
                                                   @project, @ref_name)
        end

        subject_action = @action_name.dup
        subject_action[0] = subject_action[0].capitalize
        @subject << "#{subject_action} #{@ref_type} #{@ref_name}"
      end

      @disable_footer = true

      reply_to = 
        if send_from_committer_email && can_send_from_user_email?(@author)
          @author.email
        else
          Gitlab.config.gitlab.email_reply_to
        end

      mail(from:      sender(author_id, send_from_committer_email),
           reply_to:  reply_to,
           to:        recipient,
           subject:   @subject)
    end
  end
end