OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/app/services/scm/repository_factory_service.rb

99 lines
2.9 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 COPYRIGHT and LICENSE files for more details.
#++
##
# Implements a repository factory for building temporary and permanent repositories.
class SCM::RepositoryFactoryService
attr_reader :project, :params, :repository
def initialize(project, params)
@project = project
@params = params
end
##
# Build a full repository from a given scm_type
# and persists it.
#
# @return [Boolean] true iff the repository was built
def build_and_save
build_guarded do
repository = build_with_type(params.fetch(:scm_type).to_sym)
if repository.save
repository
else
raise OpenProject::SCM::Exceptions::RepositoryBuildError.new(
repository.errors.full_messages.join("\n")
)
end
end
end
##
# Build a temporary repository used only for determining available settings and types
# of that particular vendor.
#
# @return [Boolean] true iff the repository was built
def build_temporary
build_guarded do
build_with_type(nil)
end
end
def build_error
I18n.t('repositories.errors.build_failed', reason: @build_failed_msg)
end
private
##
# Helper to actually build the repository and return it.
# May raise +OpenProject::SCM::Exceptions::RepositoryBuildError+ internally.
#
# @param [Symbol] scm_type Type to build the repository with. May be nil
# during temporary build
def build_with_type(scm_type)
Repository.build(
project,
params.fetch(:scm_vendor).to_sym,
params.fetch(:repository, {}),
scm_type
)
end
def build_guarded
@repository = yield
@repository.present?
rescue OpenProject::SCM::Exceptions::RepositoryBuildError => e
@build_failed_msg = e.message
nil
end
end