Skip to content

Commit

Permalink
Improves upgrade app action. Closes #359 (#371)
Browse files Browse the repository at this point in the history
## 🎯 Aim

Improves upgrade app action by allowing users to specify only the
relative path and then concatenate it under the hood with the tenant URL

## 📷 Result

![image](https://github.com/user-attachments/assets/6699f07d-ce5c-45d0-bfaf-5dd26e744bac)

## ✅ What was done

[X] Prompt for the relative site URL when upgrading tenant app catalog
apps. Concatenate the same under the hood with the tenant URL.

## 🔗 Related issue

Closes: #359
  • Loading branch information
Saurabh7019 authored Dec 6, 2024
1 parent 72b7828 commit fece84f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/panels/CommandPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class CommandPanel {
new ActionTreeItem('Remove', '', undefined, undefined, Commands.removeAppCatalogApp, [app.ID, app.Title], ContextKeys.removeApp),
new ActionTreeItem('Enable', '', undefined, undefined, Commands.enableAppCatalogApp, [app.Title, tenantAppCatalogUrl, app.Enabled], ContextKeys.enableApp),
new ActionTreeItem('Disable', '', undefined, undefined, Commands.disableAppCatalogApp, [app.Title, tenantAppCatalogUrl, app.Enabled], ContextKeys.disableApp),
new ActionTreeItem('Upgrade', '', undefined, undefined, Commands.upgradeAppCatalogApp, [app.ID, app.Title], ContextKeys.upgradeApp)
new ActionTreeItem('Upgrade', '', undefined, undefined, Commands.upgradeAppCatalogApp, [app.ID, app.Title, tenantAppCatalogUrl, true], ContextKeys.upgradeApp)
]
)
);
Expand Down Expand Up @@ -252,7 +252,7 @@ export class CommandPanel {
new ActionTreeItem('Remove', '', undefined, undefined, Commands.removeAppCatalogApp, [app.ID, app.Title, siteAppCatalogUrl], ContextKeys.removeApp),
new ActionTreeItem('Enable', '', undefined, undefined, Commands.enableAppCatalogApp, [app.Title, siteAppCatalogUrl, app.Enabled], ContextKeys.enableApp),
new ActionTreeItem('Disable', '', undefined, undefined, Commands.disableAppCatalogApp, [app.Title, siteAppCatalogUrl, app.Enabled], ContextKeys.disableApp),
new ActionTreeItem('Upgrade', '', undefined, undefined, Commands.upgradeAppCatalogApp, [app.ID, app.Title, siteAppCatalogUrl], ContextKeys.upgradeApp)
new ActionTreeItem('Upgrade', '', undefined, undefined, Commands.upgradeAppCatalogApp, [app.ID, app.Title, siteAppCatalogUrl, false], ContextKeys.upgradeApp)
]
)
);
Expand Down
48 changes: 30 additions & 18 deletions src/services/actions/CliActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,33 +241,45 @@ export class CliActions {
return;
}

const [appID, appTitle, appCatalogUrl] = actionNode.command.arguments;
const [appID, appTitle, appCatalogUrl, isTenantApp] = actionNode.command.arguments;

const siteUrl = appCatalogUrl?.trim() || await window.showInputBox({
prompt: 'Enter the URL of the site to upgrade the app in',
placeHolder: 'https://contoso.sharepoint.com/sites/sales',
validateInput: (input) => (input.trim() ? undefined : 'Site URL cannot be empty')
});
let siteUrl: string = appCatalogUrl;

if (!siteUrl) {
Notifications.warning('No site URL provided. App upgrade aborted.');
return;
if (isTenantApp) {
const relativeUrl = await window.showInputBox({
prompt: 'Enter the relative URL of the site to upgrade the app in',
placeHolder: 'e.g., sites/sales or leave blank for root site',
validateInput: (input) => {
const trimmedInput = input.trim();

if (trimmedInput.startsWith('https://')) {
return 'Please provide a relative URL, not an absolute URL.';
}
if (trimmedInput.startsWith('/')) {
return 'Please provide a relative URL without a leading slash.';
}

return undefined;
}
});

if (relativeUrl === undefined) {
Notifications.warning('No site URL provided. App upgrade aborted.');
return;
}

siteUrl = `${new URL(appCatalogUrl).origin}/${relativeUrl.trim()}`;
}

const commandOptions: any = {
id: appID,
...(appCatalogUrl?.trim()
? { appCatalogScope: 'sitecollection', siteUrl, }
: { siteUrl })
...(isTenantApp
? { siteUrl }
: { appCatalogScope: 'sitecollection', siteUrl })
};

await CliExecuter.execute('spo app upgrade', 'json', commandOptions);

Notifications.info(`App '${appTitle}' has been successfully upgraded.`);

// refresh the environmentTreeView
await commands.executeCommand('spfx-toolkit.refreshAppCatalogTreeView');

Notifications.info(`App '${appTitle}' has been successfully upgraded on site '${siteUrl}'.`);
} catch (e: any) {
const message = e?.message || 'An unexpected error occurred during the app upgrade.';
Notifications.error(message);
Expand Down

0 comments on commit fece84f

Please sign in to comment.