Skip to content

Commit c4c1536

Browse files
bors[bot]Veetaha
andauthored
Merge #3295
3295: Refactoring fetchArtifactReleaseInfo() r=matklad a=Veetaha https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md I fact this rule doesn't work when you put an unnecessary non-null assertion in an expression (as we had `(awat f())!`, but it is useful in other cases... Closes #3295, i guess... Co-authored-by: Veetaha <[email protected]>
2 parents 1fe48a0 + 6ec4a7d commit c4c1536

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

editors/code/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
"@typescript-eslint/semi": [
3333
"error",
3434
"always"
35-
]
35+
],
36+
"@typescript-eslint/no-unnecessary-type-assertion": "error"
3637
}
3738
};

editors/code/src/installation/fetch_artifact_release_info.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,61 @@ import { log } from "../util";
44

55
const GITHUB_API_ENDPOINT_URL = "https://api.github.com";
66

7-
87
/**
9-
* Fetches the release with `releaseTag` (or just latest release when not specified)
10-
* from GitHub `repo` and returns metadata about `artifactFileName` shipped with
11-
* this release or `null` if no such artifact was published.
8+
* Fetches the release with `releaseTag` from GitHub `repo` and
9+
* returns metadata about `artifactFileName` shipped with
10+
* this release.
11+
*
12+
* @throws Error upon network failure or if no such repository, release, or artifact exists.
1213
*/
1314
export async function fetchArtifactReleaseInfo(
14-
repo: GithubRepo, artifactFileName: string, releaseTag?: string
15-
): Promise<null | ArtifactReleaseInfo> {
15+
repo: GithubRepo,
16+
artifactFileName: string,
17+
releaseTag: string
18+
): Promise<ArtifactReleaseInfo> {
1619

1720
const repoOwner = encodeURIComponent(repo.owner);
1821
const repoName = encodeURIComponent(repo.name);
1922

20-
const apiEndpointPath = releaseTag
21-
? `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}`
22-
: `/repos/${repoOwner}/${repoName}/releases/latest`;
23+
const apiEndpointPath = `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}`;
2324

2425
const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath;
2526

26-
// We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`)
27-
2827
log.debug("Issuing request for released artifacts metadata to", requestUrl);
2928

30-
// FIXME: handle non-ok response
31-
const response: GithubRelease = await fetch(requestUrl, {
32-
headers: { Accept: "application/vnd.github.v3+json" }
33-
})
34-
.then(res => res.json());
29+
const response = await fetch(requestUrl, { headers: { Accept: "application/vnd.github.v3+json" } });
3530

36-
const artifact = response.assets.find(artifact => artifact.name === artifactFileName);
31+
if (!response.ok) {
32+
log.error("Error fetching artifact release info", {
33+
requestUrl,
34+
releaseTag,
35+
artifactFileName,
36+
response: {
37+
headers: response.headers,
38+
status: response.status,
39+
body: await response.text(),
40+
}
41+
});
42+
43+
throw new Error(
44+
`Got response ${response.status} when trying to fetch ` +
45+
`"${artifactFileName}" artifact release info for ${releaseTag} release`
46+
);
47+
}
3748

38-
if (!artifact) return null;
49+
// We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`)
50+
const release: GithubRelease = await response.json();
51+
52+
const artifact = release.assets.find(artifact => artifact.name === artifactFileName);
53+
54+
if (!artifact) {
55+
throw new Error(
56+
`Artifact ${artifactFileName} was not found in ${release.name} release!`
57+
);
58+
}
3959

4060
return {
41-
releaseName: response.name,
61+
releaseName: release.name,
4262
downloadUrl: artifact.browser_download_url
4363
};
4464

editors/code/src/installation/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
6363

6464
async function downloadServer(source: BinarySource.GithubRelease): Promise<boolean> {
6565
try {
66-
const releaseInfo = (await fetchArtifactReleaseInfo(source.repo, source.file, source.version))!;
66+
const releaseInfo = await fetchArtifactReleaseInfo(source.repo, source.file, source.version);
6767

6868
await downloadArtifact(releaseInfo, source.file, source.dir, "language server");
6969
await setServerVersion(source.storage, releaseInfo.releaseName);

0 commit comments

Comments
 (0)