Skip to content

Commit e9c36b6

Browse files
nayounsangAndarist
andauthored
Avoid hitting a deprecation warning when encountering errors from @octokit/request-error (#461)
* fix: use status when http err * tweak code * add changeset --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
1 parent c1f57e5 commit e9c36b6

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

.changeset/shiny-pianos-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Avoid hitting a deprecation warning when encountering errors from `@octokit/request-error`

src/run.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getChangedPackages,
1515
getChangelogEntry,
1616
getVersionsByDirectory,
17+
isErrorWithCode,
1718
sortTheThings,
1819
} from "./utils";
1920

@@ -27,38 +28,32 @@ const createRelease = async (
2728
octokit: Octokit,
2829
{ pkg, tagName }: { pkg: Package; tagName: string }
2930
) => {
31+
let changelog;
3032
try {
31-
let changelogFileName = path.join(pkg.dir, "CHANGELOG.md");
32-
33-
let changelog = await fs.readFile(changelogFileName, "utf8");
34-
35-
let changelogEntry = getChangelogEntry(changelog, pkg.packageJson.version);
36-
if (!changelogEntry) {
37-
// we can find a changelog but not the entry for this version
38-
// if this is true, something has probably gone wrong
39-
throw new Error(
40-
`Could not find changelog entry for ${pkg.packageJson.name}@${pkg.packageJson.version}`
41-
);
42-
}
43-
44-
await octokit.rest.repos.createRelease({
45-
name: tagName,
46-
tag_name: tagName,
47-
body: changelogEntry.content,
48-
prerelease: pkg.packageJson.version.includes("-"),
49-
...github.context.repo,
50-
});
33+
changelog = await fs.readFile(path.join(pkg.dir, "CHANGELOG.md"), "utf8");
5134
} catch (err) {
52-
// if we can't find a changelog, the user has probably disabled changelogs
53-
if (
54-
err &&
55-
typeof err === "object" &&
56-
"code" in err &&
57-
err.code !== "ENOENT"
58-
) {
59-
throw err;
35+
if (isErrorWithCode(err, "ENOENT")) {
36+
// if we can't find a changelog, the user has probably disabled changelogs
37+
return;
6038
}
39+
throw err;
40+
}
41+
let changelogEntry = getChangelogEntry(changelog, pkg.packageJson.version);
42+
if (!changelogEntry) {
43+
// we can find a changelog but not the entry for this version
44+
// if this is true, something has probably gone wrong
45+
throw new Error(
46+
`Could not find changelog entry for ${pkg.packageJson.name}@${pkg.packageJson.version}`
47+
);
6148
}
49+
50+
await octokit.rest.repos.createRelease({
51+
name: tagName,
52+
tag_name: tagName,
53+
body: changelogEntry.content,
54+
prerelease: pkg.packageJson.version.includes("-"),
55+
...github.context.repo,
56+
});
6257
};
6358

6459
type PublishOptions = {
@@ -169,14 +164,10 @@ const requireChangesetsCliPkgJson = (cwd: string) => {
169164
try {
170165
return require(resolveFrom(cwd, "@changesets/cli/package.json"));
171166
} catch (err) {
172-
if (
173-
err &&
174-
typeof err === "object" &&
175-
"code" in err &&
176-
err.code === "MODULE_NOT_FOUND"
177-
) {
167+
if (isErrorWithCode(err, "MODULE_NOT_FOUND")) {
178168
throw new Error(
179-
`Have you forgotten to install \`@changesets/cli\` in "${cwd}"?`
169+
`Have you forgotten to install \`@changesets/cli\` in "${cwd}"?`,
170+
{ cause: err }
180171
);
181172
}
182173
throw err;

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ export function sortTheThings(
9696
}
9797
return -1;
9898
}
99+
100+
export function isErrorWithCode(err: unknown, code: string) {
101+
return (
102+
typeof err === "object" &&
103+
err !== null &&
104+
"code" in err &&
105+
err.code === code
106+
);
107+
}

0 commit comments

Comments
 (0)