Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager/nuget): add support for "disabledPackageSources" in nuget.config #32011

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions lib/modules/manager/nuget/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,120 @@ describe('modules/manager/nuget/util', () => {
'NuGet.Common',
]);
});

it('reads nuget config file with default registry disabled', async () => {
fs.findUpLocal.mockReturnValue(
Promise.resolve<string | null>('NuGet.config'),
);
fs.readLocalFile.mockResolvedValueOnce(
codeBlock`
<configuration>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
</configuration>`,
);

const registries = await getConfiguredRegistries('NuGet.config');
expect(registries?.length).toBe(0);
});

it('reads nuget config file with default registry disabled and added sources', async () => {
fs.findUpLocal.mockReturnValue(
Promise.resolve<string | null>('NuGet.config'),
);
fs.readLocalFile.mockResolvedValueOnce(
codeBlock`
<configuration>
<packageSources>
<add key="contoso.com" value="https://contoso.com/packages/"/>
</packageSources>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
</configuration>`,
);

const registries = await getConfiguredRegistries('NuGet.config');
expect(registries?.length).toBe(1);
expect(registries![0].name).toBe('contoso.com');
expect(registries![0].url).toBe('https://contoso.com/packages/');
});

it('reads nuget config file with default registry disabled given default registry added', async () => {
fs.findUpLocal.mockReturnValue(
Promise.resolve<string | null>('NuGet.config'),
);
fs.readLocalFile.mockResolvedValueOnce(
codeBlock`
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
<add key="contoso.com" value="https://contoso.com/packages/"/>
</packageSources>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
</configuration>`,
);

const registries = await getConfiguredRegistries('NuGet.config');
expect(registries?.length).toBe(1);
expect(registries![0].name).toBe('contoso.com');
expect(registries![0].url).toBe('https://contoso.com/packages/');
});

it('reads nuget config file with unknown disabled source', async () => {
fs.findUpLocal.mockReturnValue(
Promise.resolve<string | null>('NuGet.config'),
);
fs.readLocalFile.mockResolvedValueOnce(
codeBlock`
<configuration>
<packageSources>
<add key="contoso.com" value="https://contoso.com/packages/"/>
</packageSources>
<disabledPackageSources>
<add key="unknown" value="true" />
</disabledPackageSources>
</configuration>`,
);

const registries = await getConfiguredRegistries('NuGet.config');
expect(registries?.length).toBe(2);
expect(registries![0].name).toBe('nuget.org');
expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json');

expect(registries![1].name).toBe('contoso.com');
expect(registries![1].url).toBe('https://contoso.com/packages/');
});

it('reads nuget config file with disabled source with value false', async () => {
fs.findUpLocal.mockReturnValue(
Promise.resolve<string | null>('NuGet.config'),
);
fs.readLocalFile.mockResolvedValueOnce(
codeBlock`
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
<add key="contoso.com" value="https://contoso.com/packages/"/>
</packageSources>
<disabledPackageSources>
<add key="contoso.com" value="false" />
</disabledPackageSources>
</configuration>`,
);

const registries = await getConfiguredRegistries('NuGet.config');
expect(registries?.length).toBe(2);
expect(registries![0].name).toBe('nuget.org');
expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json');

expect(registries![1].name).toBe('contoso.com');
expect(registries![1].url).toBe('https://contoso.com/packages/');
});
});

describe('applyRegistries', () => {
Expand Down
94 changes: 57 additions & 37 deletions lib/modules/manager/nuget/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ export async function getConfiguredRegistries(
}

const packageSources = nuGetConfig.childNamed('packageSources');
if (!packageSources) {
const disabledPackageSources = nuGetConfig.childNamed(
'disabledPackageSources',
);

if (!packageSources && !disabledPackageSources) {
return undefined;
}

const packageSourceMapping = nuGetConfig.childNamed('packageSourceMapping');

const registries = getDefaultRegistries();
let registries = getDefaultRegistries();

// Map optional source mapped package patterns to default registries
for (const registry of registries) {
Expand All @@ -70,47 +74,63 @@ export async function getConfiguredRegistries(
registry.sourceMappedPackagePatterns = sourceMappedPackagePatterns;
}

for (const child of packageSources.children) {
if (child.type === 'element') {
if (child.name === 'clear') {
logger.debug(`clearing registry URLs`);
registries.length = 0;
} else if (child.name === 'add') {
const isHttpUrl = regEx(/^https?:\/\//i).test(child.attr.value);
if (isHttpUrl) {
let registryUrl = child.attr.value;
if (child.attr.protocolVersion) {
registryUrl += `#protocolVersion=${child.attr.protocolVersion}`;
}
const sourceMappedPackagePatterns = packageSourceMapping
?.childWithAttribute('key', child.attr.key)
?.childrenNamed('package')
.map((packagePattern) => packagePattern.attr['pattern']);

logger.debug(
{
if (packageSources) {
for (const child of packageSources.children) {
if (child.type === 'element') {
if (child.name === 'clear') {
logger.debug(`clearing registry URLs`);
registries.length = 0;
} else if (child.name === 'add') {
const isHttpUrl = regEx(/^https?:\/\//i).test(child.attr.value);
if (isHttpUrl) {
let registryUrl = child.attr.value;
if (child.attr.protocolVersion) {
registryUrl += `#protocolVersion=${child.attr.protocolVersion}`;
}
const sourceMappedPackagePatterns = packageSourceMapping
?.childWithAttribute('key', child.attr.key)
?.childrenNamed('package')
.map((packagePattern) => packagePattern.attr['pattern']);

logger.debug(
{
name: child.attr.key,
registryUrl,
sourceMappedPackagePatterns,
},
`Adding registry URL ${registryUrl}`,
);

registries.push({
name: child.attr.key,
registryUrl,
url: registryUrl,
sourceMappedPackagePatterns,
},
`Adding registry URL ${registryUrl}`,
);

registries.push({
name: child.attr.key,
url: registryUrl,
sourceMappedPackagePatterns,
});
} else {
logger.debug(
{ registryUrl: child.attr.value },
'ignoring local registry URL',
);
});
} else {
logger.debug(
{ registryUrl: child.attr.value },
'ignoring local registry URL',
);
}
}
}
// child.name === 'remove' not supported
}
}

if (disabledPackageSources) {
for (const child of disabledPackageSources.children) {
if (
child.type === 'element' &&
child.name === 'add' &&
child.attr.value === 'true'
) {
const disabledRegistryKey = child.attr.key;
registries = registries.filter((o) => o.name !== disabledRegistryKey);
logger.debug(`Disabled registry with key: ${disabledRegistryKey}`);
}
}
}

return registries;
}

Expand Down
Loading