Skip to content

Commit

Permalink
Merge pull request #68 from tiagonnascimento/feature-disable-tracking
Browse files Browse the repository at this point in the history
feat: adding the possibility to disable source tracking before deploy
  • Loading branch information
tiagonnascimento authored Apr 29, 2024
2 parents 1efaa3a + f41744d commit 8019e26
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 89 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Each release is a different release. It may be necessary to perform deployments
"preDestructiveChanges": "manifest/preDestructive/destructiveChanges.xml",
"postDestructiveChanges": "manifest/postDestructive/destructiveChanges.xml",
"timeout": "33",
"ignoreWarnings": true
"ignoreWarnings": true,
"disableTracking": true
},
{
"type": "datapack",
Expand All @@ -112,6 +113,7 @@ Some considerations regarding this configuration file:
- If the project has multiple metadata directories (after all, an SFDX project can have other metadata directories in addition to `force-app/main/default`), the classPath parameter must be specified when the `testLevel` is set to `RunSpecifiedTests`. Otherwise, test classes will only be searched in the default directory;
- All other parameters are optional, including pre/post destructive changes;
- If the `testLevel` is not specified, the script will deploy using `RunLocalTests`;
- Normally on your pipeline sandboxes you will deploy using `enableTracking` as a `false` - which is the default value. If you want to use source tracking you can use this value as `true`;
- For other types of deployments:
- If `type` is `datapack`, the `manifestFile` field is required;
- If `type` is `anonymousApex`, the `apexScript` field is required;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"postpack": "pinst --enable && shx rm -f oclif.manifest.json",
"prepack": "pinst --disable && sf-prepack",
"test": "wireit",
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
"test:nuts": "echo NO NUTS for now",
"test:only": "wireit",
"version": "oclif readme"
},
Expand Down Expand Up @@ -197,4 +197,4 @@
}
},
"author": "Tiago Nascimento"
}
}
190 changes: 104 additions & 86 deletions src/commands/builds/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type Build = {
preDestructiveChanges?: string;
postDestructiveChanges?: string;
testLevel?: string;
enableTracking?: string;
classPath?: string;
ignoreWarnings?: boolean;
timeout?: string;
Expand Down Expand Up @@ -209,98 +210,109 @@ export default class BuildsDeploy extends SfCommand<BuildsDeployResult> {

public static auth(authParms: AuthParameters): void {
console.log(' --- auth --- ');
const buildCommand = 'sf' as string;
const buildCommandArgs = [];
const authCommand = 'sf' as string;
const authCommandArgs = [];
const instanceURL = authParms.instanceUrl ? authParms.instanceUrl.toString() : 'https://login.salesforce.com';

buildCommandArgs.push('org');
buildCommandArgs.push('login');
buildCommandArgs.push('jwt');
buildCommandArgs.push('--instance-url');
buildCommandArgs.push(instanceURL);
buildCommandArgs.push('--client-id');
buildCommandArgs.push(authParms.clientId!);
buildCommandArgs.push('--jwt-key-file');
buildCommandArgs.push(authParms.jwtKeyFile!);
buildCommandArgs.push('--username');
buildCommandArgs.push(authParms.username!);

execCommand(buildCommand, buildCommandArgs);
authCommandArgs.push('org');
authCommandArgs.push('login');
authCommandArgs.push('jwt');
authCommandArgs.push('--instance-url');
authCommandArgs.push(instanceURL);
authCommandArgs.push('--client-id');
authCommandArgs.push(authParms.clientId!);
authCommandArgs.push('--jwt-key-file');
authCommandArgs.push(authParms.jwtKeyFile!);
authCommandArgs.push('--username');
authCommandArgs.push(authParms.username!);

execCommand(authCommand, authCommandArgs);
}

public static deploy(builds: Build[], username: string): void {
console.log(' --- deploy --- ');
for (const build of builds) {
console.log(` --- build type: ${build.type} --- `);
let buildCommand: string;
let buildCommandArgs: string[] = [];

if (build.type === 'metadata') {
buildCommand = 'sf';
buildCommandArgs.push('project');
buildCommandArgs.push('deploy');
buildCommandArgs.push('start');
buildCommandArgs.push('--verbose');
buildCommandArgs.push('--manifest');
buildCommandArgs.push(build.manifestFile!);
buildCommandArgs.push('--target-org');
buildCommandArgs.push(username);
if (build.preDestructiveChanges) {
buildCommandArgs.push('--pre-destructive-changes');
buildCommandArgs.push(build.preDestructiveChanges);
}
if (build.postDestructiveChanges) {
buildCommandArgs.push('--post-destructive-changes');
buildCommandArgs.push(build.postDestructiveChanges);
}
if (build.testLevel === 'RunSpecifiedTests') {
const testClasses = BuildsDeploy.getApexTestClassesFromPackageXml(build.manifestFile!, build.classPath);
if (testClasses.length === 0) {
throw new Error('You should have at least one test class on your package.xml');
}
buildCommandArgs.push('--test-level');
buildCommandArgs.push('RunSpecifiedTests');
buildCommandArgs.push('--tests');
buildCommandArgs.push(testClasses.join(','));
} else if (build.testLevel) {
buildCommandArgs.push('--test-level');
buildCommandArgs.push(build.testLevel);
} else {
buildCommandArgs.push('--test-level');
buildCommandArgs.push('RunLocalTests');
}
if (build.ignoreWarnings) {
buildCommandArgs.push('--ignore-warnings');
}
if (build.timeout) {
buildCommandArgs.push('--wait');
buildCommandArgs.push(build.timeout);
public static disableTracking(username: string): void {
console.log(' --- disabling source tracking on target sandbox --- ');
const configCommand = 'sf' as string;
const configCommandArgs: string[] = [];
configCommandArgs.push('org');
configCommandArgs.push('disable');
configCommandArgs.push('tracking');
configCommandArgs.push('--target-org');
configCommandArgs.push(username);

execCommand(configCommand, configCommandArgs);
}

public static deploy(build: Build, username: string): void {
console.log(` --- build type: ${build.type} --- `);

let buildCommand: string;
let buildCommandArgs: string[] = [];

if (build.type === 'metadata') {
buildCommand = 'sf';
buildCommandArgs.push('project');
buildCommandArgs.push('deploy');
buildCommandArgs.push('start');
buildCommandArgs.push('--verbose');
buildCommandArgs.push('--manifest');
buildCommandArgs.push(build.manifestFile!);
buildCommandArgs.push('--target-org');
buildCommandArgs.push(username);
if (build.preDestructiveChanges) {
buildCommandArgs.push('--pre-destructive-changes');
buildCommandArgs.push(build.preDestructiveChanges);
}
if (build.postDestructiveChanges) {
buildCommandArgs.push('--post-destructive-changes');
buildCommandArgs.push(build.postDestructiveChanges);
}
if (build.testLevel === 'RunSpecifiedTests') {
const testClasses = BuildsDeploy.getApexTestClassesFromPackageXml(build.manifestFile!, build.classPath);
if (testClasses.length === 0) {
throw new Error('You should have at least one test class on your package.xml');
}
buildCommandArgs.push('--json');
} else if (build.type === 'datapack') {
buildCommand = 'vlocity';
buildCommandArgs.push('-sfdx.username');
buildCommandArgs.push(username);
buildCommandArgs.push('-job');
buildCommandArgs.push(build.manifestFile!);
buildCommandArgs.push('packDeploy');
} else if (build.type === 'anonymousApex') {
buildCommand = 'sf';
buildCommandArgs.push('apex');
buildCommandArgs.push('run');
buildCommandArgs.push('--target-org');
buildCommandArgs.push(username);
buildCommandArgs.push('--file');
buildCommandArgs.push(build.apexScript!);
buildCommandArgs.push('--json');
} else if (build.type === 'command') {
const [head, ...tail] = build.command!.split(' ');
buildCommand = head;
buildCommandArgs = tail;
buildCommandArgs.push('--test-level');
buildCommandArgs.push('RunSpecifiedTests');
buildCommandArgs.push('--tests');
buildCommandArgs.push(testClasses.join(','));
} else if (build.testLevel) {
buildCommandArgs.push('--test-level');
buildCommandArgs.push(build.testLevel);
} else {
buildCommandArgs.push('--test-level');
buildCommandArgs.push('RunLocalTests');
}

execCommand(buildCommand!, buildCommandArgs);
if (build.ignoreWarnings) {
buildCommandArgs.push('--ignore-warnings');
}
if (build.timeout) {
buildCommandArgs.push('--wait');
buildCommandArgs.push(build.timeout);
}
buildCommandArgs.push('--json');
} else if (build.type === 'datapack') {
buildCommand = 'vlocity';
buildCommandArgs.push('-sfdx.username');
buildCommandArgs.push(username);
buildCommandArgs.push('-job');
buildCommandArgs.push(build.manifestFile!);
buildCommandArgs.push('packDeploy');
} else if (build.type === 'anonymousApex') {
buildCommand = 'sf';
buildCommandArgs.push('apex');
buildCommandArgs.push('run');
buildCommandArgs.push('--target-org');
buildCommandArgs.push(username);
buildCommandArgs.push('--file');
buildCommandArgs.push(build.apexScript!);
buildCommandArgs.push('--json');
} else if (build.type === 'command') {
const [head, ...tail] = build.command!.split(' ');
buildCommand = head;
buildCommandArgs = tail;
}

execCommand(buildCommand!, buildCommandArgs);
}

public async run(): Promise<BuildsDeployResult> {
Expand Down Expand Up @@ -332,7 +344,13 @@ export default class BuildsDeploy extends SfCommand<BuildsDeployResult> {
}

try {
BuildsDeploy.deploy(builds, userNameOrAlias);
console.log(' --- deploy --- ');
for (const build of builds) {
if (build.type === 'metadata' && !build.enableTracking) {
BuildsDeploy.disableTracking(userNameOrAlias);
}
BuildsDeploy.deploy(build, userNameOrAlias);
}
} catch (error) {
console.error('Error trying to run the build');
console.error(error);
Expand Down
1 change: 1 addition & 0 deletions test/commands/builds/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('BuildsDeploy', () => {
postDestructiveChanges: 'path/to/postDestructiveChanges.xml',
ignoreWarnings: true,
timeout: 60,
enableTracking: true,
},
{
type: 'datapack',
Expand Down

0 comments on commit 8019e26

Please sign in to comment.