Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e66adcc

Browse files
committedNov 27, 2018
DynamoDB updates
1 parent 24cb5a3 commit e66adcc

File tree

8 files changed

+113
-21
lines changed

8 files changed

+113
-21
lines changed
 

‎config/default.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ module.exports = {
2424
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key') // eslint-disable-line no-sync
2525
}
2626
},
27-
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/topcoderx'
27+
DYNAMODB: {
28+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID || '',
29+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY || '',
30+
AWS_REGION: process.env.AWS_REGION || '',
31+
IS_LOCAL: process.env.IS_LOCAL || ''
32+
}
2833
};

‎configuration.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ The following config parameters are supported, they are defined in `config/defau
66
| :------------------------------------- | :----------------------------------------: | :------------------------------: |
77
| PORT | The port the application will listen on | 3002 |
88
| LOG_LEVEL | The log level | info |
9-
| MONGODB_URI | The MongoDB URI. This needs to be the same MongoDB used by topcoder-x-receiver, topcoder-x-processor, and topcoder-x-site | mongodb://127.0.0.1:27017/topcoderx |
109
|TOPIC | The Kafka topic where events are published. This must be the same as the configured value for topcoder-x-processor| |
1110
|KAFKA_OPTIONS | Kafka connection options| |
1211
|KAFKA_HOST | The Kafka host to connect to| localhost:9092 |
1312
|KAFKA_CLIENT_CERT | The Kafka SSL certificate to use when connecting| Read from kafka_client.cer file, but this can be set as a string like it is on Heroku |
1413
|KAFKA_CLIENT_CERT_KEY | The Kafka SSL certificate key to use when connecting| Read from kafka_client.key file, but this can be set as a string like it is on Heroku|
14+
|AWS_ACCESS_KEY_ID | The Amazon certificate key to use when connecting. Use local dynamodb you can set fake value|FAKE_ACCESS_KEY_ID |
15+
|AWS_SECRET_ACCESS_KEY | The Amazon certificate access key to use when connecting. Use local dynamodb you can set fake value|FAKE_SECRET_ACCESS_KEY |
16+
|AWS_REGION | The Amazon certificate region to use when connecting. Use local dynamodb you can set fake value|FAKE_REGION |
17+
|IS_LOCAL | Use Amazon DynamoDB Local or server |true|
1518

1619
KAFKA_OPTIONS should be object as described in https://github.com/oleksiyk/kafka#ssl
1720
For using with SSL, the options should be as
1821
```
1922
{
2023
connectionString: '<server>',
2124
ssl: {
22-
cert: '<certificate>',
25+
cert: '<certificate>',
2326
key: '<key>'
2427
}
2528
}
@@ -39,7 +42,7 @@ Configure a Github project with a webhook with a format like this: https://<rece
3942
#### Smoke test
4043
- Create an issue in the repo, you can see the logs in `receiver`, the `issue.created` event is generated.
4144

42-
You can test other events, but just validating that an issue.created event is generated in Kafka is enough to smoke test the receiver is set up properly.
45+
You can test other events, but just validating that an issue.created event is generated in Kafka is enough to smoke test the receiver is set up properly.
4346

4447
## Gitlab Verification
4548

‎models/Project.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,33 @@
88
* @author TCSCODER
99
* @version 1.0
1010
*/
11-
const mongoose = require('mongoose');
11+
const dynamoose = require('dynamoose');
1212

