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

138
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,27 +72,18 @@ 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>
if (json.configuration.packageSources.add['@_value']
.toLowerCase()
.includes(feedUrl.toLowerCase())) {
const key = json.configuration.packageSources.add['@_key'];
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
}
}
else {
// file has 2+ <add>
for (let i = 0; i < json.configuration.packageSources.add.length; i++) {
const source = json.configuration.packageSources.add[i];
const value = source['@_value']; const value = source['@_value'];
core.debug(`source '${value}'`); core.debug(`source '${value}'`);
if (value.toLowerCase().includes(feedUrl.toLowerCase())) { if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
@ -101,48 +91,98 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
sourceKeys.push(key); sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`); core.debug(`Found a URL with key ${key}`);
} }
});
}
else {
if (packageSources['@_value']
.toLowerCase()
.includes(feedUrl.toLowerCase())) {
const key = packageSources['@_key'];
sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`);
} }
} }
} }
} }
const xmlSource = [
{
'?xml': [
{
'#text': ''
} }
xml = xmlbuilder ],
.create('configuration') ':@': {
.ele('config') '@_version': '1.0'
.ele('add', { key: 'defaultPushSource', value: sourceUrl }) }
.up() },
.up(); {
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), });
// use this for the value above xmlSource[1].configuration.push({
// process.platform == 'win32' packageSourceCredentials
// ? '%NUGET_AUTH_TOKEN%' });
// : '$NUGET_AUTH_TOKEN' const xmlBuilderOptions = {
const output = xml.end({ pretty: true }); format: 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);
} }