Commit d03f4c72 authored by Alex Kalderimis's avatar Alex Kalderimis

Change a number of meta-methods

This changes a number of ancillary methods with 'service' in them to
refer to 'integration' instead.

hamllint would have us prefer format to percent formatting.

This updates the spec definitions to use the new factory names. Rather
than inferring the factory names, they are provided explicitly.
parent fa426857
......@@ -12,19 +12,19 @@ module Resolvers
argument :active,
GraphQL::BOOLEAN_TYPE,
required: false,
description: 'Indicates if the service is active.'
description: 'Indicates if the integration is active.'
argument :type,
Types::Projects::ServiceTypeEnum,
required: false,
description: 'Class name of the service.'
description: 'Type of integration.'
alias_method :project, :object
def resolve(active: nil, type: nil)
servs = project.integrations
servs = servs.by_active_flag(active) unless active.nil?
servs = servs.by_type(type) unless type.blank?
servs
items = project.integrations
items = items.by_active_flag(active) unless active.nil?
items = items.by_type(type) unless type.blank?
items
end
end
end
......
......@@ -5,14 +5,8 @@ module Types
class ServiceTypeEnum < BaseEnum
graphql_name 'ServiceType'
::Integration.available_services_types(include_dev: false).each do |service_type|
replacement = Integration.integration_type_for_service_type(service_type)
value service_type.underscore.upcase, value: service_type, description: "#{service_type} type"
end
::Integration.available_integration_types(include_dev: false).each do |type|
value type.underscore.upcase, value: type, description: "#{type} integration"
::Integration.available_services_types(include_dev: false).each do |type|
value type.underscore.upcase, value: type, description: "#{type} type"
end
end
end
......
......@@ -201,7 +201,7 @@ class Integration < ApplicationRecord
def self.find_or_initialize_non_project_specific_integration(name, instance: false, group_id: nil)
return unless name.in?(available_services_names(include_project_specific: false))
service_name_to_model(name).find_or_initialize_by(instance: instance, group_id: group_id)
integration_name_to_model(name).find_or_initialize_by(instance: instance, group_id: group_id)
end
def self.find_or_initialize_all_non_project_specific(scope)
......@@ -210,7 +210,7 @@ class Integration < ApplicationRecord
def self.build_nonexistent_services_for(scope)
nonexistent_services_types_for(scope).map do |service_type|
service_type_to_model(service_type).new
integration_type_to_model(service_type).new
end
end
private_class_method :build_nonexistent_services_for
......@@ -235,11 +235,6 @@ class Integration < ApplicationRecord
service_names.sort_by(&:downcase)
end
def self.available_integration_names(**args)
available_services_names(**args)
.select { RENAMED_TO_INTEGRATION.include?(_1) }
end
def self.integration_names
INTEGRATION_NAMES
end
......@@ -262,45 +257,29 @@ class Integration < ApplicationRecord
# Example: ["AsanaService", ...]
def self.available_services_types(include_project_specific: true, include_dev: true)
available_services_names(include_project_specific: include_project_specific, include_dev: include_dev).map do |service_name|
service_name_to_type(service_name)
end
end
def self.available_integration_types(include_project_specific: true, include_dev: true)
available_integration_names(include_project_specific: include_project_specific, include_dev: include_dev).map do |name|
integration_name_to_type(name)
integration_name_to_type(service_name)
end
end
def self.integration_type_for_service_type(type_name)
name = type_name.chomp(type_name)
integration_name_to_type(name) if RENAMED_TO_INTEGRATION.include?(name)
end
# Returns the model for the given service name.
# Example: "asana" => Integrations::Asana
def self.service_name_to_model(name)
type = service_name_to_type(name)
service_type_to_model(type)
def self.integration_name_to_model(name)
type = integration_name_to_type(name)
integration_type_to_model(type)
end
# Returns the STI type for the given service name.
# Example: "asana" => "AsanaService"
def self.service_name_to_type(name)
"#{name}_service".camelize
end
def self.integration_name_to_type(name)
"#{name}_integration".camelize
"#{name}_service".camelize
end
# Returns the model for the given STI type.
# Example: "AsanaService" => Integrations::Asana
def self.service_type_to_model(type)
def self.integration_type_to_model(type)
Gitlab::Integrations::StiType.new.cast(type).constantize
end
private_class_method :service_type_to_model
private_class_method :integration_type_to_model
def self.build_from_integration(integration, project_id: nil, group_id: nil)
new_integration = integration.dup
......
......@@ -2645,7 +2645,7 @@ class Project < ApplicationRecord
end
def build_service(name)
Integration.service_name_to_model(name).new(project_id: id)
Integration.integration_name_to_model(name).new(project_id: id)
end
def services_templates
......
......@@ -6,8 +6,8 @@
= s_('WikiEmpty|Confluence is enabled')
%p
- wiki_confluence_epic_link_url = 'https://gitlab.com/groups/gitlab-org/-/epics/3629'
- wiki_confluence_epic_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: wiki_confluence_epic_link_url }
= s_("WikiEmpty|You've enabled the Confluence Workspace integration. Your wiki will be viewable directly within Confluence. We are hard at work integrating Confluence more seamlessly into GitLab. If you'd like to stay up to date, follow our %{wiki_confluence_epic_link_start}Confluence epic%{wiki_confluence_epic_link_end}.").html_safe % { wiki_confluence_epic_link_start: wiki_confluence_epic_link_start, wiki_confluence_epic_link_end: '</a>'.html_safe }
- wiki_confluence_epic_link_start = format('<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe, url: wiki_confluence_epic_link_url)
= format(s_("WikiEmpty|You've enabled the Confluence Workspace integration. Your wiki will be viewable directly within Confluence. We are hard at work integrating Confluence more seamlessly into GitLab. If you'd like to stay up to date, follow our %{wiki_confluence_epic_link_start}Confluence epic%{wiki_confluence_epic_link_end}.").html_safe, wiki_confluence_epic_link_start: wiki_confluence_epic_link_start, wiki_confluence_epic_link_end: '</a>'.html_safe)
= link_to @project.confluence_integration.confluence_url, target: '_blank', rel: 'noopener noreferrer', class: 'gl-button btn btn-success external-url', title: s_('WikiEmpty|Go to Confluence') do
= sprite_icon('external-link')
= s_('WikiEmpty|Go to Confluence')
......
......@@ -11928,8 +11928,8 @@ four standard [pagination arguments](#connection-pagination-arguments):
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="projectservicesactive"></a>`active` | [`Boolean`](#boolean) | Indicates if the service is active. |
| <a id="projectservicestype"></a>`type` | [`ServiceType`](#servicetype) | Class name of the service. |
| <a id="projectservicesactive"></a>`active` | [`Boolean`](#boolean) | Indicates if the integration is active. |
| <a id="projectservicestype"></a>`type` | [`ServiceType`](#servicetype) | Type of integration. |
##### `Project.snippets`
......@@ -14852,18 +14852,12 @@ State of a Sentry error.
| Value | Description |
| ----- | ----------- |
| <a id="servicetypeasana_integration"></a>`ASANA_INTEGRATION` | AsanaIntegration integration. |
| <a id="servicetypeasana_service"></a>`ASANA_SERVICE` | AsanaService type. |
| <a id="servicetypeassembla_integration"></a>`ASSEMBLA_INTEGRATION` | AssemblaIntegration integration. |
| <a id="servicetypeassembla_service"></a>`ASSEMBLA_SERVICE` | AssemblaService type. |
| <a id="servicetypebamboo_integration"></a>`BAMBOO_INTEGRATION` | BambooIntegration integration. |
| <a id="servicetypebamboo_service"></a>`BAMBOO_SERVICE` | BambooService type. |
| <a id="servicetypebugzilla_integration"></a>`BUGZILLA_INTEGRATION` | BugzillaIntegration integration. |
| <a id="servicetypebugzilla_service"></a>`BUGZILLA_SERVICE` | BugzillaService type. |
| <a id="servicetypebuildkite_service"></a>`BUILDKITE_SERVICE` | BuildkiteService type. |
| <a id="servicetypecampfire_integration"></a>`CAMPFIRE_INTEGRATION` | CampfireIntegration integration. |
| <a id="servicetypecampfire_service"></a>`CAMPFIRE_SERVICE` | CampfireService type. |
| <a id="servicetypeconfluence_integration"></a>`CONFLUENCE_INTEGRATION` | ConfluenceIntegration integration. |
| <a id="servicetypeconfluence_service"></a>`CONFLUENCE_SERVICE` | ConfluenceService type. |
| <a id="servicetypecustom_issue_tracker_service"></a>`CUSTOM_ISSUE_TRACKER_SERVICE` | CustomIssueTrackerService type. |
| <a id="servicetypedatadog_service"></a>`DATADOG_SERVICE` | DatadogService type. |
......
......@@ -403,15 +403,15 @@ module Gitlab
def services_usage
# rubocop: disable UsageData/LargeTable:
Integration.available_services_names(include_dev: false).each_with_object({}) do |service_name, response|
service_type = Integration.service_name_to_type(service_name)
response["projects_#{service_name}_active".to_sym] = count(Integration.active.where.not(project: nil).where(type: service_type))
response["groups_#{service_name}_active".to_sym] = count(Integration.active.where.not(group: nil).where(type: service_type))
response["templates_#{service_name}_active".to_sym] = count(Integration.active.where(template: true, type: service_type))
response["instances_#{service_name}_active".to_sym] = count(Integration.active.where(instance: true, type: service_type))
response["projects_inheriting_#{service_name}_active".to_sym] = count(Integration.active.where.not(project: nil).where.not(inherit_from_id: nil).where(type: service_type))
response["groups_inheriting_#{service_name}_active".to_sym] = count(Integration.active.where.not(group: nil).where.not(inherit_from_id: nil).where(type: service_type))
Integration.available_services_names(include_dev: false).each_with_object({}) do |name, response|
type = Integration.integration_name_to_type(name)
response[:"projects_#{name}_active"] = count(Integration.active.where.not(project: nil).where(type: type))
response[:"groups_#{name}_active"] = count(Integration.active.where.not(group: nil).where(type: type))
response[:"templates_#{name}_active"] = count(Integration.active.where(template: true, type: type))
response[:"instances_#{name}_active"] = count(Integration.active.where(instance: true, type: type))
response[:"projects_inheriting_#{name}_active"] = count(Integration.active.where.not(project: nil).where.not(inherit_from_id: nil).where(type: type))
response[:"groups_inheriting_#{name}_active"] = count(Integration.active.where.not(group: nil).where.not(inherit_from_id: nil).where(type: type))
end.merge(jira_usage, jira_import_usage)
# rubocop: enable UsageData/LargeTable:
end
......
......@@ -3,13 +3,11 @@
require 'spec_helper'
RSpec.describe GitlabSchema.types['ServiceType'] do
specify { expect(described_class.graphql_name).to eq('ServiceType') }
it 'exposes all the existing project services' do
expect(described_class.values.keys).to match_array(available_services_enum)
end
end
def available_services_enum
::Integration.available_services_types(include_dev: false).map(&:underscore).map(&:upcase)
def available_services_enum
::Integration.available_services_types(include_dev: false).map(&:underscore).map(&:upcase)
end
end
......@@ -4822,7 +4822,7 @@ RSpec.describe Ci::Build do
context 'with project services' do
before do
create(:service, active: true, job_events: true, project: project)
create(:integration, active: true, job_events: true, project: project)
end
it 'executes services' do
......@@ -4836,7 +4836,7 @@ RSpec.describe Ci::Build do
context 'without relevant project services' do
before do
create(:service, active: true, job_events: false, project: project)
create(:integration, active: true, job_events: false, project: project)
end
it 'does not execute services' do
......
......@@ -160,7 +160,7 @@ RSpec.describe Integration do
context 'when instance-level service' do
Integration.available_services_types.each do |service_type|
let(:service) do
described_class.send(:service_type_to_model, service_type).new(instance: true)
described_class.send(:integration_type_to_model, service_type).new(instance: true)
end
it { is_expected.to be_falsey }
......@@ -170,7 +170,7 @@ RSpec.describe Integration do
context 'when group-level service' do
Integration.available_services_types.each do |service_type|
let(:service) do
described_class.send(:service_type_to_model, service_type).new(group_id: group.id)
described_class.send(:integration_type_to_model, service_type).new(group_id: group.id)
end
it { is_expected.to be_falsey }
......@@ -668,16 +668,16 @@ RSpec.describe Integration do
end
end
describe '.service_name_to_model' do
describe '.integration_name_to_model' do
it 'returns the model for the given service name', :aggregate_failures do
expect(described_class.service_name_to_model('asana')).to eq(Integrations::Asana)
expect(described_class.integration_name_to_model('asana')).to eq(Integrations::Asana)
# TODO We can remove this test when all models have been namespaced:
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/60968#note_570994955
expect(described_class.service_name_to_model('prometheus')).to eq(PrometheusService)
expect(described_class.integration_name_to_model('prometheus')).to eq(PrometheusService)
end
it 'raises an error if service name is invalid' do
expect { described_class.service_name_to_model('foo') }.to raise_exception(NameError, /uninitialized constant FooService/)
expect { described_class.integration_name_to_model('foo') }.to raise_exception(NameError, /uninitialized constant FooService/)
end
end
......
......@@ -251,22 +251,22 @@ RSpec.describe MergeRequests::BuildService do
end
context 'when the source branch matches an issue' do
where(:issue_tracker, :source_branch, :closing_message) do
:jira | 'FOO-123-fix-issue' | 'Closes FOO-123'
:jira | 'fix-issue' | nil
:custom_issue_tracker | '123-fix-issue' | 'Closes #123'
:custom_issue_tracker | 'fix-issue' | nil
:internal | '123-fix-issue' | 'Closes #123'
:internal | 'fix-issue' | nil
where(:factory, :source_branch, :closing_message) do
:jira_service | 'FOO-123-fix-issue' | 'Closes FOO-123'
:jira_service | 'fix-issue' | nil
:custom_issue_tracker_integration | '123-fix-issue' | 'Closes #123'
:custom_issue_tracker_integration | 'fix-issue' | nil
nil | '123-fix-issue' | 'Closes #123'
nil | 'fix-issue' | nil
end
with_them do
before do
if issue_tracker == :internal
issue.update!(iid: 123)
else
create(:"#{issue_tracker}_service", project: project)
if factory
create(factory, project: project)
project.reload
else
issue.update!(iid: 123)
end
end
......@@ -350,23 +350,23 @@ RSpec.describe MergeRequests::BuildService do
end
context 'when the source branch matches an issue' do
where(:issue_tracker, :source_branch, :title, :closing_message) do
:jira | 'FOO-123-fix-issue' | 'Resolve FOO-123 "Fix issue"' | 'Closes FOO-123'
:jira | 'fix-issue' | 'Fix issue' | nil
:custom_issue_tracker | '123-fix-issue' | 'Resolve #123 "Fix issue"' | 'Closes #123'
:custom_issue_tracker | 'fix-issue' | 'Fix issue' | nil
:internal | '123-fix-issue' | 'Resolve "A bug"' | 'Closes #123'
:internal | 'fix-issue' | 'Fix issue' | nil
:internal | '124-fix-issue' | '124 fix issue' | nil
where(:factory, :source_branch, :title, :closing_message) do
:jira_service | 'FOO-123-fix-issue' | 'Resolve FOO-123 "Fix issue"' | 'Closes FOO-123'
:jira_service | 'fix-issue' | 'Fix issue' | nil
:custom_issue_tracker_integration | '123-fix-issue' | 'Resolve #123 "Fix issue"' | 'Closes #123'
:custom_issue_tracker_integration | 'fix-issue' | 'Fix issue' | nil
nil | '123-fix-issue' | 'Resolve "A bug"' | 'Closes #123'
nil | 'fix-issue' | 'Fix issue' | nil
nil | '124-fix-issue' | '124 fix issue' | nil
end
with_them do
before do
if issue_tracker == :internal
issue.update!(iid: 123)
else
create(:"#{issue_tracker}_service", project: project)
if factory
create(factory, project: project)
project.reload
else
issue.update!(iid: 123)
end
end
......@@ -399,23 +399,23 @@ RSpec.describe MergeRequests::BuildService do
end
context 'when the source branch matches an issue' do
where(:issue_tracker, :source_branch, :title, :closing_message) do
:jira | 'FOO-123-fix-issue' | 'Resolve FOO-123 "Fix issue"' | 'Closes FOO-123'
:jira | 'fix-issue' | 'Fix issue' | nil
:custom_issue_tracker | '123-fix-issue' | 'Resolve #123 "Fix issue"' | 'Closes #123'
:custom_issue_tracker | 'fix-issue' | 'Fix issue' | nil
:internal | '123-fix-issue' | 'Resolve "A bug"' | 'Closes #123'
:internal | 'fix-issue' | 'Fix issue' | nil
:internal | '124-fix-issue' | '124 fix issue' | nil
where(:factory, :source_branch, :title, :closing_message) do
:jira_service | 'FOO-123-fix-issue' | 'Resolve FOO-123 "Fix issue"' | 'Closes FOO-123'
:jira_service | 'fix-issue' | 'Fix issue' | nil
:custom_issue_tracker_integration | '123-fix-issue' | 'Resolve #123 "Fix issue"' | 'Closes #123'
:custom_issue_tracker_integration | 'fix-issue' | 'Fix issue' | nil
nil | '123-fix-issue' | 'Resolve "A bug"' | 'Closes #123'
nil | 'fix-issue' | 'Fix issue' | nil
nil | '124-fix-issue' | '124 fix issue' | nil
end
with_them do
before do
if issue_tracker == :internal
issue.update!(iid: 123)
else
create(:"#{issue_tracker}_service", project: project)
if factory
create(factory, project: project)
project.reload
else
issue.update!(iid: 123)
end
end
......
# frozen_string_literal: true
RSpec::Matchers.define :be_one_of do |collection|
match do |item|
expect(collection).to include(item)
end
failure_message do |item|
"expected #{item} to be one of #{collection}"
end
end
......@@ -5,8 +5,8 @@ Integration.available_services_names.each do |service|
include JiraServiceHelper if service == 'jira'
let(:dashed_service) { service.dasherize }
let(:service_method) { "#{service}_service".to_sym }
let(:service_klass) { Integration.service_name_to_model(service) }
let(:service_method) { integration_association(service) }
let(:service_klass) { Integration.integration_name_to_model(service) }
let(:service_instance) { service_klass.new }
let(:service_fields) { service_instance.fields }
let(:service_attrs_list) { service_fields.inject([]) {|arr, hash| arr << hash[:name].to_sym } }
......@@ -57,6 +57,16 @@ Integration.available_services_names.each do |service|
service_item
end
# Returns the association name for the given integration.
# Example: 'asana' => 'asana_integration'
def integration_association(name)
if Integration::RENAMED_TO_INTEGRATION.include?(name)
"#{name}_integration".to_sym
else
"#{name}_service".to_sym
end
end
private
def enable_license_for_service(service)
......
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