Skip to content

Commit

Permalink
Refactor BsRequestAction#create_expand_package
Browse files Browse the repository at this point in the history
Reduce complexity from create_expand_package and add specs

Use Nokogiri instead of REXML

Disable cops on create_expand_package
  • Loading branch information
vpereira committed Dec 10, 2019
1 parent be2f95e commit 6f494cf
Show file tree
Hide file tree
Showing 7 changed files with 705 additions and 21 deletions.
37 changes: 18 additions & 19 deletions src/api/app/models/bs_request_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def source_cleanup
Package.source_path(self.source_project, self.source_package)
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def create_expand_package(packages, opts = {})
Expand All @@ -415,9 +416,8 @@ def create_expand_package(packages, opts = {})
new_targets = []

packages.each do |pkg|
unless pkg.is_a?(Package)
raise RemoteSource, 'No support for auto expanding from remote instance. You need to submit a full specified request in that case.'
end
raise RemoteSource unless pkg.is_a?(Package)

# find target via linkinfo or submit to all.
# FIXME: this is currently handling local project links for packages with multiple spec files.
# This can be removed when we handle this as shadow packages in the backend.
Expand Down Expand Up @@ -469,11 +469,9 @@ def create_expand_package(packages, opts = {})
# do not allow release requests without binaries
if is_maintenance_release? && pkg.is_patchinfo? && data && !opts[:ignore_build_state]
# check for build state and binaries
state = REXML::Document.new(Backend::Api::BuildResults::Status.version_releases(pkg.project.name))
results = state.get_elements("/resultlist/result[@project='#{pkg.project.name}'')]")
unless results
raise BuildNotFinished, "The project'#{pkg.project.name}' has no building repositories"
end
results = pkg.project.build_results
raise BuildNotFinished, "The project'#{pkg.project.name}' has no building repositories" unless results

versrel = {}
results.each do |result|
repo = result.attributes['repository']
Expand All @@ -482,21 +480,21 @@ def create_expand_package(packages, opts = {})
raise BuildNotFinished, "The repository '#{pkg.project.name}' / '#{repo}' / #{arch} " \
'needs recalculation by the schedulers'
end
if result.attributes['state'].in?(['finished', 'publishing'])
if result.attributes['state'].value.in?(['finished', 'publishing'])
raise BuildNotFinished, "The repository '#{pkg.project.name}' / '#{repo}' / #{arch}" \
'did not finish the publish yet'
end
unless result.attributes['state'].in?(['published', 'unpublished'])
unless result.attributes['state'].value.in?(['published', 'unpublished'])
raise BuildNotFinished, "The repository '#{pkg.project.name}' / '#{repo}' / #{arch} " \
'did not finish the build yet'
end

# all versrel are the same
versrel[repo] ||= {}
result.get_elements('status').each do |status|
result.search('status').each do |status|
package = status.attributes['package']
vrel = status.attributes['versrel']
next unless vrel
next unless status.attributes['versrel']
vrel = status.attributes['versrel'].value
if versrel[repo][package] && versrel[repo][package] != vrel
raise VersionReleaseDiffers, "#{package} has a different version release in same repository"
end
Expand All @@ -510,9 +508,11 @@ def create_expand_package(packages, opts = {})
next unless firstarch

# skip excluded patchinfos
status = state.get_elements("/resultlist/result[@repository='#{repo.name}' and @arch='#{firstarch.name}']").first
next if status && (s = status.get_elements("status[@package='#{pkg.name}']").first) && s.attributes['code'] == 'excluded'
raise BuildNotFinished, "patchinfo #{pkg.name} is broken" if s.attributes['code'] == 'broken'
status = pkg.project.project_state.search("/resultlist/result[@repository='#{repo.name}' and @arch='#{firstarch.name}']").first

next if status && (s = status.search("status[@package='#{pkg.name}']").first) && s.attributes['code'].value == 'excluded'

raise BuildNotFinished, "patchinfo #{pkg.name} is broken" if s.attributes['code'].value == 'broken'

check_maintenance_release(pkg, repo, firstarch)

Expand Down Expand Up @@ -601,9 +601,7 @@ def create_expand_package(packages, opts = {})
newactions << new_action
end
end
if is_maintenance_release? && !found_patchinfo && !opts[:ignore_build_state]
raise MissingPatchinfo, 'maintenance release request without patchinfo would release no binaries'
end
raise MissingPatchinfo if is_maintenance_release? && !found_patchinfo && !opts[:ignore_build_state]

# new packages (eg patchinfos) go to all target projects by default in maintenance requests
new_targets.uniq!
Expand Down Expand Up @@ -642,6 +640,7 @@ def create_expand_package(packages, opts = {})

newactions
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity

Expand Down
8 changes: 6 additions & 2 deletions src/api/app/models/bs_request_action/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module BsRequestAction::Errors

# a diff error can have many reasons, but most likely something within us
class DiffError < APIError; setup 404; end
class RemoteSource < APIError; end
class RemoteSource < APIError
setup 'remote_source', 400, 'No support for auto expanding from remote instance. You need to submit a full specified request in that case.'
end
class RemoteTarget < APIError; end
class InvalidReleaseTarget < APIError; end
class LackingMaintainership < APIError
Expand All @@ -22,7 +24,9 @@ class BuildNotFinished < APIError; end
class UnknownTargetProject < APIError; end
class UnknownTargetPackage < APIError; end
class WrongLinkedPackageSource < APIError; end
class MissingPatchinfo < APIError; end
class MissingPatchinfo < APIError
setup 'missing_patchinfo', 400, 'maintenance release request without patchinfo would release no binaries'
end
class VersionReleaseDiffers < APIError; end
class LackingReleaseMaintainership < APIError; setup 'lacking_maintainership', 403; end
class RepositoryWithoutReleaseTarget < APIError; setup 'repository_without_releasetarget'; end
Expand Down
8 changes: 8 additions & 0 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,14 @@ def self.very_important_projects_with_categories
end
end

def build_results
project_state.search("/resultlist/result[@project='#{name}']")
end

def project_state
Nokogiri::XML(Backend::Api::BuildResults::Status.version_releases(name))
end

private

def discard_cache
Expand Down
Loading

0 comments on commit 6f494cf

Please sign in to comment.