From 92dfd6e8bcd61d305cef93ed3a562f0d010bda85 Mon Sep 17 00:00:00 2001 From: Yuliia Likhytska Date: Sun, 13 Aug 2023 14:08:45 +0200 Subject: [PATCH] #984: added interactive questions, no email validation at this point --- docs/dist/documentation.md | 13 ++++++++ lib/metadataTypes/Automation.js | 53 +++++++++++++++++++-------------- lib/util/cli.js | 28 ++++++++++++++++- 3 files changed, 71 insertions(+), 23 deletions(-) diff --git a/docs/dist/documentation.md b/docs/dist/documentation.md index c752211cd..de0f5aae4 100644 --- a/docs/dist/documentation.md +++ b/docs/dist/documentation.md @@ -6595,6 +6595,7 @@ CLI helper class * [Cli](#Cli) * [.initMcdevConfig()](#Cli.initMcdevConfig) ⇒ Promise.<boolean> * [.addExtraCredential(properties)](#Cli.addExtraCredential) ⇒ Promise.<(boolean\|string)> + * [.updateNotificationNotes(type)](#Cli.updateNotificationNotes) ⇒ Promise.<Array.<string>> * [.postFixKeysReretrieve(type, dependentTypes)](#Cli.postFixKeysReretrieve) ⇒ Promise.<boolean> * [.logExistingCredentials(properties)](#Cli.logExistingCredentials) ⇒ void * [.updateCredential(properties, credName)](#Cli.updateCredential) ⇒ Promise.<boolean> @@ -6626,6 +6627,18 @@ Extends template file for properties.json | --- | --- | --- | | properties | TYPE.Mcdevrc | config file's json | + + +### Cli.updateNotificationNotes(type) ⇒ Promise.<Array.<string>> +interactive helper to set automation run completion/error note + +**Kind**: static method of [Cli](#Cli) +**Returns**: Promise.<Array.<string>> - responses + +| Param | Type | Description | +| --- | --- | --- | +| type | TYPE.SupportedMetadataTypes | note type (error/completion) | + ### Cli.postFixKeysReretrieve(type, dependentTypes) ⇒ Promise.<boolean> diff --git a/lib/metadataTypes/Automation.js b/lib/metadataTypes/Automation.js index bb2708c5d..d60117272 100644 --- a/lib/metadataTypes/Automation.js +++ b/lib/metadataTypes/Automation.js @@ -7,6 +7,7 @@ const File = require('../util/file'); const Definitions = require('../MetadataTypeDefinitions'); const cache = require('../util/cache'); const pLimit = require('p-limit'); +const Cli = require('../util/cli'); /** * Automation MetadataType @@ -1653,41 +1654,49 @@ class Automation extends MetadataType { `Error retrieving notifications for automation '${key}': ${ex.message} (${ex.code}))` ); } - - // check if there are any notifications set - if (Array.isArray(notificationsResult?.workers)) { - // if completion note was set check if there is an email address - if ( - completionNote && - completionEmail.length === 0 && + // if a note was provided and email address was not - check if notif email address exists + if ( + completionNote && + completionEmail.length === 0 && + (!notificationsResult.workers || !notificationsResult.workers.find( (notification) => notification.notificationType == 'Complete' - ) - ) { - // if a note was provided and email address was not - check if notif email address exists + )) + ) { + const email = await Cli.updateNotificationNotes('completionEmail'); + if (email) { + completionEmail.push(email); + } else { + completionNote = undefined; + shouldUpdateCompletion = false; Util.logger.info( ` ☇ skipping --completionNote' - the email address for Run completion was not set` ); - completionNote = undefined; - shouldUpdateCompletion = false; - // TODO interactive question "do you want to set completionEmail" then ask for email ? } - - if ( - errorNote && - errorEmail.length === 0 && + } + // same for errorNote + if ( + errorNote && + errorEmail.length === 0 && + (!notificationsResult.workers || !notificationsResult.workers.find( (notification) => notification.notificationType == 'Error' - ) - ) { + )) + ) { + const email = await Cli.updateNotificationNotes('errorEmail'); + if (email) { + errorEmail.push(email); + } else { + errorNote = undefined; + shouldUpdateError = false; Util.logger.info( ` ☇ skipping --errorNote' - the email address for Runtime error was not set` ); - errorNote = undefined; - shouldUpdateError = false; - // TODO interactive question "do you want to set errorEmail" then ask for email ? } + } + // check if there are any notifications set + if (Array.isArray(notificationsResult?.workers)) { // check again if there is anything else to update if (!shouldUpdateCompletion && !shouldUpdateError) { Util.logger.info(` ☇ skipping ${key}' - nothing to update`); diff --git a/lib/util/cli.js b/lib/util/cli.js index d16c4fc62..20697920a 100644 --- a/lib/util/cli.js +++ b/lib/util/cli.js @@ -53,7 +53,33 @@ const Cli = { return null; } }, - + /** + * interactive helper to set automation run completion/error note + * + * @param {TYPE.SupportedMetadataTypes} type note type (error/completion) + * @returns {Promise.} responses + */ + async updateNotificationNotes(type) { + const question1 = { + type: 'confirm', + name: 'provideEmail', + message: `To set run ${type} note, an email address should be set. Do you want to set it?`, + default: true, + }; + const question2 = { + type: 'input', + name: 'updateNotificationNotes', + message: `Please enter email addresses separated by a comma:`, + }; + try { + if (!(await inquirer.prompt(question1)).provideEmail) { + return; + } + return (await inquirer.prompt(question2)).updateNotificationNotes; + } catch (ex) { + Util.logger.info(ex); + } + }, /** * * @param {TYPE.SupportedMetadataTypes} type limit execution to given metadata type