Skip to content
Snippets Groups Projects
Unverified Commit 46fda979 authored by Florian Greinacher's avatar Florian Greinacher Committed by GitHub
Browse files

fix(nuget): start with empty nuget.config for updating lockfile (#7808)

The previously used command, `dotnet new nugetconfig` creates a nuget.config with some defaults that we are not interested in while creating the temporary file for authentication.

Fixes #7769
parent bf252be7
Branches
No related tags found
No related merge requests found
...@@ -2,24 +2,6 @@ ...@@ -2,24 +2,6 @@
exports[`updateArtifacts aborts if lock file is unchanged 1`] = ` exports[`updateArtifacts aborts if lock file is unchanged 1`] = `
Array [ Array [
Object {
"cmd": "dotnet new nugetconfig --output ./others/nuget/not-so-random",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object { Object {
"cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config", "cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config",
"options": Object { "options": Object {
...@@ -46,25 +28,7 @@ exports[`updateArtifacts aborts if no lock file found 1`] = `Array []`; ...@@ -46,25 +28,7 @@ exports[`updateArtifacts aborts if no lock file found 1`] = `Array []`;
exports[`updateArtifacts authenticates at registries 1`] = ` exports[`updateArtifacts authenticates at registries 1`] = `
Array [ Array [
Object { Object {
"cmd": "dotnet new nugetconfig --output ./others/nuget/not-so-random", "cmd": "dotnet nuget add source https://my-registry.example.org --configfile others/nuget/not-so-random/nuget.config --name myRegistry --username some-username --password some-password --store-password-in-clear-text",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "dotnet nuget add source https://my-registry.example.org --name myRegistry --configfile others/nuget/not-so-random/nuget.config --username some-username --password some-password --store-password-in-clear-text",
"options": Object { "options": Object {
"cwd": "/tmp/github/some/repo", "cwd": "/tmp/github/some/repo",
"encoding": "utf-8", "encoding": "utf-8",
...@@ -119,24 +83,6 @@ exports[`updateArtifacts does not update lock file when non-proj file is changed ...@@ -119,24 +83,6 @@ exports[`updateArtifacts does not update lock file when non-proj file is changed
exports[`updateArtifacts performs lock file maintenance 1`] = ` exports[`updateArtifacts performs lock file maintenance 1`] = `
Array [ Array [
Object {
"cmd": "dotnet new nugetconfig --output ./others/nuget/not-so-random",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object { Object {
"cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config", "cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config",
"options": Object { "options": Object {
...@@ -173,7 +119,7 @@ Array [ ...@@ -173,7 +119,7 @@ Array [
}, },
}, },
Object { Object {
"cmd": "docker run --rm --name=renovate_dotnet --label=renovate_child --user=foobar -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/renovate/cache\\":\\"/tmp/renovate/cache\\" -w \\"/tmp/github/some/repo\\" renovate/dotnet bash -l -c \\"dotnet new nugetconfig --output ./others/nuget/not-so-random && dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config\\"", "cmd": "docker run --rm --name=renovate_dotnet --label=renovate_child --user=foobar -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/renovate/cache\\":\\"/tmp/renovate/cache\\" -w \\"/tmp/github/some/repo\\" renovate/dotnet bash -l -c \\"dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config\\"",
"options": Object { "options": Object {
"cwd": "/tmp/github/some/repo", "cwd": "/tmp/github/some/repo",
"encoding": "utf-8", "encoding": "utf-8",
...@@ -195,24 +141,6 @@ Array [ ...@@ -195,24 +141,6 @@ Array [
exports[`updateArtifacts supports global mode 1`] = ` exports[`updateArtifacts supports global mode 1`] = `
Array [ Array [
Object {
"cmd": "dotnet new nugetconfig --output ./others/nuget/not-so-random",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object { Object {
"cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config", "cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config",
"options": Object { "options": Object {
...@@ -236,24 +164,6 @@ Array [ ...@@ -236,24 +164,6 @@ Array [
exports[`updateArtifacts updates lock file 1`] = ` exports[`updateArtifacts updates lock file 1`] = `
Array [ Array [
Object {
"cmd": "dotnet new nugetconfig --output ./others/nuget/not-so-random",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object { Object {
"cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config", "cmd": "dotnet restore project.csproj --force-evaluate --configfile others/nuget/not-so-random/nuget.config",
"options": Object { "options": Object {
......
...@@ -9,7 +9,8 @@ import * as _env from '../../util/exec/env'; ...@@ -9,7 +9,8 @@ import * as _env from '../../util/exec/env';
import * as _hostRules from '../../util/host-rules'; import * as _hostRules from '../../util/host-rules';
import * as nuget from './artifacts'; import * as nuget from './artifacts';
import { import {
determineRegistries as _determineRegistries, getConfiguredRegistries as _getConfiguredRegistries,
getDefaultRegistries as _getDefaultRegistries,
getRandomString as _getRandomString, getRandomString as _getRandomString,
} from './util'; } from './util';
...@@ -21,7 +22,8 @@ jest.mock('./util'); ...@@ -21,7 +22,8 @@ jest.mock('./util');
const exec: jest.Mock<typeof _exec> = _exec as any; const exec: jest.Mock<typeof _exec> = _exec as any;
const env = mocked(_env); const env = mocked(_env);
const determineRegistries: jest.Mock<typeof _determineRegistries> = _determineRegistries as any; const getConfiguredRegistries: jest.Mock<typeof _getConfiguredRegistries> = _getConfiguredRegistries as any;
const getDefaultRegistries: jest.Mock<typeof _getDefaultRegistries> = _getDefaultRegistries as any;
const getRandomString: jest.Mock<typeof _getRandomString> = _getRandomString as any; const getRandomString: jest.Mock<typeof _getRandomString> = _getRandomString as any;
const hostRules = mocked(_hostRules); const hostRules = mocked(_hostRules);
...@@ -36,6 +38,7 @@ describe('updateArtifacts', () => { ...@@ -36,6 +38,7 @@ describe('updateArtifacts', () => {
beforeEach(async () => { beforeEach(async () => {
jest.resetAllMocks(); jest.resetAllMocks();
jest.resetModules(); jest.resetModules();
getDefaultRegistries.mockReturnValue([] as any);
env.getChildProcessEnv.mockReturnValue(envMock.basic); env.getChildProcessEnv.mockReturnValue(envMock.basic);
fs.ensureCacheDir.mockImplementation((dirName: string) => fs.ensureCacheDir.mockImplementation((dirName: string) =>
Promise.resolve(dirName) Promise.resolve(dirName)
...@@ -191,7 +194,7 @@ describe('updateArtifacts', () => { ...@@ -191,7 +194,7 @@ describe('updateArtifacts', () => {
fs.getSiblingFileName.mockReturnValueOnce('packages.lock.json'); fs.getSiblingFileName.mockReturnValueOnce('packages.lock.json');
fs.readLocalFile.mockResolvedValueOnce('Current packages.lock.json' as any); fs.readLocalFile.mockResolvedValueOnce('Current packages.lock.json' as any);
fs.readLocalFile.mockResolvedValueOnce('New packages.lock.json' as any); fs.readLocalFile.mockResolvedValueOnce('New packages.lock.json' as any);
determineRegistries.mockResolvedValueOnce([ getConfiguredRegistries.mockResolvedValueOnce([
{ {
name: 'myRegistry', name: 'myRegistry',
url: 'https://my-registry.example.org', url: 'https://my-registry.example.org',
......
...@@ -5,6 +5,7 @@ import { ExecOptions, exec } from '../../util/exec'; ...@@ -5,6 +5,7 @@ import { ExecOptions, exec } from '../../util/exec';
import { import {
ensureCacheDir, ensureCacheDir,
getSiblingFileName, getSiblingFileName,
outputFile,
readLocalFile, readLocalFile,
remove, remove,
writeLocalFile, writeLocalFile,
...@@ -15,25 +16,33 @@ import { ...@@ -15,25 +16,33 @@ import {
UpdateArtifactsConfig, UpdateArtifactsConfig,
UpdateArtifactsResult, UpdateArtifactsResult,
} from '../common'; } from '../common';
import { determineRegistries, getRandomString } from './util'; import {
getConfiguredRegistries,
getDefaultRegistries,
getRandomString,
} from './util';
async function addSourceCmds( async function addSourceCmds(
packageFileName: string, packageFileName: string,
config: UpdateArtifactsConfig, config: UpdateArtifactsConfig,
nugetConfigFile: string nugetConfigFile: string
): Promise<string[]> { ): Promise<string[]> {
const registries = ( const registries =
(await determineRegistries(packageFileName, config.localDir)) || [] (await getConfiguredRegistries(packageFileName, config.localDir)) ||
).filter((registry) => registry.name != null); getDefaultRegistries();
const result = []; const result = [];
for (const registry of registries) { for (const registry of registries) {
const { username, password } = hostRules.find({ const { username, password } = hostRules.find({
hostType: id, hostType: id,
url: registry.url, url: registry.url,
}); });
let addSourceCmd = `dotnet nuget add source ${registry.url} --name ${registry.name} --configfile ${nugetConfigFile}`; let addSourceCmd = `dotnet nuget add source ${registry.url} --configfile ${nugetConfigFile}`;
if (registry.name) {
// Add name for registry, if known.
addSourceCmd += ` --name ${registry.name}`;
}
if (username && password) { if (username && password) {
// Add registry credentials from host rules. // Add registry credentials from host rules, if configured.
addSourceCmd += ` --username ${username} --password ${password} --store-password-in-clear-text`; addSourceCmd += ` --username ${username} --password ${password} --store-password-in-clear-text`;
} }
result.push(addSourceCmd); result.push(addSourceCmd);
...@@ -55,8 +64,11 @@ async function runDotnetRestore( ...@@ -55,8 +64,11 @@ async function runDotnetRestore(
`./others/nuget/${getRandomString()}` `./others/nuget/${getRandomString()}`
); );
const nugetConfigFile = join(nugetConfigDir, 'nuget.config'); const nugetConfigFile = join(nugetConfigDir, 'nuget.config');
await outputFile(
nugetConfigFile,
`<?xml version="1.0" encoding="utf-8"?>\n<configuration>\n</configuration>\n`
);
const cmds = [ const cmds = [
`dotnet new nugetconfig --output ${nugetConfigDir}`,
...(await addSourceCmds(packageFileName, config, nugetConfigFile)), ...(await addSourceCmds(packageFileName, config, nugetConfigFile)),
`dotnet restore ${packageFileName} --force-evaluate --configfile ${nugetConfigFile}`, `dotnet restore ${packageFileName} --force-evaluate --configfile ${nugetConfigFile}`,
]; ];
......
...@@ -3,7 +3,7 @@ import * as datasourceNuget from '../../datasource/nuget'; ...@@ -3,7 +3,7 @@ import * as datasourceNuget from '../../datasource/nuget';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { ExtractConfig, PackageDependency, PackageFile } from '../common'; import { ExtractConfig, PackageDependency, PackageFile } from '../common';
import { DotnetToolsManifest } from './types'; import { DotnetToolsManifest } from './types';
import { determineRegistries } from './util'; import { getConfiguredRegistries } from './util';
/** /**
* https://docs.microsoft.com/en-us/nuget/concepts/package-versioning * https://docs.microsoft.com/en-us/nuget/concepts/package-versioning
...@@ -59,7 +59,10 @@ export async function extractPackageFile( ...@@ -59,7 +59,10 @@ export async function extractPackageFile(
): Promise<PackageFile | null> { ): Promise<PackageFile | null> {
logger.trace({ packageFile }, 'nuget.extractPackageFile()'); logger.trace({ packageFile }, 'nuget.extractPackageFile()');
const registries = await determineRegistries(packageFile, config.localDir); const registries = await getConfiguredRegistries(
packageFile,
config.localDir
);
const registryUrls = registries const registryUrls = registries
? registries.map((registry) => registry.url) ? registries.map((registry) => registry.url)
: undefined; : undefined;
......
...@@ -25,7 +25,16 @@ export function getRandomString(): string { ...@@ -25,7 +25,16 @@ export function getRandomString(): string {
return cryptoRandomString({ length: 16 }); return cryptoRandomString({ length: 16 });
} }
export async function determineRegistries( export function getDefaultRegistries(): Registry[] {
return datasourceNuget.defaultRegistryUrls.map(
(registryUrl) =>
({
url: registryUrl,
} as Registry)
);
}
export async function getConfiguredRegistries(
packageFile: string, packageFile: string,
localDir: string localDir: string
): Promise<Registry[] | undefined> { ): Promise<Registry[] | undefined> {
...@@ -56,12 +65,7 @@ export async function determineRegistries( ...@@ -56,12 +65,7 @@ export async function determineRegistries(
return undefined; return undefined;
} }
const registries = datasourceNuget.defaultRegistryUrls.map( const registries = getDefaultRegistries();
(registryUrl) =>
({
url: registryUrl,
} as Registry)
);
for (const child of packageSources.children) { for (const child of packageSources.children) {
if (child.type === 'element') { if (child.type === 'element') {
if (child.name === 'clear') { if (child.name === 'clear') {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment