Skip to content

Commit 7f6d729

Browse files
committed
Fallback to active PAC auth environment
Use the active pac auth list Environment Url when pac env who does not expose a usable Org URL. This avoids blocking template import when PAC has an active profile but pac env who cannot resolve it. Copilot-Session: a9952624-0a25-4d6e-ae67-537c962a8a60
1 parent 0972f2f commit 7f6d729

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

plugins/power-pages/scripts/lib/validation-helpers.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,30 @@ function parseEnvironmentUrl(whoOutput) {
344344
}
345345
}
346346

347+
function parseActiveAuthListEnvironmentUrl(authListOutput) {
348+
for (const line of String(authListOutput || '').split(/\r?\n/)) {
349+
if (!/^\s*\[\d+\]\s+\*/.test(line)) continue;
350+
const urls = line.match(/https:\/\/[^\s]+/gi) || [];
351+
const environmentUrl = urls[urls.length - 1];
352+
if (!environmentUrl) continue;
353+
try {
354+
return validateDataverseEnvironmentUrl(environmentUrl);
355+
} catch {
356+
return null;
357+
}
358+
}
359+
return null;
360+
}
361+
347362
function getEnvironmentUrl() {
348363
try {
349364
const output = execSync('pac env who', { encoding: 'utf8', timeout: 15000 });
350-
return parseEnvironmentUrl(output);
365+
const envUrl = parseEnvironmentUrl(output);
366+
if (envUrl) return envUrl;
367+
} catch {}
368+
369+
try {
370+
return parseActiveAuthListEnvironmentUrl(execSync('pac auth list', { encoding: 'utf8', timeout: 15000 }));
351371
} catch {
352372
return null;
353373
}
@@ -519,6 +539,7 @@ module.exports = {
519539
odataGetAll,
520540
getEnvironmentUrl,
521541
parseEnvironmentUrl,
542+
parseActiveAuthListEnvironmentUrl,
522543
getPacAuthInfo,
523544
CLOUD_TO_API,
524545
CLOUD_TO_SITE_DOMAIN,

plugins/power-pages/scripts/tests/validation-helpers.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ test('parseEnvironmentUrl returns null when no URL label is present (and on empt
261261
assert.equal(parseEnvironmentUrl(null), null);
262262
});
263263

264+
test('parseActiveAuthListEnvironmentUrl extracts the active auth profile Environment Url', () => {
265+
const { parseActiveAuthListEnvironmentUrl } = require(helpersPath);
266+
const output = [
267+
'Index Active Kind Name User Cloud Type Environment Environment Url',
268+
'[1] * UNIVERSAL user@contoso.com Public OperatingSystem PowerPagesProDev https://powerpagesprodev.crm.dynamics.com/',
269+
'[2] UNIVERSAL user@contoso.com Public OperatingSystem OtherEnv https://other.crm.dynamics.com/',
270+
].join('\n');
271+
assert.equal(parseActiveAuthListEnvironmentUrl(output), 'https://powerpagesprodev.crm.dynamics.com');
272+
});
273+
264274
test('getEnvironmentUrl parses the 2.8.x "Org URL:" output via mocked execSync', (t) => {
265275
const originalExecSync = childProcess.execSync;
266276
childProcess.execSync = () => ' Org URL: https://orgABC.crm.dynamics.com/\n';
@@ -271,3 +281,24 @@ test('getEnvironmentUrl parses the 2.8.x "Org URL:" output via mocked execSync',
271281
assert.equal(getEnvironmentUrl(), 'https://orgabc.crm.dynamics.com');
272282
delete require.cache[require.resolve(helpersPath)];
273283
});
284+
285+
test('getEnvironmentUrl falls back to active pac auth list Environment Url when pac env who has no URL', (t) => {
286+
const originalExecSync = childProcess.execSync;
287+
childProcess.execSync = (command) => {
288+
if (command === 'pac env who') return 'No organization selected\n';
289+
if (command === 'pac auth list') {
290+
return [
291+
'Index Active Kind Name User Cloud Type Environment Environment Url',
292+
'[1] * UNIVERSAL user@contoso.com Public OperatingSystem PowerPagesProDev https://powerpagesprodev.crm.dynamics.com/',
293+
].join('\n');
294+
}
295+
throw new Error(`unexpected command: ${command}`);
296+
};
297+
t.after(() => { childProcess.execSync = originalExecSync; });
298+
delete require.cache[require.resolve(helpersPath)];
299+
300+
const { getEnvironmentUrl } = require(helpersPath);
301+
302+
assert.equal(getEnvironmentUrl(), 'https://powerpagesprodev.crm.dynamics.com');
303+
delete require.cache[require.resolve(helpersPath)];
304+
});

0 commit comments

Comments
 (0)