Support for non-GPR sources

This commit is contained in:
Alex Mullans 2019-10-03 17:29:20 -07:00
parent 6bd4969ec6
commit 70528ac007
6 changed files with 203 additions and 65 deletions

View File

@ -15,6 +15,21 @@ exports[`authutil tests Existing config not in repo root, sets up a partial NuGe
</configuration>"
`;
exports[`authutil tests Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR 1`] = `
"<?xml version=\\"1.0\\"?>
<configuration>
<config>
<add key=\\"defaultPushSource\\" value=\\"https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json\\"/>
</config>
<packageSourceCredentials>
<AzureArtifacts>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</AzureArtifacts>
</packageSourceCredentials>
</configuration>"
`;
exports[`authutil tests Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR 1`] = `
"<?xml version=\\"1.0\\"?>
<configuration>
@ -37,13 +52,13 @@ exports[`authutil tests Existing config w/ no GPR sources, sets up a full NuGet.
<add key=\\"defaultPushSource\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</config>
<packageSources>
<add key=\\"GPR\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
<add key=\\"Source\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</packageSources>
<packageSourceCredentials>
<GPR>
<Source>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR>
</Source>
</packageSourceCredentials>
</configuration>"
`;
@ -55,13 +70,28 @@ exports[`authutil tests Existing config w/ no sources, sets up a full NuGet.conf
<add key=\\"defaultPushSource\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</config>
<packageSources>
<add key=\\"GPR\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
<add key=\\"Source\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</packageSources>
<packageSourceCredentials>
<GPR>
<Source>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR>
</Source>
</packageSourceCredentials>
</configuration>"
`;
exports[`authutil tests Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR 1`] = `
"<?xml version=\\"1.0\\"?>
<configuration>
<config>
<add key=\\"defaultPushSource\\" value=\\"https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json\\"/>
</config>
<packageSourceCredentials>
<AzureArtifacts>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</AzureArtifacts>
</packageSourceCredentials>
</configuration>"
`;
@ -92,13 +122,11 @@ exports[`authutil tests Existing config w/ two GPR sources, sets up a partial Nu
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR-GitHub>
<packageSourceCredentials>
<GPR-Actions>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR-Actions>
</packageSourceCredentials>
</packageSourceCredentials>
</configuration>"
`;
@ -109,13 +137,31 @@ exports[`authutil tests No existing config, sets up a full NuGet.config with URL
<add key=\\"defaultPushSource\\" value=\\"https://nuget.pkg.github.com/otherorg\\"/>
</config>
<packageSources>
<add key=\\"GPR\\" value=\\"https://nuget.pkg.github.com/otherorg\\"/>
<add key=\\"Source\\" value=\\"https://nuget.pkg.github.com/otherorg\\"/>
</packageSources>
<packageSourceCredentials>
<GPR>
<Source>
<add key=\\"Username\\" value=\\"otherorg\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR>
</Source>
</packageSourceCredentials>
</configuration>"
`;
exports[`authutil tests No existing config, sets up a full NuGet.config with URL and token for other source 1`] = `
"<?xml version=\\"1.0\\"?>
<configuration>
<config>
<add key=\\"defaultPushSource\\" value=\\"https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json\\"/>
</config>
<packageSources>
<add key=\\"Source\\" value=\\"https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json\\"/>
</packageSources>
<packageSourceCredentials>
<Source>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</Source>
</packageSourceCredentials>
</configuration>"
`;
@ -127,13 +173,13 @@ exports[`authutil tests No existing config, sets up a full NuGet.config with URL
<add key=\\"defaultPushSource\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</config>
<packageSources>
<add key=\\"GPR\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
<add key=\\"Source\\" value=\\"https://nuget.pkg.github.com/OwnerName\\"/>
</packageSources>
<packageSourceCredentials>
<GPR>
<Source>
<add key=\\"Username\\" value=\\"OwnerName\\"/>
<add key=\\"ClearTextPassword\\" value=\\"TEST_FAKE_AUTH_TOKEN\\"/>
</GPR>
</Source>
</packageSourceCredentials>
</configuration>"
`;

View File

@ -2,7 +2,7 @@ import io = require('@actions/io');
import fs = require('fs');
import path = require('path');
const tempDir = path.join(
const fakeSourcesDirForTesting = path.join(
__dirname,
'runner',
path.join(
@ -10,7 +10,7 @@ const tempDir = path.join(
.toString(36)
.substring(7)
),
'temp'
's'
);
const invalidNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>`;
@ -56,16 +56,32 @@ const spaceNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
</packageSources>
</configuration>`;
const nugetConfigFile = path.join(tempDir, '../nuget.config');
const azureartifactsNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
const azureartifactsnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
// We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet.
const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config');
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
process.env['RUNNER_TEMP'] = tempDir;
process.env['RUNNER_TEMP'] = fakeSourcesDirForTesting;
import * as auth from '../src/authutil';
describe('authutil tests', () => {
beforeAll(async () => {
await io.rmRF(tempDir);
await io.mkdirP(tempDir);
beforeEach(async () => {
await io.rmRF(fakeSourcesDirForTesting);
await io.mkdirP(fakeSourcesDirForTesting);
}, 100000);
beforeEach(() => {
@ -107,7 +123,10 @@ describe('authutil tests', () => {
it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig);
let thrown = false;
try {
@ -120,7 +139,10 @@ describe('authutil tests', () => {
it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig);
await auth.configAuthentication('https://nuget.pkg.github.com');
expect(fs.existsSync(nugetConfigFile)).toBe(true);
@ -131,7 +153,10 @@ describe('authutil tests', () => {
it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig);
await auth.configAuthentication('https://nuget.pkg.github.com');
expect(fs.existsSync(nugetConfigFile)).toBe(true);
@ -142,7 +167,10 @@ describe('authutil tests', () => {
it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
await auth.configAuthentication('https://nuget.pkg.github.com');
expect(fs.existsSync(nugetConfigFile)).toBe(true);
@ -153,7 +181,10 @@ describe('authutil tests', () => {
it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig);
await auth.configAuthentication('https://nuget.pkg.github.com');
expect(fs.existsSync(nugetConfigFile)).toBe(true);
@ -164,7 +195,10 @@ describe('authutil tests', () => {
it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig);
await auth.configAuthentication('https://nuget.pkg.github.com');
expect(fs.existsSync(nugetConfigFile)).toBe(true);
@ -175,7 +209,10 @@ describe('authutil tests', () => {
it('Existing config w/ spaces in key, throws for now', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(tempDir, 'nuget.config');
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig);
let thrown = false;
try {
@ -188,7 +225,10 @@ describe('authutil tests', () => {
it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigDirectory: string = path.join(tempDir, 'subfolder');
const inputNuGetConfigDirectory: string = path.join(
fakeSourcesDirForTesting,
'subfolder'
);
const inputNuGetConfigPath: string = path.join(
inputNuGetConfigDirectory,
'nuget.config'
@ -204,4 +244,47 @@ describe('authutil tests', () => {
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig);
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json'
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig);
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json'
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json'
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
});

View File

@ -22,12 +22,15 @@ exports.configAuthentication = configAuthentication;
function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
console.log(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`);
let xml;
let gprKeys = [];
let sourceKeys = [];
let owner = core.getInput('owner');
if (!owner && feedUrl.indexOf('nuget.pkg.github.com') > -1) {
let sourceUrl = feedUrl;
if (!owner) {
owner = github.context.repo.owner;
}
let sourceUrl = 'https://nuget.pkg.github.com/' + owner;
if (feedUrl.indexOf('nuget.pkg.github.com') > -1) {
sourceUrl = 'https://nuget.pkg.github.com/' + owner;
}
if (!process.env.NUGET_AUTH_TOKEN || process.env.NUGET_AUTH_TOKEN == '') {
throw new Error('The NUGET_AUTH_TOKEN environment variable was not provided. In this step, add the following: \r\nenv:\r\n NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}');
}
@ -45,10 +48,10 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
// file has only one <add>
if (json.configuration.packageSources.add['@_value']
.toLowerCase()
.includes('nuget.pkg.github.com')) {
.includes(feedUrl.toLowerCase())) {
let key = json.configuration.packageSources.add['@_key'];
gprKeys.push(key);
core.debug(`Found a GPR URL with key ${key}`);
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
}
else {
@ -57,10 +60,10 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
core.debug(json.configuration.packageSources.add[i]);
if (json.configuration.packageSources.add[i]['@_value']
.toLowerCase()
.includes('nuget.pkg.github.com')) {
.includes(feedUrl.toLowerCase())) {
let key = json.configuration.packageSources.add[i]['@_key'];
gprKeys.push(key);
core.debug(`Found a GPR URL with key ${key}`);
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
}
}
@ -73,22 +76,22 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
.ele('add', { key: 'defaultPushSource', value: sourceUrl })
.up()
.up();
if (gprKeys.length == 0) {
let keystring = 'GPR';
if (sourceKeys.length == 0) {
let keystring = 'Source';
xml = xml
.ele('packageSources')
.ele('add', { key: keystring, value: sourceUrl })
.up()
.up();
gprKeys.push(keystring);
sourceKeys.push(keystring);
}
gprKeys.forEach(key => {
xml = xml
.ele('packageSourceCredentials');
sourceKeys.forEach(key => {
if (key.indexOf(' ') > -1) {
throw new Error("This action currently can't handle source names with spaces. Remove the space from your repo's NuGet.config and try again.");
}
xml = xml
.ele('packageSourceCredentials')
.ele(key)
xml = xml.ele(key)
.ele('add', { key: 'Username', value: owner })
.up()
.ele('add', {

View File

@ -35,8 +35,9 @@ function run() {
yield dotnetInstaller.installDotnet();
}
const sourceUrl = core.getInput('source-url');
const configFile = core.getInput('config-file');
if (sourceUrl) {
auth.configAuthentication(sourceUrl);
auth.configAuthentication(sourceUrl, configFile);
}
// TODO: setup proxy from runner proxy config
const matchersPath = path.join(__dirname, '..', '.github');

View File

@ -34,12 +34,15 @@ function writeFeedToFile(
`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`
);
let xml: xmlbuilder.XMLElement;
let gprKeys: string[] = [];
let sourceKeys: string[] = [];
let owner: string = core.getInput('owner');
if (!owner && feedUrl.indexOf('nuget.pkg.github.com') > -1) {
let sourceUrl: string = feedUrl;
if (!owner) {
owner = github.context.repo.owner;
}
let sourceUrl: string = 'https://nuget.pkg.github.com/' + owner;
if (feedUrl.indexOf('nuget.pkg.github.com') > -1) {
sourceUrl = 'https://nuget.pkg.github.com/' + owner;
}
if (!process.env.NUGET_AUTH_TOKEN || process.env.NUGET_AUTH_TOKEN == '') {
throw new Error(
@ -63,11 +66,11 @@ function writeFeedToFile(
if (
json.configuration.packageSources.add['@_value']
.toLowerCase()
.includes('nuget.pkg.github.com')
.includes(feedUrl.toLowerCase())
) {
let key = json.configuration.packageSources.add['@_key'];
gprKeys.push(key);
core.debug(`Found a GPR URL with key ${key}`);
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
} else {
// file has 2+ <add>
@ -80,11 +83,11 @@ function writeFeedToFile(
if (
json.configuration.packageSources.add[i]['@_value']
.toLowerCase()
.includes('nuget.pkg.github.com')
.includes(feedUrl.toLowerCase())
) {
let key = json.configuration.packageSources.add[i]['@_key'];
gprKeys.push(key);
core.debug(`Found a GPR URL with key ${key}`);
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
}
}
@ -99,16 +102,18 @@ function writeFeedToFile(
.up()
.up();
if (gprKeys.length == 0) {
let keystring = 'GPR';
if (sourceKeys.length == 0) {
let keystring = 'Source';
xml = xml
.ele('packageSources')
.ele('add', {key: keystring, value: sourceUrl})
.up()
.up();
gprKeys.push(keystring);
sourceKeys.push(keystring);
}
gprKeys.forEach(key => {
xml = xml.ele('packageSourceCredentials');
sourceKeys.forEach(key => {
if (key.indexOf(' ') > -1) {
throw new Error(
"This action currently can't handle source names with spaces. Remove the space from your repo's NuGet.config and try again."
@ -116,7 +121,6 @@ function writeFeedToFile(
}
xml = xml
.ele('packageSourceCredentials')
.ele(key)
.ele('add', {key: 'Username', value: owner})
.up()

View File

@ -19,8 +19,9 @@ async function run() {
}
const sourceUrl: string = core.getInput('source-url');
const configFile: string = core.getInput('config-file');
if (sourceUrl) {
auth.configAuthentication(sourceUrl);
auth.configAuthentication(sourceUrl, configFile);
}
// TODO: setup proxy from runner proxy config