Skip to content

Commit 6e9cab0

Browse files
authored
Merge pull request #40 from weierophinney/feature/33-bump-changelog
When a new release is made, bump the CHANGELOG version in the next release branch and the originating release branch
2 parents df4c610 + ea39b73 commit 6e9cab0

15 files changed

+739
-16
lines changed

.github/workflows/automatic-release.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ jobs:
4545
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
4646
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
4747
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
48+
49+
- name: "Bump Changelog Version On Originating Release Branch"
50+
uses: "./"
51+
with:
52+
command-name: "laminas:automatic-releases:bump-changelog"
53+
env:
54+
"GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }}
55+
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
56+
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
57+
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 1.2.0 - TBD
6+
7+
### Added
8+
9+
- [#40](https://github.com/laminas/automatic-releases/pull/40) adds a new command, laminas:automatic-releases:bump-changelog. When a `CHANGELOG.md` file is present in the repository, it will add an entry in the file for the next patch-level release to the target branch of the closed milestone. The patch also adds the command to the end of the suggested workflow configuration.
10+
11+
### Changed
12+
13+
- [#40](https://github.com/laminas/automatic-releases/pull/40) updates the laminas:automatic-releases:switch-default-branch-to-next-minor command such that if a `CHANGELOG.md` file is present in the repository, and a new minor release branch is created, it adds an entry to the file for the next minor release.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- Nothing.
26+
527
## 1.1.0 - 2020-08-06
628

729
### Added

bin/console.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use ErrorException;
99
use Http\Discovery\HttpClientDiscovery;
1010
use Http\Discovery\Psr17FactoryDiscovery;
11+
use Laminas\AutomaticReleases\Application\Command\BumpChangelogForReleaseBranch;
1112
use Laminas\AutomaticReleases\Application\Command\CreateMergeUpPullRequest;
1213
use Laminas\AutomaticReleases\Application\Command\ReleaseCommand;
1314
use Laminas\AutomaticReleases\Application\Command\SwitchDefaultBranchToNextMinor;
15+
use Laminas\AutomaticReleases\Changelog\BumpAndCommitChangelogVersionViaKeepAChangelog;
1416
use Laminas\AutomaticReleases\Changelog\CommitReleaseChangelogViaKeepAChangelog;
1517
use Laminas\AutomaticReleases\Environment\EnvironmentVariables;
18+
use Laminas\AutomaticReleases\Git\CheckoutBranchViaConsole;
1619
use Laminas\AutomaticReleases\Git\CommitFileViaConsole;
1720
use Laminas\AutomaticReleases\Git\CreateTagViaConsole;
1821
use Laminas\AutomaticReleases\Git\FetchAndSetCurrentUserByReplacingCurrentOriginRemote;
@@ -66,6 +69,7 @@ static function (int $errorCode, string $message = '', string $file = '', int $l
6669
$httpClient,
6770
$githubToken
6871
));
72+
$checkoutBranch = new CheckoutBranchViaConsole();
6973
$commit = new CommitFileViaConsole();
7074
$push = new PushViaConsole();
7175
$commitChangelog = new CommitReleaseChangelogViaKeepAChangelog(new SystemClock(), $commit, $push, $logger);
@@ -82,6 +86,12 @@ static function (int $errorCode, string $message = '', string $file = '', int $l
8286
$httpClient,
8387
$githubToken
8488
);
89+
$bumpChangelogVersion = new BumpAndCommitChangelogVersionViaKeepAChangelog(
90+
$checkoutBranch,
91+
$commit,
92+
$push,
93+
$logger
94+
);
8595

8696
/** @psalm-suppress DeprecatedClass */
8797
$application = new Application(Versions::ROOT_PACKAGE_NAME, Versions::getVersion('laminas/automatic-releases'));
@@ -123,7 +133,15 @@ static function (int $errorCode, string $message = '', string $file = '', int $l
123133
$makeRequests,
124134
$httpClient,
125135
$githubToken
126-
)
136+
),
137+
$bumpChangelogVersion
138+
),
139+
new BumpChangelogForReleaseBranch(
140+
$variables,
141+
$loadEvent,
142+
$fetch,
143+
$getCandidateBranches,
144+
$bumpChangelogVersion
127145
),
128146
]);
129147

