Commit 50e8d6c0c03b11cc539cca37aea782538ee77262
1 parent
0447c731
Exists in
master
and in
4 other branches
Add api to collect owned user projects. Api deploy_key.create: Enable deploy key…
… if it exist in other owned project
Showing
4 changed files
with
37 additions
and
3 deletions
Show diff stats
app/controllers/deploy_keys_controller.rb
app/models/user.rb
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 | ... | ... |