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

fix: byOrg property from site #165

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"version": "0.2.0",
"configurations": [


{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--slow",
"5000",
"--colors",
"${workspaceFolder}/test/**/*.test.js",
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**"
]
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/index.js"
}
]
}
2 changes: 1 addition & 1 deletion src/digest/handler-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function externalDigestHandler(
const message = processLatestAudit(context, site, latestAuditReports);
if (Object.keys(message).length > 0) {
const siteConfig = site.getConfig();
const isDigest = isDigestReport(orgConfig, type);
const isDigest = isDigestReport(orgConfig, siteConfig, type);
if (!isDigest || !sentInitialMessage) {
slackContext = getSlackContextForAlert(orgConfig, siteConfig, type);
}
Expand Down
37 changes: 29 additions & 8 deletions src/support/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,37 @@
*/
import { isArray } from '@adobe/spacecat-shared-utils';

export const isDigestReport = (conf, alertType) => {
const alertConfig = isArray(conf?.alerts)
? conf?.alerts.find((alert) => alert.type === alertType)
: {};
return alertConfig?.byOrg;
export const getAlertConfig = (conf, alertType) => (isArray(conf?.alerts)
? conf?.alerts.find((alert) => alert.type === alertType) : null);

// export const isDigestReport = (conf, alertType) => {
// const alertConfig = isArray(conf?.alerts)
// ? conf?.alerts.find((alert) => alert.type === alertType)
// : {};
// return alertConfig?.byOrg;
// };

// used AI code just to see if I could get the test to work -- it is still failing
export const isDigestReport = (orgConf, siteConf, alertType) => {
const orgAlertConfig = getAlertConfig(orgConf, alertType);
if (orgAlertConfig?.byOrg !== undefined) {
return orgAlertConfig.byOrg;
}
const siteAlertConfig = getAlertConfig(siteConf, alertType);
if (siteAlertConfig?.byOrg !== undefined) {
return siteAlertConfig.byOrg;
}
return false;
};

export const hasAlertConfig = (conf, alertType) => isArray(conf?.alerts)
&& conf?.alerts.find((alert) => alert.type === alertType);
// TODO replace current implementation with the new one
// export const isDigestReport = (orgConf, siteConf, alertType) => {
// if getAlertConfig for siteConf is not null/undefined return siteAlertConfig.byOrg value
// else return getAlertConfig for orgConf is not null/undefined return orgAlertConfig.byOrg value
// else return false
// };

export const hasAlertConfig = (conf, alertType) => !!getAlertConfig(conf, alertType);
const getSlackContext = (conf, alertType) => {
const channel = conf?.slack?.channel;
const alertConfig = isArray(conf?.alerts)
Expand All @@ -31,7 +52,7 @@ const getSlackContext = (conf, alertType) => {
};

export const getSlackContextForAlert = (orgConf, siteConf, alertType) => {
if (isDigestReport(orgConf, alertType)) {
if (isDigestReport(orgConf, siteConf, alertType)) {
return getSlackContext(orgConf, alertType);
}
return getSlackContext(siteConf, alertType);
Expand Down
25 changes: 23 additions & 2 deletions test/support/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ describe('config util', () => {
expect(slackContext.mentions).to.deep.equal(['slackId2']);
});

it('isDigestReport for valid configs bySite', () => {
const orgConfig = {
alerts: [{
type: '404',
}],
};
const siteConfig = {
slack: {
workspace: 'workspace2',
channel: 'channel2',
},
alerts: [{
type: '404',
byOrg: false,
mentions: [{ slack: ['slackId2'] }],
}],
};
const isDigest = isDigestReport(orgConfig, siteConfig, '404');
expect(isDigest).to.be.false;
});

it('getSlackContextForAlert returns only slack channel if no alerts are in config', () => {
const orgConfig = {
slack: {
Expand All @@ -95,7 +116,7 @@ describe('config util', () => {
it(' for empty config', () => {
const config = {
};
const isByOrg = isDigestReport(config, '404');
expect(isByOrg).to.be.undefined;
const isByOrg = isDigestReport(config, {}, '404');
expect(isByOrg).to.be.false;
});
});
Loading