Class: Package::Audit::DuplicatePackageMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/package/audit/services/duplicate_package_merger.rb

Instance Method Summary collapse

Constructor Details

#initialize(pkgs) ⇒ DuplicatePackageMerger

Returns a new instance of DuplicatePackageMerger.



4
5
6
# File 'lib/package/audit/services/duplicate_package_merger.rb', line 4

def initialize(pkgs)
  @pkgs = pkgs.sort_by(&:full_name)
end

Instance Method Details

#runObject

rubocop:disable Metrics/AbcSize



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/package/audit/services/duplicate_package_merger.rb', line 8

def run # rubocop:disable Metrics/AbcSize
  pkgs = @pkgs.take(1)

  @pkgs[1..]&.each do |curr|
    prev = pkgs[-1]
    if curr.full_name == prev.full_name
      prev.update(groups: prev.groups | curr.groups,
                  vulnerabilities: prev.vulnerabilities + curr.vulnerabilities,
                  risks: prev.risks + curr.risks)
    else
      pkgs << curr
    end
  end

  pkgs
end