Last active
December 24, 2016 06:43
-
-
Save f3ath/686fcb4162ffa47fb6823cd316e13428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace F3\Changelog; | |
use Composer\Semver\Semver; | |
use F3\Changelog\Exception\NoTagsFound; | |
use F3\Changelog\Exception\TagNotFound; | |
use F3\ReleaseNotesGenerator\GitGateway; | |
class ReleaseNotes | |
{ | |
/** | |
* @var GitGateway | |
*/ | |
private $git; | |
public function write(Printer $printer, string $tag = null) | |
{ | |
$tags = $this->git->getTags(); | |
if (empty($tags)) { | |
throw new NoTagsFound(); | |
} | |
$tags = Semver::rsort($tags); | |
$tag = $tag ?: reset($tags); | |
$index = array_search($tag, $tags); | |
if (false === $index) { | |
throw new TagNotFound($tag); | |
} | |
if (++$index < count($tags)) { | |
$url = $this->git->getDiffUrl($tags[$index], $tag); | |
$revisions = $this->git->getRevisions($tags[$index], $tag); | |
} else { | |
$url = null; | |
$revisions = $this->git->getRevisions(null, $tag); | |
} | |
$printer->header($tag, $this->git->getCommit($tag)->getDate(), $url); | |
foreach ($revisions as $revision) { | |
$commit = $this->git->getCommit($revision); | |
$printer->change($commit->getSubject(), $commit->getAuthor()); | |
} | |
} | |
} | |
interface GitGateway | |
{ | |
/** | |
* Get commit details by revision (hash or tag) | |
* | |
* @param string $revision | |
* @return Commit | |
*/ | |
public function getCommit(string $revision): Commit; | |
/** | |
* Get list of tags | |
* | |
* @return string[] | |
*/ | |
public function getTags(): array; | |
/** | |
* Get the url show the diff between revisions | |
* @param string $from | |
* @param string $to | |
* @return string | |
*/ | |
public function getDiffUrl(string $from, string $to): string; | |
/** | |
* Get all revisions between $from and $to | |
* Equivalent: "git log <to>" or "git log <from>..<to>" | |
* | |
* @param string $from | |
* @param string $to | |
* @return array|\string[] Hashes | |
*/ | |
public function getRevisions(string $from = null, string $to = null): array; | |
} | |
interface Printer | |
{ | |
public function header(string $tag, \DateTimeInterface $date, string $url = null); | |
public function change(string $subject, string $author = null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment