diff --git a/src/targets/__tests__/github.test.ts b/src/targets/__tests__/github.test.ts index 8bb44ab8..f1f81aa0 100644 --- a/src/targets/__tests__/github.test.ts +++ b/src/targets/__tests__/github.test.ts @@ -2,61 +2,61 @@ import { isLatestRelease } from '../github'; describe('isLatestRelease', () => { it('works with missing latest release', () => { - const latestRelease = undefined; + const latestTag = undefined; const version = '1.2.3'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(true); }); it('works with unparseable latest release', () => { - const latestRelease = { tag_name: 'foo' }; + const latestTag = 'foo'; const version = '1.2.3'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(true); }); it('works with unparseable new version', () => { - const latestRelease = { tag_name: 'v1.0.0' }; + const latestTag = 'v1.0.0'; const version = 'foo'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(true); }); describe('with v-prefix', () => { it('detects larger new version', () => { - const latestRelease = { tag_name: 'v1.1.0' }; + const latestTag = 'v1.1.0'; const version = '1.2.0'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(true); }); it('detects smaller new version', () => { - const latestRelease = { tag_name: 'v1.1.0' }; + const latestTag = 'v1.1.0'; const version = '1.0.1'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(false); }); }); describe('without v-prefix', () => { it('detects larger new version', () => { - const latestRelease = { tag_name: '1.1.0' }; + const latestTag = '1.1.0'; const version = '1.2.0'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(true); }); it('detects smaller new version', () => { - const latestRelease = { tag_name: '1.1.0' }; + const latestTag = '1.1.0'; const version = '1.0.1'; - const actual = isLatestRelease(latestRelease, version); + const actual = isLatestRelease(latestTag, version); expect(actual).toBe(false); }); }); diff --git a/src/targets/github.ts b/src/targets/github.ts index 4d6a7016..435554f4 100644 --- a/src/targets/github.ts +++ b/src/targets/github.ts @@ -111,7 +111,8 @@ export class GitHubTarget extends BaseTarget { public async createDraftRelease( version: string, revision: string, - changes?: Changeset + changes?: Changeset, + isLatest = true ): Promise { const tag = versionToTag(version, this.githubConfig.tagPrefix); this.logger.info(`Git tag: "${tag}"`); @@ -127,15 +128,6 @@ export class GitHubTarget extends BaseTarget { }; } - const { data: latestRelease } = await this.github.repos.getLatestRelease({ - owner: this.githubConfig.owner, - repo: this.githubConfig.repo, - }); - - const isLatest = isPreview - ? false - : isLatestRelease(latestRelease, version); - const { data } = await this.github.repos.createRelease({ draft: true, name: tag, @@ -336,7 +328,7 @@ export class GitHubTarget extends BaseTarget { * * @param release Release object */ - public async publishRelease(release: GitHubRelease) { + public async publishRelease(release: GitHubRelease, isLatest = true) { if (isDryRun()) { this.logger.info(`[dry-run] Not publishing the draft release`); return; @@ -345,6 +337,7 @@ export class GitHubTarget extends BaseTarget { await this.github.repos.updateRelease({ ...this.githubConfig, release_id: release.id, + make_latest: isLatest ? 'true' : 'false', draft: false, }); } @@ -407,10 +400,29 @@ export class GitHubTarget extends BaseTarget { })) ); + const { data: latestRelease } = await this.github.repos.getLatestRelease({ + owner: this.githubConfig.owner, + repo: this.githubConfig.repo, + }); + + const latestReleaseTag = latestRelease?.tag_name; + this.logger.info( + latestReleaseTag + ? `Previous release: ${latestReleaseTag}` + : 'No previous release found' + ); + + const isPreview = + this.githubConfig.previewReleases && isPreviewRelease(version); + const isLatest = isPreview + ? false + : isLatestRelease(latestReleaseTag, version); + const draftRelease = await this.createDraftRelease( version, revision, - changelog + changelog, + isLatest ); await Promise.all( @@ -419,15 +431,19 @@ export class GitHubTarget extends BaseTarget { ) ); - await this.publishRelease(draftRelease); + await this.publishRelease(draftRelease, isLatest); } } export function isLatestRelease( - githubRelease: { tag_name: string } | undefined, + previousVersion: string | undefined, version: string ) { - const latestVersion = githubRelease && parseVersion(githubRelease.tag_name); + if (!previousVersion) { + return true; + } + + const latestVersion = parseVersion(previousVersion); const versionToPublish = parseVersion(version); return latestVersion && versionToPublish ? versionGreaterOrEqualThan(versionToPublish, latestVersion)