2013-10-29 10:39:46 -04:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class Services < Grape::API
|
|
|
|
resource :projects do
|
2016-11-14 09:10:35 -05:00
|
|
|
before { authenticate! }
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
# Set <service_slug> service for project
|
2013-10-29 10:39:46 -04:00
|
|
|
#
|
|
|
|
# Example Request:
|
2015-08-26 19:58:49 -04:00
|
|
|
#
|
2013-10-29 10:39:46 -04:00
|
|
|
# PUT /projects/:id/services/gitlab-ci
|
|
|
|
#
|
2015-08-26 19:58:49 -04:00
|
|
|
put ':id/services/:service_slug' do
|
|
|
|
if project_service
|
|
|
|
validators = project_service.class.validators.select do |s|
|
|
|
|
s.class == ActiveRecord::Validations::PresenceValidator &&
|
|
|
|
s.attributes != [:project_id]
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
required_attributes! validators.map(&:attributes).flatten.uniq
|
2015-09-11 07:38:37 -04:00
|
|
|
attrs = attributes_for_keys service_attributes
|
2014-10-14 13:07:34 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
if project_service.update_attributes(attrs.merge(active: true))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
# Delete <service_slug> service for project
|
2014-10-14 13:07:34 -04:00
|
|
|
#
|
|
|
|
# Example Request:
|
2015-08-26 19:58:49 -04:00
|
|
|
#
|
|
|
|
# DELETE /project/:id/services/gitlab-ci
|
|
|
|
#
|
|
|
|
delete ':id/services/:service_slug' do
|
|
|
|
if project_service
|
|
|
|
attrs = service_attributes.inject({}) do |hash, key|
|
|
|
|
hash.merge!(key => nil)
|
|
|
|
end
|
2015-09-11 07:38:37 -04:00
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
if project_service.update_attributes(attrs.merge(active: false))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-03 09:38:54 -04:00
|
|
|
|
|
|
|
# Get <service_slug> service settings for project
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
#
|
|
|
|
# GET /project/:id/services/gitlab-ci
|
|
|
|
#
|
|
|
|
get ':id/services/:service_slug' do
|
2015-10-12 09:24:00 -04:00
|
|
|
present project_service, with: Entities::ProjectService, include_passwords: current_user.is_admin?
|
2015-09-03 09:38:54 -04:00
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|
2016-11-14 09:10:35 -05:00
|
|
|
|
|
|
|
resource :projects do
|
2016-11-17 06:06:45 -05:00
|
|
|
desc 'Trigger a slash command' do
|
|
|
|
detail 'Added in GitLab 8.13'
|
|
|
|
end
|
2016-11-14 09:10:35 -05:00
|
|
|
post ':id/services/:service_slug/trigger' do
|
2016-11-24 07:22:38 -05:00
|
|
|
project = find_project(params[:id])
|
2016-11-14 09:10:35 -05:00
|
|
|
|
2016-11-18 06:08:30 -05:00
|
|
|
# This is not accurate, but done to prevent leakage of the project names
|
|
|
|
not_found!('Service') unless project
|
2016-11-18 05:38:54 -05:00
|
|
|
|
|
|
|
service = project_service(project)
|
2016-11-14 09:10:35 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
result = service.try(:active?) && service.try(:trigger, params)
|
2016-11-14 09:10:35 -05:00
|
|
|
|
|
|
|
if result
|
2016-11-17 15:27:12 -05:00
|
|
|
status result[:status] || 200
|
|
|
|
present result
|
2016-11-14 09:10:35 -05:00
|
|
|
else
|
2016-11-18 06:08:30 -05:00
|
|
|
not_found!('Service')
|
2016-11-14 09:10:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|
|
|
|
end
|