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

Update runPublish to output GitHub release details #347

Open
wants to merge 2 commits into
base: main
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
5 changes: 5 additions & 0 deletions .changeset/famous-cooks-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Updated the output to add GitHub release details
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This action for [Changesets](https://github.com/atlassian/changesets) creates a
### Outputs

- published - A boolean value to indicate whether a publishing is happened or not
- publishedPackages - A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
- publishedPackages - A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0", "githubRelease": {"id":123, "upload_url": "..."}}, {"name": "@xx/xy", "version": "0.8.9"}]`. `githubRelease` is the Github release object with `id` and `upload_url` if `createGithubReleases` is `true`

### Example workflow:

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ outputs:
description: A boolean value to indicate whether a publishing is happened or not
publishedPackages:
description: >
A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0", "githubRelease": {"id":123, "upload_url": "..."}}, {"name": "@xx/xy", "version": "0.8.9"}]`
hasChangesets:
description: A boolean about whether there were changesets. Useful if you want to create your own publishing functionality.
pullRequestNumber:
Expand Down
45 changes: 36 additions & 9 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ const createRelease = async (
);
}

await octokit.rest.repos.createRelease({
const { data: release } = await octokit.rest.repos.createRelease({
name: tagName,
tag_name: tagName,
body: changelogEntry.content,
prerelease: pkg.packageJson.version.includes("-"),
...github.context.repo,
});

return release;
} catch (err) {
// if we can't find a changelog, the user has probably disabled changelogs
if (
Expand Down Expand Up @@ -114,6 +116,11 @@ type PublishResult =
published: false;
};

type GitHubRelease = {
id: number;
upload_url: string;
};

export async function runPublish({
script,
githubToken,
Expand All @@ -133,7 +140,7 @@ export async function runPublish({
await gitUtils.pushTags();

let { packages, tool } = await getPackages(cwd);
let releasedPackages: Package[] = [];
let releasedPackages: (Package & { githubRelease?: GitHubRelease })[] = [];

if (tool !== "root") {
let newTagRegex = /New tag:\s+(@[^/]+\/[^@]+|[^/]+)@([^\s]+)/;
Expand All @@ -156,13 +163,23 @@ export async function runPublish({
}

if (createGithubReleases) {
await Promise.all(
releasedPackages.map((pkg) =>
createRelease(octokit, {
releasedPackages = await Promise.all(
releasedPackages.map(async (pkg) => {
const release = await createRelease(octokit, {
pkg,
tagName: `${pkg.packageJson.name}@${pkg.packageJson.version}`,
})
)
});

return {
...pkg,
githubRelease: release
? {
id: release.id,
upload_url: release.upload_url,
}
: undefined,
};
})
);
}
} else {
Expand All @@ -179,12 +196,21 @@ export async function runPublish({
let match = line.match(newTagRegex);

if (match) {
releasedPackages.push(pkg);
if (createGithubReleases) {
await createRelease(octokit, {
const release = await createRelease(octokit, {
pkg,
tagName: `v${pkg.packageJson.version}`,
});
const githubRelease = release
? {
id: release.id,
upload_url: release.upload_url,
}
: undefined;

releasedPackages.push({ ...pkg, githubRelease });
} else {
releasedPackages.push(pkg);
}
break;
}
Expand All @@ -197,6 +223,7 @@ export async function runPublish({
publishedPackages: releasedPackages.map((pkg) => ({
name: pkg.packageJson.name,
version: pkg.packageJson.version,
githubRelease: pkg.githubRelease,
})),
};
}
Expand Down