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

296 feature adding mc init #308

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
734ddba
#291 initial commit
DannyCastroVentura Nov 18, 2022
41daf5a
#296 first commit
DannyCastroVentura Nov 18, 2022
42d072b
#296 added user.email and user.name values in process envs and its lo…
Nov 18, 2022
59f5041
#296 removed CONFIG variable not needed
Nov 18, 2022
a2d0c73
#296 added user.email and user.name values in process envs and its lo…
Nov 18, 2022
0de9ccd
#296 dividing logic try catch in order to make each one do a differen…
Nov 21, 2022
3e3bafd
#296 adding chose value of gitPush and value of downloadBUs logic
Nov 21, 2022
72ad8cf
#296 adding chose value of gitPush and value of downloadBUs logic (di…
Nov 21, 2022
ea80602
#296 adding chose value of gitPush and value of downloadBUs logic (co…
Nov 21, 2022
446c825
#296 bug fixed after the copado file change
Nov 21, 2022
1c32ea3
#296 changing account_id to enterprise_id
Nov 22, 2022
fb79af2
#296 moved mcdev init and removed npm install from init function
Nov 22, 2022
8fe2ece
Merge branch 'master' into 296-feature-adding-mc_init
Nov 22, 2022
f24797d
#296 changing default values of downloadBUs and gitPush
Nov 22, 2022
9b09f8d
#296: small updates
Nov 22, 2022
bdf7a5f
#296 - updating init.env-sample
Nov 28, 2022
207ac27
Merge branch 'master' into 296-feature-adding-mc_init
JoernBerkefeld Dec 5, 2022
6ef7909
#296 setting mcdevVersion = null as we don't use it, and changed the …
Dec 6, 2022
1e3c768
#296 - adding a new env variable to get the env variables that we wan…
Dec 15, 2022
f399f8a
#296 - updating init.env-sample to have the copadoEnvionmentVariables…
Dec 15, 2022
8e76cd5
#296 - updating logic to use json array instead of json attributes
Dec 15, 2022
e63464d
#296 - creating apex function to be used as call back on the mc init …
Dec 16, 2022
5c90bfd
#296 adding examples for createEnvVariables json and createEnvironmen…
Dec 19, 2022
f259f2e
#296 updating create environment apex code example values
Dec 19, 2022
575d9ea
#296 giving logic in order to show error message if function fails an…
Dec 19, 2022
33612e1
#296 added logic to read json array in apex
Dec 19, 2022
47b54d3
#296 updated json array so it can be iterated
Dec 19, 2022
522ade8
#296 updating json example and apex code to create environment exampl…
Dec 20, 2022
9a6edd6
#296 removing the copadoEnvironmentVariables from the environment var…
Dec 20, 2022
9e4797c
#296 updating mcdo_CreateEnvironment to create the environments, the …
Dec 20, 2022
d714f60
#296 deleting TODO comment and writing a new one saying what it does
Dec 20, 2022
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
17 changes: 4 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"type": "node-terminal",
"name": "debug: dist/init",
"name": "init (dist/)",
"request": "launch",
"command": "node Init.fn.js --preserve-symlinks",
"cwd": "${workspaceFolder}/copado-function/dist",
Expand All @@ -18,7 +18,7 @@
},
{
"type": "node-terminal",
"name": "debug: dist/retrieve",
"name": "retrieve (dist/)",
"request": "launch",
"command": "node Retrieve.fn.js --preserve-symlinks",
"cwd": "${workspaceFolder}/copado-function/dist",
Expand All @@ -27,7 +27,7 @@
},
{
"type": "node-terminal",
"name": "debug: dist/commit",
"name": "commit (dist/)",
"request": "launch",
"command": "node Commit.fn.js --preserve-symlinks",
"cwd": "${workspaceFolder}/copado-function/dist",
Expand All @@ -36,21 +36,12 @@
},
{
"type": "node-terminal",
"name": "debug: dist/deploy",
"name": "deploy (dist/)",
"request": "launch",
"command": "node Deploy.fn.js --preserve-symlinks",
"cwd": "${workspaceFolder}/copado-function/dist",
"envFile": "${workspaceFolder}/copado-function/app/environments/Deploy.env",
"preLaunchTask": "esbuild"
},
{
"type": "node-terminal",
"name": "debug: dist/upgrade",
"request": "launch",
"command": "node Upgrade.fn.js --preserve-symlinks",
"cwd": "${workspaceFolder}/copado-function/dist",
"envFile": "${workspaceFolder}/copado-function/app/environments/Upgrade.env",
"preLaunchTask": "esbuild"
}
]
}
1 change: 1 addition & 0 deletions copado-function/app/Commit.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async function run() {
Util.execCommand(null, 'npm --version', null);
Util.execCommand(null, 'node --version', null);
Util.execCommand(null, 'git version', null);
Util.execCommand(null, 'mcdev --version', null);
}

Log.debug(`Change Working directory to: ${CONFIG.tmpDirectory}`);
Expand Down
1 change: 1 addition & 0 deletions copado-function/app/Deploy.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function run() {
Util.execCommand(null, 'npm --version', null);
Util.execCommand(null, 'node --version', null);
Util.execCommand(null, 'git version', null);
Util.execCommand(null, 'mcdev --version', null);
}

Log.debug(`Change Working directory to: ${CONFIG.tmpDirectory}`);
Expand Down
98 changes: 80 additions & 18 deletions copado-function/app/Init.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@
'use strict';

const resolve = require('node:path').resolve;
const fs = require('node:fs');
const CONFIG = require('./common/Config');
const Log = require('./common/Log');
const Util = require('./common/Util');
const Copado = require('./common/Copado');

// ++++ CONFIG ++++
CONFIG.mcdevCopadoVersion = '[VI]{{inject}}[/VI]';
// credentials
CONFIG.credentialNameSource = process.env.credentialNameSource;
CONFIG.credentialNameSource = process.env.credentialName;
CONFIG.credentialNameTarget = null;
CONFIG.credentials = process.env.credentials;
CONFIG.client_id = process.env.client_id;
CONFIG.client_secret = process.env.client_secret;
CONFIG.auth_url = process.env.auth_url;
CONFIG.account_id = process.env.enterprise_id;
CONFIG.credentials = `{"${CONFIG.credentialNameSource}":{"client_id":"${CONFIG.client_id}","client_secret":"${CONFIG.client_secret}","auth_url":"${CONFIG.auth_url}","account_id":"${CONFIG.account_id}"}}`;

// generic
CONFIG.configFilePath = null;
CONFIG.repoUrl = process.env.repoUrl;
CONFIG.debug = process.env.debug === 'true' ? true : false;
CONFIG.installMcdevLocally = process.env.installMcdevLocally === 'true' ? true : false;
CONFIG.installMcdevLocally = null;
CONFIG.mainBranch = null;
CONFIG.mcdevVersion = null;
CONFIG.metadataFilePath = null; // do not change - LWC depends on it! // not needed in this case, previous value: 'mcmetadata.json'
CONFIG.source_mid = null;
CONFIG.tmpDirectory = '../tmp';
CONFIG.userEmail = process.env.git_email;
CONFIG.userName = process.env.git_name;

// retrieve
CONFIG.source_sfid = null;
// commit
Expand All @@ -44,6 +54,10 @@ CONFIG.merge_strategy = null; // set default merge strategy
CONFIG.promotionBranch = null; // The promotion branch of a PR
CONFIG.promotionName = null; // The promotion name of a PR
CONFIG.target_mid = null;
// init
CONFIG.repoUrl = process.env.repoUrl;
CONFIG.downloadBUs = process.env.downloadBUs === 'false' ? false : true;
CONFIG.gitPush = process.env.gitPush === 'false' ? false : true;

/**
* main method that combines runs this function
Expand All @@ -55,14 +69,23 @@ async function run() {
Log.debug('');
Log.debug('Parameters');
Log.debug('===================');
// if one of the elements present in the array are undefined, this error will be triggered
if ([CONFIG.client_id, CONFIG.client_secret, CONFIG.auth_url, CONFIG.account_id].includes()) {
Log.error(
`Could not find credentials: ${CONFIG.client_id}, ${CONFIG.client_secret}, ${CONFIG.auth_url}, ${CONFIG.account_id}`
);
throw new Error(
`Could not find credentials: ${CONFIG.client_id}, ${CONFIG.client_secret}, ${CONFIG.auth_url}, ${CONFIG.account_id}`
);
}
try {
CONFIG.credentials = JSON.parse(CONFIG.credentials);
} catch (ex) {
Log.error('Could not parse credentials');
Log.error(`Could not parse credentials: ${CONFIG.credentials}`);
throw ex;
}
Log.debug(CONFIG);

Log.debug(CONFIG);
// ensure we got SFMC credentials for our source BU
if (!CONFIG.credentials[CONFIG.credentialNameSource]) {
Log.error(`No credentials found for source (${CONFIG.credentialNameSource})`);
Expand All @@ -76,34 +99,52 @@ async function run() {
Util.execCommand(null, 'npm --version', null);
Util.execCommand(null, 'node --version', null);
Util.execCommand(null, 'git version', null);
Util.execCommand(null, 'mcdev --version', null);
}

Log.debug(`Change Working directory to: ${CONFIG.tmpDirectory}`);
// prevent git errors down the road
try {
Util.execCommand(null, ['git config --global --add safe.directory /tmp']);
Util.execCommand(null, [
'git config --global --add safe.directory ' + resolve(CONFIG.tmpDirectory),
]);
} catch {
try {
Util.execCommand(null, [
'git config --global --add safe.directory ' + resolve(CONFIG.tmpDirectory),
]);
} catch {
Log.error('Could not set tmp directoy as safe directory');
}
Log.error('Could not set tmp directoy as safe directory');
}

// actually change working directory
if (!fs.existsSync(CONFIG.tmpDirectory)) {
fs.mkdirSync(CONFIG.tmpDirectory);
}
process.chdir(CONFIG.tmpDirectory);
Log.debug(process.cwd());

try {
Log.info('');
Log.info('Preparing');
Log.info('Adding git email and name');
Log.info('===================');
Log.info('');
Util.execCommand(null, [
`git config --global user.email "${CONFIG.userEmail}"`,
`git config --global user.name "${CONFIG.userName}"`,
]);
} catch (ex) {
Log.error('adding git email and name failed: ' + ex.message);
throw ex;
}

try {
Log.info('');
Log.info('Initializing mcdev tools');
Log.info('===================');
Log.info('');
Util.provideMCDevTools();
Copado.mcdevInit(CONFIG.credentials, CONFIG.credentialNameSource, CONFIG.repoUrl);
Init.mcdevInit(CONFIG.credentials, CONFIG.credentialNameSource, {
url: CONFIG.repoUrl,
downloadBUs: CONFIG.downloadBUs,
gitPush: CONFIG.gitPush,
});
} catch (ex) {
Log.error('initializing failed: ' + ex.message);
Log.error('Initializing failed: ' + ex.message);
throw ex;
}

Expand All @@ -115,4 +156,25 @@ async function run() {
Copado.uploadToolLogs();
}

/**
* Class for Init function
*/
class Init {
/**
*
* @param {object} credentials the credentials for the salesforce marketing cloud
* @param {string} credentialName the credential name
* @param {object} options contains the url, the downloadBUs and the gitPush values
*/
static mcdevInit(credentials, credentialName, options) {
Util.execCommand(
`Initializing mcdev`,
[
`mcdev init --y.credentialName "${credentialName}" --y.client_id "${credentials[credentialName].client_id}" --y.client_secret "${credentials[credentialName].client_secret}" --y.auth_url "${credentials[credentialName].auth_url}" --y.gitRemoteUrl "${options.url}" --y.account_id ${credentials[credentialName].account_id} --y.downloadBUs "${options.downloadBUs}" --y.gitPush "${options.gitPush}"`,
],
'Mcdev initialized!'
);
}
}

run(); // eslint-disable-line unicorn/prefer-top-level-await
1 change: 1 addition & 0 deletions copado-function/app/Retrieve.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function run() {
Util.execCommand(null, 'npm --version', null);
Util.execCommand(null, 'node --version', null);
Util.execCommand(null, 'git version', null);
Util.execCommand(null, 'mcdev --version', null);
}

