Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Abort and Show Error Message when Release Notes could not be placed #23

Merged
merged 5 commits into from
May 24, 2022
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
5 changes: 4 additions & 1 deletion app/Actions/PasteReleaseNotesAtTheTop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Actions;

use App\CreateNewReleaseHeading;
use App\Exceptions\ReleaseNotesCanNotBeplacedException;
use App\Exceptions\ReleaseNotesNotProvidedException;
use App\MarkdownParser;
use App\Queries\FindFirstSecondLevelHeading;
Expand Down Expand Up @@ -43,8 +44,10 @@ public function execute(string $latestVersion, ?string $releaseNotes, string $re
if ($previousVersionHeading !== null) {
// Insert the newest Release Notes before the previous Release Heading
$previousVersionHeading->insertBefore($parsedReleaseNotes);
} elseif ($changelog->lastChild() !== null) {
$changelog->lastChild()->insertAfter($parsedReleaseNotes);
} else {
$changelog->lastChild()?->insertAfter($parsedReleaseNotes);
throw new ReleaseNotesCanNotBeplacedException();
}

return $changelog;
Expand Down
4 changes: 3 additions & 1 deletion app/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\AddReleaseNotesToChangelog;
use App\Exceptions\ReleaseAlreadyExistsInChangelogException;
use App\Exceptions\ReleaseNotesCanNotBeplacedException;
use App\Exceptions\ReleaseNotesNotProvidedException;
use App\Support\GitHubActionsOutput;
use LaravelZero\Framework\Commands\Command;
Expand Down Expand Up @@ -55,14 +56,15 @@ public function handle(AddReleaseNotesToChangelog $addReleaseNotesToChangelog, G
compareUrlTargetRevision: $compareUrlTargetRevision
);
$this->info($updatedChangelog->getContent());

$this->writeChangelogToFile($pathToChangelog, $updatedChangelog);

return self::SUCCESS;
} catch (ReleaseAlreadyExistsInChangelogException $exception) {
$this->warn($exception->getMessage());

return self::SUCCESS;
} catch (ReleaseNotesNotProvidedException $exception) {
} catch (ReleaseNotesNotProvidedException|ReleaseNotesCanNotBeplacedException $exception) {
$this->error($exception->getMessage());

return self::FAILURE;
Expand Down
15 changes: 15 additions & 0 deletions app/Exceptions/ReleaseNotesCanNotBeplacedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use Exception;

class ReleaseNotesCanNotBeplacedException extends Exception
{
public function __construct()
{
parent::__construct("Release notes could not be placed. Is the CHANGELOG empty? Does it contain at least one heading?");
}
}
21 changes: 21 additions & 0 deletions tests/Feature/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,24 @@
->expectsOutput('Release Notes were not provided. Pass them through the `--release-notes`-option.')
->assertFailed();
});

test('it shows warning if changelog is empty and content can not be placed', function () {
$this->artisan('update', [
'--release-notes' => <<<MD
### Added
- New Feature A
- New Feature B

### Changed
- Update Feature C

### Removes
- Remove Feature D
MD,
'--latest-version' => 'v1.0.0',
'--path-to-changelog' => __DIR__ . '/../Stubs/empty-changelog.md',
'--compare-url-target-revision' => 'HEAD',
])
->expectsOutput('Release notes could not be placed. Is the CHANGELOG empty? Does it contain at least one heading?')
->assertFailed();
});
Empty file added tests/Stubs/empty-changelog.md
Empty file.