13-
const schema = new mongoose.Schema({
13+
const Schema = dynamoose.Schema;
14+
15+
const schema = new Schema({
16+
id: {
17+
type: String,
18+
hashKey: true,
19+
required: true
20+
},
1421
title: {type: String, required: true},
15-
tcDirectId: {type: Number, required: true},
22+
tcDirectId: {
23+
type: Number,
24+
required: true,
25+
index: {
26+
global: true,
27+
rangeKey: 'id',
28+
project: true,
29+
name: 'TcDirectIdIndex'
30+
}
31+
},
1632
repoUrl: {type: String, required: true},
1733
rocketChatWebhook: {type: String, required: false},
1834
rocketChatChannelName: {type: String, required: false},
1935
archived: {type: String, required: true},
20-
username: {type: String, required: true},
36+
owner: {type: String, required: true},
2137
secretWebhookKey: {type: String, required: true}
2238
});
2339

24-
schema.index({tcDirectId: 1});
25-
26-
2740
module.exports = schema;

‎models/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@
1111
'use strict';
1212

1313
const config = require('config');
14-
const mongoose = require('mongoose');
14+
const dynamoose = require('dynamoose');
15+
16+
dynamoose.AWS.config.update({
17+
accessKeyId: config.DYNAMODB.AWS_ACCESS_KEY_ID,
18+
secretAccessKey: config.DYNAMODB.AWS_SECRET_ACCESS_KEY,
19+
region: config.DYNAMODB.AWS_REGION
20+
});
21+
22+
if (config.DYNAMODB.IS_LOCAL) {
23+
dynamoose.local();
24+
}
25+
26+
dynamoose.setDefaults({
27+
create: false,
28+
update: false
29+
});
1530

16-
mongoose.Promise = global.Promise;
17-
const connection = mongoose.createConnection(config.MONGODB_URL);
1831
const IssueCreatedEvent = require('./IssueCreatedEvent');
1932
const IssueUpdatedEvent = require('./IssueUpdatedEvent');
2033
const IssueClosedEvent = require('./IssueClosedEvent');
@@ -37,6 +50,6 @@ module.exports = {
3750
PullRequestCreatedEvent,
3851
PullRequestClosedEvent,
3952
LabelUpdatedEvent,
40-
Project: connection.model('Project', Project),
53+
Project: dynamoose.model('Project', Project),
4154
IssueClosedEvent
4255
};

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"config": "^1.30.0",
1515
"cookie-parser": "~1.4.3",
1616
"debug": "^2.6.9",
17+
"dynamoose": "^1.1.0",
1718
"express": "^4.15.5",
1819
"joi": "^11.4.0",
19-
"mongoose": "^4.13.14",
2020
"morgan": "~1.8.1",
2121
"no-kafka": "^3.2.10",
2222
"winston": "^2.4.2"

‎routes/middlewares/RepositoryFilter.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const _ = require('lodash');
1212

1313
const logger = require('../../utils/logger');
1414
const Project = require('../../models').Project;
15+
const dbHelper = require('../../utils/db-helper');
1516

1617
module.exports = (provider) => async (req, res, next) => {
1718
let repoNames = [];
@@ -22,9 +23,10 @@ module.exports = (provider) => async (req, res, next) => {
2223
const repo = req.body.project || {};
2324
repoNames = [repo.homepage, repo.http_url, repo.url, repo.ssh_url, repo.web_url];
2425
}
25-
let found = false;
26-
const projects = await Project.find({archived: false});
27-
found = _.some(projects, (project) => _.includes(repoNames, project.repoUrl));
26+
const projects = await dbHelper.scan(Project, {
27+
archived: 'false'
28+
});
29+
const found = _.some(projects, (project) => _.includes(repoNames, project.repoUrl));
2830
if (found) {
2931
return next();
3032
}

‎routes/middlewares/SecurityChecker.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@
1111
const crypto = require('crypto');
1212
const logger = require('../../utils/logger');
1313
const Project = require('../../models').Project;
14+
const dbHelper = require('../../utils/db-helper');
1415

1516
module.exports = (provider) => async (req, res, next) => {
1617
let isValid = false;
1718
const params = req.body;
1819
if (provider === 'github') {
19-
const projectDetail = await Project.findOne({repoUrl: params.repository.html_url});
20+
const projectDetail = await dbHelper.scanOne(Project, {
21+
repoUrl: params.repository.html_url
22+
});
23+
2024
const hash = crypto.createHmac('sha1', projectDetail.secretWebhookKey).update(req.rawBody).digest('hex');
2125
isValid = `sha1=${hash}` === req.header('X-Hub-Signature');
2226
} else if (provider === 'gitlab') {
23-
const projectDetail = await Project.findOne({repoUrl: params.project.web_url});
27+
const projectDetail = await dbHelper.scanOne(Project, {
28+
repoUrl: params.project.web_url
29+
});
2430
isValid = projectDetail.secretWebhookKey === req.header('X-Gitlab-Token');
2531
} else {
2632
// unknown provider

‎utils/db-helper.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2018 TopCoder, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
/**
7+
* This module contains the database helper methods.
8+
*
9+
* @version 1.0
10+
*/
11+
12+
/**
13+
* Get data collection by scan parameters
14+
* @param {Object} model The dynamoose model to scan
15+
* @param {Object} scanParams The scan parameters object
16+
* @returns {Promise<void>}
17+
*/
18+
async function scan(model, scanParams) {
19+
return await new Promise((resolve, reject) => {
20+
model.scan(scanParams).exec((err, result) => {
21+
if (err) {
22+
reject(err);
23+
}
24+
return resolve(result);
25+
});
26+
});
27+
}
28+
29+
/**
30+
* Get single data by scan parameters
31+
* @param {Object} model The dynamoose model to scan
32+
* @param {Object} scanParams The scan parameters object
33+
* @returns {Promise<void>}
34+
*/
35+
async function scanOne(model, scanParams) {
36+
return await new Promise((resolve, reject) => {
37+
model.scan(scanParams).exec((err, result) => {
38+
if (err) {
39+
reject(err);
40+
}
41+
42+
return resolve(result[0]);
43+
});
44+
});
45+
}
46+
47+
module.exports = {
48+
scan,
49+
scanOne
50+
};

0 commit comments

Comments
 (0)
This repository has been archived.