BigW Consortium Gitlab

projects.rb 5.1 KB
Newer Older
1 2
module Emails
  module Projects
3 4
    def project_access_granted_email(project_member_id)
      @project_member = ProjectMember.find project_member_id
5
      @project = @project_member.project
6

Vinnie Okada committed
7
      @target_url = namespace_project_url(@project.namespace, @project)
Douwe Maan committed
8
      @current_user = @project_member.user
9

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

14 15 16 17
    def project_member_invited_email(project_member_id, token)
      @project_member = ProjectMember.find project_member_id
      @project = @project_member.project
      @token = token
18

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

22
      mail(to: @project_member.invite_email,
23
           subject: "Invitation to join project #{@project.name_with_namespace}")
24 25 26 27 28 29 30
    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
31

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

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

Douwe Maan committed
39 40 41 42
    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)
43
      @current_user = @created_by = User.find(created_by_id)
Douwe Maan committed
44 45
      @access_level = access_level
      @invite_email = invite_email
46
      
Douwe Maan committed
47 48 49
      @target_url = namespace_project_url(@project.namespace, @project)

      mail(to: @created_by.notification_email,
50
           subject: subject("Invitation declined"))
Douwe Maan committed
51 52
    end

53
    def project_was_moved_email(project_id, user_id, old_path_with_namespace)
Douwe Maan committed
54
      @current_user = @user = User.find user_id
55
      @project = Project.find project_id
Vinnie Okada committed
56
      @target_url = namespace_project_url(@project.namespace, @project)
57
      @old_path_with_namespace = old_path_with_namespace
58
      mail(to: @user.notification_email,
59
           subject: subject("Project was moved"))
60
    end
61

62 63 64
    def repository_push_email(project_id, recipient,  author_id: nil, 
                                                      ref: nil, 
                                                      action: nil, 
65 66 67 68
                                                      compare: nil, 
                                                      reverse_compare: false, 
                                                      send_from_committer_email: false, 
                                                      disable_diffs: false)
69 70 71 72
      unless author_id && ref && action
        raise ArgumentError, "missing keywords: author_id, ref, action"
      end

73
      @project = Project.find(project_id)
Douwe Maan committed
74
      @current_user = @author  = User.find(author_id)
75
      @reverse_compare = reverse_compare
76
      @compare = compare
77 78 79
      @ref_name  = Gitlab::Git.ref_name(ref)
      @ref_type  = Gitlab::Git.tag_ref?(ref) ? "tag" : "branch"
      @action  = action
80
      @disable_diffs = disable_diffs
81

82
      if @compare
83
        @commits = Commit.decorate(compare.commits, @project)
84 85 86 87 88 89 90 91 92 93 94 95 96
        @diffs   = compare.diffs
      end

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

97 98
      @subject = "[Git]"
      @subject << "[#{@project.path_with_namespace}]"
99 100 101 102 103 104 105
      @subject << "[#{@ref_name}]" if action == :push
      @subject << " "

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

114 115 116
          @subject << "Deleted 1 commit: " if @reverse_compare
          @subject << @commits.first.title
        end
117
      else
118 119 120 121
        unless action == :delete
          @target_url = namespace_project_tree_url(@project.namespace,
                                                   @project, @ref_name)
        end
122

123 124 125
        subject_action = @action_name.dup
        subject_action[0] = subject_action[0].capitalize
        @subject << "#{subject_action} #{@ref_type} #{@ref_name}"
126
      end
127

128 129
      @disable_footer = true

130 131 132 133
      reply_to = 
        if send_from_committer_email && can_send_from_user_email?(@author)
          @author.email
        else
134
          Gitlab.config.gitlab.email_reply_to
135 136 137 138 139 140
        end

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