Skip to content

Commit cc84146

Browse files
committed
improve
1 parent 0f6545c commit cc84146

File tree

6 files changed

+106
-62
lines changed

6 files changed

+106
-62
lines changed

Diff for: src/ChangelogLineFactory.php

+31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\ReleaseNotesGenerator\Configuration\Configuration;
88
use Rector\ReleaseNotesGenerator\Enum\RectorRepositoryName;
99
use Rector\ReleaseNotesGenerator\ValueObject\Commit;
10+
use stdClass;
1011

1112
final class ChangelogLineFactory
1213
{
@@ -21,6 +22,19 @@ public function __construct(
2122
) {
2223
}
2324

25+
/**
26+
* @return string[]
27+
*/
28+
public function createFromPullRequests(stdClass $pullRequests): array
29+
{
30+
$changelogLines = [];
31+
foreach ($pullRequests->items as $foundPullRequest) {
32+
$changelogLines[] = $this->createFromPullRequest($foundPullRequest);
33+
}
34+
35+
return $changelogLines;
36+
}
37+
2438
public function create(Commit $commit, Configuration $configuration): string
2539
{
2640
$searchPullRequestsResponse = $this->githubApiCaller->searchPullRequests(
@@ -63,6 +77,23 @@ public function create(Commit $commit, Configuration $configuration): string
6377
);
6478
}
6579

80+
private function createFromPullRequest(stdClass $pullRequest): string
81+
{
82+
$changelogLine = sprintf(
83+
'* %s ([#%s](%s))',
84+
$pullRequest->title,
85+
$pullRequest->number,
86+
$pullRequest->pull_request->html_url
87+
);
88+
89+
$username = $pullRequest->user->login;
90+
if (! in_array($username, Configuration::EXCLUDED_THANKS_NAMES, true)) {
91+
$changelogLine .= ', Thanks @' . $username;
92+
}
93+
94+
return $changelogLine;
95+
}
96+
6697
private function createThanks(string|null $thanks): string
6798
{
6899
if ($thanks === null) {

Diff for: src/Command/GenerateCommand.php

+59-59
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\ReleaseNotesGenerator\Exception\InvalidConfigurationException;
1313
use Rector\ReleaseNotesGenerator\GithubApiCaller;
1414
use Rector\ReleaseNotesGenerator\GitResolver;
15+
use Rector\ReleaseNotesGenerator\ValueObject\Commit;
1516
use Rector\ReleaseNotesGenerator\ValueObject\ExternalRepositoryChangelog;
1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\Input\InputInterface;
@@ -45,6 +46,8 @@ protected function configure(): void
4546
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
4647
'Remote repository (use multiple values)'
4748
);
49+
50+
$this->addOption(Option::REMOTE_ONLY, null, InputOption::VALUE_NONE, 'Show only remote repositories');
4851
}
4952

5053
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -70,76 +73,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7073
return self::FAILURE;
7174
}
7275

