2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
class DeployKeys < Grape::API
|
2017-08-17 22:00:37 +05:30
|
|
|
include PaginationParams
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
before { authenticate! }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
helpers do
|
|
|
|
def add_deploy_keys_project(project, attrs = {})
|
|
|
|
project.deploy_keys_projects.create(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_deploy_key(project, key_id)
|
|
|
|
project.deploy_keys_projects.find_by!(deploy_key: key_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Return all deploy keys'
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
get "deploy_keys" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
present paginate(DeployKey.all), with: Entities::SSHKey
|
2016-08-24 12:49:21 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2016-08-24 12:49:21 +05:30
|
|
|
before { authorize_admin_project }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc "Get a specific project's deploy keys" do
|
2018-03-17 18:26:18 +05:30
|
|
|
success Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get ":id/deploy_keys" do
|
2018-03-17 18:26:18 +05:30
|
|
|
keys = user_project.deploy_keys_projects.preload(:deploy_key)
|
|
|
|
|
|
|
|
present paginate(keys), with: Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Get single deploy key' do
|
2018-03-17 18:26:18 +05:30
|
|
|
success Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
get ":id/deploy_keys/:key_id" do
|
2018-03-17 18:26:18 +05:30
|
|
|
key = find_by_deploy_key(user_project, params[:key_id])
|
|
|
|
|
|
|
|
present key, with: Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
desc 'Add new deploy key to a project' do
|
2018-03-17 18:26:18 +05:30
|
|
|
success Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key, type: String, desc: 'The new deploy key'
|
|
|
|
requires :title, type: String, desc: 'The name of the deploy key'
|
|
|
|
optional :can_push, type: Boolean, desc: "Can deploy key push to the project's repository"
|
|
|
|
end
|
|
|
|
post ":id/deploy_keys" do
|
|
|
|
params[:key].strip!
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Check for an existing key joined to this project
|
2018-05-09 12:01:36 +05:30
|
|
|
deploy_key_project = user_project.deploy_keys_projects
|
2018-03-17 18:26:18 +05:30
|
|
|
.joins(:deploy_key)
|
|
|
|
.find_by(keys: { key: params[:key] })
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
if deploy_key_project
|
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
break
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Check for available deploy keys in other projects
|
|
|
|
key = current_user.accessible_deploy_keys.find_by(key: params[:key])
|
|
|
|
if key
|
2018-05-09 12:01:36 +05:30
|
|
|
deploy_key_project = add_deploy_keys_project(user_project, deploy_key: key, can_push: !!params[:can_push])
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
break
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Create a new deploy key
|
2018-05-09 12:01:36 +05:30
|
|
|
deploy_key_attributes = declared_params.except(:can_push).merge(user: current_user)
|
|
|
|
deploy_key_project = add_deploy_keys_project(user_project, deploy_key_attributes: deploy_key_attributes, can_push: !!params[:can_push])
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
if deploy_key_project.valid?
|
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2018-05-09 12:01:36 +05:30
|
|
|
render_validation_error!(deploy_key_project)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
desc 'Update an existing deploy key for a project' do
|
|
|
|
success Entities::SSHKey
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
optional :title, type: String, desc: 'The name of the deploy key'
|
|
|
|
optional :can_push, type: Boolean, desc: "Can deploy key push to the project's repository"
|
|
|
|
at_least_one_of :title, :can_push
|
|
|
|
end
|
|
|
|
put ":id/deploy_keys/:key_id" do
|
2018-03-17 18:26:18 +05:30
|
|
|
deploy_keys_project = find_by_deploy_key(user_project, params[:key_id])
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
authorize!(:update_deploy_key, deploy_keys_project.deploy_key)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
can_push = params[:can_push].nil? ? deploy_keys_project.can_push : params[:can_push]
|
|
|
|
title = params[:title] || deploy_keys_project.deploy_key.title
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
result = deploy_keys_project.update(can_push: can_push,
|
|
|
|
deploy_key_attributes: { id: params[:key_id],
|
|
|
|
title: title })
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
if result
|
|
|
|
present deploy_keys_project, with: Entities::DeployKeysProject
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
2018-03-17 18:26:18 +05:30
|
|
|
render_validation_error!(deploy_keys_project)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +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/deploy_keys/:key_id/enable" do
|
|
|
|
key = ::Projects::EnableDeployKeyService.new(user_project,
|
|
|
|
current_user, declared_params).execute
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if key
|
|
|
|
present key, with: Entities::SSHKey
|
|
|
|
else
|
|
|
|
not_found!('Deploy Key')
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
2018-06-03 19:52:53 +05:30
|
|
|
deploy_key_project = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
|
|
|
|
not_found!('Deploy Key') unless deploy_key_project
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-06-03 19:52:53 +05:30
|
|
|
destroy_conditionally!(deploy_key_project)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|