Skip to content

Commit

Permalink
refactor(http): Separate getJson and getJsonUnchecked (#33651)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 17, 2025
1 parent abe6122 commit 9b4e515
Show file tree
Hide file tree
Showing 60 changed files with 333 additions and 229 deletions.
2 changes: 1 addition & 1 deletion lib/config/presets/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function fetchJSONFile(
logger.trace({ url }, `Preset URL`);
let res: { body: { content: string } };
try {
res = await http.getJson(url);
res = await http.getJsonUnchecked(url);
} catch (err) {
// istanbul ignore if: not testable with nock
if (err instanceof ExternalHostError) {
Expand Down
2 changes: 1 addition & 1 deletion lib/config/presets/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function getDefaultBranchName(
urlEncodedPkgName: string,
endpoint: string,
): Promise<string> {
const res = await gitlabApi.getJson<GitlabProject>(
const res = await gitlabApi.getJsonUnchecked<GitlabProject>(
`${endpoint}projects/${urlEncodedPkgName}`,
);
return res.body.default_branch ?? 'master'; // should never happen, but we keep this to ensure the current behavior
Expand Down
2 changes: 1 addition & 1 deletion lib/config/presets/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getPreset({
'Using npm packages for Renovate presets is now deprecated. Please migrate to repository-based presets instead.',
);
const packageUrl = resolvePackageUrl(registryUrl, pkg);
const body = (await http.getJson<NpmResponse>(packageUrl)).body;
const body = (await http.getJsonUnchecked<NpmResponse>(packageUrl)).body;
// TODO: check null #22198
dep = body.versions![body['dist-tags']!.latest];
} catch {
Expand Down
18 changes: 12 additions & 6 deletions lib/modules/datasource/bitbucket-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ export class BitbucketTagsDatasource extends Datasource {
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const url = `/2.0/repositories/${repo}/refs/tags`;
const bitbucketTags = (
await this.bitbucketHttp.getJson<PagedResult<BitbucketTag>>(url, {
paginate: true,
})
await this.bitbucketHttp.getJsonUnchecked<PagedResult<BitbucketTag>>(
url,
{
paginate: true,
},
)
).body.values;

const dependency: ReleaseResult = {
Expand Down Expand Up @@ -96,8 +99,9 @@ export class BitbucketTagsDatasource extends Datasource {
): Promise<string | null> {
const url = `/2.0/repositories/${repo}/refs/tags/${tag}`;

const bitbucketTag = (await this.bitbucketHttp.getJson<BitbucketTag>(url))
.body;
const bitbucketTag = (
await this.bitbucketHttp.getJsonUnchecked<BitbucketTag>(url)
).body;

return bitbucketTag.target?.hash ?? null;
}
Expand Down Expand Up @@ -136,7 +140,9 @@ export class BitbucketTagsDatasource extends Datasource {

const url = `/2.0/repositories/${repo}/commits/${mainBranch}`;
const bitbucketCommits = (
await this.bitbucketHttp.getJson<PagedResult<BitbucketCommit>>(url)
await this.bitbucketHttp.getJsonUnchecked<PagedResult<BitbucketCommit>>(
url,
)
).body;

if (bitbucketCommits.values.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/conan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ConanDatasource extends Datasource {
);

try {
const rep = await this.http.getJson(lookupUrl);
const rep = await this.http.getJsonUnchecked(lookupUrl);
const conanJson = ConanJSON.parse(rep.body);
if (conanJson) {
logger.trace({ lookupUrl }, 'Got conan api result');
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/conda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class CondaDatasource extends Datasource {
let response: { body: CondaPackage };

try {
response = await this.http.getJson(url);
response = await this.http.getJsonUnchecked(url);

result.homepage = response.body.html_url;
result.sourceUrl = response.body.dev_url;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/crate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class CrateDatasource extends Datasource {

try {
type Response = { crate: CrateMetadata };
const response = await this.http.getJson<Response>(crateUrl);
const response = await this.http.getJsonUnchecked<Response>(crateUrl);
return response.body.crate;
} catch (err) {
logger.warn(
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/custom/formats/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CustomDatasourceFetcher } from './types';

export class JSONFetcher implements CustomDatasourceFetcher {
async fetch(http: Http, registryURL: string): Promise<unknown> {
const response = await http.getJson(registryURL);
const response = await http.getJsonUnchecked(registryURL);
return response.body;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/dart-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class DartVersionDatasource extends Datasource {
try {
for (const channel of this.channels) {
const resp = (
await this.http.getJson<DartResponse>(
await this.http.getJsonUnchecked<DartResponse>(
`${registryUrl}/storage/v1/b/dart-archive/o?delimiter=%2F&prefix=channels%2F${channel}%2Frelease%2F&alt=json`,
)
).body;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/dart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class DartDatasource extends Datasource {

let raw: HttpResponse<DartResult> | null = null;
try {
raw = await this.http.getJson<DartResult>(pkgUrl);
raw = await this.http.getJsonUnchecked<DartResult>(pkgUrl);
} catch (err) {
this.handleGenericErrors(err);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/docker/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function getAuthHeaders(
? await http.get(apiCheckUrl, options)
: // use json request, as this will be cached for tags, so it returns json
// TODO: add cache test
await http.getJson(apiCheckUrl, options);
await http.getJsonUnchecked(apiCheckUrl, options);

if (apiCheckResponse.statusCode === 200) {
logger.debug(`No registry auth required for ${apiCheckUrl}`);
Expand Down Expand Up @@ -193,7 +193,7 @@ export async function getAuthHeaders(
);
opts.noAuth = true;
const authResponse = (
await http.getJson<{ token?: string; access_token?: string }>(
await http.getJsonUnchecked<{ token?: string; access_token?: string }>(
authUrl.href,
opts,
)
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ export class DockerDatasource extends Datasource {

// typescript issue :-/
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const res = (await this.http.getJson<QuayRestDockerTags>(
const res = (await this.http.getJsonUnchecked<QuayRestDockerTags>(
url,
)) as HttpResponse<QuayRestDockerTags>;
const pageTags = res.body.tags.map((tag) => tag.name);
Expand Down Expand Up @@ -671,7 +671,7 @@ export class DockerDatasource extends Datasource {
do {
let res: HttpResponse<{ tags: string[] }>;
try {
res = await this.http.getJson<{ tags: string[] }>(url, {
res = await this.http.getJsonUnchecked<{ tags: string[] }>(url, {
headers,
noAuth: true,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/flutter-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class FlutterVersionDatasource extends Datasource {
};
try {
const resp = (
await this.http.getJson<FlutterResponse>(
await this.http.getJsonUnchecked<FlutterResponse>(
`${registryUrl}/flutter_infra_release/releases/releases_linux.json`,
)
).body;
Expand Down
14 changes: 8 additions & 6 deletions lib/modules/datasource/github-release-attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ export class GithubReleaseAttachmentsDatasource extends Datasource {
}

const apiBaseUrl = getApiBaseUrl(registryUrl);
const { body: currentRelease } = await this.http.getJson<GithubRestRelease>(
`${apiBaseUrl}repos/${repo}/releases/tags/${currentValue}`,
);
const { body: currentRelease } =
await this.http.getJsonUnchecked<GithubRestRelease>(
`${apiBaseUrl}repos/${repo}/releases/tags/${currentValue}`,
);
const digestAsset = await this.findDigestAsset(
currentRelease,
currentDigest,
Expand All @@ -221,9 +222,10 @@ export class GithubReleaseAttachmentsDatasource extends Datasource {
if (!digestAsset || newValue === currentValue) {
newDigest = currentDigest;
} else {
const { body: newRelease } = await this.http.getJson<GithubRestRelease>(
`${apiBaseUrl}repos/${repo}/releases/tags/${newValue}`,
);
const { body: newRelease } =
await this.http.getJsonUnchecked<GithubRestRelease>(
`${apiBaseUrl}repos/${repo}/releases/tags/${newValue}`,
);
newDigest = await this.mapDigestAssetToRelease(digestAsset, newRelease);
}
return newDigest;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/github-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class GithubTagsDatasource extends Datasource {
let digest: string | null = null;
try {
const url = `${apiBaseUrl}repos/${githubRepo}/commits?per_page=1`;
const res = await this.http.getJson<{ sha: string }[]>(url);
const res = await this.http.getJsonUnchecked<{ sha: string }[]>(url);
digest = res.body[0].sha;
} catch (err) {
logger.debug(
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/datasource/gitlab-packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export class GitlabPackagesDatasource extends Datasource {
let response: GitlabPackage[];
try {
response = (
await this.http.getJson<GitlabPackage[]>(apiUrl, { paginate: true })
await this.http.getJsonUnchecked<GitlabPackage[]>(apiUrl, {
paginate: true,
})
).body;

result.releases = response
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/gitlab-releases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class GitlabReleasesDatasource extends Datasource {

try {
const gitlabReleasesResponse = (
await this.http.getJson<GitlabRelease[]>(apiUrl)
await this.http.getJsonUnchecked<GitlabRelease[]>(apiUrl)
).body;

return {
Expand Down
8 changes: 5 additions & 3 deletions lib/modules/datasource/gitlab-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class GitlabTagsDatasource extends Datasource {
);

const gitlabTags = (
await this.http.getJson<GitlabTag[]>(url, {
await this.http.getJsonUnchecked<GitlabTag[]>(url, {
paginate: true,
})
).body;
Expand Down Expand Up @@ -94,7 +94,8 @@ export class GitlabTagsDatasource extends Datasource {
`repository/commits/`,
newValue,
);
const gitlabCommits = await this.http.getJson<GitlabCommit>(url);
const gitlabCommits =
await this.http.getJsonUnchecked<GitlabCommit>(url);
digest = gitlabCommits.body.id;
} else {
const url = joinUrlParts(
Expand All @@ -103,7 +104,8 @@ export class GitlabTagsDatasource extends Datasource {
urlEncodedRepo,
`repository/commits?per_page=1`,
);
const gitlabCommits = await this.http.getJson<GitlabCommit[]>(url);
const gitlabCommits =
await this.http.getJsonUnchecked<GitlabCommit[]>(url);
digest = gitlabCommits.body[0].id;
}
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/go/releases-goproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class GoProxyDatasource extends Datasource {
'@v',
`${version}.info`,
);
const res = await this.http.getJson<VersionInfo>(url);
const res = await this.http.getJsonUnchecked<VersionInfo>(url);

const result: Release = {
version: res.body.Version,
Expand All @@ -187,7 +187,7 @@ export class GoProxyDatasource extends Datasource {
this.encodeCase(packageName),
'@latest',
);
const res = await this.http.getJson<VersionInfo>(url);
const res = await this.http.getJsonUnchecked<VersionInfo>(url);
return res.body.Version;
} catch (err) {
logger.trace({ err }, 'Failed to get latest version');
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/datasource/gradle-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class GradleVersionDatasource extends Datasource {

let releases: Release[];
try {
const response = await this.http.getJson<GradleRelease[]>(registryUrl);
const response =
await this.http.getJsonUnchecked<GradleRelease[]>(registryUrl);
releases = response.body
.filter((release) => !release.snapshot && !release.nightly)
.map((release) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/hermit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class HermitDatasource extends Datasource {

const apiBaseUrl = getApiBaseUrl(`https://${host}`);

const indexRelease = await this.http.getJson<GithubRestRelease>(
const indexRelease = await this.http.getJsonUnchecked<GithubRestRelease>(
`${apiBaseUrl}repos/${owner}/${repo}/releases/tags/index`,
);

Expand Down
3 changes: 2 additions & 1 deletion lib/modules/datasource/java-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class JavaVersionDatasource extends Datasource {
): Promise<Release[] | null> {
const pgUrl = `${url}&page=${page}`;
try {
const pgRes = await this.http.getJson<AdoptiumJavaResponse>(pgUrl);
const pgRes =
await this.http.getJsonUnchecked<AdoptiumJavaResponse>(pgUrl);
return (
pgRes?.body?.versions?.map(({ semver }) => ({
version: semver,
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/jenkins-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class JenkinsPluginsDatasource extends Datasource {
try {
logger.debug(`jenkins-plugins: Fetching Jenkins plugins from ${url}`);
const startTime = Date.now();
response = (await this.http.getJson<T>(url)).body;
response = (await this.http.getJsonUnchecked<T>(url)).body;
const durationMs = Math.round(Date.now() - startTime);
logger.debug(
{ durationMs },
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/node-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class NodeVersionDatasource extends Datasource {
};
try {
const resp = (
await this.http.getJson<NodeRelease[]>(
await this.http.getJsonUnchecked<NodeRelease[]>(
joinUrlParts(registryUrl, 'index.json'),
)
).body;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/npm/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function getDependency(
});
}

const raw = await http.getJson<NpmResponse>(packageUrl, options);
const raw = await http.getJsonUnchecked<NpmResponse>(packageUrl, options);
if (cachedResult?.cacheData && raw.statusCode === 304) {
logger.trace(`Cached npm result for ${packageName} is revalidated`);
HttpCacheStats.incRemoteHits(packageUrl);
Expand Down
8 changes: 5 additions & 3 deletions lib/modules/datasource/nuget/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class NugetV3Api {
);
// istanbul ignore else: currently not testable
if (!servicesIndexRaw) {
servicesIndexRaw = (await http.getJson<ServicesIndexRaw>(url)).body;
servicesIndexRaw = (await http.getJsonUnchecked<ServicesIndexRaw>(url))
.body;
await packageCache.set(
NugetV3Api.cacheNamespace,
responseCacheKey,
Expand Down Expand Up @@ -132,7 +133,7 @@ export class NugetV3Api {
let items = catalogPage.items;
if (!items) {
const url = catalogPage['@id'];
const catalogPageFull = await http.getJson<CatalogPage>(url);
const catalogPageFull = await http.getJsonUnchecked<CatalogPage>(url);
items = catalogPageFull.body.items;
}
return items.map(({ catalogEntry }) => catalogEntry);
Expand All @@ -146,7 +147,8 @@ export class NugetV3Api {
): Promise<ReleaseResult | null> {
const baseUrl = feedUrl.replace(regEx(/\/*$/), '');
const url = `${baseUrl}/${pkgName.toLowerCase()}/index.json`;
const packageRegistration = await http.getJson<PackageRegistration>(url);
const packageRegistration =
await http.getJsonUnchecked<PackageRegistration>(url);
const catalogPages = packageRegistration.body.items || [];
const catalogPagesQueue = catalogPages.map(
(page) => (): Promise<CatalogEntry[]> => this.getCatalogEntry(http, page),
Expand Down
10 changes: 5 additions & 5 deletions lib/modules/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export class PackagistDatasource extends Datasource {
return username && password ? { username, password } : {};
}

private async getJson<T, U extends z.ZodSchema<T>>(
private async getJson<Schema extends z.ZodType<any, any, any>>(
url: string,
schema: U,
): Promise<z.infer<typeof schema>> {
schema: Schema,
): Promise<z.infer<Schema>> {
const opts = PackagistDatasource.getHostOpts(url);
const { body } = await this.http.getJson(url, opts);
return schema.parse(body);
const { body } = await this.http.getJson(url, opts, schema);
return body;
}

@cache({
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/pod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class PodDatasource extends Datasource {
packageName: string,
): Promise<T | null> {
try {
const resp = await this.githubHttp.getJson<T>(url);
const resp = await this.githubHttp.getJsonUnchecked<T>(url);
if (resp?.body) {
return resp.body;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/puppet-forge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PuppetForgeDatasource extends Datasource {
let module: PuppetModule;

try {
const response = await this.http.getJson<PuppetModule>(url);
const response = await this.http.getJsonUnchecked<PuppetModule>(url);
module = response.body;
} catch (err) {
this.handleGenericErrors(err);
Expand Down
Loading

0 comments on commit 9b4e515

Please sign in to comment.