Commit fa07c9d662cd73b751812d0def5f09244d6ee895
1 parent
dcdb2fdf
Exists in
master
and in
4 other branches
register push event
Showing
5 changed files
with
135 additions
and
106 deletions
Show diff stats
app/models/project.rb
... | ... | @@ -90,6 +90,16 @@ class Project < ActiveRecord::Base |
90 | 90 | [GIT_HOST['host'], code].join("/") |
91 | 91 | end |
92 | 92 | |
93 | + def observe_push(oldrev, newrev, ref) | |
94 | + data = web_hook_data(oldrev, newrev, ref) | |
95 | + | |
96 | + Event.create( | |
97 | + :project => self, | |
98 | + :action => Event::Pushed, | |
99 | + :data => data | |
100 | + ) | |
101 | + end | |
102 | + | |
93 | 103 | def execute_web_hooks(oldrev, newrev, ref) |
94 | 104 | ref_parts = ref.split('/') |
95 | 105 | |
... | ... | @@ -97,6 +107,7 @@ class Project < ActiveRecord::Base |
97 | 107 | return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000" |
98 | 108 | |
99 | 109 | data = web_hook_data(oldrev, newrev, ref) |
110 | + | |
100 | 111 | web_hooks.each { |web_hook| web_hook.execute(data) } |
101 | 112 | end |
102 | 113 | ... | ... |
app/workers/post_receive.rb
spec/models/activity_observer_spec.rb
... | ... | @@ -3,14 +3,18 @@ require 'spec_helper' |
3 | 3 | describe ActivityObserver do |
4 | 4 | let(:project) { Factory :project } |
5 | 5 | |
6 | + def self.it_should_be_valid_event | |
7 | + it { @event.should_not be_nil } | |
8 | + it { @event.project.should == project } | |
9 | + end | |
10 | + | |
6 | 11 | describe "Merge Request created" do |
7 | 12 | before do |
8 | 13 | @merge_request = Factory :merge_request, :project => project |
9 | 14 | @event = Event.last |
10 | 15 | end |
11 | 16 | |
12 | - it { @event.should_not be_nil } | |
13 | - it { @event.project.should == project } | |
17 | + it_should_be_valid_event | |
14 | 18 | it { @event.action.should == Event::Created } |
15 | 19 | it { @event.target.should == @merge_request } |
16 | 20 | end |
... | ... | @@ -21,8 +25,7 @@ describe ActivityObserver do |
21 | 25 | @event = Event.last |
22 | 26 | end |
23 | 27 | |
24 | - it { @event.should_not be_nil } | |
25 | - it { @event.project.should == project } | |
28 | + it_should_be_valid_event | |
26 | 29 | it { @event.action.should == Event::Created } |
27 | 30 | it { @event.target.should == @issue } |
28 | 31 | end |
... | ... | @@ -34,8 +37,7 @@ describe ActivityObserver do |
34 | 37 | @event = Event.last |
35 | 38 | end |
36 | 39 | |
37 | - it { @event.should_not be_nil } | |
38 | - it { @event.project.should == project } | |
40 | + it_should_be_valid_event | |
39 | 41 | it { @event.action.should == Event::Commented } |
40 | 42 | it { @event.target.should == @note } |
41 | 43 | end | ... | ... |
... | ... | @@ -0,0 +1,115 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe Project, "Hooks" do | |
4 | + let(:project) { Factory :project } | |
5 | + | |
6 | + describe "Post Receive Event" do | |
7 | + it "should create push event" do | |
8 | + oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master' | |
9 | + project.observe_push(oldrev, newrev, ref) | |
10 | + event = Event.last | |
11 | + | |
12 | + event.should_not be_nil | |
13 | + event.project.should == project | |
14 | + event.action.should == Event::Pushed | |
15 | + event.data == project.web_hook_data(oldrev, newrev, ref) | |
16 | + end | |
17 | + end | |
18 | + | |
19 | + describe "Web hooks" do | |
20 | + context "with no web hooks" do | |
21 | + it "raises no errors" do | |
22 | + lambda { | |
23 | + project.execute_web_hooks('oldrev', 'newrev', 'ref') | |
24 | + }.should_not raise_error | |
25 | + end | |
26 | + end | |
27 | + | |
28 | + context "with web hooks" do | |
29 | + before do | |
30 | + @webhook = Factory(:web_hook) | |
31 | + @webhook_2 = Factory(:web_hook) | |
32 | + project.web_hooks << [@webhook, @webhook_2] | |
33 | + end | |
34 | + | |
35 | + it "executes multiple web hook" do | |
36 | + @webhook.should_receive(:execute).once | |
37 | + @webhook_2.should_receive(:execute).once | |
38 | + | |
39 | + project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master') | |
40 | + end | |
41 | + end | |
42 | + | |
43 | + context "does not execute web hooks" do | |
44 | + before do | |
45 | + @webhook = Factory(:web_hook) | |
46 | + project.web_hooks << [@webhook] | |
47 | + end | |
48 | + | |
49 | + it "when pushing a branch for the first time" do | |
50 | + @webhook.should_not_receive(:execute) | |
51 | + project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master') | |
52 | + end | |
53 | + | |
54 | + it "when pushing tags" do | |
55 | + @webhook.should_not_receive(:execute) | |
56 | + project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0') | |
57 | + end | |
58 | + end | |
59 | + | |
60 | + context "when pushing new branches" do | |
61 | + | |
62 | + end | |
63 | + | |
64 | + context "when gathering commit data" do | |
65 | + before do | |
66 | + @oldrev, @newrev, @ref = project.fresh_commits(2).last.sha, project.fresh_commits(2).first.sha, 'refs/heads/master' | |
67 | + @commit = project.fresh_commits(2).first | |
68 | + | |
69 | + # Fill nil/empty attributes | |
70 | + project.description = "This is a description" | |
71 | + | |
72 | + @data = project.web_hook_data(@oldrev, @newrev, @ref) | |
73 | + end | |
74 | + | |
75 | + subject { @data } | |
76 | + | |
77 | + it { should include(before: @oldrev) } | |
78 | + it { should include(after: @newrev) } | |
79 | + it { should include(ref: @ref) } | |
80 | + | |
81 | + context "with repository data" do | |
82 | + subject { @data[:repository] } | |
83 | + | |
84 | + it { should include(name: project.name) } | |
85 | + it { should include(url: project.web_url) } | |
86 | + it { should include(description: project.description) } | |
87 | + it { should include(homepage: project.web_url) } | |
88 | + it { should include(private: project.private?) } | |
89 | + end | |
90 | + | |
91 | + context "with commits" do | |
92 | + subject { @data[:commits] } | |
93 | + | |
94 | + it { should be_an(Array) } | |
95 | + it { should have(1).element } | |
96 | + | |
97 | + context "the commit" do | |
98 | + subject { @data[:commits].first } | |
99 | + | |
100 | + it { should include(id: @commit.id) } | |
101 | + it { should include(message: @commit.safe_message) } | |
102 | + it { should include(timestamp: @commit.date.xmlschema) } | |
103 | + it { should include(url: "http://localhost/#{project.code}/commits/#{@commit.id}") } | |
104 | + | |
105 | + context "with a author" do | |
106 | + subject { @data[:commits].first[:author] } | |
107 | + | |
108 | + it { should include(name: @commit.author_name) } | |
109 | + it { should include(email: @commit.author_email) } | |
110 | + end | |
111 | + end | |
112 | + end | |
113 | + end | |
114 | + end | |
115 | +end | ... | ... |
spec/models/project_spec.rb
... | ... | @@ -70,106 +70,6 @@ describe Project do |
70 | 70 | end |
71 | 71 | end |
72 | 72 | |
73 | - describe "web hooks" do | |
74 | - let(:project) { Factory :project } | |
75 | - | |
76 | - context "with no web hooks" do | |
77 | - it "raises no errors" do | |
78 | - lambda { | |
79 | - project.execute_web_hooks('oldrev', 'newrev', 'ref') | |
80 | - }.should_not raise_error | |
81 | - end | |
82 | - end | |
83 | - | |
84 | - context "with web hooks" do | |
85 | - before do | |
86 | - @webhook = Factory(:web_hook) | |
87 | - @webhook_2 = Factory(:web_hook) | |
88 | - project.web_hooks << [@webhook, @webhook_2] | |
89 | - end | |
90 | - | |
91 | - it "executes multiple web hook" do | |
92 | - @webhook.should_receive(:execute).once | |
93 | - @webhook_2.should_receive(:execute).once | |
94 | - | |
95 | - project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master') | |
96 | - end | |
97 | - end | |
98 | - | |
99 | - context "does not execute web hooks" do | |
100 | - before do | |
101 | - @webhook = Factory(:web_hook) | |
102 | - project.web_hooks << [@webhook] | |
103 | - end | |
104 | - | |
105 | - it "when pushing a branch for the first time" do | |
106 | - @webhook.should_not_receive(:execute) | |
107 | - project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master') | |
108 | - end | |
109 | - | |
110 | - it "when pushing tags" do | |
111 | - @webhook.should_not_receive(:execute) | |
112 | - project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0') | |
113 | - end | |
114 | - end | |
115 | - | |
116 | - context "when pushing new branches" do | |
117 | - | |
118 | - end | |
119 | - | |
120 | - context "when gathering commit data" do | |
121 | - before do | |
122 | - @oldrev, @newrev, @ref = project.fresh_commits(2).last.sha, project.fresh_commits(2).first.sha, 'refs/heads/master' | |
123 | - @commit = project.fresh_commits(2).first | |
124 | - | |
125 | - # Fill nil/empty attributes | |
126 | - project.description = "This is a description" | |
127 | - | |
128 | - @data = project.web_hook_data(@oldrev, @newrev, @ref) | |
129 | - end | |
130 | - | |
131 | - subject { @data } | |
132 | - | |
133 | - it { should include(before: @oldrev) } | |
134 | - it { should include(after: @newrev) } | |
135 | - it { should include(ref: @ref) } | |
136 | - | |
137 | - context "with repository data" do | |
138 | - subject { @data[:repository] } | |
139 | - | |
140 | - it { should include(name: project.name) } | |
141 | - it { should include(url: project.web_url) } | |
142 | - it { should include(description: project.description) } | |
143 | - it { should include(homepage: project.web_url) } | |
144 | - it { should include(private: project.private?) } | |
145 | - end | |
146 | - | |
147 | - context "with commits" do | |
148 | - subject { @data[:commits] } | |
149 | - | |
150 | - it { should be_an(Array) } | |
151 | - it { should have(1).element } | |
152 | - | |
153 | - context "the commit" do | |
154 | - subject { @data[:commits].first } | |
155 | - | |
156 | - it { should include(id: @commit.id) } | |
157 | - it { should include(message: @commit.safe_message) } | |
158 | - it { should include(timestamp: @commit.date.xmlschema) } | |
159 | - it { should include(url: "http://localhost/#{project.code}/commits/#{@commit.id}") } | |
160 | - | |
161 | - context "with a author" do | |
162 | - subject { @data[:commits].first[:author] } | |
163 | - | |
164 | - it { should include(name: @commit.author_name) } | |
165 | - it { should include(email: @commit.author_email) } | |
166 | - end | |
167 | - end | |
168 | - end | |
169 | - | |
170 | - end | |
171 | - end | |
172 | - | |
173 | 73 | describe "updates" do |
174 | 74 | let(:project) { Factory :project } |
175 | 75 | ... | ... |