2013-06-06 05:37:51 -04:00
|
|
|
module API
|
|
|
|
class DeployKeys < Grape::API
|
2017-01-16 23:45:07 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2013-06-06 05:37:51 -04:00
|
|
|
before { authenticate! }
|
2016-06-01 22:25:22 -04:00
|
|
|
|
2017-01-16 23:45:07 -05:00
|
|
|
desc 'Return all deploy keys'
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-06-01 22:25:22 -04:00
|
|
|
get "deploy_keys" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
2017-01-16 23:45:07 -05:00
|
|
|
present paginate(DeployKey.all), with: Entities::SSHKey
|
2016-06-01 22:25:22 -04:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2016-08-03 10:45:35 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of the project'
|
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
resource :projects do
|
2016-06-01 22:25:22 -04:00
|
|
|
before { authorize_admin_project }
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc "Get a specific project's deploy keys" do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
2017-01-16 23:45:07 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2017-02-06 08:39:22 -05:00
|
|
|
get ":id/deploy_keys" do
|
2017-01-16 23:45:07 -05:00
|
|
|
present paginate(user_project.deploy_keys), with: Entities::SSHKey
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Get single deploy key' do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
get ":id/deploy_keys/:key_id" do
|
|
|
|
key = user_project.deploy_keys.find params[:key_id]
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Add new deploy key to currently authenticated user' do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key, type: String, desc: 'The new deploy key'
|
|
|
|
requires :title, type: String, desc: 'The name of the deploy key'
|
|
|
|
end
|
|
|
|
post ":id/deploy_keys" do
|
|
|
|
params[:key].strip!
|
|
|
|
|
|
|
|
# Check for an existing key joined to this project
|
|
|
|
key = user_project.deploy_keys.find_by(key: params[:key])
|
|
|
|
if key
|
2016-06-01 22:25:22 -04:00
|
|
|
present key, with: Entities::SSHKey
|
2017-02-06 08:39:22 -05:00
|
|
|
break
|
2016-06-01 22:25:22 -04:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
# Check for available deploy keys in other projects
|
|
|
|
key = current_user.accessible_deploy_keys.find_by(key: params[:key])
|
|
|
|
if key
|
|
|
|
user_project.deploy_keys << key
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
break
|
2017-01-23 11:06:57 -05:00
|
|
|
end
|
2016-06-01 22:25:22 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
# Create a new deploy key
|
|
|
|
key = DeployKey.new(declared_params(include_missing: false))
|
|
|
|
if key.valid? && user_project.deploy_keys << key
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
else
|
|
|
|
render_validation_error!(key)
|
2013-06-06 05:37:51 -04:00
|
|
|
end
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Enable a deploy key for a project' do
|
|
|
|
detail 'This feature was added in GitLab 8.11'
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
post ":id/deploy_keys/:key_id/enable" do
|
|
|
|
key = ::Projects::EnableDeployKeyService.new(user_project,
|
|
|
|
current_user, declared_params).execute
|
2016-08-03 08:45:32 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
if key
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
else
|
|
|
|
not_found!('Deploy Key')
|
2016-08-03 08:45:32 -04:00
|
|
|
end
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2016-08-03 08:45:32 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Delete deploy key for a project' do
|
|
|
|
success Key
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
delete ":id/deploy_keys/:key_id" do
|
|
|
|
key = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
|
2017-02-20 03:33:42 -05:00
|
|
|
not_found!('Deploy Key') unless key
|
|
|
|
|
|
|
|
key.destroy
|
2013-06-06 05:37:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|