2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class DeployKeys < Grape::API
|
|
|
|
before { authenticate! }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
get "deploy_keys" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
|
|
|
keys = DeployKey.all
|
|
|
|
present keys, with: Entities::SSHKey
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of the project'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
resource :projects do
|
2016-08-24 12:49:21 +05:30
|
|
|
before { authorize_admin_project }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# Routing "projects/:id/keys/..." is DEPRECATED and WILL BE REMOVED in version 9.0
|
|
|
|
# Use "projects/:id/deploy_keys/..." instead.
|
2014-09-02 18:07:02 +05:30
|
|
|
#
|
2016-08-24 12:49:21 +05:30
|
|
|
%w(keys deploy_keys).each do |path|
|
2016-09-13 17:45:13 +05:30
|
|
|
desc "Get a specific project's deploy keys" do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
get ":id/#{path}" do
|
|
|
|
present user_project.deploy_keys, with: Entities::SSHKey
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2016-08-24 12:49:21 +05:30
|
|
|
get ":id/#{path}/:key_id" do
|
|
|
|
key = user_project.deploy_keys.find params[:key_id]
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# TODO: for 9.0 we should check if params are there with the params block
|
|
|
|
# grape provides, at this point we'd change behaviour so we can't
|
|
|
|
# Behaviour now if you don't provide all required params: it renders a
|
|
|
|
# validation error or two.
|
|
|
|
desc 'Add new deploy key to currently authenticated user' do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
post ":id/#{path}" do
|
|
|
|
attrs = attributes_for_keys [:title, :key]
|
2016-09-13 17:45:13 +05:30
|
|
|
attrs[:key].strip! if attrs[:key]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
key = user_project.deploy_keys.find_by(key: attrs[:key])
|
|
|
|
present key, with: Entities::SSHKey if key
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# Check for available deploy keys in other projects
|
|
|
|
key = current_user.accessible_deploy_keys.find_by(key: attrs[:key])
|
|
|
|
if key
|
|
|
|
user_project.deploy_keys << key
|
|
|
|
present key, with: Entities::SSHKey
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
key = DeployKey.new attrs
|
|
|
|
|
|
|
|
if key.valid? && user_project.deploy_keys << key
|
2014-09-02 18:07:02 +05:30
|
|
|
present key, with: Entities::SSHKey
|
2016-08-24 12:49:21 +05:30
|
|
|
else
|
|
|
|
render_validation_error!(key)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
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/#{path}/:key_id/enable" do
|
|
|
|
key = ::Projects::EnableDeployKeyService.new(user_project,
|
|
|
|
current_user, declared(params)).execute
|
|
|
|
|
|
|
|
if key
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
else
|
|
|
|
not_found!('Deploy Key')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Disable 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
|
|
|
|
delete ":id/#{path}/:key_id/disable" do
|
|
|
|
key = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
|
|
|
|
key.destroy
|
|
|
|
|
|
|
|
present key.deploy_key, with: Entities::SSHKey
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Delete existing deploy key of currently authenticated user' do
|
|
|
|
success Key
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
delete ":id/#{path}/:key_id" do
|
2016-09-13 17:45:13 +05:30
|
|
|
key = user_project.deploy_keys.find(params[:key_id])
|
2016-08-24 12:49:21 +05:30
|
|
|
key.destroy
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|