Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Console/Command/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nails\Console\Command\Base;
use Nails\Console\Exception\ConsoleException;
use Nails\Factory;
use Nails\ReleaseNotes\Console\TagSyncReport;
use Nails\ReleaseNotes\Constants;
use Nails\ReleaseNotes\Resource;
use Nails\ReleaseNotes\Interfaces;
Expand Down Expand Up @@ -180,6 +181,7 @@ private function syncTags(?string $sUser, ?string $sToken): self
$oReleaseNotesModel = Factory::model('ReleaseNotes', Constants::MODULE_SLUG);

$aExisting = $oReleaseNotesModel->getAllFlat();
$oReport = new TagSyncReport();

$this->oOutput->writeln('Syncing tags...');
foreach ($this->aTags as $oTag) {
Expand All @@ -188,13 +190,8 @@ private function syncTags(?string $sUser, ?string $sToken): self

$sTag = $this->parseTagRef($oTag->ref);

$this->oOutput->write(sprintf(
' - Processing tag <info>%s</info>... ',
$sTag
));

if (in_array($sTag, $aExisting)) {
throw new ConsoleException('already imported');
if (!$oReport->consider($sTag, $aExisting)) {
continue;
}

$oResponse = $this->callGitHubApi(
Expand Down Expand Up @@ -227,7 +224,7 @@ private function syncTags(?string $sUser, ?string $sToken): self

$this->aNewTags[] = $oNewTag;

$this->oOutput->writeln('<info>done</info>');
$this->oOutput->writeln($oReport->imported($sTag));

} catch (\Throwable $e) {
$this->oOutput->writeln(sprintf(
Expand All @@ -237,6 +234,8 @@ private function syncTags(?string $sUser, ?string $sToken): self
}
}

$this->oOutput->writeln($oReport->summary());

return $this;
}

Expand Down
55 changes: 55 additions & 0 deletions src/Console/TagSyncReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Nails\ReleaseNotes\Console;

class TagSyncReport
{
private int $iChecked = 0;

private int $iImported = 0;

/**
* @param string[] $aExisting
*/
public function consider(string $sTag, array $aExisting): bool
{
$this->iChecked++;

return !in_array($sTag, $aExisting, true);
}

public function imported(string $sTag): string
{
$this->iImported++;

return 'Imported tag ' . $sTag;
}

public function summary(): string
{
return sprintf('checked %d tags, imported %d', $this->iChecked, $this->iImported);
}

/**
* @param string[] $aTags
* @param string[] $aExisting
*
* @return string[]
*/
public function lines(array $aTags, array $aExisting): array
{
$aLines = [];

foreach ($aTags as $sTag) {
if (!$this->consider($sTag, $aExisting)) {
continue;
}

$aLines[] = $this->imported($sTag);
}

$aLines[] = $this->summary();

return $aLines;
}
}
37 changes: 37 additions & 0 deletions tests/Console/TagSyncReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Test\ReleaseNotes\Console;

use Nails\ReleaseNotes\Console\TagSyncReport;
use PHPUnit\Framework\TestCase;

class TagSyncReportTest extends TestCase
{
public function test_existing_tags_collapse_to_one_summary(): void
{
$aLines = (new TagSyncReport())->lines(
['0.9.0', '0.9.1', '1.0.0'],
['0.9.0', '0.9.1', '1.0.0']
);

self::assertSame(['checked 3 tags, imported 0'], $aLines);
self::assertStringNotContainsString('already imported', implode("\n", $aLines));
}

public function test_only_imported_tags_get_a_line(): void
{
$aLines = (new TagSyncReport())->lines(
['0.9.0', '1.0.0', '1.1.0'],
['0.9.0']
);

self::assertSame(
[
'Imported tag 1.0.0',
'Imported tag 1.1.0',
'checked 3 tags, imported 2',
],
$aLines
);
}
}
Loading