Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 47 additions & 11 deletions src/support/slack/commands/toggle-site-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ export default (context) => {
CSV file must be in the format of baseURL per line(no headers).
Profiles are defined in the config/profiles.json file.`,
phrases: [PHRASE],
usageText: `${PHRASE} {enable/disable} {site} {auditType} for singleURL,
or ${PHRASE} {enable/disable} {profile/auditType} with CSV file uploaded.`,
usageText: `${PHRASE} {enable/disable} {site} {auditType} for single URL,
or ${PHRASE} {enable/disable} {profile/auditType} with CSV file uploaded.
Special: ${PHRASE} disable {site} all [profile] to disable all audits from a profile.`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Special ? we can simply add this as and

});

const { log, dataAccess } = context;
Expand Down Expand Up @@ -193,11 +194,11 @@ export default (context) => {

// single URL behavior
if (isNonEmptyArray(files) === false) {
const [, baseURLInput, singleAuditType] = args;
const [, baseURLInput, auditType, profileName] = args;

const baseURL = extractURLFromSlackInput(baseURLInput);

validateInput(enableAudit, singleAuditType);
validateInput(enableAudit, auditType);

if (isValidUrl(baseURL) === false) {
await say(`${ERROR_MESSAGE_PREFIX}Please provide either a CSV file or a single baseURL.`);
Expand All @@ -212,13 +213,48 @@ export default (context) => {
}

const registeredAudits = configuration.getHandlers();
if (!registeredAudits[singleAuditType]) {
await say(`${ERROR_MESSAGE_PREFIX}The "${singleAuditType}" is not present in the configuration.\nList of allowed audits:\n${Object.keys(registeredAudits).join('\n')}.`);

// Handle "all" keyword to disable all audits from a profile
if (auditType === 'all') {
if (isEnableAudit) {
await say(`${ERROR_MESSAGE_PREFIX}Enable all is not supported. Please enable audits individually or use a profile with CSV upload.`);
return;
}

const targetProfile = profileName || 'demo';

let profile;
try {
profile = loadProfileConfig(targetProfile);
} catch (error) {
await say(`${ERROR_MESSAGE_PREFIX}Profile "${targetProfile}" not found.`);
return;
}
const profileAuditTypes = Object.keys(profile.audits || {});
if (profileAuditTypes.length === 0) {
await say(`${ERROR_MESSAGE_PREFIX}No audits found in profile "${targetProfile}".`);
return;
}

await say(`:hourglass_flowing_sand: Disabling ${profileAuditTypes.length} audits from profile "${targetProfile}" for ${site.getBaseURL()}...`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mention as Disabling all audits instead?


profileAuditTypes.forEach((type) => {
configuration.disableHandlerForSite(type, site);
});

await configuration.save();
await say(`${SUCCESS_MESSAGE_PREFIX}Successfully disabled ${profileAuditTypes.length} audits from profile "${targetProfile}" for "${site.getBaseURL()}".\n\`\`\`${profileAuditTypes.join('\n')}\`\`\``);
return;
}

// Handle single audit type
if (!registeredAudits[auditType]) {
await say(`${ERROR_MESSAGE_PREFIX}The "${auditType}" is not present in the configuration.\nList of allowed audits:\n${Object.keys(registeredAudits).join('\n')}.`);
return;
}

if (isEnableAudit) {
if (singleAuditType === 'preflight') {
if (auditType === 'preflight') {
const authoringType = site.getAuthoringType();
const deliveryConfig = site.getDeliveryConfig();
const helixConfig = site.getHlxConfig();
Expand Down Expand Up @@ -247,18 +283,18 @@ export default (context) => {

if (configMissing) {
// Prompt user to configure missing requirements
await promptPreflightConfig(slackContext, site, singleAuditType);
await promptPreflightConfig(slackContext, site, auditType);
return;
}
}

configuration.enableHandlerForSite(singleAuditType, site);
configuration.enableHandlerForSite(auditType, site);
} else {
configuration.disableHandlerForSite(singleAuditType, site);
configuration.disableHandlerForSite(auditType, site);
}

await configuration.save();
await say(`${SUCCESS_MESSAGE_PREFIX}The audit "${singleAuditType}" has been *${enableAudit}d* for "${site.getBaseURL()}".`);
await say(`${SUCCESS_MESSAGE_PREFIX}The audit "${auditType}" has been *${enableAudit}d* for "${site.getBaseURL()}".`);
} catch (error) {
log.error(error);
await say(`${ERROR_MESSAGE_PREFIX}An error occurred while trying to enable or disable audits: ${error.message}`);
Expand Down
Loading