Skip to content

Commit

Permalink
fix: fallback to look at releases when looking for latest release
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Dec 20, 2021
1 parent fe73fc2 commit 0bdf55f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ export class Manifest {
let latestRelease = releasesByPath[path];
if (
!latestRelease &&
this.releasedVersions[path] &&
this.releasedVersions[path].toString() !== '0.0.0'
) {
const version = this.releasedVersions[path];
Expand Down Expand Up @@ -937,11 +938,16 @@ async function latestReleaseVersion(
`Looking for latest release on branch: ${targetBranch} with prefix: ${prefix}`
);

// collect set of recent commit SHAs seen to verify that the release
// is in the current branch
const commitShas = new Set<string>();

// only look at the last 250 or so commits to find the latest tag - we
// don't want to scan the entire repository history if this repo has never
// been released
const generator = github.mergeCommitIterator(targetBranch, 250);
for await (const commitWithPullRequest of generator) {
commitShas.add(commitWithPullRequest.sha);
const mergedPullRequest = commitWithPullRequest.pullRequest;
if (!mergedPullRequest) {
continue;
Expand Down Expand Up @@ -974,7 +980,34 @@ async function latestReleaseVersion(

return version;
}
return;

// If not found from recent pull requests, look at releases. Iterate
// through releases finding valid tags, then cross reference
const releaseGenerator = github.releaseIterator();
const candidateReleaseVersions: Version[] = [];
for await (const release of releaseGenerator) {
const tagName = TagName.parse(release.tagName);
if (!tagName) {
continue;
}

if (tagName.component === prefix) {
logger.debug(`found release for ${prefix}`, tagName.version);
if (!commitShas.has(release.sha)) {
logger.debug(
`SHA not found in recent commits to branch ${targetBranch}, skipping`
);
continue;
}
candidateReleaseVersions.push(tagName.version);
}
}
logger.debug(
`found ${candidateReleaseVersions.length} possible releases.`,
candidateReleaseVersions
);

return candidateReleaseVersions.sort((a, b) => a.compare(b))[0];
}

function mergeReleaserConfig(
Expand Down
6 changes: 6 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as semver from 'semver';

const VERSION_REGEX =
/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-(?<preRelease>[^+]+))?(\+(?<build>.*))?/;

Expand Down Expand Up @@ -49,6 +51,10 @@ export class Version {
return new Version(major, minor, patch, preRelease, build);
}

compare(other: Version): -1 | 0 | 1 {
return semver.compare(this.toString(), other.toString());
}

toString(): string {
const preReleasePart = this.preRelease ? `-${this.preRelease}` : '';
const buildPart = this.build ? `+${this.build}` : '';
Expand Down

0 comments on commit 0bdf55f

Please sign in to comment.