Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7b7aad5

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents a501d39 + 8e5b024 commit 7b7aad5

14 files changed

+178
-64
lines changed

app.js

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @version 1.0
99
*/
1010
'use strict';
11+
const config = require('config');
12+
const _ = require('lodash');
1113
const express = require('express');
1214
const cookieParser = require('cookie-parser');
1315
const bodyParser = require('body-parser');
@@ -72,4 +74,37 @@ process.on('unhandledRejection', (err) => {
7274
logger.logFullError(err, 'system');
7375
});
7476

77+
// dump the configuration to logger
78+
const ignoreConfigLog = ['cert', 'key', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET'];
79+
/**
80+
* Print configs to logger
81+
* @param {Object} params the config params
82+
* @param {Number} level the level of param object
83+
*/
84+
function dumpConfigs(params, level) {
85+
Object.keys(params).forEach((key) => {
86+
if (_.includes(ignoreConfigLog, key)) {
87+
return;
88+
}
89+
const item = params[key];
90+
let str = '';
91+
let n = 0;
92+
while (n < level) { // eslint-disable-line no-restricted-syntax
93+
n++;
94+
str += ' ';
95+
}
96+
if (item && _.isObject(item)) {
97+
str += `${key}=`;
98+
logger.debug(str);
99+
dumpConfigs(item, level + 1);
100+
} else {
101+
str += `${key}=${item}`;
102+
logger.debug(str);
103+
}
104+
});
105+
}
106+
logger.debug('--- List of Configurations ---');
107+
dumpConfigs(config, 0);
108+
logger.debug('--- End of List of Configurations ---');
109+
75110
module.exports = app;

config/default.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const fs = require('fs');
1515

1616
module.exports = {
1717
PORT: process.env.PORT || 3000, // eslint-disable-line no-magic-numbers
18-
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
18+
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
1919
TOPIC: process.env.TOPIC || 'tc-x-events',
2020
KAFKA_OPTIONS: {
2121
connectionString: process.env.KAFKA_URL || 'localhost:9092',

models/CommentCreatedEvent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CommentCreatedEvent.schema = Joi.object().keys({
2121
id: Joi.number().required(),
2222
body: Joi.string().allow(''),
2323
user: Joi.object().keys({
24-
id: Joi.number().required()
24+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
2525
}).required()
2626
}),
2727
repository: repositorySchema.required()

models/IssueClosedEvent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const IssueClosedEvent = {
1818
issue: issueSchema.required(),
1919
repository: repositorySchema.required(),
2020
assignee: Joi.object().keys({
21-
id: Joi.number().allow(null)
21+
id: Joi.alternatives().try(Joi.string(), Joi.number()).allow(null)
2222
}).required()
2323
})
2424
};

models/Project.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ const schema = new Schema({
3030
}
3131
},
3232
repoUrl: {type: String, required: true},
33+
repoId: {type: String, required: false},
3334
rocketChatWebhook: {type: String, required: false},
3435
rocketChatChannelName: {type: String, required: false},
3536
archived: {type: String, required: true},
3637
owner: {type: String, required: true},
37-
secretWebhookKey: {type: String, required: true}
38+
secretWebhookKey: {type: String, required: true},
39+
createCopilotPayments: {type: String, required: false},
40+
isConnect: {type: Boolean, required: false, default: true}
3841
});
3942

4043
module.exports = schema;

models/UserAssignedEvent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ UserAssignedEvent.schema = Joi.object().keys({
1919
issue: issueSchema.required(),
2020
repository: repositorySchema.required(),
2121
assignee: Joi.object().keys({
22-
id: Joi.number().required()
22+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
2323
}).required()
2424
});
2525

models/common.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Joi = require('joi');
1616

1717
// the repository schema
1818
const repositorySchema = Joi.object().keys({
19-
id: Joi.number().required(),
19+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
2020
name: Joi.string().required(),
2121
full_name: Joi.string().required()
2222
});
@@ -28,10 +28,10 @@ const issueSchema = Joi.object().keys({
2828
body: Joi.string().allow(''),
2929
labels: Joi.array().items(Joi.string()),
3030
assignees: Joi.array().items(Joi.object().keys({
31-
id: Joi.number().required()
31+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
3232
})),
3333
owner: Joi.object().keys({
34-
id: Joi.number().required()
34+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
3535
}).required()
3636
});
3737

