From 3b7adfea5d9a0257970e5009f57386eb6a846f77 Mon Sep 17 00:00:00 2001 From: "Rasamoelina, Haja Onjatiana" <26148770+rhahao@users.noreply.github.com> Date: Thu, 28 Sep 2023 00:16:17 +0300 Subject: [PATCH] fix: discussion not created if publishing release with assets (#711) --- lib/publish.js | 47 ++++++++++++------ test/publish.test.js | 113 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 15 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index 7a0ca563..6a5bdfc2 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -54,11 +54,6 @@ export default async function publish(pluginConfig, context, { Octokit }) { debug("release object: %O", release); - // If discussionCategoryName is not undefined or false - if (discussionCategoryName) { - release.discussion_category_name = discussionCategoryName; - } - const draftReleaseOptions = { ...release, draft: true }; // When there are no assets, we publish a release directly. @@ -76,12 +71,22 @@ export default async function publish(pluginConfig, context, { Octokit }) { return { url, name: RELEASE_NAME, id: releaseId }; } + // add discussion_category_name if discussionCategoryName is not undefined or false + if (discussionCategoryName) { + release.discussion_category_name = discussionCategoryName; + } + const { - data: { html_url: url, id: releaseId }, + data: { html_url: url, id: releaseId, discussion_url }, } = await octokit.request("POST /repos/{owner}/{repo}/releases", release); logger.log("Published GitHub release: %s", url); - return { url, name: RELEASE_NAME, id: releaseId }; + + if (discussionCategoryName) { + logger.log("Created GitHub release discussion: %s", discussion_url); + } + + return { url, name: RELEASE_NAME, id: releaseId, discussion_url }; } // We'll create a draft release, append the assets to it, and then publish it. @@ -152,18 +157,30 @@ export default async function publish(pluginConfig, context, { Octokit }) { return { url: draftUrl, name: RELEASE_NAME, id: releaseId }; } + const patchRelease = { + owner, + repo, + release_id: releaseId, + draft: false, + }; + + // add discussion_category_name if discussionCategoryName is not undefined or false + if (discussionCategoryName) { + patchRelease.discussion_category_name = discussionCategoryName; + } + const { - data: { html_url: url }, + data: { html_url: url, discussion_url }, } = await octokit.request( "PATCH /repos/{owner}/{repo}/releases/{release_id}", - { - owner, - repo, - release_id: releaseId, - draft: false, - }, + patchRelease, ); logger.log("Published GitHub release: %s", url); - return { url, name: RELEASE_NAME, id: releaseId }; + + if (discussionCategoryName) { + logger.log("Created GitHub release discussion: %s", discussion_url); + } + + return { url, name: RELEASE_NAME, id: releaseId, discussion_url }; } diff --git a/test/publish.test.js b/test/publish.test.js index c5701099..8b28c037 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -95,12 +95,15 @@ test("Publish a release and create discussion", async (t) => { const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`; const uploadUrl = `https://github.com${uploadUri}{?name,label}`; const branch = "test_branch"; + const discussionId = 1; + const discussionUrl = `https://github.com/${owner}/${repo}/discussions/${discussionId}`; const fetch = fetchMock.sandbox().postOnce( `https://api.github.local/repos/${owner}/${repo}/releases`, { upload_url: uploadUrl, html_url: releaseUrl, + discussion_url: discussionUrl, }, { body: { @@ -137,6 +140,10 @@ test("Publish a release and create discussion", async (t) => { "Published GitHub release: %s", releaseUrl, ]); + t.deepEqual(t.context.log.args[1], [ + "Created GitHub release discussion: %s", + discussionUrl, + ]); t.true(fetch.done()); }); @@ -276,12 +283,15 @@ test("Publish a prerelease and create discussion", async (t) => { const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`; const uploadUrl = `https://github.com${uploadUri}{?name,label}`; const branch = "test_branch"; + const discussionId = 1; + const discussionUrl = `https://github.com/${owner}/${repo}/discussions/${discussionId}`; const fetch = fetchMock.sandbox().postOnce( `https://api.github.local/repos/${owner}/${repo}/releases`, { upload_url: uploadUrl, html_url: releaseUrl, + discussion_url: discussionUrl, }, { body: { @@ -318,6 +328,10 @@ test("Publish a prerelease and create discussion", async (t) => { "Published GitHub release: %s", releaseUrl, ]); + t.deepEqual(t.context.log.args[1], [ + "Created GitHub release discussion: %s", + discussionUrl, + ]); t.true(fetch.done()); }); @@ -641,6 +655,105 @@ test("Publish a release with an array of missing assets", async (t) => { t.true(fetch.done()); }); +test("Publish a release with asset and create discussion", async (t) => { + const owner = "test_user"; + const repo = "test_repo"; + const env = { GITHUB_TOKEN: "github_token" }; + const pluginConfig = { + assets: [ + ["**", "!**/*.txt"], + { path: ".dotfile", label: "A dotfile with no ext" }, + ], + discussionCategoryName: "Announcements", + }; + const nextRelease = { + gitTag: "v1.0.0", + name: "v1.0.0", + notes: "Test release note body", + }; + const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` }; + const untaggedReleaseUrl = `https://github.com/${owner}/${repo}/releases/untagged-123`; + const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`; + const assetUrl = `https://github.com/${owner}/${repo}/releases/download/${nextRelease.version}/.dotfile`; + const releaseId = 1; + const uploadOrigin = `https://uploads.github.local`; + const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`; + const uploadUrl = `${uploadOrigin}${uploadUri}{?name,label}`; + const branch = "test_branch"; + const discussionId = 1; + const discussionUrl = `https://github.com/${owner}/${repo}/discussions/${discussionId}`; + + const fetch = fetchMock + .sandbox() + .postOnce( + `https://api.github.local/repos/${owner}/${repo}/releases`, + { + upload_url: uploadUrl, + html_url: untaggedReleaseUrl, + id: releaseId, + }, + { + body: { + tag_name: nextRelease.gitTag, + target_commitish: branch, + name: nextRelease.name, + body: nextRelease.notes, + draft: true, + prerelease: false, + }, + }, + ) + .patchOnce( + `https://api.github.local/repos/${owner}/${repo}/releases/${releaseId}`, + { + upload_url: uploadUrl, + html_url: releaseUrl, + discussion_url: discussionUrl, + }, + { + body: { + draft: false, + discussion_category_name: pluginConfig.discussionCategoryName, + }, + }, + ) + .postOnce( + `${uploadOrigin}${uploadUri}?name=${encodeURIComponent( + ".dotfile", + )}&label=${encodeURIComponent("A dotfile with no ext")}`, + { browser_download_url: assetUrl }, + ); + + const result = await publish( + pluginConfig, + { + cwd, + env, + options, + branch: { name: branch, type: "release", main: true }, + nextRelease, + logger: t.context.logger, + }, + { + Octokit: TestOctokit.defaults((options) => ({ + ...options, + request: { ...options.request, fetch }, + })), + }, + ); + + t.is(result.url, releaseUrl); + t.true(t.context.log.calledWith("Published GitHub release: %s", releaseUrl)); + t.true( + t.context.log.calledWith( + "Created GitHub release discussion: %s", + discussionUrl, + ), + ); + t.true(t.context.log.calledWith("Published file %s", assetUrl)); + t.true(fetch.done()); +}); + test("Publish a draft release", async (t) => { const owner = "test_user"; const repo = "test_repo";