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

Upload assets to GitHub release #2

Merged
merged 8 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/calm-oranges-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": minor
---

Upload assets to GitHub release
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This action for [Changesets](https://github.com/atlassian/changesets) creates a
- title - The pull request title. Default to `Version Packages`
- setupGitUser - Sets up the git user for commits as `"github-actions[bot]"`. Default to `true`
- createGithubReleases - A boolean value to indicate whether to create Github releases after `publish` or not. Default to `true`
- githubReleaseAssets - A multiline list of glob paths to assets to be uploaded to the GitHub release notes
- cwd - Changes node's `process.cwd()` if the project is not located on the root. Default to `process.cwd()`

### Outputs
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ inputs:
description: "A boolean value to indicate whether to create Github releases after `publish` or not"
required: false
default: true
githubReleaseAssets:
description: "A multiline list of glob paths to assets to be uploaded to the GitHub release notes"
required: false
outputs:
published:
description: A boolean value to indicate whether a publishing is happened or not
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/semver": "^6.0.2",
"babel-jest": "^24.9.0",
"fs-extra": "^8.1.0",
"glob": "^10.2.2",
"husky": "^3.0.3",
"jest": "^24.9.0",
"mdast-util-to-string": "^1.0.6",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
script: publishScript,
githubToken,
createGithubReleases: core.getBooleanInput("createGithubReleases"),
githubReleaseAssets: core.getMultilineInput("githubReleaseAssets"),
});

if (result.published) {
Expand All @@ -103,6 +104,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
hasPublishScript,
githubReleaseAssets: core.getMultilineInput("githubReleaseAssets"),
});

core.setOutput("pullRequestNumber", String(pullRequestNumber));
Expand Down
37 changes: 35 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import * as gitUtils from "./gitUtils";
import readChangesetState from "./readChangesetState";
import resolveFrom from "resolve-from";
import { glob } from "glob";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-glob is faster 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance is not that important here I'd say 😄


// GitHub Issues/PRs messages have a max size limit on the
// message body payload.
Expand All @@ -24,7 +25,7 @@ const MAX_CHARACTERS_PER_MESSAGE = 60000;

const createRelease = async (
octokit: ReturnType<typeof github.getOctokit>,
{ pkg, tagName }: { pkg: Package; tagName: string }
{ pkg, tagName, assets }: { pkg: Package; tagName: string; assets: string[] }
) => {
try {
let changelogFileName = path.join(pkg.dir, "CHANGELOG.md");
Expand All @@ -40,13 +41,27 @@ const createRelease = async (
);
}

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

for (const pattern of assets) {
const assets = await glob(pattern);
console.log(`Pattern ${pattern} matched the following assets: ${assets}`);
for (const asset of assets) {
await octokit.repos.uploadReleaseAsset({
release_id: release.data.id,
name: path.basename(asset),
// @ts-expect-error buffer is also accepted?
data: await fs.readFile(asset),
...github.context.repo,
});
}
}
} catch (err: any) {
// if we can't find a changelog, the user has probably disabled changelogs
if (err.code !== "ENOENT") {
Expand All @@ -59,6 +74,7 @@ type PublishOptions = {
script: string;
githubToken: string;
createGithubReleases: boolean;
githubReleaseAssets: string[];
cwd?: string;
};

Expand All @@ -77,6 +93,7 @@ export async function runPublish({
script,
githubToken,
createGithubReleases,
githubReleaseAssets,
cwd = process.cwd(),
}: PublishOptions): Promise<PublishResult> {
let octokit = github.getOctokit(githubToken);
Expand Down Expand Up @@ -119,6 +136,7 @@ export async function runPublish({
createRelease(octokit, {
pkg,
tagName: `${pkg.packageJson.name}@${pkg.packageJson.version}`,
assets: githubReleaseAssets,
})
)
);
Expand All @@ -142,6 +160,7 @@ export async function runPublish({
await createRelease(octokit, {
pkg,
tagName: `v${pkg.packageJson.version}`,
assets: githubReleaseAssets,
});
}
break;
Expand Down Expand Up @@ -186,6 +205,7 @@ type GetMessageOptions = {
}[];
prBodyMaxCharacters: number;
preState?: PreState;
githubReleaseAssets: string[];
};

export async function getVersionPrBody({
Expand All @@ -194,6 +214,7 @@ export async function getVersionPrBody({
changedPackagesInfo,
prBodyMaxCharacters,
branch,
githubReleaseAssets,
}: GetMessageOptions) {
let messageHeader = `This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and ${
hasPublishScript
Expand Down Expand Up @@ -241,6 +262,15 @@ export async function getVersionPrBody({
].join("\n");
}

// Append the assets that are to be uploaded to the GitHub release
if (githubReleaseAssets.length) {
fullMessage += "\n";
fullMessage += "# GitHub Release Assets\n";
for (const asset of githubReleaseAssets) {
fullMessage += "1. `" + asset + "`\n";
}
}

return fullMessage;
}

Expand All @@ -252,6 +282,7 @@ type VersionOptions = {
commitMessage?: string;
hasPublishScript?: boolean;
prBodyMaxCharacters?: number;
githubReleaseAssets: string[];
};

type RunVersionResult = {
Expand All @@ -266,6 +297,7 @@ export async function runVersion({
commitMessage = "Version Packages",
hasPublishScript = false,
prBodyMaxCharacters = MAX_CHARACTERS_PER_MESSAGE,
githubReleaseAssets,
}: VersionOptions): Promise<RunVersionResult> {
let repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
let branch = github.context.ref.replace("refs/heads/", "");
Expand Down Expand Up @@ -338,6 +370,7 @@ export async function runVersion({
branch,
changedPackagesInfo,
prBodyMaxCharacters,
githubReleaseAssets,
});

if (searchResult.data.items.length === 0) {
Expand Down
Loading