Log.debug(`Change Working directory to: ${CONFIG.tmpDirectory}`);
Expand Down
16 changes: 0 additions & 16 deletions copado-function/app/common/Copado.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ const Util = require('./Util');
* methods to handle interaction with the copado platform
*/
class Copado {
/**
*
* @param {object} credentials the credentials for the salesforce marketing cloud
* @param {string }credentialName the credential name
* @param {string} url the git remote URL
*/
static mcdevInit(credentials, credentialName, url) {
Util.execCommand(
`Initializing mcdev: ${credentialName}, ${credentials[credentialName].client_id}", "${credentials[credentialName].client_secret}", "${credentials[credentialName].auth_url}", "${url}", ${credentials[credentialName].account_id}`,
[
`mcdev init --y.credentialName "${credentialName}" --y.client_id "${credentials[credentialName].client_id}" --y.client_secret "${credentials[credentialName].client_secret}" --y.auth_url "${credentials[credentialName].auth_url}" --y.gitRemoteUrl "${url}" --y.account_id ${credentials[credentialName].account_id} --y.downloadBUs "false" --y.gitPush "true"`,
],
'Mcdev initialized!'
);
}

/**
* Finally, attach the resulting metadata JSON to the source environment
*
Expand Down
6 changes: 3 additions & 3 deletions copado-function/app/common/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ class Util {
'Completed installing Accenture SFMC DevTools'
);
return; // we're done here
} else if (!CONFIG.mcdevVersion) {
Log.error('Please specify mcdev_version in pipeline & environment settings');
throw new Error('Please specify mcdev_version in pipeline & environment settings');
} else if (CONFIG.mcdevVersion.charAt(0) === '#') {
// assume branch of mcdev's git repo shall be loaded

installer = `accenture/sfmc-devtools${CONFIG.mcdevVersion}`;
} else if (!CONFIG.mcdevVersion) {
Log.error('Please specify mcdev_version in pipeline & environment settings');
throw new Error('Please specify mcdev_version in pipeline & environment settings');
} else {
// default, install via npm at specified version
installer = `mcdev@${CONFIG.mcdevVersion}`;
Expand Down
16 changes: 11 additions & 5 deletions copado-function/app/environments/Init.env-sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
installMcdevLocally=true
debug=true
repoUrl="UPDATE ME"
repoUrl="UPDATE ME" # url
debug="UPDATE ME" # true or false
downloadBUs="UPDATE ME" # true or false
gitPush="UPDATE ME" # true or false

## generic
credentials="UPDATE ME"
credentialNameSource="UPDATE ME"
credentialName="UPDATE ME"
client_id="UPDATE ME"
client_secret="UPDATE ME"
auth_url="UPDATE ME" # url
enterprise_id="UPDATE ME"
git_email="UPDATE ME" # email
git_name="UPDATE ME"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Name":"_ParentBU_","environmentVariables":[{"name":"suffix","value":"_Parent"}],"properties":[{"name":"CredentialName","apiName":"credential_name","value":"CREDENTIAL_NAME","hideValue":false},{"name":"MID","apiName":"mid","value":"1111111","hideValue":false}]},{"Name":"MCDEV_Training_DEV","environmentVariables":[{"name":"suffix","value":"_DEV"}],"properties":[{"name":"CredentialName","apiName":"credential_name","value":"CREDENTIAL_NAME","hideValue":false},{"name":"MID","apiName":"mid","value":"2222222","hideValue":false}]},{"Name":"MCDEV_Training_Prod_1","environmentVariables":[{"name":"suffix","value":"_PROD1"}],"properties":[{"name":"CredentialName","apiName":"credential_name","value":"CREDENTIAL_NAME","hideValue":false},{"name":"MID","apiName":"mid","value":"33333333","hideValue":false}]},{"Name":"MCDEV_Training_Prod_2","environmentVariables":[{"name":"suffix","value":"_PROD2"}],"properties":[{"name":"CredentialName","apiName":"credential_name","value":"CREDENTIAL_NAME","hideValue":false},{"name":"MID","apiName":"mid","value":"4444444","hideValue":false}]},{"Name":"MCDEV_Training_QA","environmentVariables":[{"name":"suffix","value":"_QA"}],"properties":[{"name":"CredentialName","apiName":"credential_name","value":"CREDENTIAL_NAME","hideValue":false},{"name":"MID","apiName":"mid","value":"55555555","hideValue":false}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
copado.CreateExecution.Request request = new copado.CreateExecution.Request();
request.parentId = '[functionId]';
request.templateName = '[jobTemplateApiName]';
request.callback = '[callBackClassName]';
request.dataJson = '[{"Name":"ENVIRONMENT_NAME","environmentVariables":[{"name":"ENVIRONMENT_VARIABLE_NAME","value":"ENVIRONMENT_VARIABLE_VALUE"}],"properties":[{"name":"PROPERTY_NAME","apiName":"PROPERTY_API_NAME","value":"PROPERTY_VALUE","hideValue":TRUE_OR_FALSE}]}]';
request.runAfterInstantiation = true;
copado.CreateExecution.execute(new List<copado.CreateExecution.Request>{ request });
2 changes: 1 addition & 1 deletion copado-function/app/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const isWatch = args.includes('--watch');
'*/\n\n',
},
platform: 'node',
external: ['../tmp/*'],
external: ['../tmp/*', '../../node_modules/*'],
preserveSymlinks: true,
outdir: '../dist',
absWorkingDir: absWorkingDir,
Expand Down
20 changes: 20 additions & 0 deletions copado-function/config/Init.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Details
* [Function Name] (Marketing Cloud Init)
* [API Name] (MC_Init)
* [Type] (Custom)
* [Description] (Initializes mcdev in an empty Marketing Cloud Repository)

