Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add scopeProfiles feature to package version create #416

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions src/package/packageVersionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,27 +504,39 @@ export class PackageVersionCreate {
);
}

// don't package the profiles from any un-packagedMetadata dir in the project
const profileExcludeDirs = this.project
.getPackageDirectories()
.map((packageDir) => (packageDir as PackageDescriptorJson).unpackagedMetadata?.path)
.filter((packageDirPath) => packageDirPath) as string[];

const typesArr =
this.options?.profileApi?.filterAndGenerateProfilesForManifest(packageXmlAsJson.types, profileExcludeDirs) ??
packageXmlAsJson.types;

// Next generate profiles and retrieve any profiles that were excluded because they had no matching nodes.
const excludedProfiles = this.options?.profileApi?.generateProfiles(
packageVersProfileFolder,
typesArr,
profileExcludeDirs
);
if (this.packageObject.scopeProfiles) {
this.logger.debug(
shetzel marked this conversation as resolved.
Show resolved Hide resolved
`packageDirectory: ${this.packageObject.name} has 'scopeProfiles' set, so only including profiles from within this directory`
);
} else {
// don't package the profiles from any un-packagedMetadata dir in the project
const profileExcludeDirs = this.project
.getPackageDirectories()
.map((packageDir) => (packageDir as PackageDescriptorJson).unpackagedMetadata?.path)
.filter((packageDirPath) => packageDirPath) as string[];

let debugMsg = 'Searching for profiles to include from all packageDirectories';
if (profileExcludeDirs?.length) {
debugMsg += ` excluding these unpackagedMetadata dirs: ${profileExcludeDirs.toString()}`;
}
this.logger.debug(debugMsg);

packageXmlAsJson.types = typesArr.map((type) => {
if (type.name !== 'Profile') return type;
return { ...type, members: type.members.filter((m) => !excludedProfiles?.includes(m)) };
});
const typesArr =
this.options?.profileApi?.filterAndGenerateProfilesForManifest(packageXmlAsJson.types, profileExcludeDirs) ??
packageXmlAsJson.types;

// Next generate profiles and retrieve any profiles that were excluded because they had no matching nodes.
const excludedProfiles = this.options?.profileApi?.generateProfiles(
packageVersProfileFolder,
typesArr,
profileExcludeDirs
);

packageXmlAsJson.types = typesArr.map((type) => {
if (type.name !== 'Profile') return type;
return { ...type, members: type.members.filter((m) => !excludedProfiles?.includes(m)) };
});
}

const xml = packageXmlJsonToXmlString(packageXmlAsJson);
await fs.promises.writeFile(path.join(packageVersMetadataFolder, 'package.xml'), xml, 'utf-8');
Expand Down Expand Up @@ -629,7 +641,10 @@ export class PackageVersionCreate {

this.packageId = this.project.getPackageIdFromAlias(packageName) ?? packageName;

this.options.profileApi = await this.resolveUserLicenses(!!this.packageObject.includeProfileUserLicenses);
this.options.profileApi = await PackageProfileApi.create({
project: this.project,
includeUserLicenses: !!this.packageObject.includeProfileUserLicenses,
});

// At this point, the packageIdFromAlias should have been resolved to an Id. Now, we
// need to validate that the Id is correct.
Expand Down Expand Up @@ -687,13 +702,6 @@ export class PackageVersionCreate {
return this.pkg.getType();
}

private async resolveUserLicenses(includeUserLicenses: boolean): Promise<PackageProfileApi> {
return PackageProfileApi.create({
project: this.project,
includeUserLicenses,
});
}

private async validateOptionsForPackageType(): Promise<void> {
if ((await this.getPackageType()) === 'Unlocked') {
// Don't allow scripts in unlocked packages
Expand All @@ -720,6 +728,7 @@ export class PackageVersionCreate {
delete packageDescriptorJson.branch; // for client-side use only, not needed
delete packageDescriptorJson.fullPath; // for client-side use only, not needed
delete packageDescriptorJson.name; // for client-side use only, not needed
delete packageDescriptorJson.scopeProfiles; // for client-side use only, not needed
return packageDescriptorJson;
}

Expand Down
80 changes: 79 additions & 1 deletion test/package/packageVersionCreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import * as fs from 'fs';
import { instantiateContext, MockTestOrgData, restoreContext, stubContext } from '@salesforce/core/lib/testSetup';
import { assert, expect } from 'chai';
import { Connection, SfProject } from '@salesforce/core';
import { Connection, Logger, SfProject } from '@salesforce/core';
import {
MetadataResolver,
PackageVersionCreate,
Expand Down Expand Up @@ -120,6 +120,7 @@ describe('Package Version Create', () => {
const pvc = new PackageVersionCreate({ connection, project, packageId });
try {
await pvc.createPackageVersion();
expect(false, 'package version create should have failed').to.be.true;
} catch (e) {
assert(e instanceof Error);
expect(e.message).to.equal(
Expand Down Expand Up @@ -774,6 +775,83 @@ describe('Package Version Create', () => {
expect(excludedDirsFilter).to.contain('unpackaged-force-app');
});

it('should only package profiles in the package dir when scopeProfiles = true', async () => {
await project.getSfProjectJson().write({
packageDirectories: [
{
path: 'pkg',
package: 'dep',
versionName: 'ver 0.1',
versionNumber: '0.1.0.NEXT',
default: false,
name: 'pkg',
unpackagedMetadata: {
path: 'unpackaged-pkg',
},
},
{
path: 'force-app',
package: 'TEST',
versionName: 'ver 0.1',
versionNumber: '0.1.0.NEXT',
default: true,
ancestorId: 'TEST2',
scopeProfiles: true,
unpackagedMetadata: {
path: 'unpackaged-force-app',
},
seedMetadata: {
path: 'seed',
},
dependencies: [
{
package: 'DEP@0.1.0-1',
},
],
},
{
path: 'unpackaged-pkg',
},
{
path: 'unpackaged-force-app',
},
],
packageAliases: {
TEST: packageId,
TEST2: '05i3i000000Gmj6XXX',
DEP: '05i3i000000Gmj6XXX',
'DEP@0.1.0-1': '04t3i000002eyYXXXX',
},
});
const loggerSpy = $$.SANDBOX.spy(Logger.prototype, 'debug');
const pvc = new PackageVersionCreate({ connection, project, packageId });
const profileSpyGenerate = $$.SANDBOX.spy(PackageProfileApi.prototype, 'generateProfiles');
const profileSpyFilter = $$.SANDBOX.spy(PackageProfileApi.prototype, 'filterAndGenerateProfilesForManifest');
stubConvert();
const result = await pvc.createPackageVersion();

expect(result).to.have.all.keys(
'Branch',
'ConvertedFromVersionId',
'CreatedBy',
'CreatedDate',
'Error',
'HasMetadataRemoved',
'Id',
'Package2Id',
'Package2VersionId',
'Status',
'SubscriberPackageVersionId',
'Tag'
);
expect(loggerSpy.called).to.be.true;
const logMsg =
"packageDirectory: force-app has 'scopeProfiles' set, so only including profiles from within this directory";
expect(loggerSpy.calledWith(logMsg)).to.be.true;
expect(profileSpyGenerate.called).to.be.false;
expect(profileSpyFilter.called).to.be.false;
});

it('should not package profiles from outside of project package directories', async () => {
const pkgProfileApi = await PackageProfileApi.create({ project, includeUserLicenses: false });
const types = [
Expand Down