Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/shiny-pianos-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Avoid hitting a deprecation warning when encountering errors from `@octokit/request-error`
61 changes: 26 additions & 35 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getChangedPackages,
getChangelogEntry,
getVersionsByDirectory,
isErrorWithCode,
sortTheThings,
} from "./utils";

Expand All @@ -27,38 +28,32 @@ const createRelease = async (
octokit: Octokit,
{ pkg, tagName }: { pkg: Package; tagName: string }
) => {
let changelog;
try {
let changelogFileName = path.join(pkg.dir, "CHANGELOG.md");

let changelog = await fs.readFile(changelogFileName, "utf8");

let changelogEntry = getChangelogEntry(changelog, pkg.packageJson.version);
if (!changelogEntry) {
// we can find a changelog but not the entry for this version
// if this is true, something has probably gone wrong
throw new Error(
`Could not find changelog entry for ${pkg.packageJson.name}@${pkg.packageJson.version}`
);
}

await octokit.rest.repos.createRelease({
name: tagName,
tag_name: tagName,
body: changelogEntry.content,
prerelease: pkg.packageJson.version.includes("-"),
...github.context.repo,
});
changelog = await fs.readFile(path.join(pkg.dir, "CHANGELOG.md"), "utf8");
} catch (err) {
// if we can't find a changelog, the user has probably disabled changelogs
if (
err &&
typeof err === "object" &&
"code" in err &&
err.code !== "ENOENT"
Copy link
Member

Choose a reason for hiding this comment

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

This has never meant to target RequestError but rather the errors potentially coming from fs.readFile. I think the proper fix would be only to wrap what we have to wrap with try/catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for review & work!

) {
throw err;
if (isErrorWithCode(err, "ENOENT")) {
// if we can't find a changelog, the user has probably disabled changelogs
return;
}
throw err;
}
let changelogEntry = getChangelogEntry(changelog, pkg.packageJson.version);
if (!changelogEntry) {
// we can find a changelog but not the entry for this version
// if this is true, something has probably gone wrong
throw new Error(
`Could not find changelog entry for ${pkg.packageJson.name}@${pkg.packageJson.version}`
);
}

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

type PublishOptions = {
Expand Down Expand Up @@ -169,14 +164,10 @@ const requireChangesetsCliPkgJson = (cwd: string) => {
try {
return require(resolveFrom(cwd, "@changesets/cli/package.json"));
} catch (err) {
if (
err &&
typeof err === "object" &&
"code" in err &&
err.code === "MODULE_NOT_FOUND"
) {
if (isErrorWithCode(err, "MODULE_NOT_FOUND")) {
throw new Error(
`Have you forgotten to install \`@changesets/cli\` in "${cwd}"?`
`Have you forgotten to install \`@changesets/cli\` in "${cwd}"?`,
{ cause: err }
);
}
throw err;
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,12 @@ export function sortTheThings(
}
return -1;
}

export function isErrorWithCode(err: unknown, code: string) {
return (
typeof err === "object" &&
err !== null &&
"code" in err &&
err.code === code
);
}