Skip to content

Commit

Permalink
Merge pull request #52 from jwage/show-contributors-option
Browse files Browse the repository at this point in the history
Add --show-contributors option
  • Loading branch information
jwage authored Apr 13, 2019
2 parents 41301b4 + 0b2486e commit 74c6602
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 116 deletions.
15 changes: 15 additions & 0 deletions src/ChangelogGenerator/ChangelogConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ChangelogConfig
/** @var bool */
private $includeOpen;

/** @var bool */
private $showContributors = false;

/** @var mixed[] */
private $options = ['rootGitHubUrl' => self::DEFAULT_ROOT_GITHUB_URL];

Expand Down Expand Up @@ -117,6 +120,18 @@ public function setIncludeOpen(bool $includeOpen) : self
return $this;
}

public function showContributors() : bool
{
return $this->showContributors;
}

public function setShowContributors(bool $showContributors) : self
{
$this->showContributors = $showContributors;

return $this;
}

/**
* @return mixed[]
*/
Expand Down
65 changes: 53 additions & 12 deletions src/ChangelogGenerator/ChangelogGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function array_filter;
use function array_map;
use function array_unique;
use function array_values;
use function count;
use function mb_strlen;
use function sprintf;
Expand Down Expand Up @@ -36,12 +37,7 @@ public function generate(
$issueGroups = $this->issueGrouper->groupIssues($issues, $changelogConfig);

$output->writeln([
sprintf(
'%s%s%s',
$changelogConfig->getMilestone(),
PHP_EOL,
str_repeat('=', mb_strlen($changelogConfig->getMilestone()))
),
$this->buildMarkdownHeaderText($changelogConfig->getMilestone(), '='),
'',
sprintf('- Total issues resolved: **%s**', $this->getNumberOfIssues($issues)),
sprintf('- Total pull requests resolved: **%s**', $this->getNumberOfPullRequests($issues)),
Expand All @@ -51,12 +47,7 @@ public function generate(
foreach ($issueGroups as $issueGroup) {
$output->writeln([
'',
sprintf(
'%s%s%s',
$issueGroup->getName(),
PHP_EOL,
str_repeat('-', mb_strlen($issueGroup->getName()))
),
$this->buildMarkdownHeaderText($issueGroup->getName(), '-'),
'',
]);

Expand All @@ -65,9 +56,59 @@ public function generate(
}
}

if ($changelogConfig->showContributors()) {
$this->outputContributors($output, $issues);
}

$output->writeln('');
}

/**
* @param Issue[] $issues
*/
private function outputContributors(OutputInterface $output, array $issues) : void
{
$contributors = $this->buildContributorsList($issues);

$output->writeln([
'',
$this->buildMarkdownHeaderText('Contributors', '-'),
'',
]);

foreach ($contributors as $contributor) {
$output->writeln(sprintf(' - [@%s](https://github.com/%s)', $contributor, $contributor));
}
}

/**
* @param Issue[] $issues
*
* @return string[]
*/
private function buildContributorsList(array $issues) : array
{
$contributors = [];

foreach ($issues as $issue) {
foreach ($issue->getContributors() as $contributor) {
$contributors[$contributor] = $contributor;
}
}

return array_values($contributors);
}

private function buildMarkdownHeaderText(string $header, string $headerCharacter) : string
{
return sprintf(
'%s%s%s',
$header,
PHP_EOL,
str_repeat($headerCharacter, mb_strlen($header))
);
}

/**
* @param Issue[] $issues
*/
Expand Down
115 changes: 58 additions & 57 deletions src/ChangelogGenerator/Command/GenerateChangelogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ protected function configure() : void
InputOption::VALUE_OPTIONAL,
'Whether to also include open issues.',
''
)
->addOption(
'show-contributors',
'',
InputOption::VALUE_OPTIONAL,
'Whether to include a section with a list of contributors.',
''
);
}

Expand Down Expand Up @@ -172,22 +179,11 @@ private function getChangelogConfig(InputInterface $input) : ChangelogConfig
return $changelogConfig;
}

$user = $this->getStringOption($input, 'user');
$repository = $this->getStringOption($input, 'repository');
$milestone = $this->getStringOption($input, 'milestone');

/** @var string[] $labels */
$labels = $input->getOption('label');
$changelogConfig = (new ChangelogConfig());

$includeOpen = $this->getIncludeOpen($input);
$this->loadChangelogConfigFromInput($input, $changelogConfig);

return new ChangelogConfig(
$user,
$repository,
$milestone,
$labels,
$includeOpen
);
return $changelogConfig;
}

