Commit 25bde28d13cdffda8efa4ecae00985085e6aa565
1 parent
bb164ebf
Exists in
master
and in
4 other branches
Fixed tests. removed delegate to id
Showing
6 changed files
with
27 additions
and
13 deletions
Show diff stats
app/models/key.rb
@@ -14,7 +14,7 @@ class Key < ActiveRecord::Base | @@ -14,7 +14,7 @@ class Key < ActiveRecord::Base | ||
14 | before_save :set_identifier | 14 | before_save :set_identifier |
15 | after_save :update_repository | 15 | after_save :update_repository |
16 | after_destroy :repository_delete_key | 16 | after_destroy :repository_delete_key |
17 | - delegate :id, :name, :email, :to => :user, :prefix => true | 17 | + delegate :name, :email, :to => :user, :prefix => true |
18 | 18 | ||
19 | def set_identifier | 19 | def set_identifier |
20 | if is_deploy_key | 20 | if is_deploy_key |
app/models/project.rb
@@ -117,7 +117,7 @@ class Project < ActiveRecord::Base | @@ -117,7 +117,7 @@ class Project < ActiveRecord::Base | ||
117 | before: oldrev, | 117 | before: oldrev, |
118 | after: newrev, | 118 | after: newrev, |
119 | ref: ref, | 119 | ref: ref, |
120 | - user_id: key.user_id, | 120 | + user_id: key.user.id, |
121 | user_name: key.user_name, | 121 | user_name: key.user_name, |
122 | repository: { | 122 | repository: { |
123 | name: name, | 123 | name: name, |
app/models/users_project.rb
@@ -16,7 +16,7 @@ class UsersProject < ActiveRecord::Base | @@ -16,7 +16,7 @@ class UsersProject < ActiveRecord::Base | ||
16 | validates_presence_of :user_id | 16 | validates_presence_of :user_id |
17 | validates_presence_of :project_id | 17 | validates_presence_of :project_id |
18 | 18 | ||
19 | - delegate :id, :name, :email, :to => :user, :prefix => true | 19 | + delegate :name, :email, :to => :user, :prefix => true |
20 | 20 | ||
21 | def self.bulk_import(project, user_ids, project_access, repo_access) | 21 | def self.bulk_import(project, user_ids, project_access, repo_access) |
22 | UsersProject.transaction do | 22 | UsersProject.transaction do |
app/workers/post_receive.rb
@@ -5,6 +5,9 @@ class PostReceive | @@ -5,6 +5,9 @@ class PostReceive | ||
5 | project = Project.find_by_path(reponame) | 5 | project = Project.find_by_path(reponame) |
6 | return false if project.nil? | 6 | return false if project.nil? |
7 | 7 | ||
8 | + # Ignore push from non-gitlab users | ||
9 | + return false unless Key.find_by_identifier(author_key_id) | ||
10 | + | ||
8 | project.observe_push(oldrev, newrev, ref, author_key_id) | 11 | project.observe_push(oldrev, newrev, ref, author_key_id) |
9 | project.execute_web_hooks(oldrev, newrev, ref, author_key_id) | 12 | project.execute_web_hooks(oldrev, newrev, ref, author_key_id) |
10 | end | 13 | end |
spec/models/project_hooks_spec.rb
@@ -2,17 +2,21 @@ require 'spec_helper' | @@ -2,17 +2,21 @@ require 'spec_helper' | ||
2 | 2 | ||
3 | describe Project, "Hooks" do | 3 | describe Project, "Hooks" do |
4 | let(:project) { Factory :project } | 4 | let(:project) { Factory :project } |
5 | + before do | ||
6 | + @key = Factory :key, :user => project.owner | ||
7 | + @key_id = @key.identifier | ||
8 | + end | ||
5 | 9 | ||
6 | describe "Post Receive Event" do | 10 | describe "Post Receive Event" do |
7 | it "should create push event" do | 11 | it "should create push event" do |
8 | oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master' | 12 | oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master' |
9 | - project.observe_push(oldrev, newrev, ref) | 13 | + project.observe_push(oldrev, newrev, ref, @key_id) |
10 | event = Event.last | 14 | event = Event.last |
11 | 15 | ||
12 | event.should_not be_nil | 16 | event.should_not be_nil |
13 | event.project.should == project | 17 | event.project.should == project |
14 | event.action.should == Event::Pushed | 18 | event.action.should == Event::Pushed |
15 | - event.data == project.web_hook_data(oldrev, newrev, ref) | 19 | + event.data == project.web_hook_data(oldrev, newrev, ref, @key_id) |
16 | end | 20 | end |
17 | end | 21 | end |
18 | 22 | ||
@@ -20,7 +24,7 @@ describe Project, "Hooks" do | @@ -20,7 +24,7 @@ describe Project, "Hooks" do | ||
20 | context "with no web hooks" do | 24 | context "with no web hooks" do |
21 | it "raises no errors" do | 25 | it "raises no errors" do |
22 | lambda { | 26 | lambda { |
23 | - project.execute_web_hooks('oldrev', 'newrev', 'ref') | 27 | + project.execute_web_hooks('oldrev', 'newrev', 'ref', @key_id) |
24 | }.should_not raise_error | 28 | }.should_not raise_error |
25 | end | 29 | end |
26 | end | 30 | end |
@@ -36,7 +40,7 @@ describe Project, "Hooks" do | @@ -36,7 +40,7 @@ describe Project, "Hooks" do | ||
36 | @webhook.should_receive(:execute).once | 40 | @webhook.should_receive(:execute).once |
37 | @webhook_2.should_receive(:execute).once | 41 | @webhook_2.should_receive(:execute).once |
38 | 42 | ||
39 | - project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master') | 43 | + project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master', @key_id) |
40 | end | 44 | end |
41 | end | 45 | end |
42 | 46 | ||
@@ -48,12 +52,12 @@ describe Project, "Hooks" do | @@ -48,12 +52,12 @@ describe Project, "Hooks" do | ||
48 | 52 | ||
49 | it "when pushing a branch for the first time" do | 53 | it "when pushing a branch for the first time" do |
50 | @webhook.should_not_receive(:execute) | 54 | @webhook.should_not_receive(:execute) |
51 | - project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master') | 55 | + project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @key_id) |
52 | end | 56 | end |
53 | 57 | ||
54 | it "when pushing tags" do | 58 | it "when pushing tags" do |
55 | @webhook.should_not_receive(:execute) | 59 | @webhook.should_not_receive(:execute) |
56 | - project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0') | 60 | + project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @key_id) |
57 | end | 61 | end |
58 | end | 62 | end |
59 | 63 | ||
@@ -69,7 +73,7 @@ describe Project, "Hooks" do | @@ -69,7 +73,7 @@ describe Project, "Hooks" do | ||
69 | # Fill nil/empty attributes | 73 | # Fill nil/empty attributes |
70 | project.description = "This is a description" | 74 | project.description = "This is a description" |
71 | 75 | ||
72 | - @data = project.web_hook_data(@oldrev, @newrev, @ref) | 76 | + @data = project.web_hook_data(@oldrev, @newrev, @ref, @key_id) |
73 | end | 77 | end |
74 | 78 | ||
75 | subject { @data } | 79 | subject { @data } |
@@ -77,6 +81,8 @@ describe Project, "Hooks" do | @@ -77,6 +81,8 @@ describe Project, "Hooks" do | ||
77 | it { should include(before: @oldrev) } | 81 | it { should include(before: @oldrev) } |
78 | it { should include(after: @newrev) } | 82 | it { should include(after: @newrev) } |
79 | it { should include(ref: @ref) } | 83 | it { should include(ref: @ref) } |
84 | + it { should include(user_id: project.owner.id) } | ||
85 | + it { should include(user_name: project.owner.name) } | ||
80 | 86 | ||
81 | context "with repository data" do | 87 | context "with repository data" do |
82 | subject { @data[:repository] } | 88 | subject { @data[:repository] } |
spec/workers/post_receive_spec.rb
@@ -10,17 +10,22 @@ describe PostReceive do | @@ -10,17 +10,22 @@ describe PostReceive do | ||
10 | 10 | ||
11 | context "web hooks" do | 11 | context "web hooks" do |
12 | let(:project) { Factory :project } | 12 | let(:project) { Factory :project } |
13 | + before do | ||
14 | + @key = Factory :key, :user => project.owner | ||
15 | + @key_id = @key.identifier | ||
16 | + end | ||
13 | 17 | ||
14 | it "it retrieves the correct project" do | 18 | it "it retrieves the correct project" do |
15 | Project.should_receive(:find_by_path).with(project.path) | 19 | Project.should_receive(:find_by_path).with(project.path) |
16 | - PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master') | 20 | + Key.should_receive(:find_by_identifier).with(project.path) |
21 | + PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', @key_id) | ||
17 | end | 22 | end |
18 | 23 | ||
19 | it "asks the project to execute web hooks" do | 24 | it "asks the project to execute web hooks" do |
20 | Project.stub(find_by_path: project) | 25 | Project.stub(find_by_path: project) |
21 | - project.should_receive(:execute_web_hooks).with('sha-old', 'sha-new', 'refs/heads/master') | 26 | + project.should_receive(:execute_web_hooks).with('sha-old', 'sha-new', 'refs/heads/master', @key_id) |
22 | 27 | ||
23 | - PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master') | 28 | + PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', @key_id) |
24 | end | 29 | end |
25 | end | 30 | end |
26 | end | 31 | end |