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

Commit e82f6c6

Browse files
authored
Merge pull request #449 from 52cs/issue-448
issues/448
2 parents cef4344 + 446b2c3 commit e82f6c6

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed

src/common/db-helper.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,43 @@ async function queryOneIssue(model, repositoryId, number, provider) {
179179
});
180180
}
181181

182+
/**
183+
* Get Issue's id and challengeUUID by repoUrl
184+
* @param {String} repoUrl The repo url
185+
* @returns {Promise<Object>}
186+
*/
187+
async function queryIssueIdChallengeUUIDByRepoUrl(repoUrl) {
188+
return await new Promise((resolve, reject) => {
189+
models.Issue.scan('repoUrl').eq(repoUrl)
190+
.attributes(['id', 'challengeUUID'])
191+
.exec((err, result) => {
192+
if (err) {
193+
return reject(err);
194+
}
195+
return resolve(result);
196+
});
197+
});
198+
}
199+
200+
201+
/**
202+
* Get CopilotPayment's id by challengeUUID
203+
* @param {String} challengeUUID The challengeUUID
204+
* @returns {Promise<String>}
205+
*/
206+
async function queryPaymentIdByChallengeUUID(challengeUUID) {
207+
return await new Promise((resolve, reject) => {
208+
models.CopilotPayment.scan('challengeUUID').eq(challengeUUID)
209+
.attributes(['id'])
210+
.exec((err, result) => {
211+
if (err) {
212+
return reject(err);
213+
}
214+
return resolve(result.id);
215+
});
216+
});
217+
}
218+
182219
/**
183220
* Get single data by query parameters
184221
* @param {Object} model The dynamoose model to query
@@ -257,7 +294,7 @@ async function queryOneUserMappingByTCUsername(model, tcusername) {
257294
async function queryOneActiveProject(model, repoUrl) {
258295
return await new Promise((resolve, reject) => {
259296
queryOneActiveRepository(models.Repository, repoUrl).then((repo) => {
260-
if (!repo) resolve(null);
297+
if (!repo || repo.length === 0) resolve(null);
261298
else model.queryOne('id').eq(repo.projectId).consistent()
262299
.exec((err, result) => {
263300
if (err) {
@@ -509,8 +546,8 @@ async function queryOneActiveRepository(model, url) {
509546
return await new Promise((resolve, reject) => {
510547
model.queryOne({
511548
url,
512-
archived: 'false'
513549
})
550+
.filter('archived').eq('false')
514551
.all()
515552
.exec((err, result) => {
516553
if (err) {
@@ -531,8 +568,8 @@ async function queryActiveRepositoriesExcludeByProjectId(url, projectId) {
531568
return await new Promise((resolve, reject) => {
532569
models.Repository.query({
533570
url,
534-
archived: 'false'
535571
})
572+
.filter('archived').eq('false')
536573
.filter('projectId')
537574
.not().eq(projectId)
538575
.all()
@@ -609,6 +646,8 @@ async function populateRepoUrls(projectId) {
609646
}
610647

611648
module.exports = {
649+
queryIssueIdChallengeUUIDByRepoUrl,
650+
queryPaymentIdByChallengeUUID,
612651
getById,
613652
getByKey,
614653
scan,

src/services/CopilotPaymentService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function _ensureEditPermissionAndGetInfo(paymentId, topcoderUser) {
8686
if (dbPayment.closed === true) {
8787
throw new Error('Closed payment can not be updated');
8888
}
89-
if (dbProject.archived) {
89+
if (dbProject.archived === 'true') {
9090
throw new errors.ForbiddenError('You can\'t edit this payment in an archived project');
9191
}
9292
return dbPayment;
@@ -206,7 +206,7 @@ async function create(topcoderUser, payment) {
206206
if (dbProject.copilot !== topcoderUser.handle && dbProject.owner !== topcoderUser.handle) {
207207
throw new errors.ForbiddenError('You do not have permission to edit this payment');
208208
}
209-
if (dbProject.archived) {
209+
if (dbProject.archived === 'true') {
210210
throw new errors.ForbiddenError('You can\'t edit this payment in an archived project');
211211
}
212212
payment.username = dbProject.copilot;

src/services/IssueService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async function _ensureEditPermissionAndGetInfo(projectId, currentUser) {
117117
) {
118118
throw new errors.ForbiddenError('You don\'t have access on this project');
119119
}
120-
if (dbProject.archived) {
120+
if (dbProject.archived === 'true') {
121121
throw new errors.ForbiddenError('You can\'t access on this archived project');
122122
}
123123
return dbProject;

src/services/ProjectService.js

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async function _ensureEditPermissionAndGetInfo(projectId, currentUser) {
112112
) {
113113
throw new errors.ForbiddenError('You don\'t have access on this project');
114114
}
115-
if (dbProject.archived) {
115+
if (dbProject.archived === 'true') {
116116
throw new errors.ForbiddenError('You can\'t access on this archived project');
117117
}
118118
return dbProject;
@@ -140,33 +140,21 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
140140
or a time-sequence cornercase encountered here`);
141141
}
142142
try {
143-
let oldIssues = await models.Issue.query({repoUrl: oldRepo.url});
144-
let oldCopilotPaymentPromise = oldIssues.filter(issue => issue.challengeUUID)
145-
.map(issue => models.CopilotPayment.query({challengeUUID: issue.challengeUUID})
146-
.then(payments => {
147-
if (!payments || payments.length === 0) {
148-
/* eslint-disable-next-line no-console */
149-
console.log(`No CopilotPayment correspond to Issue with challengeUUID ${issue.challengeUUID}.
150-
The corresponding CopilotPayment may have been removed.
151-
Or, there is bug in old version.`);
152-
return null;
153-
}
154-
if (payments.length > 1) {
155-
throw new Error(`Duplicate CopilotPayment correspond to one Issue with challengeUUID ${issue.challengeUUID}.
156-
There must be bug in old version`);
157-
}
158-
return payments[0];
159-
}));
160-
let oldCopilotPayment = await Promise.all(oldCopilotPaymentPromise).filter(payment => payment);
161-
162-
await models.Repository.update({id: oldRepo.id}, {projectId: project.id, archived: false});
163-
await oldIssues.forEach(issue => models.Issue.update({id: issue.id}, {projectId: project.id}));
164-
await oldCopilotPayment.forEach(
165-
payment => models.CopilotPayment.update({id: payment.id}, {project: project.id})
143+
const oldIssues = await dbHelper.queryIssueIdChallengeUUIDByRepoUrl(repoUrl);
144+
const issueIds = oldIssues.map(issue => issue.id);
145+
const challengeUUIDs = oldIssues.map(issue => issue.challengeUUID).filter(challengeUUID => challengeUUID);
146+
const paymentIds = await Promise.all(
147+
challengeUUIDs.map(challengeUUID => dbHelper.queryPaymentIdByChallengeUUID(challengeUUID))
148+
);
149+
150+
await dbHelper.update(models.Repository, oldRepo.id, {projectId: project.id, archived: false});
151+
await Promise.all(issueIds.map(issueId => dbHelper.update(models.Issue, issueId, {projectId: project.id})));
152+
await Promise.all(
153+
paymentIds.map(paymentId => dbHelper.update(models.CopilotPayment, paymentId, {project: project.id}))
166154
);
167155
}
168156
catch (err) {
169-
throw new Error(`Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${repoUrl}. Internal Error: ${err.message}`);
157+
throw new Error(`Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${repoUrl}. Internal Error: ${err}`);
170158
}
171159
} else {
172160
try {
@@ -181,7 +169,7 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
181169
await addWikiRules({projectId: project.id}, currentUser, repoUrl);
182170
}
183171
catch (err) {
184-
throw new Error(`Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${repoUrl}. Internal Error: ${err.message}`);
172+
throw new Error(`Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${repoUrl}. Internal Error: ${err}`);
185173
}
186174
}
187175
}
@@ -213,6 +201,8 @@ async function create(project, currentUser) {
213201
project.copilot = project.copilot ? project.copilot.toLowerCase() : null;
214202
project.id = helper.generateIdentifier();
215203

204+
const createdProject = await dbHelper.create(models.Project, project);
205+
216206
// TODO: The following db operation should/could be moved into one transaction
217207
for (const repoUrl of repoUrls) { // eslint-disable-line no-restricted-syntax
218208
try {
@@ -222,7 +212,6 @@ async function create(project, currentUser) {
222212
throw new Error(`Create or migrate repository failed. Repo ${repoUrl}. Internal Error: ${err.message}`);
223213
}
224214
}
225-
const createdProject = await dbHelper.create(models.Project, project);
226215

227216
return createdProject;
228217
}
@@ -267,12 +256,12 @@ async function update(project, currentUser) {
267256
});
268257

