Merge branch 'adam-prevent-two-issue-trackers' into 'master'

Prevent more than one issue tracker to be active for the same project

Closes #22797

See merge request !9669
This commit is contained in:
Sean McGivern 2017-03-10 15:25:24 +00:00
commit dc99f343af
4 changed files with 49 additions and 1 deletions

View file

@ -13,7 +13,8 @@ class Projects::ServicesController < Projects::ApplicationController
end end
def update def update
if @service.update_attributes(service_params[:service]) @service.assign_attributes(service_params[:service])
if @service.save(context: :manual_change)
redirect_to( redirect_to(
edit_namespace_project_service_path(@project.namespace, @project, @service.to_param), edit_namespace_project_service_path(@project.namespace, @project, @service.to_param),
notice: 'Successfully updated.' notice: 'Successfully updated.'

View file

@ -1,4 +1,6 @@
class IssueTrackerService < Service class IssueTrackerService < Service
validate :one_issue_tracker, if: :activated?, on: :manual_change
default_value_for :category, 'issue_tracker' default_value_for :category, 'issue_tracker'
# Pattern used to extract links from comments # Pattern used to extract links from comments
@ -92,4 +94,13 @@ class IssueTrackerService < Service
def issues_tracker def issues_tracker
Gitlab.config.issues_tracker[to_param] Gitlab.config.issues_tracker[to_param]
end end
def one_issue_tracker
return if template?
return if project.blank?
if project.services.external_issue_trackers.where.not(id: id).any?
errors.add(:base, 'Another issue tracker is already in use. Only one issue tracker service can be active at a time')
end
end
end end

View file

@ -0,0 +1,4 @@
---
title: Prevent more than one issue tracker to be active for the same project
merge_request:
author: luisdgs19

View file

@ -0,0 +1,32 @@
require 'spec_helper'
describe IssueTrackerService, models: true do
describe 'Validations' do
let(:project) { create :project }
describe 'only one issue tracker per project' do
let(:service) { RedmineService.new(project: project, active: true) }
before do
create(:service, project: project, active: true, category: 'issue_tracker')
end
context 'when service is changed manually by user' do
it 'executes the validation' do
valid = service.valid?(:manual_change)
expect(valid).to be_falsey
expect(service.errors[:base]).to include(
'Another issue tracker is already in use. Only one issue tracker service can be active at a time'
)
end
end
context 'when service is changed internally' do
it 'does not execute the validation' do
expect(service.valid?).to be_truthy
end
end
end
end
end