Skip to content

Commit

Permalink
Fix issues handling partial version changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 14, 2024
1 parent de47fc8 commit ece63a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/EventListener/VersionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function preRemove(Version $version, LifecycleEventArgs $event): void
public function preUpdate(Version $version, PreUpdateEventArgs $event): void
{
if (($event->hasChangedField('source') || $event->hasChangedField('dist')) && !$version->isDevelopment()) {
$oldDistRef = $event->getOldValue('dist')['reference'] ?? null;
$oldSourceRef = $event->getOldValue('source')['reference'] ?? null;
$newDistRef = $event->getNewValue('dist')['reference'] ?? null;
$newSourceRef = $event->getNewValue('source')['reference'] ?? null;
$oldDistRef = $event->hasChangedField('dist') ? ($event->getOldValue('dist')['reference'] ?? null) : $version->getDist()['reference'] ?? null;
$oldSourceRef = $event->hasChangedField('source') ? ($event->getOldValue('source')['reference'] ?? null) : $version->getSource()['reference'] ?? null;
$newDistRef = $event->hasChangedField('dist') ? ($event->getNewValue('dist')['reference'] ?? null) : $version->getDist()['reference'] ?? null;
$newSourceRef = $event->hasChangedField('source') ? ($event->getNewValue('source')['reference'] ?? null) : $version->getSource()['reference'] ?? null;
if ($oldDistRef !== $newDistRef || $oldSourceRef !== $newSourceRef) {
// buffering things to be inserted in postUpdate once we can confirm it is done
$this->buffered[] = AuditRecord::versionReferenceChange($version, $oldSourceRef, $oldDistRef);
Expand Down
18 changes: 17 additions & 1 deletion tests/Audit/VersionAuditRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,24 @@ public function testVersionChangesGetRecorded(): void
self::assertSame(AuditRecordType::VersionReferenceChange->value, $logs[0]['type']);
self::assertSame('{"name": "composer/composer", "dist_to": "new-dist-ref", "version": "1.0.0", "dist_from": "old-dist-ref", "source_to": "new-source-ref", "source_from": null}', $logs[0]['attributes']);

// verify that only reference changes triggers a new audit log
// verify that unrelated changes do not create new audit logs
$version->setLicense(['MIT']);
$em->persist($version);
$em->flush();

$logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC');
self::assertCount(2, $logs); // package creation + version reference change

// verify that changing dist only without ref change does not create new audit log and does not crash
$version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip2', 'url' => 'https://example.org/dist.zip2']);
$em->persist($version);
$em->flush();

$logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC');
self::assertCount(2, $logs); // package creation + version reference change

// verify that only reference changes triggers a new audit log
$version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip3', 'url' => 'https://example.org/dist.zip2']);
$version->setSource(['reference' => 'new-source-ref', 'type' => 'git2', 'url' => 'git://example.org/dist.zip2']);
$em->persist($version);
$em->flush();
Expand Down

0 comments on commit ece63a3

Please sign in to comment.