Skip to content

Commit

Permalink
refactor(datasources): strict null checks done (#15144)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Apr 16, 2022
1 parent b35cf6e commit 02838fb
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 74 deletions.
6 changes: 4 additions & 2 deletions lib/modules/datasource/go/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export class BaseGoDatasource {
// get server base url from import url
const parsedUrl = URL.parse(goSourceUrl);

const packageName = trimLeadingSlash(parsedUrl.pathname);
// TODO: `parsedUrl.pathname` can be undefined
const packageName = trimLeadingSlash(`${parsedUrl.pathname}`);

const registryUrl = `${parsedUrl.protocol}//${parsedUrl.host}`;

Expand All @@ -150,7 +151,8 @@ export class BaseGoDatasource {
const parsedUrl = URL.parse(goImportURL);

// split the go module from the URL: host/go/module -> go/module
const packageName = trimTrailingSlash(parsedUrl.pathname)
// TODO: `parsedUrl.pathname` can be undefined
const packageName = trimTrailingSlash(`${parsedUrl.pathname}`)
.replace(regEx(/\.git$/), '')
.split('/')
.slice(-2)
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/datasource/go/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export enum GoproxyFallback {
Always = '|',
}

export function getSourceUrl(dataSource?: DataSource): string | undefined {
export function getSourceUrl(
dataSource?: DataSource | null
): string | undefined {
if (dataSource) {
const { datasource, registryUrl, packageName } = dataSource;

Expand Down
10 changes: 5 additions & 5 deletions lib/modules/datasource/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class GoDatasource extends Datasource {
*/
@cache({
namespace: GoDatasource.id,
key: ({ packageName }: Partial<DigestConfig>) => `${packageName}-digest`,
key: ({ packageName }: DigestConfig) => `${packageName}-digest`,
})
override async getDigest(
{ packageName }: Partial<DigestConfig>,
value?: string
{ packageName }: DigestConfig,
value?: string | null
): Promise<string | null> {
const source = await BaseGoDatasource.getDatasource(packageName);
if (!source) {
Expand All @@ -64,10 +64,10 @@ export class GoDatasource extends Datasource {
return this.direct.github.getDigest(source, tag);
}
case BitBucketTagsDatasource.id: {
return this.direct.bitbucket.getDigest(source, tag);
return this.direct.bitbucket.getDigest?.(source, tag) ?? null;
}
case GitlabTagsDatasource.id: {
return this.direct.gitlab.getDigest(source, tag);
return this.direct.gitlab.getDigest?.(source, tag) ?? null;
}
/* istanbul ignore next: can never happen, makes lint happy */
default: {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/go/releases-direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GoDirectDatasource extends Datasource {
async getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
const { packageName } = config;

let res: ReleaseResult = null;
let res: ReleaseResult | null = null;

logger.trace(`go.getReleases(${packageName})`);
const source = await BaseGoDatasource.getDatasource(packageName);
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/go/releases-goproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class GoProxyDatasource extends Datasource {
*
* @see https://golang.org/ref/mod#goproxy-protocol
*/
parseGoproxy(input: string = process.env.GOPROXY): GoproxyItem[] {
parseGoproxy(input: string | undefined = process.env.GOPROXY): GoproxyItem[] {
if (!is.string(input)) {
return [];
}
Expand Down
48 changes: 30 additions & 18 deletions lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const getDatasourceList = (): string[] => Array.from(datasources.keys());

const cacheNamespace = 'datasource-releases';

function getDatasourceFor(datasource: string): DatasourceApi {
return datasources.get(datasource);
function getDatasourceFor(datasource: string): DatasourceApi | null {
return datasources.get(datasource) ?? null;
}

type GetReleasesInternalConfig = GetReleasesConfig & GetPkgReleasesConfig;
Expand All @@ -57,7 +57,7 @@ async function getRegistryReleases(
datasource: DatasourceApi,
config: GetReleasesConfig,
registryUrl: string
): Promise<ReleaseResult> {
): Promise<ReleaseResult | null> {
const cacheKey = `${datasource.id} ${registryUrl} ${config.packageName}`;
if (datasource.caching) {
const cachedResult = await packageCache.get<ReleaseResult>(
Expand Down Expand Up @@ -87,7 +87,7 @@ function firstRegistry(
config: GetReleasesInternalConfig,
datasource: DatasourceApi,
registryUrls: string[]
): Promise<ReleaseResult> {
): Promise<ReleaseResult | null> {
if (registryUrls.length > 1) {
logger.warn(
{ datasource: datasource.id, depName: config.depName, registryUrls },
Expand All @@ -102,9 +102,9 @@ async function huntRegistries(
config: GetReleasesInternalConfig,
datasource: DatasourceApi,
registryUrls: string[]
): Promise<ReleaseResult> {
let res: ReleaseResult;
let caughtError: Error;
): Promise<ReleaseResult | null> {
let res: ReleaseResult | null = null;
let caughtError: Error | undefined;
for (const registryUrl of registryUrls) {
try {
res = await getRegistryReleases(datasource, config, registryUrl);
Expand Down Expand Up @@ -133,9 +133,9 @@ async function mergeRegistries(
config: GetReleasesInternalConfig,
datasource: DatasourceApi,
registryUrls: string[]
): Promise<ReleaseResult> {
let combinedRes: ReleaseResult;
let caughtError: Error;
): Promise<ReleaseResult | null> {
let combinedRes: ReleaseResult | undefined;
let caughtError: Error | undefined;
for (const registryUrl of registryUrls) {
try {
const res = await getRegistryReleases(datasource, config, registryUrl);
Expand Down Expand Up @@ -189,8 +189,8 @@ function massageRegistryUrls(registryUrls: string[]): string[] {

function resolveRegistryUrls(
datasource: DatasourceApi,
defaultRegistryUrls: string[],
registryUrls: string[]
defaultRegistryUrls: string[] | undefined,
registryUrls: string[] | undefined
): string[] {
if (!datasource.customRegistrySupport) {
if (
Expand Down Expand Up @@ -242,6 +242,7 @@ async function fetchReleases(
): Promise<ReleaseResult | null> {
const { datasource: datasourceName } = config;
let { registryUrls } = config;
// istanbul ignore if: need test
if (!datasourceName || getDatasourceFor(datasourceName) === undefined) {
logger.warn('Unknown datasource: ' + datasourceName);
return null;
Expand All @@ -255,12 +256,16 @@ async function fetchReleases(
}
}
const datasource = getDatasourceFor(datasourceName);
// istanbul ignore if: needs test
if (!datasource) {
return null;
}
registryUrls = resolveRegistryUrls(
datasource,
config.defaultRegistryUrls,
registryUrls
);
let dep: ReleaseResult = null;
let dep: ReleaseResult | null = null;
const registryStrategy = datasource.registryStrategy || 'hunt';
try {
if (is.nonEmptyArray(registryUrls)) {
Expand Down Expand Up @@ -350,7 +355,7 @@ export async function getPkgReleases(
}
return null; // filter out any we can't extract
})
.filter(Boolean);
.filter(is.truthy);
}
// Use the datasource's default versioning if none is configured
const versioning =
Expand All @@ -377,11 +382,12 @@ export async function getPkgReleases(
// TODO: Support range/range compatibility filtering #8476
if (version.isVersion(constraintValue)) {
res.releases = res.releases.filter((release) => {
if (!is.nonEmptyArray(release.constraints?.[constraintName])) {
const constraint = release.constraints?.[constraintName];
if (!is.nonEmptyArray(constraint)) {
// A release with no constraints is OK
return true;
}
return release.constraints[constraintName].some(
return constraint.some(
// If any of the release's constraints match, then it's OK
(releaseConstraint) =>
!releaseConstraint ||
Expand All @@ -398,7 +404,8 @@ export async function getPkgReleases(
}

export function supportsDigests(datasource: string | undefined): boolean {
return !!datasource && 'getDigest' in getDatasourceFor(datasource);
const ds = !!datasource && getDatasourceFor(datasource);
return !!ds && 'getDigest' in ds;
}

function getDigestConfig(
Expand All @@ -420,8 +427,13 @@ export function getDigest(
value?: string
): Promise<string | null> {
const datasource = getDatasourceFor(config.datasource);
// istanbul ignore if: need test
if (!datasource || !('getDigest' in datasource)) {
return Promise.resolve(null);
}
const digestConfig = getDigestConfig(datasource, config);
return datasource.getDigest(digestConfig, value);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return datasource.getDigest!(digestConfig, value);
}

export function getDefaultConfig(
Expand Down
30 changes: 22 additions & 8 deletions lib/modules/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ export class PackagistDatasource extends Datasource {
public override getReleases({
packageName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult> {
}: GetReleasesConfig): Promise<ReleaseResult | null> {
logger.trace(`getReleases(${packageName})`);
// istanbul ignore if
if (!registryUrl) {
return Promise.resolve(null);
}
return this.packageLookup(registryUrl, packageName);
}

Expand All @@ -59,8 +63,8 @@ export class PackagistDatasource extends Datasource {
const res = (await this.http.getJson<PackageMeta>(url, opts)).body;
const meta: RegistryMeta = {
providerPackages: {},
packages: res.packages,
};
meta.packages = res.packages;
if (res.includes) {
meta.includesFiles = [];
for (const [name, val] of Object.entries(res.includes)) {
Expand Down Expand Up @@ -132,14 +136,14 @@ export class PackagistDatasource extends Datasource {
}

private static extractDepReleases(versions: RegistryFile): ReleaseResult {
const dep: ReleaseResult = { releases: null };
const dep: ReleaseResult = { releases: [] };
// istanbul ignore if
if (!versions) {
dep.releases = [];
return dep;
}
dep.releases = Object.keys(versions).map((version) => {
const release = versions[version];
// TODO: fix function parameter type: `versions`
const release = (versions as any)[version];
const parsedVersion = release.version ?? version;
dep.homepage = release.homepage || dep.homepage;
if (release.source?.url) {
Expand All @@ -160,6 +164,11 @@ export class PackagistDatasource extends Datasource {
})
async getAllPackages(regUrl: string): Promise<AllPackages | null> {
const registryMeta = await this.getRegistryMeta(regUrl);
// istanbul ignore if: needs test
if (!registryMeta) {
return null;
}

const {
packages,
providersUrl,
Expand Down Expand Up @@ -202,7 +211,7 @@ export class PackagistDatasource extends Datasource {
return allPackages;
}

async packagistOrgLookup(name: string): Promise<ReleaseResult> {
async packagistOrgLookup(name: string): Promise<ReleaseResult | null> {
const cacheNamespace = 'datasource-packagist-org';
const cachedResult = await packageCache.get<ReleaseResult>(
cacheNamespace,
Expand All @@ -212,7 +221,7 @@ export class PackagistDatasource extends Datasource {
if (cachedResult) {
return cachedResult;
}
let dep: ReleaseResult = null;
let dep: ReleaseResult | null = null;
const regUrl = 'https://packagist.org';
const pkgUrl = [
joinUrlParts(regUrl, `/p2/${name}.json`),
Expand Down Expand Up @@ -243,6 +252,10 @@ export class PackagistDatasource extends Datasource {
return packagistResult;
}
const allPackages = await this.getAllPackages(regUrl);
// istanbul ignore if: needs test
if (!allPackages) {
return null;
}
const {
packages,
providersUrl,
Expand All @@ -261,7 +274,8 @@ export class PackagistDatasource extends Datasource {
if (name in providerPackages) {
pkgUrl = URL.resolve(
regUrl,
providersUrl
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
providersUrl!
.replace('%package%', name)
.replace('%hash%', providerPackages[name])
);
Expand Down
7 changes: 4 additions & 3 deletions lib/modules/datasource/packagist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface PackageMeta {
packages: Record<string, RegistryFile>;
'provider-includes': Record<string, { sha256: string }>;
providers: Record<string, { sha256: string }>;
'providers-lazy-url'?: string;
'providers-url'?: string;
}
export interface RegistryFile {
Expand All @@ -17,7 +18,7 @@ export interface RegistryMeta {
providersUrl?: string;
providersLazyUrl?: string;
includesFiles?: RegistryFile[];
packages?: Record<string, RegistryFile>;
packages: Record<string, RegistryFile>;
}

export interface PackagistFile {
Expand All @@ -27,8 +28,8 @@ export interface PackagistFile {

export interface AllPackages {
packages: Record<string, RegistryFile>;
providersUrl: string;
providersLazyUrl: string;
providersUrl?: string;
providersLazyUrl?: string;
providerPackages: Record<string, string>;

includesPackages: Record<string, ReleaseResult>;
Expand Down

0 comments on commit 02838fb

Please sign in to comment.