# Configuration
* [Image name] (0onz6fxsbyolcopmojdzlxa5vzgsts-sfmc-devtools:v1.0) (backup:copado-multicloud-git-utils:v1)
* [Timeout] (10)
* [Worker Size] (S)

# Parameters needs to be configured for Marketing Cloud init function
* credentialName ({...}) // por o nome?
* client_id ({...}) // não sei onde por ainda
* client_secret ({...}) // não sei onde por ainda
* auth_url ({...}) // não sei onde por ainda
* account_id ({...}) // não sei onde por ainda
* repoUrl ({...}) // não sei onde por ainda
* [debug] ({...}) // não sei onde por ainda
* [installMcdevLocally] ({...}) // não sei onde por ainda
16 changes: 4 additions & 12 deletions copado-function/dist/Commit.fn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

/*
* mcdev-copado v1.3.0 (built 2022-12-05T15:06:59.839Z)
* mcdev-copado v1.3.0 (built 2022-12-05T16:05:54.175Z)
* Function: Commit.fn.js
* Dependenies: mcdev@>=4.1.12, Copado [email protected]
* Homepage: https://github.com/Accenture/sfmc-devtools-copado#readme
Expand Down Expand Up @@ -151,11 +151,11 @@ var require_Util = __commonJS({
"Completed installing Accenture SFMC DevTools"
);
return;
} else if (CONFIG2.mcdevVersion.charAt(0) === "#") {
installer = `accenture/sfmc-devtools${CONFIG2.mcdevVersion}`;
} else if (!CONFIG2.mcdevVersion) {
Log2.error("Please specify mcdev_version in pipeline & environment settings");
throw new Error("Please specify mcdev_version in pipeline & environment settings");
} else if (CONFIG2.mcdevVersion.charAt(0) === "#") {
installer = `accenture/sfmc-devtools${CONFIG2.mcdevVersion}`;
} else {
installer = `mcdev@${CONFIG2.mcdevVersion}`;
}
Expand Down Expand Up @@ -251,15 +251,6 @@ var require_Copado = __commonJS({
var Log2 = require_Log();
var Util2 = require_Util();
var Copado2 = class {
static mcdevInit(credentials, credentialName, url) {
Util2.execCommand(
`Initializing mcdev: ${credentialName}, ${credentials[credentialName].client_id}", "${credentials[credentialName].client_secret}", "${credentials[credentialName].auth_url}", "${url}", ${credentials[credentialName].account_id}`,
[
`mcdev init --y.credentialName "${credentialName}" --y.client_id "${credentials[credentialName].client_id}" --y.client_secret "${credentials[credentialName].client_secret}" --y.auth_url "${credentials[credentialName].auth_url}" --y.gitRemoteUrl "${url}" --y.account_id ${credentials[credentialName].account_id} --y.downloadBUs "false" --y.gitPush "true"`
],
"Mcdev initialized!"
);
}
static attachJson(localPath, parentSfid, async = false, preMsg) {
Copado2._attachFile(localPath, async, parentSfid, preMsg);
}
Expand Down Expand Up @@ -484,6 +475,7 @@ async function run() {
Util.execCommand(null, "npm --version", null);
Util.execCommand(null, "node --version", null);
Util.execCommand(null, "git version", null);
Util.execCommand(null, "mcdev --version", null);
}
Log.debug(`Change Working directory to: ${CONFIG.tmpDirectory}`);
try {
Expand Down
Loading