-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(datasource/nuget): Convert to class (#14140)
* refactor(datasource/nuget): Convert to class * Fix strict nulls and obsolete URL * Fixes * Fix mutability Co-authored-by: Rhys Arkins <rhys@arkins.net> Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
- Loading branch information
1 parent
f72517d
commit b0ce30b
Showing
15 changed files
with
131 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
import { logger } from '../../logger'; | ||
import { regEx } from '../../util/regex'; | ||
|
||
export const id = 'nuget'; | ||
import { parseUrl } from '../../util/url'; | ||
import type { ParsedRegistryUrl } from './types'; | ||
|
||
const buildMetaRe = regEx(/\+.+$/g); | ||
|
||
export function removeBuildMeta(version: string): string { | ||
return version?.replace(buildMetaRe, ''); | ||
} | ||
|
||
const protocolVersionRegExp = regEx(/#protocolVersion=(?<protocol>2|3)/); | ||
|
||
export function parseRegistryUrl(registryUrl: string): ParsedRegistryUrl { | ||
const parsedUrl = parseUrl(registryUrl); | ||
if (!parsedUrl) { | ||
logger.debug( | ||
{ urL: registryUrl }, | ||
`nuget registry failure: can't parse ${registryUrl}` | ||
); | ||
return { feedUrl: registryUrl, protocolVersion: null }; | ||
} | ||
let protocolVersion = 2; | ||
const protocolVersionMatch = protocolVersionRegExp.exec( | ||
parsedUrl.hash | ||
)?.groups; | ||
if (protocolVersionMatch) { | ||
const { protocol } = protocolVersionMatch; | ||
parsedUrl.hash = ''; | ||
protocolVersion = Number.parseInt(protocol, 10); | ||
} else if (parsedUrl.pathname.endsWith('.json')) { | ||
protocolVersion = 3; | ||
} | ||
|
||
const feedUrl = parsedUrl.href; | ||
return { feedUrl, protocolVersion }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,42 @@ | ||
import urlApi from 'url'; | ||
import { logger } from '../../logger'; | ||
import { regEx } from '../../util/regex'; | ||
import * as nugetVersioning from '../../versioning/nuget'; | ||
import { Datasource } from '../datasource'; | ||
import type { GetReleasesConfig, ReleaseResult } from '../types'; | ||
import { parseRegistryUrl } from './common'; | ||
import * as v2 from './v2'; | ||
import * as v3 from './v3'; | ||
|
||
export { id } from './common'; | ||
// https://api.nuget.org/v3/index.json is a default official nuget feed | ||
export const defaultRegistryUrls = ['https://api.nuget.org/v3/index.json']; | ||
|
||
export const customRegistrySupport = true; | ||
export const defaultRegistryUrls = [v3.getDefaultFeed()]; | ||
export const defaultVersioning = nugetVersioning.id; | ||
export const registryStrategy = 'merge'; | ||
export class NugetDatasource extends Datasource { | ||
static readonly id = 'nuget'; | ||
|
||
export function parseRegistryUrl(registryUrl: string): { | ||
feedUrl: string; | ||
protocolVersion: number; | ||
} { | ||
try { | ||
const parsedUrl = urlApi.parse(registryUrl); | ||
let protocolVersion = 2; | ||
const protocolVersionRegExp = regEx(/#protocolVersion=(2|3)/); | ||
const protocolVersionMatch = protocolVersionRegExp.exec(parsedUrl.hash); | ||
if (protocolVersionMatch) { | ||
parsedUrl.hash = ''; | ||
protocolVersion = Number.parseInt(protocolVersionMatch[1], 10); | ||
} else if (parsedUrl.pathname.endsWith('.json')) { | ||
protocolVersion = 3; | ||
} | ||
return { feedUrl: urlApi.format(parsedUrl), protocolVersion }; | ||
} catch (err) { | ||
logger.debug({ err }, `nuget registry failure: can't parse ${registryUrl}`); | ||
return { feedUrl: registryUrl, protocolVersion: null }; | ||
} | ||
} | ||
override readonly defaultRegistryUrls = defaultRegistryUrls; | ||
|
||
override readonly defaultVersioning = nugetVersioning.id; | ||
|
||
export async function getReleases({ | ||
lookupName, | ||
registryUrl, | ||
}: GetReleasesConfig): Promise<ReleaseResult> { | ||
logger.trace(`nuget.getReleases(${lookupName})`); | ||
const { feedUrl, protocolVersion } = parseRegistryUrl(registryUrl); | ||
if (protocolVersion === 2) { | ||
return v2.getReleases(feedUrl, lookupName); | ||
override readonly registryStrategy = 'merge'; | ||
|
||
constructor() { | ||
super(NugetDatasource.id); | ||
} | ||
if (protocolVersion === 3) { | ||
const queryUrl = await v3.getResourceUrl(feedUrl); | ||
if (queryUrl) { | ||
return v3.getReleases(feedUrl, queryUrl, lookupName); | ||
|
||
async getReleases({ | ||
lookupName, | ||
registryUrl, | ||
}: GetReleasesConfig): Promise<ReleaseResult> { | ||
logger.trace(`nuget.getReleases(${lookupName})`); | ||
const { feedUrl, protocolVersion } = parseRegistryUrl(registryUrl); | ||
if (protocolVersion === 2) { | ||
return v2.getReleases(this.http, feedUrl, lookupName); | ||
} | ||
if (protocolVersion === 3) { | ||
const queryUrl = await v3.getResourceUrl(this.http, feedUrl); | ||
if (queryUrl) { | ||
return v3.getReleases(this.http, feedUrl, queryUrl, lookupName); | ||
} | ||
} | ||
return null; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.