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

Commit 9897bd5

Browse files
committed
Split usermapping table.
1 parent 87355ab commit 9897bd5

13 files changed

+179
-60
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"lint": "gulp lint",
2323
"heroku-postbuild": "gulp build",
2424
"create-tables": "CREATE_DB=true node scripts/create-update-tables.js",
25+
"migrate-user-mapping": "node scripts/migrate-user-mapping.js",
2526
"log-repository-collisions": "node scripts/log-repository-collisions.js"
2627
},
2728
"dependencies": {

scripts/migrate-user-mapping.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const AWS = require('aws-sdk');
2+
const helper = require('../src/common/helper');
3+
const dbHelper = require('../src/common/db-helper');
4+
const GithubUserMapping = require('../src/models').GithubUserMapping;
5+
const GitlabUserMapping = require('../src/models').GitlabUserMapping;
6+
7+
console.log(process.env.IS_LOCAL);
8+
if (process.env.IS_LOCAL) {
9+
AWS.config.update({
10+
endpoint: 'http://localhost:8000'
11+
});
12+
}
13+
var documentClient = new AWS.DynamoDB.DocumentClient();
14+
15+
(async () => {
16+
console.log('Migrating...');
17+
const params = {
18+
TableName: 'Topcoder_X.UserMapping'
19+
};
20+
21+
let items;
22+
do {
23+
items = await documentClient.scan(params).promise();
24+
items.Items.forEach(async (item) => {
25+
console.log(item);
26+
if (item.githubUserId && item.githubUsername) {
27+
await dbHelper.create(GithubUserMapping, {
28+
id: helper.generateIdentifier(),
29+
topcoderUsername: item.topcoderUsername,
30+
githubUserId: item.githubUserId,
31+
githubUsername: item.githubUsername,
32+
});
33+
}
34+
if (item.gitlabUsername && item.gitlabUserId) {
35+
await dbHelper.create(GitlabUserMapping, {
36+
id: helper.generateIdentifier(),
37+
topcoderUsername: item.topcoderUsername,
38+
gitlabUsername: item.gitlabUsername,
39+
gitlabUserId: item.gitlabUserId,
40+
});
41+
}
42+
});
43+
params.ExclusiveStartKey = items.LastEvaluatedKey;
44+
} while(typeof items.LastEvaluatedKey !== 'undefined');
45+
})();

src/common/db-helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async function queryOneUserMappingByTCUsername(model, tcusername) {
154154
model.queryOne('topcoderUsername').eq(tcusername)
155155
.all()
156156
.exec((err, result) => {
157-
if (err || !result) {
157+
if (err) {
158158
logger.debug(`queryOneUserMappingByTCUsername. Error. ${err}`);
159159
return reject(err);
160160
}

src/common/helper.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ async function getProviderType(repoUrl) {
216216
* @returns {Object} the owner/copilot for the project
217217
*/
218218
async function getProjectCopilotOrOwner(models, project, provider, isCopilot) {
219-
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(models.UserMapping,
219+
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(
220+
provider === 'github' ? models.GithubUserMapping : models.GitlabUserMapping,
220221
isCopilot ? project.copilot : project.owner);
221222

222223
if (!userMapping ||

src/controllers/GithubController.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const GithubService = require('../services/GithubService');
2020
const UserService = require('../services/UserService');
2121
const OwnerUserTeam = require('../models').OwnerUserTeam;
2222
const UserTeamMapping = require('../models').UserTeamMapping;
23-
const UserMapping = require('../models').UserMapping;
23+
const GithubUserMapping = require('../models').GithubUserMapping;
2424
const constants = require('../common/constants');
2525

2626
const request = superagentPromise(superagent, Promise);
@@ -160,18 +160,19 @@ async function addUserToTeamCallback(req, res) {
160160
console.log(`adding ${token} to ${team.teamId} with ${team.ownerToken}`); /* eslint-disable-line no-console */
161161
const githubUser = await GithubService.addTeamMember(team.teamId, team.ownerToken, token, team.accessLevel);
162162
// associate github username with TC username
163-
const mapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, req.session.tcUsername);
163+
const mapping = await dbHelper.queryOneUserMappingByTCUsername(GithubUserMapping, req.session.tcUsername);
164164

165165
// get team details
166166
const teamDetails = await GithubService.getTeamDetails(team.ownerToken, team.teamId);
167167

168168
if (mapping) {
169-
await dbHelper.update(UserMapping, mapping.id, {
169+
await dbHelper.update(GithubUserMapping, mapping.id, {
170170
githubUsername: githubUser.username,
171171
githubUserId: githubUser.id,
172172
});
173173
} else {
174-
await dbHelper.create(UserMapping, {
174+
console.log('User mapping not found. Create new mapping.'); /* eslint-disable-line no-console */
175+
await dbHelper.create(GithubUserMapping, {
175176
id: helper.generateIdentifier(),
176177
topcoderUsername: req.session.tcUsername,
177178
githubUsername: githubUser.username,

src/controllers/GitlabController.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const GitlabService = require('../services/GitlabService');
2020
const UserService = require('../services/UserService');
2121
const User = require('../models').User;
2222
const OwnerUserGroup = require('../models').OwnerUserGroup;
23-
const UserMapping = require('../models').UserMapping;
23+
const GitlabUserMapping = require('../models').GitlabUserMapping;
2424
const UserGroupMapping = require('../models').UserGroupMapping;
2525

2626
const request = superagentPromise(superagent, Promise);
@@ -209,14 +209,14 @@ async function addUserToGroupCallback(req, res) {
209209
group.expiredAt);
210210
// associate gitlab username with TC username
211211

212-
const mapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, req.session.tcUsername);
212+
const mapping = await dbHelper.queryOneUserMappingByTCUsername(GitlabUserMapping, req.session.tcUsername);
213213
if (mapping) {
214-
await dbHelper.update(UserMapping, mapping.id, {
214+
await dbHelper.update(GitlabUserMapping, mapping.id, {
215215
gitlabUsername: gitlabUser.username,
216216
gitlabUserId: gitlabUser.id,
217217
});
218218
} else {
219-
await dbHelper.create(UserMapping, {
219+
await dbHelper.create(GitlabUserMapping, {
220220
id: helper.generateIdentifier(),
221221
topcoderUsername: req.session.tcUsername,
222222
gitlabUsername: gitlabUser.username,

src/models/GithubUserMapping.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* This defines github user mapping model.
3+
*/
4+
'use strict';
5+
6+
const dynamoose = require('dynamoose');
7+
8+
const Schema = dynamoose.Schema;
9+
10+
const schema = new Schema({
11+
id: {
12+
type: String,
13+
required: true,
14+
hashKey: true
15+
},
16+
topcoderUsername: {
17+
type: String,
18+
required: true,
19+
index: {
20+
global: true,
21+
project: true,
22+
rangKey: 'id',
23+
name: 'TopcoderUsernameIndex'
24+
}
25+
},
26+
githubUsername: {
27+
type: String,
28+
index: {
29+
global: true,
30+
project: true,
31+
rangKey: 'id',
32+
name: 'GithubUsernameIndex'
33+
}
34+
},
35+
githubUserId: {
36+
type: Number,
37+
index: {
38+
global: true,
39+
project: true,
40+
rangKey: 'id',
41+
name: 'GithubUserIdIndex'
42+
}
43+
}
44+
});
45+
46+
module.exports = schema;

src/models/GitlabUserMapping.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* This defines gitlab user mapping model.
3+
*/
4+
'use strict';
5+
6+
const dynamoose = require('dynamoose');
7+
8+
const Schema = dynamoose.Schema;
9+
10+
const schema = new Schema({
11+
id: {
12+
type: String,
13+
required: true,
14+
hashKey: true
15+
},
16+
topcoderUsername: {
17+
type: String,
18+
required: true,
19+
index: {
20+
global: true,
21+
project: true,
22+
rangKey: 'id',
23+
name: 'TopcoderUsernameIndex'
24+
}
25+
},
26+
gitlabUsername: {
27+
type: String,
28+
index: {
29+
global: true,
30+
project: true,
31+
rangKey: 'id',
32+
name: 'GitlabUsernameIndex'
33+
}
34+
},
35+
gitlabUserId: {
36+
type: Number,
37+
index: {
38+
global: true,
39+
project: true,
40+
rangKey: 'id',
41+
name: 'GitlabUserIdIndex'
42+
}
43+
}
44+
});
45+
46+
module.exports = schema;

src/models/UserMapping.js

-30
This file was deleted.

src/services/GithubService.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const constants = require('../common/constants');
1717
const helper = require('../common/helper');
1818
const dbHelper = require('../common/db-helper');
1919
const User = require('../models').User;
20-
const UserMapping = require('../models').UserMapping;
20+
const GithubUserMapping = require('../models').GithubUserMapping;
2121
const OwnerUserTeam = require('../models').OwnerUserTeam;
2222
const errors = require('../common/errors');
2323

@@ -41,16 +41,16 @@ async function ensureOwnerUser(token, topcoderUsername) {
4141
constants.USER_TYPES.GITHUB,
4242
constants.USER_ROLES.OWNER);
4343

44-
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, topcoderUsername);
44+
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(GithubUserMapping, topcoderUsername);
4545
if (!userMapping) {
46-
await dbHelper.create(UserMapping, {
46+
await dbHelper.create(GithubUserMapping, {
4747
id: helper.generateIdentifier(),
4848
topcoderUsername,
4949
githubUserId: userProfile.id,
5050
githubUsername: userProfile.login,
5151
});
5252
} else {
53-
await dbHelper.update(UserMapping, userMapping.id, {
53+
await dbHelper.update(GithubUserMapping, userMapping.id, {
5454
githubUserId: userProfile.id,
5555
githubUsername: userProfile.login,
5656
});

src/services/GitlabService.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const helper = require('../common/helper');
1919
const dbHelper = require('../common/db-helper');
2020
const errors = require('../common/errors');
2121
const User = require('../models').User;
22-
const UserMapping = require('../models').UserMapping;
22+
const GitlabUserMapping = require('../models').GitlabUserMapping;
2323
const OwnerUserGroup = require('../models').OwnerUserGroup;
2424

2525
const request = superagentPromise(superagent, Promise);
@@ -52,16 +52,16 @@ async function ensureOwnerUser(token, topcoderUsername) {
5252
constants.USER_TYPES.GITLAB,
5353
constants.USER_ROLES.OWNER);
5454

55-
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, topcoderUsername);
55+
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(GitlabUserMapping, topcoderUsername);
5656
if (!userMapping) {
57-
await dbHelper.create(UserMapping, {
57+
await dbHelper.create(GitlabUserMapping, {
5858
id: helper.generateIdentifier(),
5959
topcoderUsername,
6060
gitlabUserId: userProfile.id,
6161
gitlabUsername: userProfile.username,
6262
});
6363
} else {
64-
await dbHelper.update(UserMapping, userMapping.id, {
64+
await dbHelper.update(GitlabUserMapping, userMapping.id, {
6565
gitlabUserId: userProfile.id,
6666
gitlabUsername: userProfile.username,
6767
});

src/services/TCUserService.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const Joi = require('joi');
1212
const decodeToken = require('tc-auth-lib').decodeToken;
1313
const errors = require('../common/errors');
1414
const helper = require('../common/helper');
15-
const UserMapping = require('../models').UserMapping;
15+
const GithubUserMapping = require('../models').GithubUserMapping;
16+
const GitlabUserMapping = require('../models').GitlabUserMapping;
1617

1718
/**
1819
* gets the handle of tc user.
@@ -40,7 +41,12 @@ async function getUserMapping(query) {
4041
'At least one of topcoderUsername/gitlabUsername/githubUsername should be provided.');
4142
}
4243

43-
return await helper.ensureExists(UserMapping, query, 'UserMapping');
44+
if (query.githubUsername) {
45+
return await helper.ensureExists(GithubUserMapping, query, 'GithubUserMapping');
46+
}
47+
else {
48+
return await helper.ensureExists(GitlabUserMapping, query, 'GitlabUserMapping');
49+
}
4450
}
4551

4652
getUserMapping.schema = Joi.object().keys({

0 commit comments

Comments
 (0)