Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(github): Fix making github releases latest or not #536

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/targets/__tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
46 changes: 31 additions & 15 deletions src/targets/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export class GitHubTarget extends BaseTarget {
public async createDraftRelease(
version: string,
revision: string,
changes?: Changeset
changes?: Changeset,
isLatest = true
): Promise<GitHubRelease> {
const tag = versionToTag(version, this.githubConfig.tagPrefix);
this.logger.info(`Git tag: "${tag}"`);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
});
}
Expand Down Expand Up @@ -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(
Expand 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)
Expand Down
Loading