examples/.github/workflows/release-on-milestone-closed.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ jobs:
4545
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
4646
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
4747
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
48+
49+
- name: "Bump Changelog Version On Originating Release Branch"
50+
uses: "laminas/automatic-releases@v1"
51+
with:
52+
command-name: "laminas:automatic-releases:bump-changelog"
53+
env:
54+
"GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }}
55+
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
56+
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
57+
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\AutomaticReleases\Application\Command;
6+
7+
use Laminas\AutomaticReleases\Changelog\BumpAndCommitChangelogVersion;
8+
use Laminas\AutomaticReleases\Environment\Variables;
9+
use Laminas\AutomaticReleases\Git\Fetch;
10+
use Laminas\AutomaticReleases\Git\GetMergeTargetCandidateBranches;
11+
use Laminas\AutomaticReleases\Github\Event\Factory\LoadCurrentGithubEvent;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Webmozart\Assert\Assert;
16+
17+
use function sprintf;
18+
19+
final class BumpChangelogForReleaseBranch extends Command
20+
{
21+
private Variables $environment;
22+
private LoadCurrentGithubEvent $loadEvent;
23+
private Fetch $fetch;
24+
private GetMergeTargetCandidateBranches $getMergeTargets;
25+
private BumpAndCommitChangelogVersion $bumpChangelogVersion;
26+
27+
public function __construct(
28+
Variables $environment,
29+
LoadCurrentGithubEvent $loadEvent,
30+
Fetch $fetch,
31+
GetMergeTargetCandidateBranches $getMergeTargets,
32+
BumpAndCommitChangelogVersion $bumpChangelogVersion
33+
) {
34+
parent::__construct('laminas:automatic-releases:bump-changelog');
35+
36+
$this->environment = $environment;
37+
$this->loadEvent = $loadEvent;
38+
$this->fetch = $fetch;
39+
$this->getMergeTargets = $getMergeTargets;
40+
$this->bumpChangelogVersion = $bumpChangelogVersion;
41+
}
42+
43+
public function execute(InputInterface $input, OutputInterface $output): int
44+
{
45+
$milestoneClosedEvent = ($this->loadEvent)();
46+
$repositoryName = $milestoneClosedEvent->repository();
47+
$repositoryCloneUri = $repositoryName->uriWithTokenAuthentication($this->environment->githubToken());
48+
$repositoryPath = $this->environment->githubWorkspacePath();
49+
50+
Assert::directory($repositoryPath . '/.git');
51+
52+
($this->fetch)($repositoryCloneUri, $repositoryPath);
53+
54+
$mergeCandidates = ($this->getMergeTargets)($repositoryPath);
55+
$releaseVersion = $milestoneClosedEvent->version();
56+
$releaseBranch = $mergeCandidates->targetBranchFor($releaseVersion);
57+
58+
Assert::notNull(
59+
$releaseBranch,
60+
sprintf('No valid release branch found for version %s', $releaseVersion->fullReleaseName())
61+
);
62+
63+
($this->bumpChangelogVersion)(
64+
BumpAndCommitChangelogVersion::BUMP_PATCH,
65+
$repositoryPath,
66+
$releaseVersion,
67+
$releaseBranch
68+
);
69+
70+
return 0;
71+
}
72+
}

src/Application/Command/SwitchDefaultBranchToNextMinor.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Laminas\AutomaticReleases\Application\Command;
66

7+
use Laminas\AutomaticReleases\Changelog\BumpAndCommitChangelogVersion;
78
use Laminas\AutomaticReleases\Environment\Variables;
89
use Laminas\AutomaticReleases\Git\Fetch;
910
use Laminas\AutomaticReleases\Git\GetMergeTargetCandidateBranches;
@@ -23,23 +24,26 @@ final class SwitchDefaultBranchToNextMinor extends Command
2324
private GetMergeTargetCandidateBranches $getMergeCandidates;
2425
private Push $push;
2526
private SetDefaultBranch $switchDefaultBranch;
27+
private BumpAndCommitChangelogVersion $bumpChangelogVersion;
2628

2729
public function __construct(
2830
Variables $variables,
2931
LoadCurrentGithubEvent $loadGithubEvent,
3032
Fetch $fetch,
3133
GetMergeTargetCandidateBranches $getMergeCandidates,
3234
Push $push,
33-
SetDefaultBranch $switchDefaultBranch
35+
SetDefaultBranch $switchDefaultBranch,
36+
BumpAndCommitChangelogVersion $bumpChangelogVersion
3437
) {
3538
parent::__construct('laminas:automatic-releases:switch-default-branch-to-next-minor');
3639

37-
$this->variables = $variables;
38-
$this->loadGithubEvent = $loadGithubEvent;
39-
$this->fetch = $fetch;
40-
$this->getMergeCandidates = $getMergeCandidates;
41-
$this->push = $push;
42-
$this->switchDefaultBranch = $switchDefaultBranch;
40+
$this->variables = $variables;
41+
$this->loadGithubEvent = $loadGithubEvent;
42+
$this->fetch = $fetch;
43+
$this->getMergeCandidates = $getMergeCandidates;
44+
$this->push = $push;
45+
$this->switchDefaultBranch = $switchDefaultBranch;
46+
$this->bumpChangelogVersion = $bumpChangelogVersion;
4347
}
4448

4549
public function execute(InputInterface $input, OutputInterface $output): int
@@ -73,6 +77,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
7377
$newestBranch->name(),
7478
$nextDefaultBranch->name()
7579
);
80+
($this->bumpChangelogVersion)(
81+
BumpAndCommitChangelogVersion::BUMP_MINOR,
82+
$repositoryPath,
83+
$releaseVersion,
84+
$newestBranch
85+
);
7686
}
7787