/**
Expand All @@ -208,6 +204,39 @@ private function getStringOption(InputInterface $input, string $name) : string
throw new RuntimeException(sprintf('Invalid option value type: %s', gettype($value)));
}

private function getBooleanOption(InputInterface $input, string $name) : bool
{
$value = $input->getOption($name);

// option not provided, default to false
if ($value === '') {
return false;
}

// option provided, but no value was given, default to true
if ($value === null) {
return true;
}

// option provided and value was provided
if (is_string($value) && in_array($value, ['1', 'true'], true)) {
return true;
}

return false;
}

/**
* @return string[]
*/
private function getArrayOption(InputInterface $input, string $name) : array
{
/** @var string[] $value */
$value = $input->getOption($name);

return $value;
}

/**
* @return false|resource
*/
Expand Down Expand Up @@ -314,7 +343,7 @@ private function loadConfigFile(InputInterface $input) : ?ChangelogConfig

$changelogConfig = $this->findChangelogConfig($input, $changelogConfigs);

$this->overrideChangelogConfig($input, $changelogConfig);
$this->loadChangelogConfigFromInput($input, $changelogConfig);

return $changelogConfig;
}
Expand All @@ -341,60 +370,32 @@ private function findChangelogConfig(InputInterface $input, array $changelogConf
return $changelogConfig;
}

private function overrideChangelogConfig(InputInterface $input, ChangelogConfig $changelogConfig) : void
private function loadChangelogConfigFromInput(InputInterface $input, ChangelogConfig $changelogConfig) : void
{
$user = $input->getOption('user');
$repository = $input->getOption('repository');
$milestone = $input->getOption('milestone');
$labels = $input->getOption('label');
$includeOpen = $input->getOption('include-open');

if ($user !== null) {
assert(is_string($user));
$changelogConfig->setUser($user);
}

if ($repository !== null) {
assert(is_string($repository));
$changelogConfig->setRepository($repository);
}

if ($milestone !== null) {
assert(is_string($milestone));
$changelogConfig->setMilestone($milestone);
if ($input->getOption('user') !== null) {
$changelogConfig->setUser($this->getStringOption($input, 'user'));
}

if ($labels !== []) {
assert(is_array($labels));
$changelogConfig->setLabels($labels);
if ($input->getOption('repository') !== null) {
$changelogConfig->setRepository($this->getStringOption($input, 'repository'));
}

if ($includeOpen === '') {
return;
if ($input->getOption('milestone') !== null) {
$changelogConfig->setMilestone($this->getStringOption($input, 'milestone'));
}

$changelogConfig->setIncludeOpen($this->getIncludeOpen($input));
}

private function getIncludeOpen(InputInterface $input) : bool
{
$includeOpen = $input->getOption('include-open');

// --include-open option not provided, default to false
if ($includeOpen === '') {
return false;
if ($input->getOption('label') !== []) {
$changelogConfig->setLabels($this->getArrayOption($input, 'label'));
}

// --include-open option provided, but no value was given, default to true
if ($includeOpen === null) {
return true;
if ($input->getOption('include-open') !== '') {
$changelogConfig->setIncludeOpen($this->getBooleanOption($input, 'include-open'));
}

// --include-open option provided and value was provided
if (is_string($includeOpen) && in_array($includeOpen, ['1', 'true'], true)) {
return true;
if ($input->getOption('show-contributors') === '') {
return;
}

return false;
$changelogConfig->setShowContributors($this->getBooleanOption($input, 'show-contributors'));
}
}
21 changes: 21 additions & 0 deletions src/ChangelogGenerator/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ChangelogGenerator;

use function array_values;
use function sprintf;

class Issue
Expand Down Expand Up @@ -117,6 +118,26 @@ public function setLinkedIssue(Issue $linkedIssue) : void
$this->linkedIssue = $linkedIssue;
}

/**
* @return string[]
*/
public function getContributors() : array
{
$contributors = [];

$contributors[$this->user] = $this->user;

if ($this->linkedPullRequest !== null) {
$contributors[$this->linkedPullRequest->getUser()] = $this->linkedPullRequest->getUser();
}

if ($this->linkedIssue !== null) {
$contributors[$this->linkedIssue->getUser()] = $this->linkedIssue->getUser();
}

return array_values($contributors);
}

public function render() : string
{
if ($this->linkedIssue instanceof self && $this->linkedIssue->getUser() !== $this->user) {
Expand Down
Loading

0 comments on commit 74c6602

Please sign in to comment.