Commit 70e3bffd95eb5736dd108e0836abaa85a2f1c742
1 parent
39e37677
Exists in
master
and in
4 other branches
Fixed: post-receive, project remove, tests
Showing
10 changed files
with
57 additions
and
61 deletions
Show diff stats
app/observers/project_observer.rb
... | ... | @@ -15,11 +15,10 @@ class ProjectObserver < ActiveRecord::Observer |
15 | 15 | def after_destroy(project) |
16 | 16 | GitoliteWorker.perform_async( |
17 | 17 | :remove_repository, |
18 | - self.path_with_namespace | |
18 | + project.path_with_namespace | |
19 | 19 | ) |
20 | 20 | |
21 | 21 | project.satellite.destroy |
22 | - project.destroy_repository | |
23 | 22 | |
24 | 23 | log_info("Project \"#{project.name}\" was removed") |
25 | 24 | end | ... | ... |
app/workers/post_receive.rb
... | ... | @@ -27,8 +27,9 @@ class PostReceive |
27 | 27 | User.find_by_email(email) if email |
28 | 28 | elsif /^[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}$/.match(identifier) |
29 | 29 | User.find_by_email(identifier) |
30 | - else | |
31 | - User.find_by_username(identifier.strip) | |
30 | + elsif identifier =~ /key/ | |
31 | + key_id = identifier.gsub("key-", "") | |
32 | + Key.find_by_id(key_id).try(:user) | |
32 | 33 | end |
33 | 34 | |
34 | 35 | unless user | ... | ... |
lib/api/internal.rb
1 | 1 | module Gitlab |
2 | - # Access API | |
2 | + # Internal access API | |
3 | 3 | class Internal < Grape::API |
4 | + namespace 'internal' do | |
5 | + # | |
6 | + # Check if ssh key has access to project code | |
7 | + # | |
8 | + get "/allowed" do | |
9 | + key = Key.find(params[:key_id]) | |
10 | + user = key.user | |
4 | 11 | |
5 | - get "/allowed" do | |
6 | - user = User.find_by_username(params[:username]) | |
7 | - project = Project.find_with_namespace(params[:project]) | |
8 | - action = case params[:action] | |
9 | - when 'git-upload-pack' | |
10 | - then :download_code | |
11 | - when 'git-receive-pack' | |
12 | - then | |
13 | - if project.protected_branch?(params[:ref]) | |
14 | - :push_code_to_protected_branches | |
15 | - else | |
16 | - :push_code | |
12 | + project = Project.find_with_namespace(params[:project]) | |
13 | + action = case params[:action] | |
14 | + when 'git-upload-pack' | |
15 | + then :download_code | |
16 | + when 'git-receive-pack' | |
17 | + then | |
18 | + if project.protected_branch?(params[:ref]) | |
19 | + :push_code_to_protected_branches | |
20 | + else | |
21 | + :push_code | |
22 | + end | |
17 | 23 | end |
18 | - end | |
19 | 24 | |
20 | - user.can?(action, project) | |
25 | + user.can?(action, project) | |
26 | + end | |
27 | + | |
28 | + # | |
29 | + # Discover user by ssh key | |
30 | + # | |
31 | + get "/discover" do | |
32 | + key = Key.find(params[:key_id]) | |
33 | + present key.user, with: Entities::User | |
34 | + end | |
21 | 35 | end |
22 | 36 | end |
23 | 37 | end | ... | ... |
spec/lib/gitolite_spec.rb
... | ... | @@ -1,18 +0,0 @@ |
1 | -require 'spec_helper' | |
2 | - | |
3 | -describe Gitlab::Gitolite do | |
4 | - let(:project) { double('Project', id: 7, path: 'diaspora') } | |
5 | - let(:gitolite) { Gitlab::Gitolite.new } | |
6 | - | |
7 | - before do | |
8 | - Project.stub(find: project) | |
9 | - end | |
10 | - | |
11 | - it { should respond_to :set_key } | |
12 | - it { should respond_to :remove_key } | |
13 | - | |
14 | - it { should respond_to :add_repository } | |
15 | - it { should respond_to :remove_repository } | |
16 | - | |
17 | - it { gitolite.url_to_repo('diaspora').should == Gitlab.config.gitolite.ssh_path_prefix + "diaspora.git" } | |
18 | -end |
... | ... | @@ -0,0 +1,17 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe Gitlab::Shell do | |
4 | + let(:project) { double('Project', id: 7, path: 'diaspora') } | |
5 | + let(:gitolite) { Gitlab::Shell.new } | |
6 | + | |
7 | + before do | |
8 | + Project.stub(find: project) | |
9 | + end | |
10 | + | |
11 | + it { should respond_to :add_key } | |
12 | + it { should respond_to :remove_key } | |
13 | + it { should respond_to :add_repository } | |
14 | + it { should respond_to :remove_repository } | |
15 | + | |
16 | + it { gitolite.url_to_repo('diaspora').should == Gitlab.config.gitolite.ssh_path_prefix + "diaspora.git" } | |
17 | +end | ... | ... |
spec/models/project_spec.rb
... | ... | @@ -77,8 +77,6 @@ describe Project do |
77 | 77 | it { should respond_to(:url_to_repo) } |
78 | 78 | it { should respond_to(:repo_exists?) } |
79 | 79 | it { should respond_to(:satellite) } |
80 | - it { should respond_to(:update_repository) } | |
81 | - it { should respond_to(:destroy_repository) } | |
82 | 80 | it { should respond_to(:observe_push) } |
83 | 81 | it { should respond_to(:update_merge_requests) } |
84 | 82 | it { should respond_to(:execute_hooks) } | ... | ... |
spec/models/protected_branch_spec.rb
... | ... | @@ -24,19 +24,4 @@ describe ProtectedBranch do |
24 | 24 | it { should validate_presence_of(:project) } |
25 | 25 | it { should validate_presence_of(:name) } |
26 | 26 | end |
27 | - | |
28 | - describe 'Callbacks' do | |
29 | - let(:branch) { build(:protected_branch) } | |
30 | - | |
31 | - it 'call update_repository after save' do | |
32 | - branch.should_receive(:update_repository) | |
33 | - branch.save | |
34 | - end | |
35 | - | |
36 | - it 'call update_repository after destroy' do | |
37 | - branch.save | |
38 | - branch.should_receive(:update_repository) | |
39 | - branch.destroy | |
40 | - end | |
41 | - end | |
42 | 27 | end | ... | ... |
spec/observers/key_observer_spec.rb
... | ... | @@ -3,7 +3,7 @@ require 'spec_helper' |
3 | 3 | describe KeyObserver do |
4 | 4 | before do |
5 | 5 | @key = double('Key', |
6 | - identifier: 'admin_654654', | |
6 | + shell_id: 'key-32', | |
7 | 7 | key: '== a vaild ssh key', |
8 | 8 | projects: [], |
9 | 9 | is_deploy_key: false |
... | ... | @@ -14,14 +14,14 @@ describe KeyObserver do |
14 | 14 | |
15 | 15 | context :after_save do |
16 | 16 | it do |
17 | - GitoliteWorker.should_receive(:perform_async).with(:set_key, @key.identifier, @key.key, @key.projects.map(&:id)) | |
17 | + GitoliteWorker.should_receive(:perform_async).with(:add_key, @key.shell_id, @key.key) | |
18 | 18 | @observer.after_save(@key) |
19 | 19 | end |
20 | 20 | end |
21 | 21 | |
22 | 22 | context :after_destroy do |
23 | 23 | it do |
24 | - GitoliteWorker.should_receive(:perform_async).with(:remove_key, @key.identifier, @key.projects.map(&:id)) | |
24 | + GitoliteWorker.should_receive(:perform_async).with(:remove_key, @key.shell_id, @key.key) | |
25 | 25 | @observer.after_destroy(@key) |
26 | 26 | end |
27 | 27 | end | ... | ... |
spec/support/stubbed_repository.rb
spec/workers/post_receive_spec.rb
... | ... | @@ -11,7 +11,7 @@ describe PostReceive do |
11 | 11 | context "web hook" do |
12 | 12 | let(:project) { create(:project) } |
13 | 13 | let(:key) { create(:key, user: project.owner) } |
14 | - let(:key_id) { key.identifier } | |
14 | + let(:key_id) { key.shell_id } | |
15 | 15 | |
16 | 16 | it "fetches the correct project" do |
17 | 17 | Project.should_receive(:find_with_namespace).with(project.path_with_namespace).and_return(project) |
... | ... | @@ -19,7 +19,7 @@ describe PostReceive do |
19 | 19 | end |
20 | 20 | |
21 | 21 | it "does not run if the author is not in the project" do |
22 | - Key.stub(find_by_identifier: nil) | |
22 | + Key.stub(find_by_id: nil) | |
23 | 23 | |
24 | 24 | project.should_not_receive(:observe_push) |
25 | 25 | project.should_not_receive(:execute_hooks) | ... | ... |