Fixed GitHub Release changelog overrides

Fixes #77
Fixes #90
This commit is contained in:
Kir_Antipov 2024-01-02 13:28:10 +00:00
parent 689f343a8c
commit 86e7074aca

View file

@ -64,8 +64,7 @@ export class GitHubUploader extends GenericPlatformUploader<GitHubUploaderOption
const api = new GitHubApiClient({ token: request.token.unwrap(), baseUrl: this._context.apiUrl }); const api = new GitHubApiClient({ token: request.token.unwrap(), baseUrl: this._context.apiUrl });
const repo = this._context.repo; const repo = this._context.repo;
const releaseId = await this.getOrCreateReleaseId(request, api); const release = await this.updateOrCreateRelease(request, api);
const release = await this.updateRelease(request, releaseId, api);
return { return {
repo: `${repo.owner}/${repo.repo}`, repo: `${repo.owner}/${repo.repo}`,
@ -82,23 +81,25 @@ export class GitHubUploader extends GenericPlatformUploader<GitHubUploaderOption
* @param request - Contains parameters that define the desired release. * @param request - Contains parameters that define the desired release.
* @param api - An instance of the GitHub API client for interacting with GitHub services. * @param api - An instance of the GitHub API client for interacting with GitHub services.
* *
* @returns The ID of the release corresponding to the request parameters. * @returns The ID of the release and a boolean indicating whether a new release was created.
*/ */
private async getOrCreateReleaseId(request: GitHubUploadRequest, api: GitHubApiClient): Promise<number> { private async getOrCreateReleaseId(request: GitHubUploadRequest, api: GitHubApiClient): Promise<[id: number, created: boolean]> {
const repo = this._context.repo; const repo = this._context.repo;
const tag = request.tag || this._context.tag || request.version; const tag = request.tag || this._context.tag || request.version;
let releaseId = undefined as number; let id = undefined as number;
let created = false;
if (request.tag) { if (request.tag) {
releaseId = await api.getRelease({ ...repo, tag_name: request.tag }).then(x => x?.id); id = await api.getRelease({ ...repo, tag_name: request.tag }).then(x => x?.id);
} else if (this._context.payload.release?.id) { } else if (this._context.payload.release?.id) {
releaseId = this._context.payload.release.id; id = this._context.payload.release.id;
} else if (tag) { } else if (tag) {
releaseId = await api.getRelease({ ...repo, tag_name: tag }).then(x => x?.id); id = await api.getRelease({ ...repo, tag_name: tag }).then(x => x?.id);
} }
if (!releaseId && tag) { if (!id && tag) {
releaseId = (await api.createRelease({ id = (await api.createRelease({
...repo, ...repo,
tag_name: tag, tag_name: tag,
target_commitish: request.commitish, target_commitish: request.commitish,
@ -109,29 +110,35 @@ export class GitHubUploader extends GenericPlatformUploader<GitHubUploaderOption
discussion_category_name: request.discussion, discussion_category_name: request.discussion,
generate_release_notes: request.generateChangelog ?? !request.changelog, generate_release_notes: request.generateChangelog ?? !request.changelog,
}))?.id; }))?.id;
created = true;
} }
if (!releaseId) { if (!id) {
throw new Error(`Cannot find or create GitHub Release${tag ? ` (${tag})` : ""}.`); throw new Error(`Cannot find or create GitHub Release${tag ? ` (${tag})` : ""}.`);
} }
return releaseId;
return [id, created];
} }
/** /**
* Updates the content of an existing GitHub release based on the provided request. * Updates or creates a GitHub release based on the provided request.
* *
* @param request - Contains parameters that define the changes to apply to the release. * @param request - Contains parameters that define the changes to apply to the release.
* @param releaseId - The ID of the release to be updated.
* @param api - An instance of the GitHub API client for interacting with GitHub services. * @param api - An instance of the GitHub API client for interacting with GitHub services.
* *
* @returns The updated release data from GitHub. * @returns The release data from GitHub.
*/ */
private async updateRelease(request: GitHubUploadRequest, releaseId: number, api: GitHubApiClient): Promise<GitHubRelease> { private async updateOrCreateRelease(request: GitHubUploadRequest, api: GitHubApiClient): Promise<GitHubRelease> {
const [id, created] = await this.getOrCreateReleaseId(request, api);
const body = (!request.changelog || created) ? undefined : request.changelog;
const assets = request.files;
return await api.updateRelease({ return await api.updateRelease({
...this._context.repo, ...this._context.repo,
id: releaseId, id,
body: request.changelog, body,
assets: request.files, assets,
}); });
} }
} }