Skip to content

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

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

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Changes from 3 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
58 changes: 35 additions & 23 deletions src/targets/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,13 @@ export class GitHubTarget extends BaseTarget {
};
}

let latestRelease: { tag_name: string } | undefined;
try {
latestRelease = (
await this.github.repos.getLatestRelease({
owner: this.githubConfig.owner,
repo: this.githubConfig.repo,
})
).data;
} catch (error) {
// if the error is a 404 error, it means that no release exists yet
// all other errors should be rethrown
if (error.status !== 404) {
throw error;
}
}

const isLatest = isPreview
? false
: isLatestRelease(latestRelease, version);

const { data } = await this.github.repos.createRelease({
draft: true,
name: tag,
owner: this.githubConfig.owner,
prerelease: isPreview,
repo: this.githubConfig.repo,
tag_name: tag,
make_latest: isLatest ? 'true' : 'false',
target_commitish: revision,
...changes,
});
Expand Down Expand Up @@ -347,7 +326,10 @@ export class GitHubTarget extends BaseTarget {
*
* @param release Release object
*/
public async publishRelease(release: GitHubRelease) {
public async publishRelease(
release: GitHubRelease,
options: { makeLatest: boolean } = { makeLatest: true }
) {
if (isDryRun()) {
this.logger.info(`[dry-run] Not publishing the draft release`);
return;
Expand All @@ -356,6 +338,7 @@ export class GitHubTarget extends BaseTarget {
await this.github.repos.updateRelease({
...this.githubConfig,
release_id: release.id,
make_latest: options.makeLatest ? 'true' : 'false',
draft: false,
});
}
Expand Down Expand Up @@ -418,6 +401,35 @@ export class GitHubTarget extends BaseTarget {
}))
);

let latestRelease: { tag_name: string } | undefined = undefined;
try {
latestRelease = (
await this.github.repos.getLatestRelease({
owner: this.githubConfig.owner,
repo: this.githubConfig.repo,
})
).data;
} catch (error) {
// if the error is a 404 error, it means that no release exists yet
// all other errors should be rethrown
if (error.status !== 404) {
throw error;
}
}

const latestReleaseTag = latestRelease?.tag_name;
this.logger.info(
latestReleaseTag
? `Previous release: ${latestReleaseTag}`
: 'No previous release found'
);

const isPreview =
this.githubConfig.previewReleases && isPreviewRelease(version);
const makeLatest = isPreview
? false
: isLatestRelease(latestRelease, version);

const draftRelease = await this.createDraftRelease(
version,
revision,
Expand All @@ -430,7 +442,7 @@ export class GitHubTarget extends BaseTarget {
)
);

await this.publishRelease(draftRelease);
await this.publishRelease(draftRelease, { makeLatest });
}
}

Expand Down
Loading