Skip to content

Commit

Permalink
[Fixed] Duplicate approvals in decisions file
Browse files Browse the repository at this point in the history
Signed-off-by: Shane Lattanzio <slattanzio@pivotal.io>
  • Loading branch information
Daniil Kouznetsov authored and xtreme-shane-lattanzio committed Jan 16, 2018
1 parent 856d461 commit a8e6141
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
12 changes: 5 additions & 7 deletions lib/license_finder/decisions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def ignored_group?(name)
#######

TXN = Struct.new(:who, :why, :safe_when, :safe_versions) do
def self.from_hash(txn)
new(txn[:who], txn[:why], txn[:when], txn[:versions].nil? ? [] : txn[:versions])
end
def self.from_hash(txn, versions)
new(txn[:who], txn[:why], txn[:when], versions || [])
end
end

def initialize
Expand Down Expand Up @@ -96,10 +96,8 @@ def approve(name, txn = {})

versions = []
versions = @approvals[name][:safe_versions] if @approvals.key?(name)

@approvals[name] = TXN.from_hash(txn)

@approvals[name][:safe_versions].concat(versions)
@approvals[name] = TXN.from_hash(txn, versions)
@approvals[name][:safe_versions].concat(txn[:versions]) unless txn[:versions].nil?
self
end

Expand Down
23 changes: 19 additions & 4 deletions spec/lib/license_finder/decisions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,29 @@ def roundtrip(decisions)
expect(licenses).to eq [License.find_by_name('GPL')].to_set
end

it 'can restore approvals' do

it 'can restore approvals without versions' do
time = Time.now.getutc
decisions = roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time))
expect(decisions).to be_approved('dep')
approval = decisions.approval_of('dep')
roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time))

approval = subject.approval_of('dep')
expect(approval.who).to eq 'Somebody'
expect(approval.why).to eq 'Some reason'
expect(approval.safe_when).to eq time
expect(approval.safe_versions).to eq []
end

it 'can restore approvals with versions' do
time = Time.now.getutc
roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['1.0']))
roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['2.0']))
roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['3.0']))

approval = subject.approval_of('dep', '1.0')
expect(approval.who).to eq 'Somebody'
expect(approval.why).to eq 'Some reason'
expect(approval.safe_when).to eq time
expect(approval.safe_versions).to eq ['1.0', '2.0', '3.0']
end

it 'can restore unapprovals' do
Expand Down

0 comments on commit a8e6141

Please sign in to comment.