Skip to content

Commit

Permalink
#1025: add new command schedule and deploy option --schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Jul 14, 2023
1 parent 5864211 commit 9ad3e1b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 13 deletions.
15 changes: 15 additions & 0 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ main class
* [.buildDefinition(businessUnit, selectedType, name, market)](#Mcdev.buildDefinition) ⇒ <code>Promise.&lt;void&gt;</code>
* [.buildDefinitionBulk(listName, type, name)](#Mcdev.buildDefinitionBulk) ⇒ <code>Promise.&lt;void&gt;</code>
* [.getFilesToCommit(businessUnit, selectedType, keyArr)](#Mcdev.getFilesToCommit) ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code>
* [.schedule(businessUnit, [selectedType], [keys])](#Mcdev.schedule) ⇒ <code>Promise.&lt;boolean&gt;</code>
* [.execute(businessUnit, [selectedType], [keys])](#Mcdev.execute) ⇒ <code>Promise.&lt;boolean&gt;</code>
* [.pause(businessUnit, [selectedType], [keys])](#Mcdev.pause) ⇒ <code>Promise.&lt;boolean&gt;</code>

Expand Down Expand Up @@ -773,6 +774,20 @@ Build a specific metadata file based on a template using a list of bu-market com
| selectedType | <code>string</code> | supported metadata type |
| keyArr | <code>Array.&lt;string&gt;</code> | customerkey of the metadata |

<a name="Mcdev.schedule"></a>

### Mcdev.schedule(businessUnit, [selectedType], [keys]) ⇒ <code>Promise.&lt;boolean&gt;</code>
Schedule an item (shortcut for execute --schedule)

**Kind**: static method of [<code>Mcdev</code>](#Mcdev)
**Returns**: <code>Promise.&lt;boolean&gt;</code> - true if all started successfully, false if not

| Param | Type | Description |
| --- | --- | --- |
| businessUnit | <code>string</code> | name of BU |
| [selectedType] | <code>TYPE.SupportedMetadataTypes</code> | limit to given metadata types |
| [keys] | <code>Array.&lt;string&gt;</code> | customerkey of the metadata |

<a name="Mcdev.execute"></a>

### Mcdev.execute(businessUnit, [selectedType], [keys]) ⇒ <code>Promise.&lt;boolean&gt;</code>
Expand Down
41 changes: 39 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ yargs
'optional for asset-message: runs refresh command for related triggeredSends after deploy',
})
.option('execute', {
type: 'string',
type: 'boolean',
group: 'Options for deploy:',
describe:
'optional: executes item after deploy; for automations you can set it to --execute=schedule; alternatively it will run the automation once immediately',
'optional: executes item after deploy; this will run the item once immediately',
})
.option('schedule', {
type: 'boolean',
group: 'Options for deploy:',
describe:
'optionally start existing schedule instead of running item once immediately (only works for automations)',
});
},
handler: (argv) => {
Expand Down Expand Up @@ -445,6 +451,37 @@ yargs
Mcdev.execute(argv.BU, argv.TYPE, csvToArray(argv.KEY));
},
})
.command({
command: 'schedule <BU> <TYPE> [KEY]',
aliases: ['sched'],
desc: 'starts the predefined schedule of the item (shortcut for running execute --schedule)',
builder: (yargs) => {
yargs
.positional('BU', {
type: 'string',
describe: 'the business unit where to start an item',
})
.positional('TYPE', {
type: 'string',
describe: 'metadata type',
})
.positional('KEY', {
type: 'string',
describe: 'key(s) of the metadata component(s)',
})
.option('like', {
type: 'string',
group: 'Options for execute:',
describe:
'filter metadata components (can include % as wildcard or _ for a single character)',
});
},
handler: (argv) => {
Mcdev.setOptions(argv);
// ! do not allow multiple types to be passed in here via csvToArray
Mcdev.schedule(argv.BU, argv.TYPE, csvToArray(argv.KEY));
},
})
.command({
command: 'pause <BU> <TYPE> [KEY]',
aliases: ['p', 'stop'],
Expand Down
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ class Mcdev {
return DevOps.getFilesToCommit(properties, buObject, selectedType, keyArr);
}
}
/**
* Schedule an item (shortcut for execute --schedule)
*
* @param {string} businessUnit name of BU
* @param {TYPE.SupportedMetadataTypes} [selectedType] limit to given metadata types
* @param {string[]} [keys] customerkey of the metadata
* @returns {Promise.<boolean>} true if all started successfully, false if not
*/
static async schedule(businessUnit, selectedType, keys) {
this.setOptions({ schedule: true });
return this.#runMethod('execute', businessUnit, selectedType, keys);
}
/**
* Start/execute an item
*
Expand Down
13 changes: 5 additions & 8 deletions lib/metadataTypes/Automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class Automation extends MetadataType {
static async execute(keyArr) {
const metadataMap = {};
for (const key of keyArr) {
if (Util.OPTIONS.schedule || Util.OPTIONS.execute === 'schedule') {
if (Util.OPTIONS.schedule) {
// schedule
const results = await this.retrieve(undefined, undefined, undefined, key);
if (Object.keys(results.metadata).length) {
Expand Down Expand Up @@ -492,17 +492,14 @@ class Automation extends MetadataType {
}
Util.logger.info(
`Starting automations ${
Util.OPTIONS.schedule || Util.OPTIONS.execute === 'schedule'
Util.OPTIONS.schedule
? 'according to schedule'
: 'to run once (use --schedule or --execute=schedule to schedule instead)'
}: ${Object.keys(metadataMap).length}`
);
const promiseResults = [];
for (const key of Object.keys(metadataMap)) {
if (
(Util.OPTIONS.schedule || Util.OPTIONS.execute === 'schedule') &&
metadataMap[key].status === 'Scheduled'
) {
if (Util.OPTIONS.schedule && metadataMap[key].status === 'Scheduled') {
// schedule
Util.logger.info(
` - skipping ${this.definition.type} ${metadataMap[key].name}: already scheduled.`
Expand All @@ -527,7 +524,7 @@ class Automation extends MetadataType {
* @returns {Promise.<object>} Returns the result of the API call
*/
static async #executeItem(metadataMap, key) {
if (Util.OPTIONS.schedule || Util.OPTIONS.execute === 'schedule') {
if (Util.OPTIONS.schedule) {
this.#preDeploySchedule(metadataMap[key]);
metadataMap[key].status = 'Scheduled';
return this.#scheduleAutomation(metadataMap, metadataMap, key);
Expand Down Expand Up @@ -905,7 +902,7 @@ class Automation extends MetadataType {
}
}
}
if (Util.OPTIONS.execute) {
if (Util.OPTIONS.execute || Util.OPTIONS.schedule) {
Util.logger.info(`Executing: ${this.definition.type}`);
await this.execute(Object.keys(metadataMap));
}
Expand Down
30 changes: 27 additions & 3 deletions test/type.automation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('type: automation', () => {
});
it('Should update & schedule an automation with --execute option', async () => {
// WHEN
handler.setOptions({ execute: 'schedule' });
handler.setOptions({ schedule: true });
const deployed = await handler.deploy(
'testInstance/testBU',
['automation'],
Expand Down Expand Up @@ -382,6 +382,30 @@ describe('type: automation', () => {
});
describe('Execute ================', () => {
it('Should schedule an automation by key', async () => {
const execute = await handler.schedule('testInstance/testBU', 'automation', [
'testExisting_automation',
]);
assert.equal(process.exitCode, false, 'execute should not have thrown an error');
assert.equal(execute, true, 'automation was supposed to be executed');
return;
});
it('Should schedule an automation selected via --like', async () => {
handler.setOptions({ like: { key: 'testExist%automation' } });
const execute = await handler.schedule('testInstance/testBU', 'automation');
assert.equal(process.exitCode, false, 'execute should not have thrown an error');
assert.equal(execute, true, 'automation was supposed to be executed');
return;
});
it('Should not schedule executing an automation because key and --like was specified', async () => {
handler.setOptions({ like: { key: 'testExisting%' } });
const execute = await handler.schedule('testInstance/testBU', 'automation', [
'testExisting_automation',
]);
assert.equal(process.exitCode, true, 'execute should not have thrown an error');
assert.equal(execute, false, 'automation was not supposed to be executed');
return;
});
it('Should execute --schedule an automation by key', async () => {
handler.setOptions({ schedule: true });
const execute = await handler.execute('testInstance/testBU', 'automation', [
'testExisting_automation',
Expand All @@ -390,14 +414,14 @@ describe('type: automation', () => {
assert.equal(execute, true, 'automation was supposed to be executed');
return;
});
it('Should schedule an automation selected via --like', async () => {
it('Should execute --schedule an automation selected via --like', async () => {
handler.setOptions({ like: { key: 'testExist%automation' }, schedule: true });
const execute = await handler.execute('testInstance/testBU', 'automation');
assert.equal(process.exitCode, false, 'execute should not have thrown an error');
assert.equal(execute, true, 'automation was supposed to be executed');
return;
});
it('Should not schedule executing an automation because key and --like was specified', async () => {
it('Should not execute --schedule executing an automation because key and --like was specified', async () => {
handler.setOptions({ like: { key: 'testExisting%' }, schedule: true });
const execute = await handler.execute('testInstance/testBU', 'automation', [
'testExisting_automation',
Expand Down

0 comments on commit 9ad3e1b

Please sign in to comment.