From 0ae8cc2e894d44bcb7b71e3e1ae2008a60a3123c Mon Sep 17 00:00:00 2001 From: Sergio Zharinov <zharinov@users.noreply.github.com> Date: Thu, 27 Aug 2020 11:07:58 +0400 Subject: [PATCH] refactor(datasource): Fix lint warnings (#7115) --- lib/datasource/common.ts | 4 ++-- lib/datasource/docker/index.spec.ts | 6 ++--- lib/datasource/docker/index.ts | 4 ++-- lib/datasource/galaxy/index.ts | 9 +++---- lib/datasource/index.ts | 15 ++++++------ lib/datasource/npm/get.ts | 26 +++++++++++++++++++-- lib/datasource/npm/npmrc.ts | 2 +- lib/datasource/nuget/v3.ts | 2 +- lib/datasource/pypi/index.ts | 6 ++--- lib/datasource/repology/index.spec.ts | 6 ++--- lib/datasource/rubygems/get-rubygems-org.ts | 10 ++++---- 11 files changed, 55 insertions(+), 35 deletions(-) diff --git a/lib/datasource/common.ts b/lib/datasource/common.ts index 47090ef876..c9355b9cbb 100644 --- a/lib/datasource/common.ts +++ b/lib/datasource/common.ts @@ -28,7 +28,7 @@ export interface GetPkgReleasesConfig extends ReleasesConfigBase { } export function isGetPkgReleasesConfig( - input: any + input: unknown ): input is GetPkgReleasesConfig { return ( (input as GetPkgReleasesConfig).datasource !== undefined && @@ -75,7 +75,7 @@ export interface DatasourceApi { getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null>; defaultRegistryUrls?: string[]; appendRegistryUrls?: string[]; - defaultConfig?: object; + defaultConfig?: Record<string, unknown>; registryStrategy?: 'first' | 'hunt' | 'merge'; } diff --git a/lib/datasource/docker/index.spec.ts b/lib/datasource/docker/index.spec.ts index b6f5b38d80..aeedd05c80 100644 --- a/lib/datasource/docker/index.spec.ts +++ b/lib/datasource/docker/index.spec.ts @@ -230,7 +230,7 @@ describe(getName(__filename), () => { AWSMock.mock( 'ECR', 'getAuthorizationToken', - (params: {}, callback: Function) => { + (params: unknown, callback: (...unknown) => void) => { callback(null, { authorizationData: [{ authorizationToken: 'abcdef' }], }); @@ -262,7 +262,7 @@ describe(getName(__filename), () => { AWSMock.mock( 'ECR', 'getAuthorizationToken', - (params: {}, callback: Function) => { + (params: unknown, callback: (...unknown) => void) => { callback(null, {}); } ); @@ -291,7 +291,7 @@ describe(getName(__filename), () => { AWSMock.mock( 'ECR', 'getAuthorizationToken', - (params: {}, callback: Function) => { + (params: unknown, callback: (...unknown) => void) => { callback(Error('some error'), null); } ); diff --git a/lib/datasource/docker/index.ts b/lib/datasource/docker/index.ts index eed90202f8..a1ffbd65b1 100644 --- a/lib/datasource/docker/index.ts +++ b/lib/datasource/docker/index.ts @@ -175,7 +175,7 @@ async function getAuthHeaders( } // prettier-ignore - const authUrl = `${authenticateHeader.parms.realm}?service=${authenticateHeader.parms.service}&scope=repository:${repository}:pull`; + const authUrl = `${String(authenticateHeader.parms.realm)}?service=${String(authenticateHeader.parms.service)}&scope=repository:${repository}:pull`; logger.trace( `Obtaining docker registry token for ${repository} using url ${authUrl}` ); @@ -497,7 +497,7 @@ async function getLabels( return {}; } let labels: Record<string, string> = {}; - const configDigest = manifest.config.digest; + const configDigest: string = manifest.config.digest; const headers = await getAuthHeaders(registry, repository); // istanbul ignore if: Should never be happen if (!headers) { diff --git a/lib/datasource/galaxy/index.ts b/lib/datasource/galaxy/index.ts index 48ef50b7c7..5f1e856d49 100644 --- a/lib/datasource/galaxy/index.ts +++ b/lib/datasource/galaxy/index.ts @@ -71,12 +71,9 @@ export async function getReleases({ }; result.dependencyUrl = galaxyProjectUrl; - if (resultObject.github_user && resultObject.github_repo) { - result.sourceUrl = - 'https://github.com/' + - resultObject.github_user + - '/' + - resultObject.github_repo; + const { github_user: user = null, github_repo: repo = null } = resultObject; + if (typeof user === 'string' && typeof repo === 'string') { + result.sourceUrl = `https://github.com/${user}/${repo}`; } result.releases = versions.map( diff --git a/lib/datasource/index.ts b/lib/datasource/index.ts index dbae11adc2..24fbc2d4d7 100644 --- a/lib/datasource/index.ts +++ b/lib/datasource/index.ts @@ -214,11 +214,10 @@ async function fetchReleases( function getRawReleases( config: GetReleasesInternalConfig ): Promise<ReleaseResult | null> { - const cacheKey = - cacheNamespace + - config.datasource + - config.lookupName + - config.registryUrls; + const { datasource, lookupName, registryUrls } = config; + const cacheKey = `${cacheNamespace}${datasource}${lookupName}${String( + registryUrls + )}`; // By returning a Promise and reusing it, we should only fetch each package at most once const cachedResult = memCache.get(cacheKey); // istanbul ignore if @@ -291,7 +290,9 @@ export function getDigest( ); } -export function getDefaultConfig(datasource: string): Promise<object> { +export function getDefaultConfig( + datasource: string +): Promise<Record<string, unknown>> { const loadedDatasource = load(datasource); - return Promise.resolve(loadedDatasource?.defaultConfig || {}); + return Promise.resolve(loadedDatasource?.defaultConfig || Object.create({})); } diff --git a/lib/datasource/npm/get.ts b/lib/datasource/npm/get.ts index 76e38c0ec8..08ca613f5a 100644 --- a/lib/datasource/npm/get.ts +++ b/lib/datasource/npm/get.ts @@ -45,6 +45,29 @@ export interface NpmDependency extends ReleaseResult { sourceDirectory?: string; } +interface NpmResponse { + _id: string; + name?: string; + versions?: Record< + string, + { + repository?: { + url: string; + directory: string; + }; + homepage?: string; + deprecated?: boolean; + gitHead?: string; + } + >; + repository?: { + url?: string; + directory?: string; + }; + homepage?: string; + time?: Record<string, string>; +} + export async function getDependency( packageName: string, retries = 3 @@ -135,8 +158,7 @@ export async function getDependency( headers, useCache, }; - // TODO: fix type - const raw = await http.getJson<any>(pkgUrl, opts); + const raw = await http.getJson<NpmResponse>(pkgUrl, opts); if (retries < 3) { logger.debug({ pkgUrl, retries }, 'Recovered from npm error'); } diff --git a/lib/datasource/npm/npmrc.ts b/lib/datasource/npm/npmrc.ts index c383519ae4..dcc7e28c17 100644 --- a/lib/datasource/npm/npmrc.ts +++ b/lib/datasource/npm/npmrc.ts @@ -39,7 +39,7 @@ function sanitize(key: string, val: string): void { add(val); const password = Buffer.from(val, 'base64').toString(); add(password); - const username = npmrc[key.replace(':_password', ':username')]; + const username: string = npmrc[key.replace(':_password', ':username')]; add(Buffer.from(`${username}:${password}`).toString('base64')); } } diff --git a/lib/datasource/nuget/v3.ts b/lib/datasource/nuget/v3.ts index d94c3c002e..52db9e9d4a 100644 --- a/lib/datasource/nuget/v3.ts +++ b/lib/datasource/nuget/v3.ts @@ -133,7 +133,7 @@ export async function getReleases( ).flat(); let homepage = null; - let latestStable = null; + let latestStable: string = null; const releases = catalogEntries.map( ({ version, published: releaseTimestamp, projectUrl }) => { const release: Release = { version }; diff --git a/lib/datasource/pypi/index.ts b/lib/datasource/pypi/index.ts index 76021275ad..d27b3f9d6f 100644 --- a/lib/datasource/pypi/index.ts +++ b/lib/datasource/pypi/index.ts @@ -14,7 +14,7 @@ export const defaultRegistryUrls = [ ]; export const registryStrategy = 'merge'; -const github_repo_pattern = /^https?:\/\/github\.com\/[^\\/]+\/[^\\/]+$/; +const githubRepoPattern = /^https?:\/\/github\.com\/[^\\/]+\/[^\\/]+$/; const http = new Http(id); type PypiJSONRelease = { @@ -82,7 +82,7 @@ async function getDependency( if (dep.info?.home_page) { dependency.homepage = dep.info.home_page; - if (github_repo_pattern.exec(dep.info.home_page)) { + if (githubRepoPattern.exec(dep.info.home_page)) { dependency.sourceUrl = dep.info.home_page.replace('http://', 'https://'); } } @@ -96,7 +96,7 @@ async function getDependency( (lower.startsWith('repo') || lower === 'code' || lower === 'source' || - github_repo_pattern.exec(projectUrl)) + githubRepoPattern.exec(projectUrl)) ) { dependency.sourceUrl = projectUrl; } diff --git a/lib/datasource/repology/index.spec.ts b/lib/datasource/repology/index.spec.ts index bd08b9d04e..844388c4c4 100644 --- a/lib/datasource/repology/index.spec.ts +++ b/lib/datasource/repology/index.spec.ts @@ -8,13 +8,13 @@ import { RepologyPackage, id as datasource } from '.'; const repologyApiHost = 'https://repology.org/'; -type mockResponse = { status: number; body?: string }; +type ResponseMock = { status: number; body?: string }; const mockProjectBy = ( repo: string, name: string, - binary: mockResponse, - source: mockResponse + binary: ResponseMock, + source: ResponseMock ) => { const endpoint = '/tools/project-by'; const defaultParams = { diff --git a/lib/datasource/rubygems/get-rubygems-org.ts b/lib/datasource/rubygems/get-rubygems-org.ts index 05dbf41e5a..4bd78ff8bc 100644 --- a/lib/datasource/rubygems/get-rubygems-org.ts +++ b/lib/datasource/rubygems/get-rubygems-org.ts @@ -94,15 +94,15 @@ function isDataStale(): boolean { return minutesElapsed >= 5; } -let _updateRubyGemsVersions: Promise<void> | undefined; +let updateRubyGemsVersionsPromise: Promise<void> | undefined; async function syncVersions(): Promise<void> { if (isDataStale()) { - _updateRubyGemsVersions = + updateRubyGemsVersionsPromise = // eslint-disable-next-line @typescript-eslint/no-misused-promises - _updateRubyGemsVersions || updateRubyGemsVersions(); - await _updateRubyGemsVersions; - _updateRubyGemsVersions = null; + updateRubyGemsVersionsPromise || updateRubyGemsVersions(); + await updateRubyGemsVersionsPromise; + updateRubyGemsVersionsPromise = null; } } -- GitLab