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: discussion not created if publishing release with assets #711

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
47 changes: 32 additions & 15 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 };
}
113 changes: 113 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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());
});

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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());
});

Expand Down Expand Up @@ -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";
Expand Down