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

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module.exports = {
6969
NEW_CHALLENGE_DURATION_IN_DAYS: process.env.NEW_CHALLENGE_DURATION_IN_DAYS || 5,
7070
TC_URL: process.env.TC_URL || 'https://www.topcoder-dev.com',
7171
GITLAB_API_BASE_URL: process.env.GITLAB_API_BASE_URL || 'https://gitlab.com',
72+
ISSUE_LABEL_PREFIX: process.env.ISSUE_LABEL_PREFIX || 'tcx_',
7273
PAID_ISSUE_LABEL: process.env.PAID_ISSUE_LABEL || 'tcx_Paid',
7374
FIX_ACCEPTED_ISSUE_LABEL: process.env.FIX_ACCEPTED_ISSUE_LABEL || 'tcx_FixAccepted',
7475
READY_FOR_REVIEW_ISSUE_LABEL: process.env.READY_FOR_REVIEW_ISSUE_LABEL || 'tcx_ReadyForReview',

services/CopilotPaymentService.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ async function _updateChallenge(copilotUsername, projectId, challengeId) {
119119
await topcoderApiHelper.updateChallenge(challengeId, changedPayment);
120120
logger.debug(`challenge with id:${challengeId} was updated successfully.`);
121121
} else { // if there is no payment due to delete cancel the challenge as well
122-
topcoderApiHelper.cancelPrivateContent(challengeId);
122+
logger.debug(`challenge with id:${challengeId} is cancelled.`);
123+
// Currently, there is no working API for closing challenge.
124+
// The process is just ignored.
123125
}
124126
}
125127

