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

feat: init db wrapper #7

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
057be2d
feat: init db wrapper
alinarublea Oct 19, 2023
9d0971b
feat: create wrapper for SQSClient
dzehnder Oct 19, 2023
95ff60d
feat: init db wrapper
alinarublea Oct 19, 2023
2418fb3
feat: init db wrapper
alinarublea Oct 19, 2023
63e21cc
feat: init db wrapper
alinarublea Oct 19, 2023
b71d19b
feat: init db wrapper
alinarublea Oct 19, 2023
e678ad8
feat: init db wrapper
alinarublea Oct 19, 2023
fabbd71
feat: init db wrapper
alinarublea Oct 19, 2023
83e34ce
fix: PR review
dzehnder Oct 19, 2023
2f90385
fix: PR review
dzehnder Oct 19, 2023
c15a214
feat: init db wrapper
alinarublea Oct 19, 2023
a4a362d
feat: init db wrapper
alinarublea Oct 19, 2023
e673748
feat: init db wrapper
alinarublea Oct 19, 2023
3271e0f
feat: init db wrapper
alinarublea Oct 19, 2023
0cbdaa6
fix: secret wrapper at end
dzehnder Oct 19, 2023
930c617
feat: update according to code review
alinarublea Oct 20, 2023
306bf56
feat: update according to code review x2
alinarublea Oct 20, 2023
9734949
feat: update according to code review x2
alinarublea Oct 20, 2023
221dfe4
feat: update according to code review x2
alinarublea Oct 20, 2023
912dd28
feat: update according to code review x2
alinarublea Oct 20, 2023
71285f7
feat: change SQS queue from functional to class component
dzehnder Oct 20, 2023
a05ac6f
fix: class usage
dzehnder Oct 20, 2023
db9a7d4
Merge branch 'sqs-wrapper' into init-db-wrapper
dzehnder Oct 20, 2023
42dee05
feat: update according to code review x2
alinarublea Oct 20, 2023
a873045
test: adding unit tests for queue wrapper
dzehnder Oct 20, 2023
56f314b
test: adding unit tests for sqs client
dzehnder Oct 24, 2023
06dc51f
feat: partially write tests and fix get site command
alinarublea Oct 24, 2023
e14cce0
feat: add more code coverage
alinarublea Oct 25, 2023
b4ec36c
feat: add more code coverage
alinarublea Oct 25, 2023
3a2180a
feat: remove codeql
alinarublea Oct 25, 2023
0360407
feat: remove codeql
alinarublea Oct 25, 2023
a506edf
feat: remove codeql
alinarublea Oct 26, 2023
f4992fb
feat: remove codeql
alinarublea Nov 13, 2023
efbf8ad
Update src/queue-wrapper.js
alinarublea Nov 13, 2023
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
29 changes: 29 additions & 0 deletions src/db-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023 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 createDynamoDBService from './db.js';
import serviceWrap from './service-wrap.js';

function wrapper(func) {
alinarublea marked this conversation as resolved.
Show resolved Hide resolved
const region = process.env.AWS_REGION;
if (!region) {
throw Error('Please define region in secrets');
}
return (request, context) => serviceWrap(
func,
request,
{ ...context, region },
'__ow_dynamodb',
createDynamoDBService,
);
}
const dynamoDBWrapper = wrapper;
export { dynamoDBWrapper };
alinarublea marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 11 additions & 8 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { log } from './util.js';
const TABLE_SITES = 'spacecat-site';
const TABLE_AUDITS = 'spacecat-audit-index';

function DB(config) {
const client = new DynamoDBClient({ region: config.region });
function DB(context) {
alinarublea marked this conversation as resolved.
Show resolved Hide resolved
const client = new DynamoDBClient({ region: context.region });
const docClient = DynamoDBDocumentClient.from(client);

/**
Expand Down Expand Up @@ -48,10 +48,10 @@ function DB(config) {

const newAudit = {

Choose a reason for hiding this comment

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

maybe it would help to include a typedef for audit and site

either as jsdoc comment @typedef Audit ... or as typescript type

id: uuid,
siteId: site.id,
siteId: `${site.domain}/${site.path}`,
audit_date: now,
type: 'psi',
is_live: site.isLive,
is_live: false,
content_publication_date: '',
git_hashes: [],
tag_manager: '',
Expand Down Expand Up @@ -100,11 +100,12 @@ function DB(config) {
}
/**
* Fetches a site by its ID and gets its latest audit.
* @param {string} siteId - The ID of the site to fetch.
* @param {string} domain - The domain of the site to fetch.
* @param {string} path - The path of the site to fetch.
* @returns {Promise<object>} Site document with its latest audit.
*/
async function getSite(domain, path) {
const params = {
const commandParams = {
TableName: TABLE_SITES, // Replace with your table name
Key: {
Domain: { S: domain }, // Partition key
Expand All @@ -113,7 +114,7 @@ function DB(config) {
};

try {
const command = new GetItemCommand(params);
const command = new GetItemCommand(commandParams);
const response = await client.send(command);
const item = response.Item;
if (item) {
Expand All @@ -135,4 +136,6 @@ function DB(config) {
};
}

export default DB;
const createDynamoDBService = (context) => DB(context);

export default { createDynamoDBService };
alinarublea marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 5 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import wrap from '@adobe/helix-shared-wrap';
import { logger } from '@adobe/helix-universal-logger';
import { helixStatus } from '@adobe/helix-status';
import SQSQueue from './sqs-queue.js';
import DB from './db.js'; // Assuming the exported content of './db' is default exported
import { dynamoDBWrapper } from './db-wrapper.js'; // Assuming the exported content of './db' is default exported
import PSIClient from './psi-client.js'; // Assuming the exported content of './psi-client' is default exported

/**
Expand All @@ -24,9 +24,9 @@ import PSIClient from './psi-client.js'; // Assuming the exported content of './
* @returns {Response} a response
*/
async function run(request, context) {
const db = DB({
region: process.env.REGION,
});
const {
__ow_dynamodb: db,
} = context;
const sqsQueue = SQSQueue();
const { message } = JSON.parse(context.invocation.event.Records[0].body);

Expand All @@ -36,11 +36,8 @@ async function run(request, context) {
});

const site = {
id: message.siteId,
githubURL: message.githubURL,
domain: message.domain,
path: message.path,
isLive: message.isLive,
};
const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`);
const auditResultMin = await db.saveAuditIndex(site, auditResult);
Expand All @@ -50,6 +47,7 @@ async function run(request, context) {

export const main = wrap(run)
.with(helixStatus)
.with(dynamoDBWrapper)
.with(logger.trace)
.with(logger)
.with(secrets);
28 changes: 28 additions & 0 deletions src/service-wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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.
*/
const serviceWrap = (
lambdaFn,
lambdaRequest,
lambdaContext,
paramName,
factoryFn,
) => {
if (!lambdaContext[paramName]) {
// pass params by reference and not value so later modifications
// of the params are accessible to the wrap
// eslint-disable-next-line no-param-reassign
lambdaContext[paramName] = factoryFn(lambdaContext);
}
return lambdaFn(lambdaContext);
};

module.exports = serviceWrap;
alinarublea marked this conversation as resolved.
Show resolved Hide resolved
Loading