Select Git revision
get.ts 7.15 KiB
import url from 'url';
import is from '@sindresorhus/is';
import { DateTime } from 'luxon';
import { GlobalConfig } from '../../../config/global';
import { HOST_DISABLED } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import * as packageCache from '../../../util/cache/package';
import type { Http } from '../../../util/http';
import type { HttpOptions } from '../../../util/http/types';
import { regEx } from '../../../util/regex';
import { joinUrlParts } from '../../../util/url';
import { id } from './common';
import type {
CachedNpmDependency,
NpmDependency,
NpmRelease,
NpmResponse,
} from './types';
interface PackageSource {
sourceUrl?: string;
sourceDirectory?: string;
}
const SHORT_REPO_REGEX = regEx(
/^((?<platform>bitbucket|github|gitlab):)?(?<shortRepo>[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)$/
);
const platformMapping: Record<string, string> = {
bitbucket: 'https://bitbucket.org/',
github: 'https://github.com/',
gitlab: 'https://gitlab.com/',
};
function getPackageSource(repository: any): PackageSource {
const res: PackageSource = {};
if (repository) {
if (is.nonEmptyString(repository)) {
const shortMatch = repository.match(SHORT_REPO_REGEX);
if (shortMatch?.groups) {
const { platform = 'github', shortRepo } = shortMatch.groups;
res.sourceUrl = platformMapping[platform] + shortRepo;
} else {
res.sourceUrl = repository;
}
} else if (is.nonEmptyString(repository.url)) {
res.sourceUrl = repository.url;
}
if (is.nonEmptyString(repository.directory)) {
res.sourceDirectory = repository.directory;
}
}
return res;
}
export async function getDependency(
http: Http,
registryUrl: string,
packageName: string
): Promise<NpmDependency | null> {
logger.trace(`npm.getDependency(${packageName})`);
const packageUrl = joinUrlParts(registryUrl, packageName.replace('/', '%2F'));
// Now check the persistent cache
const cacheNamespace = 'datasource-npm';
const cachedResult = await packageCache.get<CachedNpmDependency>(
cacheNamespace,
packageUrl