Skip to content

Commit

Permalink
Merge pull request #64 from open-sausages/pulls/2.0/bugfixes-stuff
Browse files Browse the repository at this point in the history
BUG Fix changelog generation / initial plan generation
  • Loading branch information
Damian Mooyman authored Jan 12, 2018
2 parents d27b173 + 4826e35 commit 126366d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
53 changes: 48 additions & 5 deletions src/Model/Modules/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,10 @@ public function getComposerPath()
* @param string $ref Git ref (tag / branch / SHA)
* @param bool $recursive Search recursively
* @param int $maxDepth Safeguard against infinite loops
* @param bool $stableOnly Check if we only want to check stable tags
* @return array|null
*/
public function getHistoryComposerData($ref, $recursive = true, $maxDepth = 6)
public function getHistoryComposerData($ref, $recursive = true, $maxDepth = 6, $stableOnly = false)
{
if ($maxDepth <= 0) {
return null;
Expand All @@ -535,13 +536,23 @@ public function getHistoryComposerData($ref, $recursive = true, $maxDepth = 6)
}

// Get all recursive changes
foreach ($results['require'] as $libraryName => $version) {
// If this library belongs to this project, and this tag is stable, recurse
foreach ($results['require'] as $libraryName => $libraryConstraint) {
// If this library belongs to this project, recurse
$library = $this->getProject()->getLibrary($libraryName);
if (!$library || !Version::parse($version)) {
if (!$library) {
continue;
}
// Get oldest version for this library and constraint
if (!ComposerConstraint::parse($libraryConstraint)) {
continue;
}
$constraint = new ComposerConstraint($libraryConstraint, $ref, $libraryName);
$version = $library->getOldestVersionMatching($constraint, $stableOnly);
if (!$version) {
continue;
}
$nextResults = $library->getHistoryComposerData($version, true, $maxDepth - 1);
// With this history version in hand, recurse
$nextResults = $library->getHistoryComposerData($version->getValue(), true, $maxDepth - 1, $stableOnly);
if (isset($nextResults['require'])) {
$results['require'] = array_merge(
$nextResults['require'],
Expand All @@ -552,6 +563,38 @@ public function getHistoryComposerData($ref, $recursive = true, $maxDepth = 6)
return $results;
}

/**
* Returns the oldest version that matches the given version constraint
*
* @param ComposerConstraint $composerConstraint
* @param bool $stableOnly Check if we only want to check stable tags
* @return Version
*/
public function getOldestVersionMatching(ComposerConstraint $composerConstraint, $stableOnly = false)
{
// Filter known tags by the constraint first
$childVersions = $composerConstraint->filterVersions($this->getTags());

// Check if we want to filter out stable tags only
// E.g. prefer "3.4.0..3.4.1" over "3.4.0-rc1..3.4.1"
if ($stableOnly) {
$stableChildVersions = Version::filter($childVersions, function (Version $nextTag) {
return $nextTag->isStable();
});
// Use stable versions if available, or fall back to unstable
if (!empty($stableChildVersions)) {
$childVersions = $stableChildVersions;
}
}

// Get smallest matching version
$childVersions = Version::sort($childVersions, Version::ASC);
if (!empty($childVersions)) {
return reset($childVersions);
}
return null;
}

/**
* @return array List of test commands
*/
Expand Down
19 changes: 8 additions & 11 deletions src/Model/Release/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,26 +424,23 @@ public static function filter($tags, $callback)
* Guess the next version to release from this version
*
* @param string $stability Stability of the next version to use
* @param int $stabilityVersion
* @param int $stabilityVersion Version of stability (e.g. 2 for -rc2)
* @return Version
*/
public function getNextVersion($stability = '', $stabilityVersion = 0)
{
// Create new version with specified stability
$canditate = clone $this;

// Check if we can simply release a new stability of the same version
// E.g. 4.0.0-alpha1 -> 4.0.0, or 4.0.0-alpha1 -> 4.0.0-beta1
$canditate->setStability($stability);
$canditate->setStabilityVersion($stabilityVersion);
if ($this->compareTo($canditate) < 0) {
return $canditate;

// If last release is stable, or new version isn't never, bump patch version
// This ensures the new version is always a new version
// E.g. 1.0.0 -> 1.0.1-rc1, 1.0.1-rc1 -> 1.0.1-rc2
if ($this->isStable() || $this->compareTo($canditate) >= 0) {
$canditate->setPatch($this->getPatch() + 1);
}

// if suggested stability isn't more stable, advance to next stable patch release
// E.g. 4.0.0-alpha2 -> 4.0.1
$canditate->setStability('');
$canditate->setStabilityVersion(null);
$canditate->setPatch($this->getPatch() + 1);
return $canditate;
}

Expand Down
28 changes: 9 additions & 19 deletions src/Steps/Release/CreateChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ protected function getChangelogLibrary(LibraryRelease $newRelease, Version $hist
// Build root release node
$historicRelease = new ChangelogLibrary($newRelease, $historicVersion);

// Check if "stable only" mode
$preferStable = $newRelease->getVersion()->isStable();

// Check all dependencies from this past commit.
// Note that we flatten both current and historic dependecy trees in case dependencies
// have been re-arranged since the prior tag.
Expand All @@ -173,7 +176,7 @@ protected function getChangelogLibrary(LibraryRelease $newRelease, Version $hist
// Get flat composer history up to 6 levels deep
$pastComposer = $newRelease
->getLibrary()
->getHistoryComposerData($historicVersion->getValue(), true, 6);
->getHistoryComposerData($historicVersion->getValue(), true, 6, $preferStable);
}

// Check if this release has a historic tag.
Expand All @@ -185,30 +188,17 @@ protected function getChangelogLibrary(LibraryRelease $newRelease, Version $hist

// Get oldest existing tag that matches the given constraint as the "from" for changelog purposes.
$historicConstraint = new ComposerConstraint($historicConstraintName, $historicVersion, $childReleaseName);
$childVersions = $historicConstraint->filterVersions($childNewRelease->getLibrary()->getTags());

// If "to" is stable, then filter out unstable "from"
// E.g. prefer "3.4.0..3.4.1" over "3.4.0-rc1..3.4.1"
if ($childNewRelease->getVersion()->isStable()) {
$stableChildVersions = Version::filter($childVersions, function (Version $nextTag) {
return $nextTag->isStable();
});
// Use stable versions if available, or fall back to unstable
if (!empty($stableChildVersions)) {
$childVersions = $stableChildVersions;
}
}

// Get smallest matching version
$childVersions = Version::sort($childVersions, Version::ASC);
if (empty($childVersions)) {
$childHistoricVersion = $childNewRelease->getLibrary()->getOldestVersionMatching(
$historicConstraint,
$childNewRelease->getVersion()->isStable()
);
if (!$childHistoricVersion) {
throw new \LogicException(
"No historic version for library {$childReleaseName} matches constraint {$historicConstraintName}"
);
}

// Check if to == from version
$childHistoricVersion = reset($childVersions);
if ($childHistoricVersion->getValue() === $childNewRelease->getVersion()->getValue()) {
continue;
}
Expand Down

0 comments on commit 126366d

Please sign in to comment.