OSDN Git Service

Email on push: dont send email if new branch was pushed or branch was removed
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Tue, 17 Dec 2013 14:20:45 +0000 (16:20 +0200)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Tue, 17 Dec 2013 14:20:45 +0000 (16:20 +0200)
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/mailers/emails/projects.rb
app/models/project_services/emails_on_push_service.rb
app/views/notify/repository_push_email.html.haml
app/views/notify/repository_push_email.text.haml

index 809ce32..df21d7b 100644 (file)
@@ -14,10 +14,10 @@ module Emails
            subject: subject("Project was moved"))
     end
 
-    def repository_push_email(project_id, recipient, branch, compare)
-      @project = Project.find project_id
-      @commits = Commit.decorate compare.commits
-      @commit  = compare.commit
+    def repository_push_email(project_id, recipient, author_id, branch, compare)
+      @project = Project.find(project_id)
+      @author  = User.find(author_id)
+      @commits = Commit.decorate(compare.commits)
       @diffs   = compare.diffs
       @branch  = branch
 
index 1d4828f..8df7313 100644 (file)
@@ -36,11 +36,20 @@ class EmailsOnPushService < Service
     before_sha = push_data[:before]
     after_sha = push_data[:after]
     branch = push_data[:ref]
+    author_id = push_data[:user_id]
+
+    if before_sha =~ /^000000/ || after_sha =~ /^000000/
+      # skip if new branch was pushed or branch was removed
+      return true
+    end
 
     compare = Gitlab::Git::Compare.new(project.repository.raw_repository, before_sha, after_sha)
 
+    # Do not send emails if git compare failed
+    return false unless compare && compare.commits.present?
+
     recipients.split(" ").each do |recipient|
-      Notify.delay.repository_push_email(project_id, recipient, branch, compare)
+      Notify.delay.repository_push_email(project_id, recipient, author_id, branch, compare)
     end
   end
 
index 48c3b32..d0b30c0 100644 (file)
@@ -1,4 +1,4 @@
-%h3 New push to #{@branch} at #{@project.name_with_namespace}
+%h3 #{@author.name} pushed to #{@branch} at #{@project.name_with_namespace}
 
 %h4 Commits:
 
index 0da5b95..6718ca6 100644 (file)
@@ -1,4 +1,4 @@
-New push to #{@branch} at #{@project.name_with_namespace}
+#{@author.name} pushed to #{@branch} at #{@project.name_with_namespace}
 
 \
 Commits: