Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
support branched package dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
zhebinliu committed May 30, 2023
1 parent 8ca17f9 commit 7e4f5e2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 19 deletions.
71 changes: 55 additions & 16 deletions packages/core/src/package/dependencies/PackageDependencyResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import lodash = require('lodash');
import Git from '../../git/Git';
import GitTags from '../../git/GitTags';
import Package2VersionFetcher, { Package2Version } from '../version/Package2VersionFetcher';
import SFPLogger, { LoggerLevel } from '@dxatscale/sfp-logger';


/**
* Resolves package dependency versions to their exact versions
Expand Down Expand Up @@ -39,7 +41,6 @@ export default class PackageDependencyResolver {
if (this.packagesToBeResolved && !this.packagesToBeResolved.includes(packageDirectory.package)) {
continue;
}

if (packageDirectory.dependencies && Array.isArray(packageDirectory.dependencies)) {
for (let i = 0; i < packageDirectory.dependencies.length; i++) {
let dependency = packageDirectory.dependencies[i];
Expand All @@ -55,16 +56,45 @@ export default class PackageDependencyResolver {
continue;
}

if (this.packagesToBeSkipped && this.packagesToBeSkipped.includes(dependency.package)) {
if (this.packagesToBeSkipped && this.packagesToBeSkipped.includes(dependency.package) && !dependency.branch) {
// Dependency is part of the same build, will be resolved when new version is created
continue;
}
let package2VersionForDependency: any = '';
if ( dependency.branch && dependency.branch !== '' ) {
SFPLogger.log(`Specified branch: ${dependency.branch} for dependency: ${dependency.package}`, LoggerLevel.INFO);
package2VersionForDependency = await this.getPackage2VersionForDependency(
this.conn,
dependency,
packageVersionId,
dependency.branch
);
SFPLogger.log(`Fetched latest branched package of ${dependency.package},`
+`version: ${package2VersionForDependency.MajorVersion}.`
+`${package2VersionForDependency.MinorVersion}.`
+`${package2VersionForDependency.PatchVersion}.`
+`${package2VersionForDependency.BuildNumber}`, LoggerLevel.INFO);

const package2VersionForDependency = await this.getPackage2VersionForDependency(
this.conn,
dependency,
packageVersionId
);
let branchedPackageAlias = `${dependency.package}@`
+`${package2VersionForDependency.MajorVersion}.`
+`${package2VersionForDependency.MinorVersion}.`
+`${package2VersionForDependency.PatchVersion}.`
+`${package2VersionForDependency.BuildNumber}-`
+`${dependency.branch}`;
dependency.package = branchedPackageAlias;
this.projectConfig.packageAliases[branchedPackageAlias] = package2VersionForDependency.SubscriberPackageVersionId;
delete dependency.versionNumber;
delete dependency.branch;
continue;

}else {
package2VersionForDependency = await this.getPackage2VersionForDependency(
this.conn,
dependency,
packageVersionId
);
}


if (package2VersionForDependency == null) {
packageDirectory.dependencies.splice(i, 1);
Expand All @@ -74,7 +104,6 @@ export default class PackageDependencyResolver {
}
}
}

return this.projectConfig;
}

Expand All @@ -87,7 +116,8 @@ export default class PackageDependencyResolver {
private async getPackage2VersionForDependency(
conn: Connection,
dependency: { package: string; versionNumber: string },
packageVersionId: string
packageVersionId: string,
branch?: string,
): Promise<Package2Version> {

//Dont hit api's if its only for external dependencies
Expand All @@ -111,11 +141,21 @@ export default class PackageDependencyResolver {
);
} else {
const package2VersionFetcher = new Package2VersionFetcher(conn);
const records = await package2VersionFetcher.fetchByPackage2Id(
packageVersionId,
versionNumber,
true
);
let records;
if( branch ){
records = await package2VersionFetcher.fetchByPackageBranchAndName(
branch,
dependency.package,
versionNumber
);
}else{
records = await package2VersionFetcher.fetchByPackage2Id(
packageVersionId,
versionNumber,
true
);
}

this.package2VersionCache.set(
packageVersionId,
versionNumber,
Expand All @@ -133,13 +173,12 @@ export default class PackageDependencyResolver {
);
}

if (this.projectConfig.packageDirectories.find((dir) => dir.package === dependency.package)) {
if (this.projectConfig.packageDirectories.find((dir) => dir.package === dependency.package && !branch)) {
package2Version = await this.getPackage2VersionFromCurrentBranch(package2Versions, dependency);
} else {
// Take last validated package for external packages
package2Version = package2Versions[0];
}

return package2Version;
}

Expand Down
24 changes: 23 additions & 1 deletion packages/core/src/package/version/Package2VersionFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import semver from 'semver';
*/
export default class Package2VersionFetcher {
private readonly query: string =
'Select SubscriberPackageVersionId, Package2Id, Package2.Name, IsPasswordProtected, IsReleased, MajorVersion, MinorVersion, PatchVersion, BuildNumber, CodeCoverage, HasPassedCodeCoverageCheck from Package2Version ';
'Select SubscriberPackageVersionId, Package2Id, Package2.Name, IsPasswordProtected, IsReleased, MajorVersion, MinorVersion, PatchVersion, BuildNumber, CodeCoverage, HasPassedCodeCoverageCheck, Branch from Package2Version ';

constructor(private conn: Connection) {}

Expand Down Expand Up @@ -65,6 +65,27 @@ export default class Package2VersionFetcher {
const records = await QueryHelper.query<Package2Version>(query, this.conn, true);
return records[0];
}

async fetchByPackageBranchAndName(packageBranch: string, packageName: string, versionNumber?: string,): Promise<Package2Version[]> {
let query = this.query;

let whereClause: string = `where Branch='${packageBranch}' and Package2.Name ='${packageName}' `;
if (versionNumber) {
// TODO: validate version number
const versions = versionNumber.split('.');
if (versions[0]) whereClause += `and MajorVersion=${versions[0]} `;
if (versions[1]) whereClause += `and MinorVersion=${versions[1]} `;
if (versions[2]) whereClause += `and PatchVersion=${versions[2]} `;
}
query += whereClause;

let orderByClause: string = `order by CreatedDate desc`;
query += orderByClause;

const records = await QueryHelper.query<Package2Version>(query, this.conn, true);
return records;

}
}

export interface Package2Version {
Expand All @@ -79,4 +100,5 @@ export interface Package2Version {
BuildNumber: number;
CodeCoverage: { apexCodeCoveragePercentage: number };
HasPassedCodeCoverageCheck: boolean;
Branch: string;
}
30 changes: 28 additions & 2 deletions packages/sfpowerscripts-cli/src/impl/parallelBuilder/BuildImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,17 @@ export default class BuildImpl {
private resolveDependenciesOnCompletedPackage(dependentPackage: string, completedPackage: SfpPackage) {
const pkgDescriptor = ProjectConfig.getPackageDescriptorFromConfig(dependentPackage, this.projectConfig);
const dependency = pkgDescriptor.dependencies.find(
(dependency) => dependency.package === completedPackage.packageName
(dependency) => (dependency.package === completedPackage.packageName) || (dependency.package.includes(`${completedPackage.packageName}@`))
);
dependency.versionNumber = completedPackage.versionNumber;
if( dependency.package.includes(`${completedPackage.packageName}@`) ){
const [packageName, version, branch] = this.splitString(dependency.package);
SFPLogger.log(`New branched package is created for dependency: ${packageName}, update the package version id`, LoggerLevel.INFO);
dependency.package = `${packageName}@${completedPackage.package_version_number}-${branch}`;
this.projectConfig.packageAliases[dependency.package] = completedPackage.package_version_id;
}else{
dependency.versionNumber = completedPackage.versionNumber;
}

}

private getPriorityandTypeOfAPackage(projectConfig: any, pkg: string) {
Expand Down Expand Up @@ -641,4 +649,22 @@ export default class BuildImpl {
}
}

private splitString(input: string): [string, string, string] {
const parts = input.split('@');

if (parts.length === 2) {
const packageName = parts[0];
const versionAndFeature = parts[1].split('-');

if (versionAndFeature.length === 2) {
const version = versionAndFeature[0];
const branch = versionAndFeature[1];

return [packageName, version, branch];
}
}

return ['', '', ''];
}

}

0 comments on commit 7e4f5e2

Please sign in to comment.