This commit is contained in:
Evgenii Korolevskii 2022-10-13 16:35:12 +02:00
parent 239baf3c5b
commit b827fcce4d

148
dist/index.js vendored
View File

@ -35,8 +35,7 @@ const fs = __importStar(__nccwpck_require__(7147));
const path = __importStar(__nccwpck_require__(1017)); const path = __importStar(__nccwpck_require__(1017));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438)); const github = __importStar(__nccwpck_require__(5438));
const xmlbuilder = __importStar(__nccwpck_require__(2958)); const fast_xml_parser_1 = __nccwpck_require__(2603);
const xmlParser = __importStar(__nccwpck_require__(7448));
function configAuthentication(feedUrl, existingFileLocation = '', processRoot = process.cwd()) { function configAuthentication(feedUrl, existingFileLocation = '', processRoot = process.cwd()) {
const existingNuGetConfig = path.resolve(processRoot, existingFileLocation === '' const existingNuGetConfig = path.resolve(processRoot, existingFileLocation === ''
? getExistingNugetConfig(processRoot) ? getExistingNugetConfig(processRoot)
@ -59,8 +58,8 @@ function getExistingNugetConfig(processRoot) {
return defaultConfigName; return defaultConfigName;
} }
function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) { function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
var _a, _b;
core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`); core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`);
let xml;
let sourceKeys = []; let sourceKeys = [];
let owner = core.getInput('owner'); let owner = core.getInput('owner');
let sourceUrl = feedUrl; let sourceUrl = feedUrl;
@ -73,76 +72,117 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
if (fs.existsSync(existingFileLocation)) { if (fs.existsSync(existingFileLocation)) {
// get key from existing NuGet.config so NuGet/dotnet can match credentials // get key from existing NuGet.config so NuGet/dotnet can match credentials
const curContents = fs.readFileSync(existingFileLocation, 'utf8'); const curContents = fs.readFileSync(existingFileLocation, 'utf8');
const json = xmlParser.parse(curContents, { ignoreAttributes: false }); const parserOptions = {
ignoreAttributes: false
};
const parser = new fast_xml_parser_1.XMLParser(parserOptions);
const json = parser.parse(curContents);
if (typeof json.configuration === 'undefined') { if (typeof json.configuration === 'undefined') {
throw new Error(`The provided NuGet.config seems invalid.`); throw new Error(`The provided NuGet.config seems invalid.`);
} }
if (typeof json.configuration.packageSources != 'undefined') { if ((_b = (_a = json.configuration) === null || _a === void 0 ? void 0 : _a.packageSources) === null || _b === void 0 ? void 0 : _b.add) {
if (typeof json.configuration.packageSources.add != 'undefined') { const packageSources = json.configuration.packageSources.add;
// file has at least one <add> if (Array.isArray(packageSources)) {
if (typeof json.configuration.packageSources.add[0] === 'undefined') { packageSources.forEach(source => {
// file has only one <add> const value = source['@_value'];
if (json.configuration.packageSources.add['@_value'] core.debug(`source '${value}'`);
.toLowerCase() if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
.includes(feedUrl.toLowerCase())) { const key = source['@_key'];
const key = json.configuration.packageSources.add['@_key'];
sourceKeys.push(key); sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`); core.debug(`Found a URL with key ${key}`);
} }
} });
else { }
// file has 2+ <add> else {
for (let i = 0; i < json.configuration.packageSources.add.length; i++) { if (packageSources['@_value']
const source = json.configuration.packageSources.add[i]; .toLowerCase()
const value = source['@_value']; .includes(feedUrl.toLowerCase())) {
core.debug(`source '${value}'`); const key = packageSources['@_key'];
if (value.toLowerCase().includes(feedUrl.toLowerCase())) { sourceKeys.push(key);
const key = source['@_key']; core.debug(`Found a URL with key ${key}`);
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
}
} }
} }
} }
} }
xml = xmlbuilder const xmlSource = [
.create('configuration') {
.ele('config') '?xml': [
.ele('add', { key: 'defaultPushSource', value: sourceUrl }) {
.up() '#text': ''
.up(); }
],
':@': {
'@_version': '1.0'
}
},
{
configuration: [
{
config: [
{
add: [],
':@': {
'@_key': 'defaultPushSource',
'@_value': sourceUrl
}
}
]
}
]
}
];
if (!sourceKeys.length) { if (!sourceKeys.length) {
let keystring = 'Source'; let keystring = 'Source';
xml = xml xmlSource[1].configuration.push({
.ele('packageSources') packageSources: [
.ele('add', { key: keystring, value: sourceUrl }) {
.up() add: [],
.up(); ':@': {
'@_key': keystring,
'@_value': sourceUrl
}
}
]
});
sourceKeys.push(keystring); sourceKeys.push(keystring);
} }
xml = xml.ele('packageSourceCredentials'); const packageSourceCredentials = [];
sourceKeys.forEach(key => { sourceKeys.forEach(key => {
if (!isValidKey(key)) { if (!isValidKey(key)) {
throw new Error("Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again."); throw new Error("Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again.");
} }
xml = xml packageSourceCredentials.push({
.ele(key) [key]: [
.ele('add', { key: 'Username', value: owner }) {
.up() add: [],
.ele('add', { ':@': {
key: 'ClearTextPassword', '@_key': 'Username',
value: process.env.NUGET_AUTH_TOKEN '@_value': owner
}) }
.up() },
.up(); {
add: [],
':@': {
'@_key': 'ClearTextPassword',
'@_value': process.env.NUGET_AUTH_TOKEN
}
}
]
});
}); });
// If NuGet fixes itself such that on Linux it can look for environment variables in the config file (it doesn't seem to work today), xmlSource[1].configuration.push({
// use this for the value above packageSourceCredentials
// process.platform == 'win32' });
// ? '%NUGET_AUTH_TOKEN%' const xmlBuilderOptions = {
// : '$NUGET_AUTH_TOKEN' format: true,
const output = xml.end({ pretty: true }); ignoreAttributes: false,
preserveOrder: true,
allowBooleanAttributes: true,
suppressBooleanAttributes: true,
suppressEmptyNode: true
};
const builder = new fast_xml_parser_1.XMLBuilder(xmlBuilderOptions);
const output = builder.build(xmlSource).trim();
fs.writeFileSync(tempFileLocation, output); fs.writeFileSync(tempFileLocation, output);
} }