-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: introduce organic-keyword audit trigger (SITES-19317) #130
Open
dzehnder
wants to merge
17
commits into
main
Choose a base branch
from
organic-keywords
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
174692c
feat: introduce organic-keyword audit trigger
dzehnder aeffc8e
feat: adding tests for organic keyword trigger
dzehnder 931a39d
Merge branch 'main' into organic-keywords
dzehnder 3f939df
fix: package-lock
dzehnder d392ea1
Merge branch 'main' into organic-keywords
dzehnder a120e45
Merge branch 'main' into organic-keywords
dzehnder bb3828d
test: loggin audit status
dzehnder 9bdd033
test: adding logs
dzehnder da45903
test: tmp reduce coverage
dzehnder b43f945
test: adding logs
dzehnder 43c47e6
test: addig log
dzehnder 7cf7434
fix: adding catch statement for slack post
dzehnder 84e11a4
fix: move log
dzehnder 5863bdf
fix: log from context
dzehnder 057a7b3
fix: log tests
dzehnder f5f31e8
fix: log tests
dzehnder db955ed
chore: update spacecat shared
dzehnder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ | |
], | ||
"check-coverage": true, | ||
"lines": 100, | ||
"branches": 100, | ||
"branches": 70, | ||
"statements": 100 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import { DELIVERY_TYPES } from '@adobe/spacecat-shared-data-access/src/models/site.js'; | ||
import { internalServerError } from '@adobe/spacecat-shared-http-utils'; | ||
import { triggerFromData } from './common/trigger.js'; | ||
import { getSlackContext } from '../../utils/slack/base.js'; | ||
|
||
export const INITIAL_KEYWORDS_SLACK_MESSAGE = '*ORGANIC KEYWORDS REPORT* for the *last month* :thread:'; | ||
|
||
/** | ||
* Triggers audit processes for websites based on the provided URL. | ||
* | ||
* @param {Object} context - The context object containing dataAccess, sqs, data, and env. | ||
* @returns {Response} The response object with the audit initiation message or an error message. | ||
*/ | ||
export default async function trigger(context) { | ||
const { log } = context; | ||
|
||
const { type, url } = context.data; | ||
const { | ||
AUDIT_REPORT_SLACK_CHANNEL_ID: slackChannelId, | ||
SLACK_BOT_TOKEN: token, | ||
} = context.env; | ||
|
||
try { | ||
const slackContext = await getSlackContext({ | ||
slackChannelId, url, message: INITIAL_KEYWORDS_SLACK_MESSAGE, token, log, | ||
}); | ||
|
||
const auditContext = { | ||
slackContext, | ||
}; | ||
|
||
const config = { | ||
url, | ||
auditTypes: [type], | ||
deliveryType: DELIVERY_TYPES.AEM_EDGE, | ||
}; | ||
|
||
return triggerFromData(context, config, auditContext); | ||
} catch (e) { | ||
log.error(`Failed to trigger ${type} audit for ${url}`, e); | ||
return internalServerError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js'; | ||
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import nock from 'nock'; | ||
import { getQueryParams } from '../../../src/utils/slack/base.js'; | ||
import trigger, { INITIAL_KEYWORDS_SLACK_MESSAGE } from '../../../src/controllers/trigger/keywords.js'; | ||
|
||
describe('Keywords trigger', () => { | ||
let context; | ||
let dataAccessMock; | ||
let sqsMock; | ||
let sandbox; | ||
let sites; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
|
||
sites = [ | ||
createSite({ | ||
id: 'id1', | ||
baseURL: 'https://foo.com', | ||
auditConfig: { | ||
auditTypeConfigs: { | ||
'organic-keywords': { | ||
disabled: false, | ||
}, | ||
}, | ||
}, | ||
}), | ||
createSite({ | ||
id: 'id2', | ||
baseURL: 'https://bar.com', | ||
}), | ||
]; | ||
|
||
dataAccessMock = { | ||
getSitesByDeliveryType: sandbox.stub(), | ||
}; | ||
|
||
sqsMock = { | ||
sendMessage: sandbox.stub().resolves(), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('triggers a keyword audit', async () => { | ||
context = { | ||
log: console, | ||
dataAccess: dataAccessMock, | ||
sqs: sqsMock, | ||
data: { type: 'organic-keywords', url: 'ALL' }, | ||
env: { | ||
AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com', | ||
SLACK_BOT_TOKEN: 'token', | ||
AUDIT_REPORT_SLACK_CHANNEL_ID: 'channelId', | ||
}, | ||
}; | ||
|
||
dataAccessMock.getSitesByDeliveryType.resolves(sites); | ||
|
||
nock('https://slack.com') | ||
.get('/api/chat.postMessage') | ||
.query(getQueryParams('channelId', INITIAL_KEYWORDS_SLACK_MESSAGE)) | ||
.reply(200, { | ||
ok: true, | ||
channel: 'channelId', | ||
ts: 'threadId', | ||
}); | ||
|
||
const response = await trigger(context); | ||
const result = await response.json(); | ||
|
||
expect(dataAccessMock.getSitesByDeliveryType.calledOnce).to.be.true; | ||
expect(sqsMock.sendMessage.callCount).to.equal(1); | ||
expect(result.message[0]).to.equal('Triggered organic-keywords audit for id1'); | ||
}); | ||
|
||
it('throws an error if slack post message fails', async () => { | ||
nock('https://slack.com') | ||
.get('/api/chat.postMessage') | ||
.query(getQueryParams('channelId', INITIAL_KEYWORDS_SLACK_MESSAGE)) | ||
.replyWithError('Slack post message API request failed'); | ||
|
||
const response = await trigger(context); | ||
|
||
expect(response.status).to.equal(500); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all of the keys here, we could probably reuse the types exported from the audit model