services/IssueService.js

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async function ensureChallengeExists(event, issue, create = true) {
8585
if (!dbIssue && create) {
8686
logger.debug('dbIssue is NULL, process to create new record and challenge');
8787

88-
await handleIssueCreate(event, issue);
88+
await handleIssueCreate(event, issue, true);
8989
dbIssue = await dbHelper.queryOneIssue(models.Issue, issue.repositoryId, issue.number, issue.provider);
9090
logger.debug(`dbIssue is CREATED ${dbIssue ? 'Succesfully' : 'Failed'}`);
9191
}
@@ -188,7 +188,9 @@ async function handleIssueAssignment(event, issue, force = false) {
188188
const err = errors.internalDependencyError('Can\'t find the issue in DB. It\'s not found or not accessible');
189189
// The dbissue is not found, the db is not accessible, or the issue is still in creation process.
190190
// Handle it for rescheduling.
191-
await eventService.handleEventGracefully(event, issue, err);
191+
if (issue.tcxReady) {
192+
await eventService.handleEventGracefully(event, issue, err);
193+
}
192194
return;
193195
}
194196

@@ -304,7 +306,9 @@ async function handleIssueUpdate(event, issue) {
304306
const err = errors.internalDependencyError('Can\'t find the issue in DB. It\'s not found or not accessible');
305307
// The dbissue is not found, the db is not accessible, or the issue is still in creation process.
306308
// Handle it for rescheduling.
307-
await eventService.handleEventGracefully(event, issue, err);
309+
if (issue.tcxReady) {
310+
await eventService.handleEventGracefully(event, issue, err);
311+
}
308312
return;
309313
}
310314

@@ -347,7 +351,7 @@ async function handleIssueUpdate(event, issue) {
347351
* @param {Object} issue the issue
348352
* @private
349353
*/
350-
async function handleIssueClose(event, issue) {
354+
async function handleIssueClose(event, issue) { // eslint-disable-line
351355
let dbIssue;
352356
try {
353357
dbIssue = await ensureChallengeExists(event, issue);
@@ -356,7 +360,9 @@ async function handleIssueClose(event, issue) {
356360
const err = errors.internalDependencyError('Can\'t find the issue in DB. It\'s not found or not accessible');
357361
// The dbissue is not found, the db is not accessible, or the issue is still in creation process.
358362
// Handle it for rescheduling.
359-
await eventService.handleEventGracefully(event, issue, err);
363+
if (issue.tcxReady) {
364+
await eventService.handleEventGracefully(event, issue, err);
365+
}
360366
return;
361367
}
362368

@@ -389,10 +395,8 @@ async function handleIssueClose(event, issue) {
389395

390396
if (closeChallenge) {
391397
logger.debug(`The associated challenge ${dbIssue.challengeId} is being scheduled for cancellation since no payment will be given`);
392-
setTimeout(async () => {
393-
await topcoderApiHelper.cancelPrivateContent(dbIssue.challengeId);
394-
logger.debug(`The challenge ${dbIssue.challengeId} is deleted`);
395-
}, config.CANCEL_CHALLENGE_INTERVAL); //eslint-disable-line
398+
// Currently, there is no working API for closing challenge.
399+
// The process is just ignored.
396400
return;
397401
}
398402

@@ -526,10 +530,10 @@ async function handleIssueClose(event, issue) {
526530
* handles the issue create event
527531
* @param {Object} event the event
528532
* @param {Object} issue the issue
529-
* @param {Boolean} recreate indicate that the process is to recreate an issue
533+
* @param {Boolean} forceAssign force the creation process to assign user
530534
* @private
531535
*/
532-
async function handleIssueCreate(event, issue, recreate = false) {
536+
async function handleIssueCreate(event, issue, forceAssign = false) {
533537
// check if project for such repository is already created
534538
const project = await getProjectDetail(event);
535539

@@ -546,6 +550,11 @@ async function handleIssueCreate(event, issue, recreate = false) {
546550
`Issue ${issue.number} is already in ${dbIssue.status}`);
547551
}
548552

553+
if (!issue.tcxReady) {
554+
logger.debug('The issue doesn\'t have tcx_ labels. Creation ignored.');
555+
return;
556+
}
557+
549558
// create issue with challenge creation pending
550559
const issueObject = _.assign({}, _.omit(issue, 'assignee'), {
551560
id: helper.generateIdentifier(),
@@ -592,7 +601,7 @@ async function handleIssueCreate(event, issue, recreate = false) {
592601
const comment = `Contest ${contestUrl} has been created for this ticket.`;
593602
await gitHelper.createComment(event, issue.number, comment);
594603

595-
if (event.provider === 'gitlab' || recreate) {
604+
if (event.provider === 'gitlab' || forceAssign) {
596605
// if assignee is added during issue create then assign as well
597606
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
598607
event.data.assignee = {
@@ -613,7 +622,7 @@ async function handleIssueCreate(event, issue, recreate = false) {
613622
async function handleIssueLabelUpdated(event, issue) {
614623
let dbIssue;
615624
try {
616-
dbIssue = await ensureChallengeExists(event, issue, false);
625+
dbIssue = await ensureChallengeExists(event, issue, true);
617626
} catch (e) {
618627
await eventService.handleEventGracefully(event, issue, e);
619628
return;
@@ -639,13 +648,11 @@ async function handleIssueLabelUpdated(event, issue) {
639648
async function handleIssueUnAssignment(event, issue) {
640649
let dbIssue;
641650
try {
642-
dbIssue = await ensureChallengeExists(event, issue);
651+
dbIssue = await ensureChallengeExists(event, issue, false);
643652

644653
if (!dbIssue) {
645-
const err = errors.internalDependencyError('Can\'t find the issue in DB. It\'s not found or not accessible');
646654
// The dbissue is not found, the db is not accessible, or the issue is still in creation process.
647-
// Handle it for rescheduling.
648-
await eventService.handleEventGracefully(event, issue, err);
655+
// Ignore it.
649656
return;
650657
}
651658

@@ -727,15 +734,44 @@ async function handleIssueUnAssignment(event, issue) {
727734
async function handleIssueRecreate(event, issue) {
728735
const dbIssue = await dbHelper.queryOneIssue(models.Issue, issue.repositoryId, issue.number, issue.provider);
729736

737+
// remove open for pickup and add assigned
738+
const updateLabels = _(issue.labels) // eslint-disable-line lodash/chaining
739+
.filter((i) => !i.startsWith(config.ISSUE_LABEL_PREFIX))
740+
.value();
741+
742+
await gitHelper.addLabels(event, issue.number, updateLabels);
743+
744+
// Unassign the user.
745+
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
746+
event.data.assignee = {
747+
id: event.data.issue.assignees[0].id
748+
};
749+
const assigneeUserId = event.data.assignee.id;
750+
const assigneeUsername = await gitHelper.getUsernameById(event, assigneeUserId);
751+
await gitHelper.removeAssign(event, issue.number, assigneeUserId, assigneeUsername);
752+
}
753+
730754
try {
731755
await dbIssue.delete();
732756
} catch (err) {
733757
// Just log the error, keep the process go on.
734758
logger.error(`Error cleaning the old DB and its challenge.\n ${err}`);
735759
}
736760

737-
await handleIssueCreate(event, issue, true);
738-
// handleIssueLabelUpdated(event, issue);
761+
const issueLabels = _(updateLabels).push(config.OPEN_FOR_PICKUP_ISSUE_LABEL).value(); // eslint-disable-line lodash/chaining
762+
logger.debug(`Adding label ${config.OPEN_FOR_PICKUP_ISSUE_LABEL}`);
763+
await gitHelper.addLabels(event, issue.number, issueLabels);
764+
765+
await handleIssueCreate(event, issue, false);
766+
767+
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
768+
event.data.assignee = {
769+
id: event.data.issue.assignees[0].id
770+
};
771+
const assigneeUserId = event.data.assignee.id;
772+
const assigneeUsername = await gitHelper.getUsernameById(event, assigneeUserId);
773+
await gitHelper.assignUser(event, issue.number, assigneeUsername);
774+
}
739775
}
740776

741777
/**
@@ -751,7 +787,8 @@ async function process(event) {
751787
body: event.data.issue.body,
752788
provider: event.provider,
753789
repositoryId: event.data.repository.id,
754-
labels: event.data.issue.labels
790+
labels: event.data.issue.labels,
791+
tcxReady: true
755792
};
756793
const fullRepoUrl = gitHelper.getFullRepoUrl(event);
757794
const project = await dbHelper.scanOne(models.Project, {
@@ -766,6 +803,15 @@ async function process(event) {
766803
if (!hasPrizes) {
767804
return;
768805
}
806+
807+
const tcxLabels = _(issue.labels) // eslint-disable-line lodash/chaining
808+
.filter((i) => i.startsWith(config.ISSUE_LABEL_PREFIX))
809+
.value();
810+
811+
if (!tcxLabels || tcxLabels.length === 0) {
812+
issue.tcxReady = false;
813+
}
814+
769815
const copilot = await userService.getRepositoryCopilotOrOwner(event.provider, event.data.repository.full_name);
770816
event.copilot = copilot;
771817

0 commit comments

Comments
 (0)