269258
// TODO: move the following logic into one dynamoose transaction
270-
const repoUrl2Repo = await dbHelper.queryRepositoriesByProjectId(dbProject.id)
271-
.map(repo => { return {[repo.url]: repo}; });
259+
const repos = await dbHelper.queryRepositoriesByProjectId(dbProject.id);
272260

273261
for (const repoUrl of repoUrls) { // eslint-disable-line no-restricted-syntax
274-
if (repoUrl in repoUrl2Repo) {
275-
await models.Repository.update({id: repoUrl2Repo[repoUrl].id}, {archived: project.archived});
262+
if (repos.find(repo => repo.url === repoUrl)) {
263+
const repoId = repos.find(repo => repo.url === repoUrl).id
264+
await dbHelper.update(models.Repository, repoId, {archived: project.archived});
276265
} else {
277266
try {
278267
await _createOrMigrateRepository(repoUrl, project, currentUser);
@@ -319,7 +308,6 @@ async function getAll(query, currentUser) {
319308
query.lastKey = parseInt(query.lastKey, 10);
320309
}
321310
const slicedProjects = _.slice(projects, query.lastKey, query.lastKey + query.perPage);
322-
// console.log(projects);
323311
for (const project of slicedProjects) { // eslint-disable-line
324312
project.repoUrls = await dbHelper.populateRepoUrls(project.id);
325313
}

0 commit comments

Comments
 (0)