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

This commit is contained in:
Luis Del Giudice 2016-10-08 12:51:44 -04:00 committed by Adam Niedzielski
parent 81ad611131
commit 6bc3edee11
4 changed files with 49 additions and 1 deletions

View File

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

View File

@ -1,4 +1,6 @@
class IssueTrackerService < Service
validate :one_issue_tracker, if: :activated?, on: :manual_change
default_value_for :category, 'issue_tracker'
# Pattern used to extract links from comments
@ -92,4 +94,13 @@ class IssueTrackerService < Service
def issues_tracker
Gitlab.config.issues_tracker[to_param]
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

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