Skip to content

Commit

Permalink
Fix rspec failures pipeline config v5 (gocd#5274)
Browse files Browse the repository at this point in the history
* Replace all the occurances of apiv5_admin_pipeline_url with spark pipeline config v6 url

* Add materials representer to ApiV1 module
* Do not refer to representers from other module.

* Replace all occurances of default_url_options with incoming request to construct URL
  • Loading branch information
GaneshSPatil committed Oct 16, 2018
1 parent 880f5a1 commit e5bdf9e
Show file tree
Hide file tree
Showing 21 changed files with 1,207 additions and 14 deletions.
Expand Up @@ -25,10 +25,10 @@ class MaterialTestController < ::ApiV1::BaseController
before_action :check_admin_user_or_group_admin_user_and_403

def test
material_config = ApiV5::Admin::Pipelines::Materials::MaterialRepresenter.new(ApiV5::Admin::Pipelines::Materials::MaterialRepresenter.get_material_type(params[:type]).new).from_hash(params)
material_config = ApiV1::Admin::Pipelines::Materials::MaterialRepresenter.new(ApiV1::Admin::Pipelines::Materials::MaterialRepresenter.get_material_type(params[:type]).new).from_hash(params)
material_config.validateConcreteScmMaterial()
if material_config.errors.any?
json = ApiV5::Admin::Pipelines::Materials::MaterialRepresenter.new(material_config).to_hash(url_builder: self)
json = ApiV1::Admin::Pipelines::Materials::MaterialRepresenter.new(material_config).to_hash(url_builder: self)
return render_message('There was an error with the material configuration!', 422, {data: json})
end

Expand Down
Expand Up @@ -26,15 +26,19 @@ def initialize(options)
end

link :self do |opts|
opts[:url_builder].apiv5_admin_pipeline_url(@pipeline.getName())
req = opts[:url_builder].request
ctx = com.thoughtworks.go.spark.RequestContext.new(req.ssl? ? 'https' : 'http', req.host, req.port, '/go')
ctx.urlFor(com.thoughtworks.go.spark.Routes::PipelineConfig.name(@pipeline.name.toString))
end

link :doc do |opts|
'https://api.gocd.org/#pipeline-config'
end

link :find do |opts|
opts[:url_builder].apiv5_admin_pipeline_url(pipeline_name: '__pipeline_name__').gsub(/__pipeline_name__/, ':pipeline_name')
req = opts[:url_builder].request
ctx = com.thoughtworks.go.spark.RequestContext.new(req.ssl? ? 'https' : 'http', req.host, req.port, '/go')
ctx.urlFor(com.thoughtworks.go.spark.Routes::PipelineConfig.find)
end

property :name,
Expand Down
@@ -0,0 +1,32 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class DependencyMaterialRepresenter < BaseRepresenter
alias_method :material_config, :represented

property :pipeline_name, as: :pipeline, case_insensitive_string: true
property :stage_name, as: :stage, case_insensitive_string: true
property :name, case_insensitive_string: true
property :auto_update
end
end
end
end
end
@@ -0,0 +1,33 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
module EncryptedPasswordSupport
def from_hash(data, options={})
super
data = data.with_indifferent_access
encrypted_password = Services.password_deserializer.deserialize(data[:password], data[:encrypted_password], represented)
represented.setEncryptedPassword(encrypted_password)
represented
end
end
end
end
end
end
@@ -0,0 +1,39 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class FilterRepresenter < BaseRepresenter
alias_method :filter, :represented

collection :ignore, exec_context: :decorator

def to_hash(*options)
ignored_files=filter.map { |item| item.getPattern() }
{ignore: ignored_files} if !ignored_files.empty?
end

def ignore=(value)
filter.clear()
value.each { |pattern| filter.add(IgnoredFiles.new(pattern)) }
end
end
end
end
end
end
@@ -0,0 +1,36 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class GitMaterialRepresenter < ScmMaterialRepresenter
property :branch,
getter: lambda { |args|
branch = self.getBranch
branch.blank? ? 'master' : branch
},
setter: lambda { |value, options|
value.blank? ? self.setBranch('master') : self.setBranch(value)
}
property :submodule_folder
property :shallow_clone
end
end
end
end
end
@@ -0,0 +1,26 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class HgMaterialRepresenter < ScmMaterialRepresenter
end
end
end
end
end
@@ -0,0 +1,81 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class MaterialRepresenter < BaseRepresenter
TYPE_TO_MATERIAL_MAP = {
'git' => GitMaterialConfig,
'svn' => SvnMaterialConfig,
'hg' => HgMaterialConfig,
'p4' => P4MaterialConfig,
'tfs' => TfsMaterialConfig,
'dependency' => DependencyMaterialConfig,
'package' => PackageMaterialConfig,
'plugin' => PluggableSCMMaterialConfig
}

MATERIAL_TO_TYPE_MAP = TYPE_TO_MATERIAL_MAP.invert

MATERIAL_TYPE_TO_REPRESENTER_MAP = {
GitMaterialConfig => GitMaterialRepresenter,
SvnMaterialConfig => SvnMaterialRepresenter,
HgMaterialConfig => HgMaterialRepresenter,
P4MaterialConfig => PerforceMaterialRepresenter,
TfsMaterialConfig => TfsMaterialRepresenter,
DependencyMaterialConfig => DependencyMaterialRepresenter,
PackageMaterialConfig => PackageMaterialRepresenter,
PluggableSCMMaterialConfig => PluggableScmMaterialRepresenter
}
alias_method :material_config, :represented

error_representer({
"materialName" => "name",
"folder" => "destination",
"autoUpdate" => "auto_update",
"filterAsString" => "filter",
"checkexternals" => "check_externals",
"serverAndPort" => "port",
"useTickets" => "use_tickets",
"pipelineName" => "pipeline",
"stageName" => "stage",
"pipelineStageName" => "pipeline",
"packageId" => "ref",
"scmId" => "ref",
"password" => "password",
"encryptedPassword" => "encrypted_password",
}
)

property :type, getter: lambda { |options| MATERIAL_TO_TYPE_MAP[self.class] }, skip_parse: true

nested :attributes,
decorator: lambda { |material_config, *|
MATERIAL_TYPE_TO_REPRESENTER_MAP[material_config.class]
}

class << self
def get_material_type(type)
TYPE_TO_MATERIAL_MAP[type] or (raise UnprocessableEntity, "Invalid material type '#{type}'. It has to be one of '#{TYPE_TO_MATERIAL_MAP.keys.join(' ')}'")
end
end
end
end
end
end
end
@@ -0,0 +1,33 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class PackageMaterialRepresenter < BaseRepresenter
alias_method :material_config, :represented

property :packageId, as: :ref, setter: lambda { |value, options|
package_definition = options[:go_config].getPackageRepositories().findPackageDefinitionWith(value)
self.setPackageDefinition(package_definition)
self.setPackageId(value)
}
end
end
end
end
end
@@ -0,0 +1,39 @@
##########################################################################
# Copyright 2017 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

module ApiV1
module Admin
module Pipelines
module Materials
class PerforceMaterialRepresenter < ScmMaterialRepresenter
include EncryptedPasswordSupport

property :url, skip_render: true, skip_parse: true #This is done so as to avoid setting the url property of super class ScmMaterialConfig
property :server_and_port, as: :port
property :user_name, as: :username
property :password,
skip_render: true,
skip_nil: true,
skip_parse: true

property :encrypted_password, skip_nil: true, skip_parse: true
property :use_tickets
property :view
end
end
end
end
end

0 comments on commit e5bdf9e

Please sign in to comment.