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

Version 1.1 #418

Merged
merged 4 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Split usermapping table.
  • Loading branch information
afrisalyp committed Jul 9, 2021
commit 9897bd505e23b3e3b2eb6a4a7ce3d768e1154fee
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lint": "gulp lint",
"heroku-postbuild": "gulp build",
"create-tables": "CREATE_DB=true node scripts/create-update-tables.js",
"migrate-user-mapping": "node scripts/migrate-user-mapping.js",
"log-repository-collisions": "node scripts/log-repository-collisions.js"
},
"dependencies": {
Expand Down
45 changes: 45 additions & 0 deletions scripts/migrate-user-mapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const AWS = require('aws-sdk');
const helper = require('../src/common/helper');
const dbHelper = require('../src/common/db-helper');
const GithubUserMapping = require('../src/models').GithubUserMapping;
const GitlabUserMapping = require('../src/models').GitlabUserMapping;

console.log(process.env.IS_LOCAL);
if (process.env.IS_LOCAL) {
AWS.config.update({
endpoint: 'http://localhost:8000'
});
}
var documentClient = new AWS.DynamoDB.DocumentClient();

(async () => {
console.log('Migrating...');
const params = {
TableName: 'Topcoder_X.UserMapping'
};

let items;
do {
items = await documentClient.scan(params).promise();
items.Items.forEach(async (item) => {
console.log(item);
if (item.githubUserId && item.githubUsername) {
await dbHelper.create(GithubUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername: item.topcoderUsername,
githubUserId: item.githubUserId,
githubUsername: item.githubUsername,
});
}
if (item.gitlabUsername && item.gitlabUserId) {
await dbHelper.create(GitlabUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername: item.topcoderUsername,
gitlabUsername: item.gitlabUsername,
gitlabUserId: item.gitlabUserId,
});
}
});
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while(typeof items.LastEvaluatedKey !== 'undefined');
})();
2 changes: 1 addition & 1 deletion src/common/db-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async function queryOneUserMappingByTCUsername(model, tcusername) {
model.queryOne('topcoderUsername').eq(tcusername)
.all()
.exec((err, result) => {
if (err || !result) {
if (err) {
logger.debug(`queryOneUserMappingByTCUsername. Error. ${err}`);
return reject(err);
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ async function getProviderType(repoUrl) {
* @returns {Object} the owner/copilot for the project
*/
async function getProjectCopilotOrOwner(models, project, provider, isCopilot) {
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(models.UserMapping,
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(
provider === 'github' ? models.GithubUserMapping : models.GitlabUserMapping,
isCopilot ? project.copilot : project.owner);

if (!userMapping ||
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/GithubController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const GithubService = require('../services/GithubService');
const UserService = require('../services/UserService');
const OwnerUserTeam = require('../models').OwnerUserTeam;
const UserTeamMapping = require('../models').UserTeamMapping;
const UserMapping = require('../models').UserMapping;
const GithubUserMapping = require('../models').GithubUserMapping;
const constants = require('../common/constants');

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

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

if (mapping) {
await dbHelper.update(UserMapping, mapping.id, {
await dbHelper.update(GithubUserMapping, mapping.id, {
githubUsername: githubUser.username,
githubUserId: githubUser.id,
});
} else {
await dbHelper.create(UserMapping, {
console.log('User mapping not found. Create new mapping.'); /* eslint-disable-line no-console */
await dbHelper.create(GithubUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername: req.session.tcUsername,
githubUsername: githubUser.username,
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/GitlabController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const GitlabService = require('../services/GitlabService');
const UserService = require('../services/UserService');
const User = require('../models').User;
const OwnerUserGroup = require('../models').OwnerUserGroup;
const UserMapping = require('../models').UserMapping;
const GitlabUserMapping = require('../models').GitlabUserMapping;
const UserGroupMapping = require('../models').UserGroupMapping;

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

const mapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, req.session.tcUsername);
const mapping = await dbHelper.queryOneUserMappingByTCUsername(GitlabUserMapping, req.session.tcUsername);
if (mapping) {
await dbHelper.update(UserMapping, mapping.id, {
await dbHelper.update(GitlabUserMapping, mapping.id, {
gitlabUsername: gitlabUser.username,
gitlabUserId: gitlabUser.id,
});
} else {
await dbHelper.create(UserMapping, {
await dbHelper.create(GitlabUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername: req.session.tcUsername,
gitlabUsername: gitlabUser.username,
Expand Down
46 changes: 46 additions & 0 deletions src/models/GithubUserMapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* This defines github user mapping model.
*/
'use strict';

const dynamoose = require('dynamoose');

const Schema = dynamoose.Schema;

const schema = new Schema({
id: {
type: String,
required: true,
hashKey: true
},
topcoderUsername: {
type: String,
required: true,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'TopcoderUsernameIndex'
}
},
githubUsername: {
type: String,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'GithubUsernameIndex'
}
},
githubUserId: {
type: Number,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'GithubUserIdIndex'
}
}
});

module.exports = schema;
46 changes: 46 additions & 0 deletions src/models/GitlabUserMapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* This defines gitlab user mapping model.
*/
'use strict';

const dynamoose = require('dynamoose');

const Schema = dynamoose.Schema;

const schema = new Schema({
id: {
type: String,
required: true,
hashKey: true
},
topcoderUsername: {
type: String,
required: true,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'TopcoderUsernameIndex'
}
},
gitlabUsername: {
type: String,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'GitlabUsernameIndex'
}
},
gitlabUserId: {
type: Number,
index: {
global: true,
project: true,
rangKey: 'id',
name: 'GitlabUserIdIndex'
}
}
});

module.exports = schema;
30 changes: 0 additions & 30 deletions src/models/UserMapping.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/services/GithubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const constants = require('../common/constants');
const helper = require('../common/helper');
const dbHelper = require('../common/db-helper');
const User = require('../models').User;
const UserMapping = require('../models').UserMapping;
const GithubUserMapping = require('../models').GithubUserMapping;
const OwnerUserTeam = require('../models').OwnerUserTeam;
const errors = require('../common/errors');

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

const userMapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, topcoderUsername);
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(GithubUserMapping, topcoderUsername);
if (!userMapping) {
await dbHelper.create(UserMapping, {
await dbHelper.create(GithubUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername,
githubUserId: userProfile.id,
githubUsername: userProfile.login,
});
} else {
await dbHelper.update(UserMapping, userMapping.id, {
await dbHelper.update(GithubUserMapping, userMapping.id, {
githubUserId: userProfile.id,
githubUsername: userProfile.login,
});
Expand Down
8 changes: 4 additions & 4 deletions src/services/GitlabService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const helper = require('../common/helper');
const dbHelper = require('../common/db-helper');
const errors = require('../common/errors');
const User = require('../models').User;
const UserMapping = require('../models').UserMapping;
const GitlabUserMapping = require('../models').GitlabUserMapping;
const OwnerUserGroup = require('../models').OwnerUserGroup;

const request = superagentPromise(superagent, Promise);
Expand Down Expand Up @@ -52,16 +52,16 @@ async function ensureOwnerUser(token, topcoderUsername) {
constants.USER_TYPES.GITLAB,
constants.USER_ROLES.OWNER);

const userMapping = await dbHelper.queryOneUserMappingByTCUsername(UserMapping, topcoderUsername);
const userMapping = await dbHelper.queryOneUserMappingByTCUsername(GitlabUserMapping, topcoderUsername);
if (!userMapping) {
await dbHelper.create(UserMapping, {
await dbHelper.create(GitlabUserMapping, {
id: helper.generateIdentifier(),
topcoderUsername,
gitlabUserId: userProfile.id,
gitlabUsername: userProfile.username,
});
} else {
await dbHelper.update(UserMapping, userMapping.id, {
await dbHelper.update(GitlabUserMapping, userMapping.id, {
gitlabUserId: userProfile.id,
gitlabUsername: userProfile.username,
});
Expand Down
10 changes: 8 additions & 2 deletions src/services/TCUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const Joi = require('joi');
const decodeToken = require('tc-auth-lib').decodeToken;
const errors = require('../common/errors');
const helper = require('../common/helper');
const UserMapping = require('../models').UserMapping;
const GithubUserMapping = require('../models').GithubUserMapping;
const GitlabUserMapping = require('../models').GitlabUserMapping;

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

return await helper.ensureExists(UserMapping, query, 'UserMapping');
if (query.githubUsername) {
return await helper.ensureExists(GithubUserMapping, query, 'GithubUserMapping');
}
else {
return await helper.ensureExists(GitlabUserMapping, query, 'GitlabUserMapping');
}
}

getUserMapping.schema = Joi.object().keys({
Expand Down
Loading