Commit 50e8d6c0c03b11cc539cca37aea782538ee77262

Authored by Dmitriy Zaporozhets
1 parent 0447c731

Add api to collect owned user projects. Api deploy_key.create: Enable deploy key…

… if it exist in other owned project
app/controllers/deploy_keys_controller.rb
... ... @@ -54,6 +54,6 @@ class DeployKeysController < ProjectResourceController
54 54 protected
55 55  
56 56 def available_keys
57   - @available_keys ||= DeployKey.in_projects(current_user.owned_projects).uniq
  57 + @available_keys ||= current_user.owned_deploy_keys
58 58 end
59 59 end
... ...
app/models/user.rb
... ... @@ -352,4 +352,8 @@ class User < ActiveRecord::Base
352 352 def ldap_user?
353 353 extern_uid && provider == 'ldap'
354 354 end
  355 +
  356 + def owned_deploy_keys
  357 + DeployKey.in_projects(self.owned_projects).uniq
  358 + end
355 359 end
... ...
lib/api/entities.rb
... ... @@ -26,9 +26,9 @@ module API
26 26 end
27 27  
28 28 class Project < Grape::Entity
29   - expose :id, :name, :description, :default_branch
  29 + expose :id, :description, :default_branch, :public, :ssh_url_to_repo, :http_url_to_repo, :web_url
30 30 expose :owner, using: Entities::UserBasic
31   - expose :public
  31 + expose :name, :name_with_namespace
32 32 expose :path, :path_with_namespace
33 33 expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at
34 34 expose :namespace
... ...
lib/api/projects.rb
... ... @@ -22,6 +22,15 @@ module API
22 22 present @projects, with: Entities::Project
23 23 end
24 24  
  25 + # Get an owned projects list for authenticated user
  26 + #
  27 + # Example Request:
  28 + # GET /projects/owned
  29 + get '/owned' do
  30 + @projects = paginate current_user.owned_projects
  31 + present @projects, with: Entities::Project
  32 + end
  33 +
25 34 # Get a single project
26 35 #
27 36 # Parameters:
... ... @@ -408,6 +417,8 @@ module API
408 417 end
409 418  
410 419 # Add new ssh key to currently authenticated user
  420 + # If deploy key already exists - it will be joined to project
  421 + # but only if original one was owned by same user
411 422 #
412 423 # Parameters:
413 424 # key (required) - New SSH Key
... ... @@ -416,7 +427,26 @@ module API
416 427 # POST /projects/:id/keys
417 428 post ":id/keys" do
418 429 attrs = attributes_for_keys [:title, :key]
  430 +
  431 + attrs[:key].strip!
  432 +
  433 + # check if key already exist in project
  434 + key = user_project.deploy_keys.find_by_key(attrs[:key])
  435 + if key
  436 + present key, with: Entities::SSHKey
  437 + return
  438 + end
  439 +
  440 + # Check for available deploy keys in other projects
  441 + key = current_user.owned_deploy_keys.find_by_key(attrs[:key])
  442 + if key
  443 + user_project.deploy_keys << key
  444 + present key, with: Entities::SSHKey
  445 + return
  446 + end
  447 +
419 448 key = DeployKey.new attrs
  449 +
420 450 if key.valid? && user_project.deploy_keys << key
421 451 present key, with: Entities::SSHKey
422 452 else
... ...