|
| 1 | +/* |
| 2 | + * Copyright 2025, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { Connection, Messages, SfError } from '@salesforce/core'; |
| 17 | +import { BundleSObjects } from '../interfaces'; |
| 18 | +import { massageErrorMessage } from '../utils/bundleUtils'; |
| 19 | + |
| 20 | +Messages.importMessagesDirectory(__dirname); |
| 21 | +const messages = Messages.loadMessages('@salesforce/packaging', 'bundle_installed_list'); |
| 22 | + |
| 23 | +export class PackageBundleInstalledList { |
| 24 | + /** |
| 25 | + * Get all installed package bundles in the target org |
| 26 | + * |
| 27 | + * @param connection - Connection to the target org (where bundles are installed) |
| 28 | + * @returns Array of installed bundle versions with their component packages |
| 29 | + */ |
| 30 | + public static async getInstalledBundles( |
| 31 | + connection: Connection |
| 32 | + ): Promise<BundleSObjects.InstalledPackageBundleVersion[]> { |
| 33 | + try { |
| 34 | + // Query InstalledPkgBundleVersion directly from the target org |
| 35 | + // Note: PackageBundle and PackageBundleVersion are CROSSORGFOREIGNKEY fields |
| 36 | + // Use CreatedDate and LastModifiedDate as proxies for InstalledDate and LastUpgradedDate |
| 37 | + const query = |
| 38 | + 'SELECT Id, BundleName, BundleVersionName, MajorVersion, MinorVersion, ' + |
| 39 | + 'PackageBundleId, PackageBundleVersionId, CreatedDate, LastModifiedDate ' + |
| 40 | + 'FROM InstalledPkgBundleVersion ' + |
| 41 | + 'ORDER BY CreatedDate DESC'; |
| 42 | + |
| 43 | + const queryResult = await connection.autoFetchQuery<BundleSObjects.InstalledBundleRecord>(query, { |
| 44 | + tooling: true, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!queryResult.records || queryResult.records.length === 0) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + // For each installed bundle, get component details |
| 52 | + const installedBundles = await Promise.all( |
| 53 | + queryResult.records.map(async (record) => { |
| 54 | + // Get component details from PkgBundleVersionComponentInstallReq |
| 55 | + // We need to find the install request that corresponds to this installed bundle |
| 56 | + const components = await PackageBundleInstalledList.getBundleComponents(connection, record.Id); |
| 57 | + |
| 58 | + return { |
| 59 | + Id: record.Id, |
| 60 | + BundleName: record.BundleName, |
| 61 | + BundleId: record.PackageBundleId ?? '', |
| 62 | + BundleVersionId: record.PackageBundleVersionId ?? '', |
| 63 | + BundleVersionName: record.BundleVersionName, |
| 64 | + MajorVersion: record.MajorVersion, |
| 65 | + MinorVersion: record.MinorVersion, |
| 66 | + Description: '', |
| 67 | + InstalledDate: record.CreatedDate ?? '', |
| 68 | + LastUpgradedDate: record.LastModifiedDate ?? record.CreatedDate ?? '', |
| 69 | + Components: components, |
| 70 | + }; |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + // Filter out any null results |
| 75 | + return installedBundles.filter( |
| 76 | + (bundle): bundle is BundleSObjects.InstalledPackageBundleVersion => bundle !== null |
| 77 | + ); |
| 78 | + } catch (err) { |
| 79 | + const error = |
| 80 | + err instanceof Error ? err : new Error(messages.getMessage('failedToGetInstalledBundles')); |
| 81 | + throw SfError.wrap(massageErrorMessage(error)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the component packages for a specific installed bundle |
| 87 | + * |
| 88 | + * @param connection - Connection to the target org |
| 89 | + * @param installedBundleVersionId - The InstalledPackageBundleVersion ID |
| 90 | + * @returns Array of components with expected and actual package versions |
| 91 | + */ |
| 92 | + private static async getBundleComponents( |
| 93 | + connection: Connection, |
| 94 | + installedBundleVersionId: string |
| 95 | + ): Promise<BundleSObjects.InstalledPackageBundleVersionComponent[]> { |
| 96 | + // First find the install request that created this installed bundle |
| 97 | + const installRequestQuery = |
| 98 | + 'SELECT Id ' + |
| 99 | + 'FROM PkgBundleVersionInstallReq ' + |
| 100 | + `WHERE InstalledPkgBundleVersionId = '${installedBundleVersionId}' ` + |
| 101 | + 'ORDER BY CreatedDate DESC LIMIT 1'; |
| 102 | + |
| 103 | + const installRequestResult = await connection.autoFetchQuery<BundleSObjects.InstallRequestRecord>( |
| 104 | + installRequestQuery, |
| 105 | + { |
| 106 | + tooling: true, |
| 107 | + } |
| 108 | + ); |
| 109 | + |
| 110 | + if (!installRequestResult.records || installRequestResult.records.length === 0) { |
| 111 | + return []; |
| 112 | + } |
| 113 | + |
| 114 | + const installRequestId = installRequestResult.records[0].Id; |
| 115 | + |
| 116 | + // Query expected packages from PkgBundleVerCpntIstlReq (abbreviated name) |
| 117 | + // These have SubscriberPackageVersion as a foreign key showing what was expected |
| 118 | + // Use InstalledComponent to get the actual package name from InstalledSubscriberPackage |
| 119 | + const componentQuery = |
| 120 | + 'SELECT SubscriberPackageVersion.Id, SubscriberPackageVersion.SubscriberPackageId, ' + |
| 121 | + 'SubscriberPackageVersion.MajorVersion, SubscriberPackageVersion.MinorVersion, ' + |
| 122 | + 'SubscriberPackageVersion.PatchVersion, SubscriberPackageVersion.BuildNumber, ' + |
| 123 | + 'InstalledComponent.SubscriberPackage.Name, ' + |
| 124 | + 'SequenceOrder ' + |
| 125 | + 'FROM PkgBundleVerCpntIstlReq ' + |
| 126 | + `WHERE PkgBundleVersionInstallReqId = '${installRequestId}' ` + |
| 127 | + 'ORDER BY SequenceOrder'; |
| 128 | + |
| 129 | + const componentResult = await connection.autoFetchQuery<BundleSObjects.BundleComponentInstallRecord>( |
| 130 | + componentQuery, |
| 131 | + { |
| 132 | + tooling: true, |
| 133 | + } |
| 134 | + ); |
| 135 | + |
| 136 | + if (!componentResult.records || componentResult.records.length === 0) { |
| 137 | + return []; |
| 138 | + } |
| 139 | + |
| 140 | + // Query actual installed packages from the org |
| 141 | + const installedPackagesQuery = |
| 142 | + 'SELECT Id, SubscriberPackageId, SubscriberPackage.Name, SubscriberPackageVersion.Id, ' + |
| 143 | + 'SubscriberPackageVersion.MajorVersion, SubscriberPackageVersion.MinorVersion, ' + |
| 144 | + 'SubscriberPackageVersion.PatchVersion, SubscriberPackageVersion.BuildNumber ' + |
| 145 | + 'FROM InstalledSubscriberPackage ' + |
| 146 | + 'ORDER BY SubscriberPackageId'; |
| 147 | + |
| 148 | + const installedResult = await connection.autoFetchQuery<BundleSObjects.InstalledPackageRecord>( |
| 149 | + installedPackagesQuery, |
| 150 | + { |
| 151 | + tooling: true, |
| 152 | + } |
| 153 | + ); |
| 154 | + |
| 155 | + // Create a map of actually installed packages by subscriber package ID |
| 156 | + const installedMap = new Map<string, { name: string; version: string }>(); |
| 157 | + if (installedResult.records) { |
| 158 | + installedResult.records.forEach((pkg) => { |
| 159 | + const versionNumber = `${pkg.SubscriberPackageVersion.MajorVersion}.${pkg.SubscriberPackageVersion.MinorVersion}.${pkg.SubscriberPackageVersion.PatchVersion}.${pkg.SubscriberPackageVersion.BuildNumber}`; |
| 160 | + installedMap.set(pkg.SubscriberPackageId, { |
| 161 | + name: pkg.SubscriberPackage.Name, |
| 162 | + version: versionNumber, |
| 163 | + }); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + // Compare expected vs actual |
| 168 | + return componentResult.records.map((record) => { |
| 169 | + // Expected (what the bundle was supposed to install) |
| 170 | + const expectedVersion = `${record.SubscriberPackageVersion.MajorVersion}.${record.SubscriberPackageVersion.MinorVersion}.${record.SubscriberPackageVersion.PatchVersion}.${record.SubscriberPackageVersion.BuildNumber}`; |
| 171 | + const expectedName = record.InstalledComponent?.SubscriberPackage?.Name ?? 'Unknown'; |
| 172 | + const subscriberPackageId = record.SubscriberPackageVersion.SubscriberPackageId; |
| 173 | + |
| 174 | + // Actual (what's currently installed in the org) |
| 175 | + const installed = installedMap.get(subscriberPackageId); |
| 176 | + |
| 177 | + return { |
| 178 | + ExpectedPackageName: expectedName, |
| 179 | + ExpectedPackageVersionNumber: expectedVersion, |
| 180 | + ActualPackageName: installed ? installed.name : 'Uninstalled', |
| 181 | + ActualPackageVersionNumber: installed ? installed.version : 'N/A', |
| 182 | + }; |
| 183 | + }); |
| 184 | + } |
| 185 | +} |
0 commit comments