73-
$externalRepositoryChangelogs = [];
74-
75-
if ($configuration->hasRemoteRepositories()) {
76-
// process remote repositories by date first
77-
$startCommit = $commits[array_key_first($commits)];
78-
$endCommit = $commits[array_key_last($commits)];
79-
80-
foreach ($configuration->getRemoteRepositories() as $remoteRepository) {
76+
$externalRepositoryChangelogs = $this->resolveExternalRepositoryChangelogs($commits, $configuration);
8177

82-
$foundPullRequests = $this->githubApiCaller->findRepositoryPullRequestsBetweenDates(
83-
$remoteRepository,
84-
$configuration->getGithubToken(),
85-
$startCommit->getDate(),
86-
$endCommit->getDate()
87-
);
78+
if ($configuration->isRemoteOnly()) {
79+
$releaseChangelogContents = '';
80+
} else {
81+
$i = 0;
8882

89-
// nothing to process
90-
if ($foundPullRequests->total_count === 0) {
91-
continue;
92-
}
93-
94-
$externalChangelogLines = [];
83+
$changelogLines = [];
84+
foreach ($commits as $commit) {
85+
$changelogLine = $this->changelogLineFactory->create($commit, $configuration);
9586

96-
foreach ($foundPullRequests->items as $foundPullRequest) {
97-
$username = $foundPullRequest->user->login;
98-
$pullRequestUrl = $foundPullRequest->pull_request->url;
87+
// just to show the script is doing something :)
88+
$this->symfonyStyle->writeln($changelogLine);
9989

100-
$changelogLine = sprintf(
101-
'* %s ([#%s](%s))',
102-
$foundPullRequest->title,
103-
$foundPullRequest->number,
104-
$pullRequestUrl
105-
);
90+
$changelogLines[] = $changelogLine;
10691

107-
if (! in_array($username, Configuration::EXCLUDED_THANKS_NAMES, true)) {
108-
$changelogLine .= ', Thanks @' . $username;
109-
}
110-
111-
$externalChangelogLines[] = $changelogLine;
92+
// not to throttle the GitHub API
93+
if ($i > 0 && $i % 8 === 0) {
94+
sleep(60);
11295
}
11396

114-
$externalRepositoryChangelogs[] = new ExternalRepositoryChangelog(
115-
$remoteRepository,
116-
$externalChangelogLines
117-
);
118-
}
119-
}
120-
121-
$i = 0;
122-
123-
$changelogLines = [];
124-
125-
foreach ($commits as $commit) {
126-
$changelogLine = $this->changelogLineFactory->create($commit, $configuration);
127-
128-
// just to show the script is doing something :)
129-
$this->symfonyStyle->writeln($changelogLine);
130-
131-
$changelogLines[] = $changelogLine;
132-
133-
// not to throttle the GitHub API
134-
if ($i > 0 && $i % 8 === 0) {
135-
sleep(60);
97+
++$i;
13698
}
13799

138-
++$i;
100+
$releaseChangelogContents = $this->changelogContentsFactory->create($changelogLines);
139101
}
140102

141-
$releaseChangelogContents = $this->changelogContentsFactory->create($changelogLines);
142-
143103
foreach ($externalRepositoryChangelogs as $externalRepositoryChangelog) {
144104
$releaseChangelogContents .= PHP_EOL . PHP_EOL;
145105
$releaseChangelogContents .= $externalRepositoryChangelog->toString();
@@ -157,4 +117,44 @@ private function printToFile(string $releaseChangelogContents): void
157117

158118
$this->symfonyStyle->writeln(sprintf('Release notes dumped into "%s" file', $filePath));
159119
}
120+
121+
/**
122+
* @param Commit[] $commits
123+
* @return ExternalRepositoryChangelog[]
124+
*/
125+
private function resolveExternalRepositoryChangelogs(array $commits, Configuration $configuration): array
126+
{
127+
if (! $configuration->hasRemoteRepositories()) {
128+
return [];
129+
}
130+
131+
$externalRepositoryChangelogs = [];
132+
133+
// process remote repositories by date first
134+
$startCommit = $commits[array_key_first($commits)];
135+
$endCommit = $commits[array_key_last($commits)];
136+
137+
foreach ($configuration->getRemoteRepositories() as $remoteRepository) {
138+
$foundPullRequests = $this->githubApiCaller->findRepositoryPullRequestsBetweenDates(
139+
$remoteRepository,
140+
$configuration->getGithubToken(),
141+
$startCommit->getDate(),
142+
$endCommit->getDate()
143+
);
144+
145+
// nothing to process
146+
if ($foundPullRequests->total_count === 0) {
147+
continue;
148+
}
149+
150+
$externalChangelogLines = $this->changelogLineFactory->createFromPullRequests($foundPullRequests);
151+
152+
$externalRepositoryChangelogs[] = new ExternalRepositoryChangelog(
153+
$remoteRepository,
154+
$externalChangelogLines
155+
);
156+
}
157+
158+
return $externalRepositoryChangelogs;
159+
}
160160
}

Diff for: src/Configuration/Configuration.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public function __construct(
2020
private string $fromCommit,
2121
private string $toCommit,
2222
private string $githubToken,
23-
private array $remoteRepositories
23+
private array $remoteRepositories,
24+
private bool $isRemoteOnly
2425
) {
2526
Assert::allString($remoteRepositories);
2627
}
@@ -52,4 +53,9 @@ public function getRemoteRepositories(): array
5253
{
5354
return $this->remoteRepositories;
5455
}
56+
57+
public function isRemoteOnly(): bool
58+
{
59+
return $this->isRemoteOnly;
60+
}
5561
}

Diff for: src/Configuration/ConfigurationResolver.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function resolve(InputInterface $input): Configuration
2929
);
3030
}
3131

32+
$isRemoteOnly = (bool) $input->getOption(Option::REMOTE_ONLY);
33+
3234
$remoteRepositories = (array) $input->getOption(Option::REMOTE_REPOSITORY);
33-
return new Configuration($fromCommit, $toCommit, $githubToken, $remoteRepositories);
35+
return new Configuration($fromCommit, $toCommit, $githubToken, $remoteRepositories, $isRemoteOnly);
3436
}
3537
}

Diff for: src/Enum/Option.php

+5
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ final class Option
2525
* @var string
2626
*/
2727
public const REMOTE_REPOSITORY = 'remote-repository';
28+
29+
/**
30+
* @var string
31+
*/
32+
public const REMOTE_ONLY = 'remote-only';
2833
}

Diff for: src/ValueObject/ExternalRepositoryChangelog.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getLines(): array
3131

3232
public function toString(): string
3333
{
34-
$changelogContents = '## ' . $this->title;
34+
$changelogContents = '## ' . $this->title . PHP_EOL . PHP_EOL;
3535
$changelogContents .= implode(PHP_EOL, $this->lines);
3636
$changelogContents .= PHP_EOL . PHP_EOL;
3737

0 commit comments

Comments
 (0)