Commit 7ffb8fc616a2890cc46924b099c28709e491495e

Authored by Ariejan de Vroom
1 parent edab46e9

Added specs for special cases

We don't execute web hooks when:
 * You create a new branch. Make sure you first create the branch, and then push any commits. This is the way Github works, so its expected behavior.
 * When tags are pushed.
app/models/project.rb
... ... @@ -92,6 +92,11 @@ class Project < ActiveRecord::Base
92 92 end
93 93  
94 94 def execute_web_hooks(oldrev, newrev, ref)
  95 + ref_parts = ref.split('/')
  96 +
  97 + # Return if this is not a push to a branch (e.g. new commits)
  98 + return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
  99 +
95 100 data = web_hook_data(oldrev, newrev, ref)
96 101 web_hooks.each { |web_hook| web_hook.execute(data) }
97 102 end
... ...
spec/models/project_spec.rb
... ... @@ -91,10 +91,31 @@ describe Project do
91 91 @webhook.should_receive(:execute).once
92 92 @webhook_2.should_receive(:execute).once
93 93  
94   - project.execute_web_hooks('oldrev', 'newrev', 'ref')
  94 + project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master')
95 95 end
96 96 end
97 97  
  98 + context "does not execute web hooks" do
  99 + before do
  100 + @webhook = Factory(:web_hook)
  101 + project.web_hooks << [@webhook]
  102 + end
  103 +
  104 + it "when pushing a branch for the first time" do
  105 + @webhook.should_not_receive(:execute)
  106 + project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/mster')
  107 + end
  108 +
  109 + it "when pushing tags" do
  110 + @webhook.should_not_receive(:execute)
  111 + project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0')
  112 + end
  113 + end
  114 +
  115 + context "when pushing new branches" do
  116 +
  117 + end
  118 +
98 119 context "when gathering commit data" do
99 120 before do
100 121 @oldrev, @newrev, @ref = project.fresh_commits(2).last.sha, project.fresh_commits(2).first.sha, 'refs/heads/master'
... ...