Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #97 from localheinz/fix/io
Browse files Browse the repository at this point in the history
Fix: Use SymfonyStyle for output
  • Loading branch information
localheinz committed Feb 14, 2016
2 parents b53b4c2 + 159fcb0 commit 3ed2687
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
89 changes: 55 additions & 34 deletions src/Console/PullRequestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;

Expand Down Expand Up @@ -96,6 +97,13 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
{
$this->stopwatch->start('changelog');

$io = new SymfonyStyle(
$input,
$output
);

$io->title('Localheinz GitHub Changelog');

$authToken = $input->getOption('auth-token');
if (null !== $authToken) {
$this->client->authenticate(
Expand All @@ -109,6 +117,18 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
$startReference = $input->getArgument('start-reference');
$endReference = $input->getArgument('end-reference');

$range = $this->range(
$startReference,
$endReference
);

$io->section(sprintf(
'Pull Requests for %s/%s %s',
$owner,
$repository,
$range
));

try {
$pullRequests = $this->pullRequestRepository->items(
$owner,
Expand All @@ -117,49 +137,20 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
$endReference
);
} catch (Exception $exception) {
$output->writeln(sprintf(
'<error>An error occurred: %s</error>',
$io->error(sprintf(
'An error occurred: %s',
$exception->getMessage()
));

return 1;
}

if ($endReference === null) {
$range = sprintf(
'since <info>%s</info>',
$startReference
);
} else {
$range = sprintf(
'between <info>%s</info> and <info>%s</info>',
$startReference,
$endReference
);
}

if (!count($pullRequests)) {
$output->writeln(sprintf(
'Could not find any pull requests merged for <info>%s/%s</info> %s.',
$owner,
$repository,
$range
));
$io->warning('Could not find any pull requests');
} else {
$output->writeln(sprintf(
'Found <info>%s</info> pull request(s) merged for <info>%s/%s</info> %s.',
count($pullRequests),
$owner,
$repository,
$range
));

$output->writeln('');

$template = $input->getOption('template');

array_walk($pullRequests, function (Resource\PullRequestInterface $pullRequest) use ($output, $template) {

$message = str_replace(
[
'%title%',
Expand All @@ -174,16 +165,46 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $

$output->writeln($message);
});

$io->newLine();

$io->success(sprintf(
'Found %s pull request%s.',
count($pullRequests),
count($pullRequests) === 1 ? '' : 's',
$range
));
}

$event = $this->stopwatch->stop('changelog');

$output->writeln('');
$output->writeln($this->formatStopwatchEvent($event));
$io->writeln($this->formatStopwatchEvent($event));

return 0;
}

/**
* @param string $startReference
* @param string $endReference
*
* @return string
*/
private function range($startReference, $endReference)
{
if ($endReference === null) {
return sprintf(
'since %s',
$startReference
);
}

return sprintf(
'between %s and %s',
$startReference,
$endReference
);
}

/**
* @param StopwatchEvent $event
*
Expand Down
30 changes: 6 additions & 24 deletions test/Console/PullRequestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,7 @@ public function testExecuteRendersMessageIfNoPullRequestsWereFound()
$startReference = $faker->sha1;
$endReference = $faker->sha1;

$expectedMessage = sprintf(
'Could not find any pull requests merged for %s/%s between %s and %s.',
$owner,
$repository,
$startReference,
$endReference
);
$expectedMessage = 'Could not find any pull requests';

$pullRequestRepository = $this->pullRequestRepositoryMock();

Expand Down Expand Up @@ -287,12 +281,7 @@ public function testExecuteRendersDifferentMessageIfNoPullRequestsWereFoundAndNo
$repository = $faker->slug();
$startReference = $faker->sha1;

$expectedMessage = sprintf(
'Could not find any pull requests merged for %s/%s since %s.',
$owner,
$repository,
$startReference
);
$expectedMessage = 'Could not find any pull requests';

$pullRequestRepository = $this->pullRequestRepositoryMock();

Expand Down Expand Up @@ -336,12 +325,8 @@ public function testExecuteRendersPullRequestsWithTemplate()

$expectedMessages = [
sprintf(
'Found %s pull request(s) merged for %s/%s between %s and %s.',
count($pullRequests),
$owner,
$repository,
$startReference,
$endReference
'Found %s pull requests',
count($pullRequests)
),
];

Expand Down Expand Up @@ -401,11 +386,8 @@ public function testExecuteRendersDifferentMessageWhenNoEndReferenceWasGiven()
$pullRequests = $this->pullRequests($count);

$expectedMessage = sprintf(
'Found %s pull request(s) merged for %s/%s since %s.',
count($pullRequests),
$owner,
$repository,
$startReference
'Found %s pull requests',
count($pullRequests)
);

$pullRequestRepository = $this->pullRequestRepositoryMock();
Expand Down

0 comments on commit 3ed2687

Please sign in to comment.