From 3e175cf086f5b2f7de1ce6460b9bb7539f7f9ef1 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 16 May 2024 13:48:01 +0200 Subject: [PATCH 1/3] ref: Better logging for github target --- src/targets/github.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/targets/github.ts b/src/targets/github.ts index 4d6a7016..10be12fb 100644 --- a/src/targets/github.ts +++ b/src/targets/github.ts @@ -132,9 +132,22 @@ export class GitHubTarget extends BaseTarget { repo: this.githubConfig.repo, }); + const latestReleaseTag = latestRelease?.tag_name; + this.logger.info( + latestReleaseTag + ? `Previous release: ${latestReleaseTag}` + : 'No previous release found' + ); + const isLatest = isPreview ? false - : isLatestRelease(latestRelease, version); + : isLatestRelease(latestReleaseTag, version); + + this.logger.info( + isLatest + ? 'Tagging release as latest, because new version is greater than old version' + : 'Not tagging release as latest, because new version is not greater than old version' + ); const { data } = await this.github.repos.createRelease({ draft: true, @@ -423,11 +436,8 @@ export class GitHubTarget extends BaseTarget { } } -export function isLatestRelease( - githubRelease: { tag_name: string } | undefined, - version: string -) { - const latestVersion = githubRelease && parseVersion(githubRelease.tag_name); +export function isLatestRelease(previousVersion: string, version: string) { + const latestVersion = parseVersion(previousVersion); const versionToPublish = parseVersion(version); return latestVersion && versionToPublish ? versionGreaterOrEqualThan(versionToPublish, latestVersion) From 9727b98990c87f1b00cef52fb6dc99a3205d8624 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 16 May 2024 14:08:18 +0200 Subject: [PATCH 2/3] fix stuff --- src/targets/__tests__/github.test.ts | 28 ++++++++++++++-------------- src/targets/github.ts | 9 ++++++++- 2 files changed, 22 insertions(+), 15 deletions(-) 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 10be12fb..0472bbcf 100644 --- a/src/targets/github.ts +++ b/src/targets/github.ts @@ -436,7 +436,14 @@ export class GitHubTarget extends BaseTarget { } } -export function isLatestRelease(previousVersion: string, version: string) { +export function isLatestRelease( + previousVersion: string | undefined, + version: string +) { + if (!previousVersion) { + return true; + } + const latestVersion = parseVersion(previousVersion); const versionToPublish = parseVersion(version); return latestVersion && versionToPublish From 55d73ce546c828e83a88e3401fd197ba41493d08 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 16 May 2024 14:37:49 +0200 Subject: [PATCH 3/3] fix github latest check --- src/targets/github.ts | 51 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/targets/github.ts b/src/targets/github.ts index 0472bbcf..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,28 +128,6 @@ 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 isLatest = isPreview - ? false - : isLatestRelease(latestReleaseTag, version); - - this.logger.info( - isLatest - ? 'Tagging release as latest, because new version is greater than old version' - : 'Not tagging release as latest, because new version is not greater than old version' - ); - const { data } = await this.github.repos.createRelease({ draft: true, name: tag, @@ -349,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; @@ -358,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, }); } @@ -420,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( @@ -432,7 +431,7 @@ export class GitHubTarget extends BaseTarget { ) ); - await this.publishRelease(draftRelease); + await this.publishRelease(draftRelease, isLatest); } }