Skip to content

Commit

Permalink
[frontend] Refactor Package#last_build_reason
Browse files Browse the repository at this point in the history
We've been temporarely converting the xml string, eg. '<a></b>' to
a Nokogiri::XML object before creating a hash from it.
By using Xmlhash#parse we can skip the intermediate step and directly
create a hash.
This reduces the code and slightly improves the performance of that
method.
  • Loading branch information
bgeuken committed Jun 19, 2018
1 parent bf314bb commit 0094e9d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BuildReasonController < WebuiController

def index
@details = @package.last_build_reason(@repository, @architecture.name, @package_name)
return if @details.explain
return if @details.explain.present?

redirect_back(fallback_location: package_binaries_path(package: @package, project: @project, repository: @repository.name),
notice: "No build reason found for #{@repository.name}:#{@architecture.name}")
Expand Down
7 changes: 3 additions & 4 deletions src/api/app/models/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1442,15 +1442,14 @@ def self.what_depends_on(project, package, repository, architecture)
def last_build_reason(repo, arch, package_name = nil)
repo = repo.name if repo.is_a? Repository

xml_data = Nokogiri::XML(BuildReasonFile.new(
xml_data = BuildReasonFile.new(
project_name: project.name,
package_name: package_name || name,
repo: repo,
arch: arch
).to_s).xpath('reason')

data = Hash.from_xml(xml_data.to_s)['reason']
)

data = Xmlhash.parse(xml_data.to_s)
# ensure that if 'packagechange' exists, it is an Array and not a Hash
# Bugreport: https://github.com/openSUSE/open-build-service/issues/3230
data['packagechange'] = [data['packagechange']] if data && data['packagechange'].is_a?(Hash)
Expand Down

0 comments on commit 0094e9d

Please sign in to comment.