refactor(datasource/nuget): move v2/v3 API logic to classes (#28117)

This commit is contained in:
Florian Greinacher 2024-03-27 10:49:05 +01:00 committed by GitHub
parent fde2dff36d
commit 87bba9d31a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 288 additions and 262 deletions

View file

@ -1,6 +1,6 @@
import { sortNugetVersions } from './v3'; import { sortNugetVersions } from './common';
describe('modules/datasource/nuget/v3', () => { describe('modules/datasource/nuget/common', () => {
it.each<{ version: string; other: string; result: number }>` it.each<{ version: string; other: string; result: number }>`
version | other | result version | other | result
${'invalid1'} | ${'invalid2'} | ${0} ${'invalid1'} | ${'invalid2'} | ${0}

View file

@ -1,6 +1,7 @@
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import { parseUrl } from '../../../util/url'; import { parseUrl } from '../../../util/url';
import { api as versioning } from '../../versioning/nuget';
import type { ParsedRegistryUrl } from './types'; import type { ParsedRegistryUrl } from './types';
const buildMetaRe = regEx(/\+.+$/g); const buildMetaRe = regEx(/\+.+$/g);
@ -47,3 +48,23 @@ export function parseRegistryUrl(registryUrl: string): ParsedRegistryUrl {
const feedUrl = parsedUrl.href; const feedUrl = parsedUrl.href;
return { feedUrl, protocolVersion }; return { feedUrl, protocolVersion };
} }
/**
* Compare two versions. Return:
* - `1` if `a > b` or `b` is invalid
* - `-1` if `a < b` or `a` is invalid
* - `0` if `a == b` or both `a` and `b` are invalid
*/
export function sortNugetVersions(a: string, b: string): number {
if (versioning.isValid(a)) {
if (versioning.isValid(b)) {
return versioning.sortVersions(a, b);
} else {
return 1;
}
} else if (versioning.isValid(b)) {
return -1;
} else {
return 0;
}
}

View file

@ -3,8 +3,8 @@ import * as nugetVersioning from '../../versioning/nuget';
import { Datasource } from '../datasource'; import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types'; import type { GetReleasesConfig, ReleaseResult } from '../types';
import { parseRegistryUrl } from './common'; import { parseRegistryUrl } from './common';
import * as v2 from './v2'; import { NugetV2Api } from './v2';
import * as v3 from './v3'; import { NugetV3Api } from './v3';
// https://api.nuget.org/v3/index.json is a default official nuget feed // https://api.nuget.org/v3/index.json is a default official nuget feed
export const nugetOrg = 'https://api.nuget.org/v3/index.json'; export const nugetOrg = 'https://api.nuget.org/v3/index.json';
@ -18,6 +18,10 @@ export class NugetDatasource extends Datasource {
override readonly registryStrategy = 'merge'; override readonly registryStrategy = 'merge';
readonly v2Api = new NugetV2Api();
readonly v3Api = new NugetV3Api();
constructor() { constructor() {
super(NugetDatasource.id); super(NugetDatasource.id);
} }
@ -33,12 +37,17 @@ export class NugetDatasource extends Datasource {
} }
const { feedUrl, protocolVersion } = parseRegistryUrl(registryUrl); const { feedUrl, protocolVersion } = parseRegistryUrl(registryUrl);
if (protocolVersion === 2) { if (protocolVersion === 2) {
return v2.getReleases(this.http, feedUrl, packageName); return this.v2Api.getReleases(this.http, feedUrl, packageName);
} }
if (protocolVersion === 3) { if (protocolVersion === 3) {
const queryUrl = await v3.getResourceUrl(this.http, feedUrl); const queryUrl = await this.v3Api.getResourceUrl(this.http, feedUrl);
if (queryUrl) { if (queryUrl) {
return v3.getReleases(this.http, feedUrl, queryUrl, packageName); return this.v3Api.getReleases(
this.http,
feedUrl,
queryUrl,
packageName,
);
} }
} }
return null; return null;

View file

@ -1,16 +1,16 @@
import { XmlDocument, XmlElement } from 'xmldoc'; import { XmlDocument, XmlElement } from 'xmldoc';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import type { Http } from '../../../util/http'; import type { Http } from '../../../util/http';
import type { HttpResponse } from '../../../util/http/types';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import type { ReleaseResult } from '../types'; import type { ReleaseResult } from '../types';
import { massageUrl, removeBuildMeta } from './common'; import { massageUrl, removeBuildMeta } from './common';
function getPkgProp(pkgInfo: XmlElement, propName: string): string | undefined { export class NugetV2Api {
getPkgProp(pkgInfo: XmlElement, propName: string): string | undefined {
return pkgInfo.childNamed('m:properties')?.childNamed(`d:${propName}`)?.val; return pkgInfo.childNamed('m:properties')?.childNamed(`d:${propName}`)?.val;
} }
export async function getReleases( async getReleases(
http: Http, http: Http,
feedUrl: string, feedUrl: string,
pkgName: string, pkgName: string,
@ -24,24 +24,27 @@ export async function getReleases(
)}/FindPackagesById()?id=%27${pkgName}%27&$select=Version,IsLatestVersion,ProjectUrl,Published`; )}/FindPackagesById()?id=%27${pkgName}%27&$select=Version,IsLatestVersion,ProjectUrl,Published`;
while (pkgUrlList !== null) { while (pkgUrlList !== null) {
// typescript issue // typescript issue
const pkgVersionsListRaw: HttpResponse<string> = await http.get(pkgUrlList); const pkgVersionsListRaw = await http.get(pkgUrlList);
const pkgVersionsListDoc = new XmlDocument(pkgVersionsListRaw.body); const pkgVersionsListDoc = new XmlDocument(pkgVersionsListRaw.body);
const pkgInfoList = pkgVersionsListDoc.childrenNamed('entry'); const pkgInfoList = pkgVersionsListDoc.childrenNamed('entry');
for (const pkgInfo of pkgInfoList) { for (const pkgInfo of pkgInfoList) {
const version = getPkgProp(pkgInfo, 'Version'); const version = this.getPkgProp(pkgInfo, 'Version');
const releaseTimestamp = getPkgProp(pkgInfo, 'Published'); const releaseTimestamp = this.getPkgProp(pkgInfo, 'Published');
dep.releases.push({ dep.releases.push({
// TODO: types (#22198) // TODO: types (#22198)
version: removeBuildMeta(`${version}`), version: removeBuildMeta(`${version}`),
releaseTimestamp, releaseTimestamp,
}); });
try { try {
const pkgIsLatestVersion = getPkgProp(pkgInfo, 'IsLatestVersion'); const pkgIsLatestVersion = this.getPkgProp(
pkgInfo,
'IsLatestVersion',
);
if (pkgIsLatestVersion === 'true') { if (pkgIsLatestVersion === 'true') {
dep['tags'] = { latest: removeBuildMeta(`${version}`) }; dep['tags'] = { latest: removeBuildMeta(`${version}`) };
const projectUrl = getPkgProp(pkgInfo, 'ProjectUrl'); const projectUrl = this.getPkgProp(pkgInfo, 'ProjectUrl');
if (projectUrl) { if (projectUrl) {
dep.sourceUrl = massageUrl(projectUrl); dep.sourceUrl = massageUrl(projectUrl);
} }
@ -68,3 +71,4 @@ export async function getReleases(
return dep; return dep;
} }
}

View file

@ -10,7 +10,7 @@ import { regEx } from '../../../util/regex';
import { ensureTrailingSlash } from '../../../util/url'; import { ensureTrailingSlash } from '../../../util/url';
import { api as versioning } from '../../versioning/nuget'; import { api as versioning } from '../../versioning/nuget';
import type { Release, ReleaseResult } from '../types'; import type { Release, ReleaseResult } from '../types';
import { massageUrl, removeBuildMeta } from './common'; import { massageUrl, removeBuildMeta, sortNugetVersions } from './common';
import type { import type {
CatalogEntry, CatalogEntry,
CatalogPage, CatalogPage,
@ -18,9 +18,10 @@ import type {
ServicesIndexRaw, ServicesIndexRaw,
} from './types'; } from './types';
const cacheNamespace = 'datasource-nuget'; export class NugetV3Api {
static readonly cacheNamespace = 'datasource-nuget';
export async function getResourceUrl( async getResourceUrl(
http: Http, http: Http,
url: string, url: string,
resourceType = 'RegistrationsBaseUrl', resourceType = 'RegistrationsBaseUrl',
@ -28,7 +29,7 @@ export async function getResourceUrl(
// https://docs.microsoft.com/en-us/nuget/api/service-index // https://docs.microsoft.com/en-us/nuget/api/service-index
const resultCacheKey = `${url}:${resourceType}`; const resultCacheKey = `${url}:${resourceType}`;
const cachedResult = await packageCache.get<string>( const cachedResult = await packageCache.get<string>(
cacheNamespace, NugetV3Api.cacheNamespace,
resultCacheKey, resultCacheKey,
); );
@ -40,14 +41,14 @@ export async function getResourceUrl(
try { try {
const responseCacheKey = url; const responseCacheKey = url;
servicesIndexRaw = await packageCache.get<ServicesIndexRaw>( servicesIndexRaw = await packageCache.get<ServicesIndexRaw>(
cacheNamespace, NugetV3Api.cacheNamespace,
responseCacheKey, responseCacheKey,
); );
// istanbul ignore else: currently not testable // istanbul ignore else: currently not testable
if (!servicesIndexRaw) { if (!servicesIndexRaw) {
servicesIndexRaw = (await http.getJson<ServicesIndexRaw>(url)).body; servicesIndexRaw = (await http.getJson<ServicesIndexRaw>(url)).body;
await packageCache.set( await packageCache.set(
cacheNamespace, NugetV3Api.cacheNamespace,
responseCacheKey, responseCacheKey,
servicesIndexRaw, servicesIndexRaw,
3 * 24 * 60, 3 * 24 * 60,
@ -70,7 +71,12 @@ export async function getResourceUrl(
); );
if (services.length === 0) { if (services.length === 0) {
await packageCache.set(cacheNamespace, resultCacheKey, null, 60); await packageCache.set(
NugetV3Api.cacheNamespace,
resultCacheKey,
null,
60,
);
logger.debug( logger.debug(
{ url, servicesIndexRaw }, { url, servicesIndexRaw },
`no ${resourceType} services found`, `no ${resourceType} services found`,
@ -93,7 +99,12 @@ export async function getResourceUrl(
); );
} }
await packageCache.set(cacheNamespace, resultCacheKey, serviceId, 60); await packageCache.set(
NugetV3Api.cacheNamespace,
resultCacheKey,
serviceId,
60,
);
return serviceId; return serviceId;
} catch (err) { } catch (err) {
// istanbul ignore if: not easy testable with nock // istanbul ignore if: not easy testable with nock
@ -108,7 +119,7 @@ export async function getResourceUrl(
} }
} }
async function getCatalogEntry( async getCatalogEntry(
http: Http, http: Http,
catalogPage: CatalogPage, catalogPage: CatalogPage,
): Promise<CatalogEntry[]> { ): Promise<CatalogEntry[]> {
@ -121,27 +132,7 @@ async function getCatalogEntry(
return items.map(({ catalogEntry }) => catalogEntry); return items.map(({ catalogEntry }) => catalogEntry);
} }
/** async getReleases(
* Compare two versions. Return:
* - `1` if `a > b` or `b` is invalid
* - `-1` if `a < b` or `a` is invalid
* - `0` if `a == b` or both `a` and `b` are invalid
*/
export function sortNugetVersions(a: string, b: string): number {
if (versioning.isValid(a)) {
if (versioning.isValid(b)) {
return versioning.sortVersions(a, b);
} else {
return 1;
}
} else if (versioning.isValid(b)) {
return -1;
} else {
return 0;
}
}
export async function getReleases(
http: Http, http: Http,
registryUrl: string, registryUrl: string,
feedUrl: string, feedUrl: string,
@ -152,7 +143,7 @@ export async function getReleases(
const packageRegistration = await http.getJson<PackageRegistration>(url); const packageRegistration = await http.getJson<PackageRegistration>(url);
const catalogPages = packageRegistration.body.items || []; const catalogPages = packageRegistration.body.items || [];
const catalogPagesQueue = catalogPages.map( const catalogPagesQueue = catalogPages.map(
(page) => (): Promise<CatalogEntry[]> => getCatalogEntry(http, page), (page) => (): Promise<CatalogEntry[]> => this.getCatalogEntry(http, page),
); );
const catalogEntries = (await p.all(catalogPagesQueue)) const catalogEntries = (await p.all(catalogPagesQueue))
.flat() .flat()
@ -193,7 +184,7 @@ export async function getReleases(
}; };
try { try {
const packageBaseAddress = await getResourceUrl( const packageBaseAddress = await this.getResourceUrl(
http, http,
registryUrl, registryUrl,
'PackageBaseAddress', 'PackageBaseAddress',
@ -242,3 +233,4 @@ export async function getReleases(
return dep; return dep;
} }
}