Skip to content

Commit d85c903

Browse files
committed
Production deploy updates
1 parent 121e4dc commit d85c903

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-12
lines changed

constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const USER_ROLES = {
2626
const CHALLENGE_STATUS = {
2727
ACTIVE: 'Active',
2828
COMPLETED: 'Completed',
29-
CANCELED: 'Canceled'
29+
CANCELED: 'Cancelled'
3030
};
3131

3232
const SERVICE_ERROR_STATUS = 500;

models/Project.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const schema = new Schema({
4242
type: Date,
4343
default: Date.now
4444
},
45-
createCopilotPayments: {type: String, required: false}
45+
createCopilotPayments: {type: String, required: false},
46+
isConnect: {type: Boolean, required: false, default: true}
4647
});
4748

4849
module.exports = schema;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"test": "mocha",
1010
"test:github": "mocha test/github.test.js",
1111
"test:gitlab": "mocha test/gitlab.test.js",
12-
"create-tables": "CREATE_DB=true node scripts/create-update-tables.js"
12+
"create-tables": "CREATE_DB=true node scripts/create-update-tables.js",
13+
"direct-connect-migration": "node scripts/direct-connect-migration.js"
1314
},
1415
"engines": {
1516
"node": "~8.6.0"

scripts/direct-connect-migration.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const models = require('../models');
5+
const dbHelper = require('../utils/db-helper');
6+
const logger = require('../utils/logger');
7+
const topcoderApiHelper = require('../utils/topcoder-api-helper');
8+
9+
/**
10+
* Migrate direct ids to connect ids
11+
*/
12+
async function migrate() {
13+
// How many projects per scan
14+
const batchSize = process.env.BATCH_SIZE || 15; // eslint-disable-line no-magic-numbers
15+
let batch = 1;
16+
// How many projects have been returned in previous scan
17+
let previousSize = batchSize;
18+
// Key of a last project
19+
let previousKey = null;
20+
while (previousSize === batchSize) { // eslint-disable-line no-restricted-syntax
21+
logger.info(`Starting batch ${batch}`);
22+
// Scan for projects with field isConnect null and return its id and tcDirectId
23+
const projects = await models.Project.scan('isConnect')
24+
.null()
25+
.attributes(['id', 'tcDirectId'])
26+
.limit(batchSize)
27+
.startAt(previousKey)
28+
.consistent()
29+
.exec();
30+
previousSize = projects.count;
31+
previousKey = projects.lastKey;
32+
const promises = _.map(projects, (project) => topcoderApiHelper.getProjectByDirectId(project.id, project.tcDirectId));
33+
// Execute all promises and process data
34+
await Promise.all(promises).then(async (resArray) => {
35+
// Filter out empty arrays
36+
const directProjects = _.filter(resArray, (res) => res.data.length > 0);
37+
await Promise.all(_.map(directProjects, (directProject) =>
38+
// Promise to update tcDirectId and set isConnect field to true for project with its id
39+
dbHelper.update(models.Project,
40+
directProject.dbId,
41+
{
42+
tcDirectId: directProject.data[0].id,
43+
isConnect: true
44+
}).then(() => {
45+
// eslint-disable-next-line max-len
46+
logger.debug(`Migrated direct project: ${directProject.data[0].directProjectId} to connect: ${directProject.data[0].id}, database id: ${directProject.dbId}`);
47+
})));
48+
});
49+
batch += 1;
50+
}
51+
}
52+
53+
migrate().then(() => {
54+
logger.info('Migration completed');
55+
}).catch((err) => {
56+
logger.logFullError(err, 'migration');
57+
});

services/IssueService.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
const config = require('config');
1414
const _ = require('lodash');
1515
const Joi = require('joi');
16-
const MarkdownIt = require('markdown-it');
1716
const logger = require('../utils/logger');
1817
const errors = require('../utils/errors');
1918
const topcoderApiHelper = require('../utils/topcoder-api-helper');
@@ -25,7 +24,6 @@ const userService = require('./UserService');
2524
const eventService = require('./EventService');
2625
const constants = require('../constants');
2726

28-
const md = new MarkdownIt();
2927

3028
// A variable to store issue creation lock to prevent duplicate creation process.
3129
// The key is `${provider}-${repositoryId}-${number}`. The value is True.
@@ -216,13 +214,18 @@ async function handleIssueAssignment(event, issue, force = false) {
216214
logger.debugWithContext(`${userMapping.topcoderUsername} Already registered as assignee`, event, issue);
217215
return;
218216
}
219-
220-
// The issue has registered assignee. Ignore it.
217+
const hasAssignedLabel = _.includes(issue.labels, config.ASSIGNED_ISSUE_LABEL);
218+
// Gitlab doesn't send separate unassignment hook if we unassigne and assigne users in the same step
219+
// in result new assignee was not handled previously
220+
if (dbIssue.assignee && event.provider === 'gitlab' && dbIssue.assignee !== assigneeUserId && hasAssignedLabel) {
221+
await handleIssueUnAssignment(event, issue);
222+
return;
223+
}
224+
// The github issue has registered assignee. Ignore it.
221225
// If there is assignee changes, it will be handled at handleIssueUnassignment and this func will be called again.
222226
if (dbIssue.assignee) {
223227
return;
224228
}
225-
226229
// ensure issue has open for pickup label
227230
const hasOpenForPickupLabel = _(issue.labels).includes(config.OPEN_FOR_PICKUP_ISSUE_LABEL); // eslint-disable-line lodash/chaining
228231
const hasNotReadyLabel = _(issue.labels).includes(config.NOT_READY_ISSUE_LABEL); // eslint-disable-line lodash/chaining
@@ -693,7 +696,6 @@ async function handleIssueUnAssignment(event, issue) {
693696
// Ignore it.
694697
return;
695698
}
696-
697699
if (dbIssue.assignee) {
698700
const assigneeUserId = await gitHelper.getUserIdByLogin(event, dbIssue.assignee);
699701
if (!assigneeUserId) {
@@ -865,8 +867,7 @@ async function process(event) {
865867
issue.repositoryId = helper.hashCode(issue.repositoryId);
866868
}
867869

868-
// Markdown the body
869-
issue.body = md.render(_.get(issue, 'body', ''));
870+
issue.body = _.get(issue, 'body', '');
870871
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
871872
issue.assignee = await gitHelper.getUsernameById(event, event.data.issue.assignees[0].id);
872873
}

utils/topcoder-api-helper.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,34 @@ async function removeResourceToChallenge(id, handle, roleId) {
431431
throw errors.convertTopcoderApiError(err, 'Failed to remove resource from the challenge.');
432432
}
433433
}
434+
/**
435+
* Finds project by given direct id and returns it
436+
* @param {id} id in the database
437+
* @param {Number} directId
438+
* @returns {Promise}
439+
*/
440+
async function getProjectByDirectId(id, directId) {
441+
const apiKey = await getM2Mtoken();
442+
return axios.get(`${config.TC_API_URL}/projects`, {
443+
params: {
444+
directProjectId: directId
445+
},
446+
headers: {
447+
authorization: `Bearer ${apiKey}`
448+
}
449+
}).then((data) => {
450+
// Append database project id to response
451+
data.dbId = id;
452+
// Log to debug if project is empty
453+
if (data.data.length === 0) {
454+
logger.debug(`Project with direct id ${directId} (database: ${id}) was not found in connect`);
455+
}
456+
return data;
457+
}).catch((err) => {
458+
logger.logFullError(err);
459+
throw errors.convertTopcoderApiError(err, `Failed to fetch project with direct id ${directId}`);
460+
});
461+
}
434462

435463

436464
module.exports = {
@@ -448,5 +476,6 @@ module.exports = {
448476
unregisterUserFromChallenge,
449477
cancelPrivateContent,
450478
assignUserAsRegistrant,
451-
removeResourceToChallenge
479+
removeResourceToChallenge,
480+
getProjectByDirectId
452481
};

0 commit comments

Comments
 (0)