diff --git a/app/assets/javascripts/settings.js.erb b/app/assets/javascripts/settings.js.erb index b275eb3f68..0bd046cc73 100644 --- a/app/assets/javascripts/settings.js.erb +++ b/app/assets/javascripts/settings.js.erb @@ -27,8 +27,18 @@ See doc/COPYRIGHT.rdoc for more details. ++#%> -jQuery(document).ready(function($) { - $('#settings_session_ttl_enabled').on('change', function(){ - $('#settings_session_ttl_container').toggle($(this).is(':checked')); - }).trigger('change'); -}); +(function($) { + $(function() { + + $('#settings_session_ttl_enabled').on('change', function(){ + $('#settings_session_ttl_container').toggle($(this).is(':checked')); + }).trigger('change'); + + $('#settings_repositories_automatic_managed_repos').change(function() { + var checked = this.checked; + $('#settings_repositories_automatic_managed_vendor').prop('disabled', function() { + return !checked; + }) + }); + }); +}(jQuery)); diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index acc0a69e8a..8095000f3f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -45,8 +45,8 @@ class RepositoriesController < ApplicationController default_search_scope :changesets before_filter :find_project_by_project_id - before_filter :find_repository, except: [:edit, :update, :create, :destroy, :destroy_info] before_filter :authorize + before_filter :find_repository, except: [:edit, :update, :create, :destroy, :destroy_info] accept_key_auth :revisions rescue_from OpenProject::Scm::Exceptions::ScmError, with: :show_error_command_failed diff --git a/app/models/enabled_module.rb b/app/models/enabled_module.rb index 8aa4b23e6d..2f34af0f07 100644 --- a/app/models/enabled_module.rb +++ b/app/models/enabled_module.rb @@ -47,6 +47,23 @@ class EnabledModule < ActiveRecord::Base if project && project.wiki.nil? Wiki.create(project: project, start_page: 'Wiki') end + when 'repository' + if project && + project.repository.nil? && + Setting.repositories_automatic_managed_repos? + create_managed_repository + end end end + + def create_managed_repository + params = { + scm_vendor: Setting.repositories_automatic_managed_vendor, + scm_type: Repository.managed_type + } + + service = Scm::RepositoryFactoryService.new(project, + ActionController::Parameters.new(params)) + service.build_and_save + end end diff --git a/app/views/settings/_repositories.html.erb b/app/views/settings/_repositories.html.erb index 65275ad378..4f1797699b 100644 --- a/app/views/settings/_repositories.html.erb +++ b/app/views/settings/_repositories.html.erb @@ -51,6 +51,23 @@ See doc/COPYRIGHT.rdoc for more details.
<%= setting_text_field :repository_log_display_limit, :size => 6 %>
<%= setting_check_box :repository_authentication_caching_enabled %>
+
<%= l('repositories.settings.automatic_managed_repos') %> +
<%= setting_check_box :repositories_automatic_managed_repos %>
+
+ <%= styled_label_tag :repositories_automatic_managed_vendor, l(:setting_repositories_automatic_managed_vendor) %> +
+ <%= setting_select :repositories_automatic_managed_vendor, + [[ + "--- #{l(:actionview_instancetag_blank_option)} ---", + '', + { disabled: true, selected: true } + ]] + OpenProject::Scm::Manager.manageable, + disabled: !Setting.repositories_automatic_managed_repos?, + required: true, + label: false %> +
+
+
<%= l(:text_work_packages_ref_in_commit_messages) %>
<%= setting_text_field :commit_ref_keywords, :size => 30 %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 9b5f29397d..fe282a7fad 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1309,6 +1309,9 @@ en: path: "Path to Git repository" path_encoding: "Path encoding" go_to_revision: "Go to revision" + settings: + automatic_managed_repos: "Automatic creation of managed repositories" + automatic_managed_repos_text: "With the following configuration options, you can automatically create a managed repository of the selected vendor whenever a new project is created." scm_vendor: "Source control management system" scm_type: "Repository type" scm_types: @@ -1393,6 +1396,8 @@ en: setting_per_page_options: "Objects per page options" setting_plain_text_mail: "Plain text mail (no HTML)" setting_protocol: "Protocol" + setting_repositories_automatic_managed_repos: "Setup managed repositories on project creation" + setting_repositories_automatic_managed_vendor: "Automatic repository vendor type" setting_repositories_encodings: "Repositories encodings" setting_repository_authentication_caching_enabled: "Enable caching for authentication request of version control software" setting_repository_log_display_limit: "Maximum number of revisions displayed on file log" diff --git a/config/settings.yml b/config/settings.yml index c404fe1d1e..a572d3c618 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -137,6 +137,11 @@ sys_api_key: default: '' repository_authentication_caching_enabled: default: 1 +repositories_automatic_managed_repos: + default: 0 + format: bool +repositories_automatic_managed_vendor: + default: '' commit_ref_keywords: default: 'refs,references,IssueID' commit_fix_keywords: diff --git a/lib/open_project/scm/manager.rb b/lib/open_project/scm/manager.rb index d168b3f331..76a14bc757 100644 --- a/lib/open_project/scm/manager.rb +++ b/lib/open_project/scm/manager.rb @@ -46,6 +46,11 @@ module OpenProject registered.select { |scm| Setting.enabled_scm.include?(scm) } end + # Return all manageable vendors + def manageable + enabled.select { |_, vendor| vendor.manageable? }.keys + end + # Add a new SCM adapter and repository def add(scm_name) # Force model lookup to avoid diff --git a/spec/legacy/unit/enabled_module_spec.rb b/spec/legacy/unit/enabled_module_spec.rb deleted file mode 100644 index 953b5111cc..0000000000 --- a/spec/legacy/unit/enabled_module_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -#-- encoding: UTF-8 -#-- copyright -# OpenProject is a project management system. -# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License version 3. -# -# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: -# Copyright (C) 2006-2013 Jean-Philippe Lang -# Copyright (C) 2010-2013 the ChiliProject Team -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# See doc/COPYRIGHT.rdoc for more details. -#++ -require 'legacy_spec_helper' - -describe EnabledModule do - it 'should enabling_wiki_should_create_a_wiki' do - CustomField.delete_all - FactoryGirl.create(:type_standard) - project = Project.create!(name: 'Project with wiki', identifier: 'wikiproject') - assert_nil project.wiki - project.enabled_module_names = ['wiki'] - wiki = FactoryGirl.create :wiki, project: project - project.reload - assert_not_nil project.wiki - assert_equal 'Wiki', project.wiki.start_page - end - - it 'should reenabling_wiki_should_not_create_another_wiki' do - project = FactoryGirl.create :project - wiki = FactoryGirl.create :wiki, project: project - project.reload - assert_not_nil project.wiki - project.enabled_module_names = [] - project.reload - assert_no_difference 'Wiki.count' do - project.enabled_module_names = ['wiki'] - end - assert_not_nil project.wiki - end -end diff --git a/spec/models/enabled_module_spec.rb b/spec/models/enabled_module_spec.rb new file mode 100644 index 0000000000..25ea2b3969 --- /dev/null +++ b/spec/models/enabled_module_spec.rb @@ -0,0 +1,148 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' + +describe EnabledModule, type: :model do + # Force reload, as association is not always(?) showing + let(:project) { FactoryGirl.create(:project, enabled_module_names: modules).reload } + + describe '#wiki' do + let(:modules) { %w[wiki] } + + it 'creates a wiki' do + expect(project.wiki).not_to be_nil + expect(project.wiki.start_page).to eq('Wiki') + end + + it 'should not create a separate wiki when one exists already' do + expect(project.wiki).to_not be_nil + + expect { + project.enabled_module_names = [] + project.reload + }.to_not change { Wiki.count } + + expect { + project.enabled_module_names = ['wiki'] + }.to_not change { Wiki.count } + + expect(project.wiki).to_not be_nil + end + + context 'with disabled module' do + let(:modules) { [] } + + it 'does not create a wiki' do + expect(project.wiki).to be_nil + end + + it 'creates a wiki when the module is enabled at a later time' do + project.enabled_module_names = ['wiki'] + project.reload + + expect(project.wiki).to_not be_nil + expect(project.wiki.start_page).to eq('Wiki') + end + end + end + + describe '#repository' do + let(:modules) { %w[repository] } + let(:vendor) { nil } + + before do + allow(Setting).to receive(:repositories_automatic_managed_repos?).and_return(setting) + allow(Setting).to receive(:repositories_automatic_managed_vendor).and_return(vendor) + end + + shared_examples 'does not create a repository when one exists' do + let!(:repository) { FactoryGirl.create(:repository_git, project: project) } + + it 'should not create a separate repository when one exists already' do + project.reload + expect(project.repository).to_not be_nil + + expect { + project.enabled_module_names = [] + project.reload + }.to_not change { Repository.count } + + expect { + project.enabled_module_names = ['repository'] + }.to_not change { Repository.count } + + expect(project.repository).to_not be_nil + end + end + + context 'with disabled setting' do + let(:setting) { false } + + it 'does not create a repository' do + expect(project.repository).to be_nil + end + + it_behaves_like 'does not create a repository when one exists' + end + + context 'with enabled setting' do + let(:setting) { true } + let(:vendor) { 'Git' } + + before do + allow(Setting).to receive(:enabled_scm).and_return(['Git']) + end + + it 'creates a repository of the given vendor' do + expect(project.repository).not_to be_nil + expect(project.repository.vendor).to eq('Git') + expect(project.repository.managed?).to be true + end + + it 'does not remove the repository when setting is removed' do + project.enabled_module_names = [] + project.reload + + expect(project.repository).not_to be_nil + end + + it_behaves_like 'does not create a repository when one exists' + end + + context 'with invalid setting' do + let(:setting) { true } + let(:vendor) { 'some weird vendor' } + + it 'does not create a repository' do + expect(project.repository).to be_nil + end + end + end +end