7888
$this->switchDefaultBranch->__invoke($event->repository(), $nextDefaultBranch);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\AutomaticReleases\Changelog;
6+
7+
use Laminas\AutomaticReleases\Git\Value\BranchName;
8+
use Laminas\AutomaticReleases\Git\Value\SemVerVersion;
9+
10+
interface BumpAndCommitChangelogVersion
11+
{
12+
public const BUMP_MINOR = 'bumpMinorVersion';
13+
public const BUMP_PATCH = 'bumpPatchVersion';
14+
15+
/**
16+
* @psalm-param self::BUMP_* $bumpType
17+
* @psalm-param non-empty-string $repositoryDirectory
18+
*/
19+
public function __invoke(
20+
string $bumpType,
21+
string $repositoryDirectory,
22+
SemVerVersion $version,
23+
BranchName $sourceBranch
24+
): void;
25+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\AutomaticReleases\Changelog;
6+
7+
use Laminas\AutomaticReleases\Git\CheckoutBranch;
8+
use Laminas\AutomaticReleases\Git\CommitFile;
9+
use Laminas\AutomaticReleases\Git\Push;
10+
use Laminas\AutomaticReleases\Git\Value\BranchName;
11+
use Laminas\AutomaticReleases\Git\Value\SemVerVersion;
12+
use Phly\KeepAChangelog\Bump\ChangelogBump;
13+
use Psr\Log\LoggerInterface;
14+
use Webmozart\Assert\Assert;
15+
16+
use function file_exists;
17+
use function sprintf;
18+
19+
class BumpAndCommitChangelogVersionViaKeepAChangelog implements BumpAndCommitChangelogVersion
20+
{
21+
private const CHANGELOG_FILE = 'CHANGELOG.md';
22+
23+
private const COMMIT_TEMPLATE = <<< 'COMMIT'
24+
Bumps changelog version to %s
25+
26+
Updates the %s file to add a changelog entry for a new %s version.
27+
COMMIT;
28+
29+
private CheckoutBranch $checkoutBranch;
30+
private CommitFile $commitFile;
31+
private Push $push;
32+
private LoggerInterface $logger;
33+
34+
public function __construct(
35+
CheckoutBranch $checkoutBranch,
36+
CommitFile $commitFile,
37+
Push $push,
38+
LoggerInterface $logger
39+
) {
40+
$this->checkoutBranch = $checkoutBranch;
41+
$this->commitFile = $commitFile;
42+
$this->push = $push;
43+
$this->logger = $logger;
44+
}
45+
46+
public function __invoke(
47+
string $bumpType,
48+
string $repositoryDirectory,
49+
SemVerVersion $version,
50+
BranchName $sourceBranch
51+
): void {
52+
($this->checkoutBranch)($repositoryDirectory, $sourceBranch);
53+
54+
$changelogFile = sprintf('%s/%s', $repositoryDirectory, self::CHANGELOG_FILE);
55+
if (! file_exists($changelogFile)) {
56+
// No changelog
57+
$this->logger->info('BumpAndCommitChangelog: No CHANGELOG.md file detected');
58+
59+
return;
60+
}
61+
62+
$versionString = $version->fullReleaseName();
63+
$bumper = new ChangelogBump($changelogFile);
64+
$newVersion = $bumper->$bumpType($versionString);
65+
66+
Assert::stringNotEmpty($newVersion);
67+
$bumper->updateChangelog($newVersion);
68+
69+
($this->commitFile)(
70+
$repositoryDirectory,
71+
$sourceBranch,
72+
self::CHANGELOG_FILE,
73+
sprintf(self::COMMIT_TEMPLATE, $newVersion, self::CHANGELOG_FILE, $newVersion)
74+
);
75+
76+
($this->push)($repositoryDirectory, $sourceBranch->name());
77+
78+
$this->logger->info(sprintf(
79+
'BumpAndCommitChangelog: bumped %s to version %s in branch %s',
80+
self::CHANGELOG_FILE,
81+
$newVersion,
82+
$sourceBranch->name()
83+
));
84+
}
85+
}

src/Git/CheckoutBranch.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\AutomaticReleases\Git;
6+
7+
use Laminas\AutomaticReleases\Git\Value\BranchName;
8+
9+
interface CheckoutBranch
10+
{
11+
/**
12+
* @psalm-param non-empty-string $repositoryDirectory
13+
*/
14+
public function __invoke(
15+
string $repositoryDirectory,
16+
BranchName $branchName
17+
): void;
18+
}

src/Git/CheckoutBranchViaConsole.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\AutomaticReleases\Git;
6+
7+
use Laminas\AutomaticReleases\Git\Value\BranchName;
8+
use Symfony\Component\Process\Process;
9+
10+
class CheckoutBranchViaConsole implements CheckoutBranch
11+
{
12+
public function __invoke(
13+
string $repositoryDirectory,
14+
BranchName $branchName
15+
): void {
16+
(new Process(['git', 'switch', $branchName->name()], $repositoryDirectory))
17+
->mustRun();
18+
}
19+
}

0 commit comments

Comments
 (0)