@@ -35,8 +35,7 @@ const fs = __importStar(__nccwpck_require__(7147));
35
35
const path = __importStar(__nccwpck_require__(1017));
36
36
const core = __importStar(__nccwpck_require__(2186));
37
37
const github = __importStar(__nccwpck_require__(5438));
38
- const xmlbuilder = __importStar(__nccwpck_require__(2958));
39
- const xmlParser = __importStar(__nccwpck_require__(7448));
38
+ const fast_xml_parser_1 = __nccwpck_require__(2603);
40
39
function configAuthentication(feedUrl, existingFileLocation = '', processRoot = process.cwd()) {
41
40
const existingNuGetConfig = path.resolve(processRoot, existingFileLocation === ''
42
41
? getExistingNugetConfig(processRoot)
@@ -59,8 +58,8 @@ function getExistingNugetConfig(processRoot) {
59
58
return defaultConfigName;
60
59
}
61
60
function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
61
+ var _a, _b;
62
62
core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`);
63
- let xml;
64
63
let sourceKeys = [];
65
64
let owner = core.getInput('owner');
66
65
let sourceUrl = feedUrl;
@@ -73,76 +72,117 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
73
72
if (fs.existsSync(existingFileLocation)) {
74
73
// get key from existing NuGet.config so NuGet/dotnet can match credentials
75
74
const curContents = fs.readFileSync(existingFileLocation, 'utf8');
76
- const json = xmlParser.parse(curContents, { ignoreAttributes: false });
75
+ const parserOptions = {
76
+ ignoreAttributes: false
77
+ };
78
+ const parser = new fast_xml_parser_1.XMLParser(parserOptions);
79
+ const json = parser.parse(curContents);
77
80
if (typeof json.configuration === 'undefined') {
78
81
throw new Error(`The provided NuGet.config seems invalid.`);
79
82
}
80
- if (typeof json.configuration.packageSources != 'undefined') {
81
- if (typeof json.configuration.packageSources.add != 'undefined') {
82
- // file has at least one <add>
83
- if (typeof json.configuration.packageSources.add[0] === 'undefined') {
84
- // file has only one <add>
85
- if (json.configuration.packageSources.add['@_value']
86
- .toLowerCase()
87
- .includes(feedUrl.toLowerCase())) {
88
- const key = json.configuration.packageSources.add['@_key'];
83
+ if ((_b = (_a = json.configuration) === null || _a === void 0 ? void 0 : _a.packageSources) === null || _b === void 0 ? void 0 : _b.add) {
84
+ const packageSources = json.configuration.packageSources.add;
85
+ if (Array.isArray(packageSources)) {
86
+ packageSources.forEach(source => {
87
+ const value = source['@_value'];
88
+ core.debug(`source '${value}'`);
89
+ if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
90
+ const key = source['@_key'];
89
91
sourceKeys.push(key);
90
92
core.debug(`Found a URL with key ${key}`);
91
93
}
92
- }
93
- else {
94
- // file has 2+ <add>
95
- for (let i = 0; i < json.configuration.packageSources.add.length; i++) {
96
- const source = json.configuration.packageSources.add[i];
97
- const value = source['@_value'];
98
- core.debug(`source '${value}'`);
99
- if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
100
- const key = source['@_key'];
101
- sourceKeys.push(key);
102
- core.debug(`Found a URL with key ${key}`);
103
- }
104
- }
94
+ });
95
+ }
96
+ else {
97
+ if (packageSources['@_value']
98
+ .toLowerCase()
99
+ .includes(feedUrl.toLowerCase())) {
100
+ const key = packageSources['@_key'];
101
+ sourceKeys.push(key);
102
+ core.debug(`Found a URL with key ${key}`);
105
103
}
106
104
}
107
105
}
108
106
}
109
- xml = xmlbuilder
110
- .create('configuration')
111
- .ele('config')
112
- .ele('add', { key: 'defaultPushSource', value: sourceUrl })
113
- .up()
114
- .up();
107
+ const xmlSource = [
108
+ {
109
+ '?xml': [
110
+ {
111
+ '#text': ''
112
+ }
113
+ ],
114
+ ':@': {
115
+ '@_version': '1.0'
116
+ }
117
+ },
118
+ {
119
+ configuration: [
120
+ {
121
+ config: [
122
+ {
123
+ add: [],
124
+ ':@': {
125
+ '@_key': 'defaultPushSource',
126
+ '@_value': sourceUrl
127
+ }
128
+ }
129
+ ]
130
+ }
131
+ ]
132
+ }
133
+ ];
115
134
if (!sourceKeys.length) {
116
135
let keystring = 'Source';
117
- xml = xml
118
- .ele('packageSources')
119
- .ele('add', { key: keystring, value: sourceUrl })
120
- .up()
121
- .up();
136
+ xmlSource[1].configuration.push({
137
+ packageSources: [
138
+ {
139
+ add: [],
140
+ ':@': {
141
+ '@_key': keystring,
142
+ '@_value': sourceUrl
143
+ }
144
+ }
145
+ ]
146
+ });
122
147
sourceKeys.push(keystring);
123
148
}
124
- xml = xml.ele('packageSourceCredentials') ;
149
+ const packageSourceCredentials = [] ;
125
150
sourceKeys.forEach(key => {
126
151
if (!isValidKey(key)) {
127
152
throw new Error("Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again.");
128
153
}
129
- xml = xml
130
- .ele(key)
131
- .ele('add', { key: 'Username', value: owner })
132
- .up()
133
- .ele('add', {
134
- key: 'ClearTextPassword',
135
- value: process.env.NUGET_AUTH_TOKEN
136
- })
137
- .up()
138
- .up();
154
+ packageSourceCredentials.push({
155
+ [key]: [
156
+ {
157
+ add: [],
158
+ ':@': {
159
+ '@_key': 'Username',
160
+ '@_value': owner
161
+ }
162
+ },
163
+ {
164
+ add: [],
165
+ ':@': {
166
+ '@_key': 'ClearTextPassword',
167
+ '@_value': process.env.NUGET_AUTH_TOKEN
168
+ }
169
+ }
170
+ ]
171
+ });
172
+ });
173
+ xmlSource[1].configuration.push({
174
+ packageSourceCredentials
139
175
});
140
- // 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),
141
- // use this for the value above
142
- // process.platform == 'win32'
143
- // ? '%NUGET_AUTH_TOKEN%'
144
- // : '$NUGET_AUTH_TOKEN'
145
- const output = xml.end({ pretty: true });
176
+ const xmlBuilderOptions = {
177
+ format: true,
178
+ ignoreAttributes: false,
179
+ preserveOrder: true,
180
+ allowBooleanAttributes: true,
181
+ suppressBooleanAttributes: true,
182
+ suppressEmptyNode: true
183
+ };
184
+ const builder = new fast_xml_parser_1.XMLBuilder(xmlBuilderOptions);
185
+ const output = builder.build(xmlSource).trim();
146
186
fs.writeFileSync(tempFileLocation, output);
147
187
}
148
188
0 commit comments