Commit db93de44 authored by Justin Ho's avatar Justin Ho

Add activate_disabled_reason to models

We are passing a list of enabled trackers which can be used
by the frontend to inform the user of which ones are
already enabled (so they can disable them if needed).
parent 99efd307
......@@ -346,6 +346,10 @@ class Integration < ApplicationRecord
true
end
def activate_disabled_reason
nil
end
def category
read_attribute(:category).to_sym
end
......
......@@ -132,8 +132,18 @@ module Integrations
# implement inside child
end
def activate_disabled_reason
{ trackers: other_external_issue_trackers } if other_external_issue_trackers.any?
end
private
def other_external_issue_trackers
return [] unless project_level?
project.integrations.external_issue_trackers.where.not(id: id)
end
def enabled_in_gitlab_config
Gitlab.config.issues_tracker &&
Gitlab.config.issues_tracker.values.any? &&
......@@ -148,7 +158,7 @@ module Integrations
return if instance?
return if project.blank?
if project.integrations.external_issue_trackers.where.not(id: id).any?
if other_external_issue_trackers.any?
errors.add(:base, _('Another issue tracker is already in use. Only one issue tracker service can be active at a time'))
end
end
......
......@@ -71,8 +71,18 @@ module Integrations
end
end
def activate_disabled_reason
{ trackers: external_issue_trackers } if external_issue_trackers.any?
end
private
def external_issue_trackers
return false unless project_level?
project.integrations.external_issue_trackers
end
def allowed_branch?(ref)
return true unless ref.present? && restrict_to_branch.present?
......
......@@ -224,6 +224,12 @@ FactoryBot.define do
recipients { 'test@example.com' }
end
factory :pivotaltracker_integration, class: 'Integrations::Pivotaltracker' do
project
active { true }
token { 'test' }
end
# this is for testing storing values inside properties, which is deprecated and will be removed in
# https://gitlab.com/gitlab-org/gitlab/issues/29404
trait :without_properties_callback do
......
......@@ -3,12 +3,12 @@
require 'spec_helper'
RSpec.describe Integrations::BaseIssueTracker do
describe 'Validations' do
let(:project) { create :project }
let(:integration) { Integrations::Redmine.new(project: project, active: true, issue_tracker_data: build(:issue_tracker_data)) }
describe 'only one issue tracker per project' do
let(:integration) { Integrations::Redmine.new(project: project, active: true, issue_tracker_data: build(:issue_tracker_data)) }
let_it_be_with_refind(:project) { create :project }
describe 'Validations' do
describe 'only one issue tracker per project' do
before do
create(:custom_issue_tracker_integration, project: project)
end
......@@ -31,4 +31,18 @@ RSpec.describe Integrations::BaseIssueTracker do
end
end
end
describe '#activate_disabled_reason' do
subject { integration.activate_disabled_reason }
context 'when there is an existing issue tracker integration' do
let_it_be(:custom_tracker) { create(:custom_issue_tracker_integration, project: project) }
it { is_expected.to eq(trackers: [custom_tracker]) }
end
context 'when there is no existing issue tracker integration' do
it { is_expected.to be(nil) }
end
end
end
......@@ -93,4 +93,22 @@ RSpec.describe Integrations::Pivotaltracker do
end
end
end
describe '#activate_disabled_reason' do
let(:integration) { build(:pivotaltracker_integration, project: project) }
let_it_be_with_refind(:project) { create :project }
subject { integration.activate_disabled_reason }
context 'when there is an external issue tracker integration' do
let_it_be(:custom_tracker) { create(:custom_issue_tracker_integration, project: project) }
it { is_expected.to eq(trackers: [custom_tracker]) }
end
context 'when there is no external issue tracker integration' do
it { is_expected.to be(nil) }
end
end
end
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