Commit 7b9d46e5 authored by Andy Soiron's avatar Andy Soiron Committed by Luke Duncalfe
parent d4bf8953
...@@ -2,22 +2,22 @@ ...@@ -2,22 +2,22 @@
module API module API
module Entities module Entities
class ProjectService < Entities::ProjectServiceBasic class ProjectIntegration < Entities::ProjectIntegrationBasic
# Expose serialized properties # Expose serialized properties
expose :properties do |service, options| expose :properties do |integration, options|
# TODO: Simplify as part of https://gitlab.com/gitlab-org/gitlab/issues/29404 # TODO: Simplify as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
attributes = attributes =
if service.data_fields_present? if integration.data_fields_present?
service.data_fields.as_json.keys integration.data_fields.as_json.keys
else else
service.properties.keys integration.properties.keys
end end
attributes &= service.api_field_names attributes &= integration.api_field_names
attributes.each_with_object({}) do |attribute, hash| attributes.each_with_object({}) do |attribute, hash|
hash[attribute] = service.public_send(attribute) # rubocop:disable GitlabSecurity/PublicSend hash[attribute] = integration.public_send(attribute) # rubocop:disable GitlabSecurity/PublicSend
end end
end end
end end
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
module API module API
module Entities module Entities
class ProjectServiceBasic < Grape::Entity class ProjectIntegrationBasic < Grape::Entity
expose :id, :title expose :id, :title
expose :slug do |service| expose :slug do |integration|
service.to_param.dasherize integration.to_param.dasherize
end end
expose :created_at, :updated_at, :active expose :created_at, :updated_at, :active
expose :commit_events, :push_events, :issues_events, :confidential_issues_events expose :commit_events, :push_events, :issues_events, :confidential_issues_events
......
...@@ -35,7 +35,7 @@ module API ...@@ -35,7 +35,7 @@ module API
end end
end end
TRIGGER_SERVICES = { TRIGGER_INTEGRATIONS = {
'mattermost-slash-commands' => [ 'mattermost-slash-commands' => [
{ {
name: :token, name: :token,
...@@ -60,24 +60,24 @@ module API ...@@ -60,24 +60,24 @@ module API
before { authorize_admin_project } before { authorize_admin_project }
helpers do helpers do
def service_attributes(service) def integration_attributes(integration)
service.fields.inject([]) do |arr, hash| integration.fields.inject([]) do |arr, hash|
arr << hash[:name].to_sym arr << hash[:name].to_sym
end end
end end
end end
desc 'Get all active project services' do desc 'Get all active project integrations' do
success Entities::ProjectServiceBasic success Entities::ProjectIntegrationBasic
end end
get ":id/services" do get ":id/services" do
services = user_project.integrations.active integrations = user_project.integrations.active
present services, with: Entities::ProjectServiceBasic present integrations, with: Entities::ProjectIntegrationBasic
end end
INTEGRATIONS.each do |slug, settings| INTEGRATIONS.each do |slug, settings|
desc "Set #{slug} service for project" desc "Set #{slug} integration for project"
params do params do
settings.each do |setting| settings.each do |setting|
if setting[:required] if setting[:required]
...@@ -92,7 +92,7 @@ module API ...@@ -92,7 +92,7 @@ module API
params = declared_params(include_missing: false).merge(active: true) params = declared_params(include_missing: false).merge(active: true)
if integration.update(params) if integration.update(params)
present integration, with: Entities::ProjectService present integration, with: Entities::ProjectIntegration
else else
render_api_error!('400 Bad Request', 400) render_api_error!('400 Bad Request', 400)
end end
...@@ -107,14 +107,14 @@ module API ...@@ -107,14 +107,14 @@ module API
integration = user_project.find_or_initialize_integration(params[:slug].underscore) integration = user_project.find_or_initialize_integration(params[:slug].underscore)
destroy_conditionally!(integration) do destroy_conditionally!(integration) do
attrs = service_attributes(integration).index_with { nil }.merge(active: false) attrs = integration_attributes(integration).index_with { nil }.merge(active: false)
render_api_error!('400 Bad Request', 400) unless integration.update(attrs) render_api_error!('400 Bad Request', 400) unless integration.update(attrs)
end end
end end
desc 'Get the integration settings for a project' do desc 'Get the integration settings for a project' do
success Entities::ProjectService success Entities::ProjectIntegration
end end
params do params do
requires :slug, type: String, values: INTEGRATIONS.keys, desc: 'The name of the service' requires :slug, type: String, values: INTEGRATIONS.keys, desc: 'The name of the service'
...@@ -124,15 +124,15 @@ module API ...@@ -124,15 +124,15 @@ module API
not_found!('Service') unless integration&.persisted? not_found!('Service') unless integration&.persisted?
present integration, with: Entities::ProjectService present integration, with: Entities::ProjectIntegration
end end
end end
TRIGGER_SERVICES.each do |service_slug, settings| TRIGGER_INTEGRATIONS.each do |integration_slug, settings|
helpers do helpers do
def slash_command_service(project, service_slug, params) def slash_command_integration(project, integration_slug, params)
project.integrations.active.find do |service| project.integrations.active.find do |integration|
service.try(:token) == params[:token] && service.to_param == service_slug.underscore integration.try(:token) == params[:token] && integration.to_param == integration_slug.underscore
end end
end end
end end
...@@ -141,7 +141,7 @@ module API ...@@ -141,7 +141,7 @@ module API
requires :id, type: String, desc: 'The ID of a project' requires :id, type: String, desc: 'The ID of a project'
end end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc "Trigger a slash command for #{service_slug}" do desc "Trigger a slash command for #{integration_slug}" do
detail 'Added in GitLab 8.13' detail 'Added in GitLab 8.13'
end end
params do params do
...@@ -149,14 +149,14 @@ module API ...@@ -149,14 +149,14 @@ module API
requires setting[:name], type: setting[:type], desc: setting[:desc] requires setting[:name], type: setting[:type], desc: setting[:desc]
end end
end end
post ":id/services/#{service_slug.underscore}/trigger" do post ":id/services/#{integration_slug.underscore}/trigger" do
project = find_project(params[:id]) project = find_project(params[:id])
# This is not accurate, but done to prevent leakage of the project names # This is not accurate, but done to prevent leakage of the project names
not_found!('Service') unless project not_found!('Service') unless project
service = slash_command_service(project, service_slug, params) integration = slash_command_integration(project, integration_slug, params)
result = service.try(:trigger, params) result = integration.try(:trigger, params)
if result if result
status result[:status] || 200 status result[:status] || 200
......
This diff is collapsed.
# frozen_string_literal: true # frozen_string_literal: true
Integration.available_integration_names.each do |service| Integration.available_integration_names.each do |integration|
RSpec.shared_context service do RSpec.shared_context integration do
include JiraServiceHelper if service == 'jira' include JiraServiceHelper if integration == 'jira'
let(:dashed_service) { service.dasherize } let(:dashed_integration) { integration.dasherize }
let(:service_method) { Project.integration_association_name(service) } let(:integration_method) { Project.integration_association_name(integration) }
let(:service_klass) { Integration.integration_name_to_model(service) } let(:integration_klass) { Integration.integration_name_to_model(integration) }
let(:service_instance) { service_klass.new } let(:integration_instance) { integration_klass.new }
let(:service_fields) { service_instance.fields } let(:integration_fields) { integration_instance.fields }
let(:service_attrs_list) { service_fields.inject([]) {|arr, hash| arr << hash[:name].to_sym } } let(:integration_attrs_list) { integration_fields.inject([]) {|arr, hash| arr << hash[:name].to_sym } }
let(:service_attrs) do let(:integration_attrs) do
service_attrs_list.inject({}) do |hash, k| integration_attrs_list.inject({}) do |hash, k|
if k =~ /^(token*|.*_token|.*_key)/ if k =~ /^(token*|.*_token|.*_key)/
hash.merge!(k => 'secrettoken') hash.merge!(k => 'secrettoken')
elsif service == 'confluence' && k == :confluence_url elsif integration == 'confluence' && k == :confluence_url
hash.merge!(k => 'https://example.atlassian.net/wiki') hash.merge!(k => 'https://example.atlassian.net/wiki')
elsif service == 'datadog' && k == :datadog_site elsif integration == 'datadog' && k == :datadog_site
hash.merge!(k => 'datadoghq.com') hash.merge!(k => 'datadoghq.com')
elsif service == 'packagist' && k == :server elsif integration == 'packagist' && k == :server
hash.merge!(k => 'https://packagist.example.com') hash.merge!(k => 'https://packagist.example.com')
elsif k =~ /^(.*_url|url|webhook)/ elsif k =~ /^(.*_url|url|webhook)/
hash.merge!(k => "http://example.com") hash.merge!(k => "http://example.com")
elsif service_klass.method_defined?("#{k}?") elsif integration_klass.method_defined?("#{k}?")
hash.merge!(k => true) hash.merge!(k => true)
elsif service == 'irker' && k == :recipients elsif integration == 'irker' && k == :recipients
hash.merge!(k => 'irc://irc.network.net:666/#channel') hash.merge!(k => 'irc://irc.network.net:666/#channel')
elsif service == 'irker' && k == :server_port elsif integration == 'irker' && k == :server_port
hash.merge!(k => 1234) hash.merge!(k => 1234)
elsif service == 'jira' && k == :jira_issue_transition_id elsif integration == 'jira' && k == :jira_issue_transition_id
hash.merge!(k => '1,2,3') hash.merge!(k => '1,2,3')
elsif service == 'emails_on_push' && k == :recipients elsif integration == 'emails_on_push' && k == :recipients
hash.merge!(k => 'foo@bar.com') hash.merge!(k => 'foo@bar.com')
elsif service == 'slack' || service == 'mattermost' && k == :labels_to_be_notified_behavior elsif integration == 'slack' || integration == 'mattermost' && k == :labels_to_be_notified_behavior
hash.merge!(k => "match_any") hash.merge!(k => "match_any")
else else
hash.merge!(k => "someword") hash.merge!(k => "someword")
...@@ -47,24 +47,24 @@ Integration.available_integration_names.each do |service| ...@@ -47,24 +47,24 @@ Integration.available_integration_names.each do |service|
end end
before do before do
enable_license_for_service(service) enable_license_for_integration(integration)
stub_jira_integration_test if service == 'jira' stub_jira_integration_test if integration == 'jira'
end end
def initialize_integration(integration, attrs = {}) def initialize_integration(integration, attrs = {})
record = project.find_or_initialize_integration(integration) record = project.find_or_initialize_integration(integration)
record.attributes = attrs record.attributes = attrs
record.properties = service_attrs record.properties = integration_attrs
record.save! record.save!
record record
end end
private private
def enable_license_for_service(service) def enable_license_for_integration(integration)
return unless respond_to?(:stub_licensed_features) return unless respond_to?(:stub_licensed_features)
licensed_feature = licensed_features[service] licensed_feature = licensed_features[integration]
return unless licensed_feature return unless licensed_feature
stub_licensed_features(licensed_feature => true) stub_licensed_features(licensed_feature => true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment