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 all commits
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"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",
"add-organisation": "node scripts/add-organisation.js",
"log-repository-collisions": "node scripts/log-repository-collisions.js"
},
"dependencies": {
Expand Down
33 changes: 33 additions & 0 deletions scripts/add-organisation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const dbHelper = require('../src/common/db-helper');
const helper = require('../src/common/helper');
const Organisation = require('../src/models').Organisation;

const args = process.argv;
if (args.length < 5) {
console.log('Please provide data. Example: npm run add-organisation MyOrganisation ownername PAT-Token');
return;
}
const organisationName = args[2];
const owner = args[3];
const pat = args[4];

(async () => {
const dbOrganisation = await dbHelper.queryOneOrganisation(Organisation, organisationName);
if (dbOrganisation) {
console.log(`Updating Organisation = ${organisationName} Owner = ${owner} PAT = ${pat}.`);
await dbHelper.update(Organisation, dbOrganisation.id, {
name: organisationName,
owner,
personalAccessToken: pat
});
}
else {
console.log(`Adding Organisation = ${organisationName} Owner = ${owner} PAT = ${pat}.`);
await dbHelper.create(Organisation, {
id: helper.generateIdentifier(),
name: organisationName,
owner,
personalAccessToken: pat
});
}
})();
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;

if (process.env.IS_LOCAL=="true") {
console.log("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');
})();
22 changes: 21 additions & 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 Expand Up @@ -367,6 +367,25 @@ async function removeUser(Model, username, type) {
});
}

/**
* Get single data by query parameters
* @param {Object} model The dynamoose model to query
* @param {String} organisation The organisation name
* @returns {Promise<void>}
*/
async function queryOneOrganisation(model, organisation) {
return await new Promise((resolve, reject) => {
model.queryOne('name').eq(organisation)
.all()
.exec((err, result) => {
if (err) {
logger.debug(`queryOneOrganisation. Error. ${err}`);
return reject(err);
}
return resolve(result);
});
});
}

module.exports = {
getById,
Expand All @@ -379,6 +398,7 @@ module.exports = {
queryOneActiveCopilotPayment,
queryOneActiveProject,
queryOneActiveProjectWithFilter,
queryOneOrganisation,
queryOneIssue,
queryOneUserByType,
queryOneUserByTypeAndRole,
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
27 changes: 20 additions & 7 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 @@ -127,6 +127,8 @@ async function addUserToTeam(req, res) {
config.GITHUB_CLIENT_ID
}&redirect_uri=${
encodeURIComponent(callbackUri)
}&scope=${
encodeURIComponent('admin:org')
}&state=${identifier}`);
}

Expand Down Expand Up @@ -156,22 +158,33 @@ async function addUserToTeamCallback(req, res) {
throw new errors.UnauthorizedError('Github authorization failed.', result.body.error_description);
}
const token = result.body.access_token;

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

// Add member to organisation
const addOrganisationResult = await GithubService.addOrganisationMember(organisation, token);
console.log(`Add organisation member, state = ${addOrganisationResult.state}`); /* eslint-disable-line no-console */
if (addOrganisationResult.state === 'pending') {
const acceptInvitation = await GithubService.acceptOrganisationInvitation(organisation, token);
console.log(`Accept organisation invitation by member, state = ${acceptInvitation.state}`); /* eslint-disable-line no-console */
}

// add user to team
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);

// get team details
const teamDetails = await GithubService.getTeamDetails(team.ownerToken, team.teamId);
const mapping = await dbHelper.queryOneUserMappingByTCUsername(GithubUserMapping, req.session.tcUsername);

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;
26 changes: 16 additions & 10 deletions src/models/UserMapping.js → src/models/Organisation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* This defines user mapping model.
* This defines organisation model.
*/
'use strict';

const dynamoose = require('dynamoose');

const Schema = dynamoose.Schema;
Expand All @@ -9,22 +11,26 @@ const schema = new Schema({
id: {
type: String,
required: true,
hashKey: true,
hashKey: true
},
topcoderUsername: {
name: {
type: String,
required: true,
index: {
global: true,
project: true,
rangeKey: 'id',
name: 'TopcoderUsernameIndex',
},
rangKey: 'id',
name: 'NameIndex'
}
},
owner: {
type: String,
required: true
},
githubUsername: String,
gitlabUsername: String,
githubUserId: Number,
gitlabUserId: Number
personalAccessToken: {
type: String,
required: true
}
});

module.exports = schema;
Loading