@@ -43,10 +43,10 @@ const pullRequestSchema = Joi.object().keys({
4343
body: Joi.string().allow(''),
4444
title: Joi.string().required(),
4545
user: Joi.object().keys({
46-
id: Joi.number().required()
46+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
4747
}),
4848
assignees: Joi.array().items({
49-
id: Joi.number().required()
49+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
5050
})
5151
});
5252

models/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ dynamoose.setDefaults({
2828
update: false
2929
});
3030

31+
if (process.env.CREATE_DB) {
32+
dynamoose.setDefaults({
33+
create: true,
34+
update: true
35+
});
36+
}
37+
3138
const IssueCreatedEvent = require('./IssueCreatedEvent');
3239
const IssueUpdatedEvent = require('./IssueUpdatedEvent');
3340
const IssueClosedEvent = require('./IssueClosedEvent');

package-lock.json

+31-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"scripts": {
66
"start": "node ./bin/www",
7-
"lint": "eslint . --ignore-pattern 'public/*' --ext .js --fix || true"
7+
"lint": "eslint . --ignore-pattern 'public/*' --ext .js --fix || true",
8+
"create-tables": "CREATE_DB=true node scripts/create-update-tables.js"
89
},
910
"engines": {
1011
"node": "~8.6.0"

routes/middlewares/SecurityChecker.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
'use strict';
1111
const crypto = require('crypto');
12+
const _ = require('lodash');
1213
const logger = require('../../utils/logger');
1314
const Project = require('../../models').Project;
1415
const dbHelper = require('../../utils/db-helper');
@@ -17,17 +18,24 @@ module.exports = (provider) => async (req, res, next) => {
1718
let isValid = false;
1819
const params = req.body;
1920
if (provider === 'github') {
20-
const projectDetail = await dbHelper.scanOne(Project, {
21+
const projectDetails = await dbHelper.scan(Project, {
2122
repoUrl: params.repository.html_url
2223
});
23-
24-
const hash = crypto.createHmac('sha1', projectDetail.secretWebhookKey).update(req.rawBody).digest('hex');
25-
isValid = `sha1=${hash}` === req.header('X-Hub-Signature');
24+
_.forEach(projectDetails, (projectDetail) => {
25+
const hash = crypto.createHmac('sha1', projectDetail.secretWebhookKey).update(req.rawBody).digest('hex');
26+
if (`sha1=${hash}` === req.header('X-Hub-Signature')) {
27+
isValid = true;
28+
}
29+
});
2630
} else if (provider === 'gitlab') {
27-
const projectDetail = await dbHelper.scanOne(Project, {
31+
const projectDetails = await dbHelper.scan(Project, {
2832
repoUrl: params.project.web_url
2933
});
30-
isValid = projectDetail.secretWebhookKey === req.header('X-Gitlab-Token');
34+
_.forEach(projectDetails, (projectDetail) => { // eslint-disable-line lodash/prefer-filter
35+
if (projectDetail.secretWebhookKey === req.header('X-Gitlab-Token')) {
36+
isValid = true;
37+
}
38+
});
3139
} else {
3240
// unknown provider
3341
return next();

scripts/create-update-tables.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2017 TopCoder, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
/**
7+
* Script to sync DB model to dynamodb service.
8+
* @author TCSCODER
9+
* @version 1.1
10+
*/
11+
12+
(async () => {
13+
try {
14+
console.log('Syncing database tables and indexes...');
15+
const models = require('../models');
16+
for (const key in models) {
17+
await pingTable(models[key]);
18+
}
19+
console.log('Remote tables is up to date.');
20+
} catch (e) {
21+
console.error(e);
22+
}
23+
})();
24+
25+
/**
26+
* Check if the table is exist
27+
* @param {Model} model the db model
28+
* @returns {Promise} the promise object
29+
*/
30+
async function pingTable(model) {
31+
return new Promise((resolve, reject) => {
32+
if (!(model.scan && typeof model.scan == 'function' && model.scan({}).exec)) {
33+
return resolve();
34+
}
35+
model.scan({id: 0}).exec((err, result) => {
36+
if (err) {
37+
console.log(`Table is not exists ${err}`);
38+
return reject(err);
39+
}
40+
return resolve();
41+
});
42+
});
43+
}

0 commit comments

Comments
 (0)