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

Commit 1d59922

Browse files
committed
August 2019 release challenge
1 parent e76c8b3 commit 1d59922

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

services/IssueService.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ function parseComment(comment) {
174174
* handles the issue assignment
175175
* @param {Object} event the event
176176
* @param {Object} issue the issue
177+
* @param {Boolean} force force to assign (if there is no OpenForPickup label)
177178
* @private
178179
*/
179-
async function handleIssueAssignment(event, issue) {
180+
async function handleIssueAssignment(event, issue, force = false) {
180181
const assigneeUserId = event.data.assignee.id;
181182
logger.debug(`Looking up TC handle of git user: ${assigneeUserId}`);
182183
const userMapping = await userService.getTCUserName(event.provider, assigneeUserId);
@@ -201,7 +202,7 @@ async function handleIssueAssignment(event, issue) {
201202
// ensure issue has open for pickup label
202203
const hasOpenForPickupLabel = _(issue.labels).includes(config.OPEN_FOR_PICKUP_ISSUE_LABEL); // eslint-disable-line lodash/chaining
203204
const hasNotReadyLabel = _(issue.labels).includes(config.NOT_READY_ISSUE_LABEL); // eslint-disable-line lodash/chaining
204-
if (!hasOpenForPickupLabel) {
205+
if (!hasOpenForPickupLabel && !force) {
205206
if (!issue.assignee) {
206207
const issueLabels = _(issue.labels).push(config.NOT_READY_ISSUE_LABEL).value(); // eslint-disable-line lodash/chaining
207208
const comment = `This ticket isn't quite ready to be worked on yet.Please wait until it has the ${config.OPEN_FOR_PICKUP_ISSUE_LABEL} label`;
@@ -464,7 +465,7 @@ async function handleIssueClose(event, issue) {
464465
labels,
465466
updatedAt: new Date()
466467
});
467-
await gitHelper.markIssueAsPaid(event, issue.number, dbIssue.challengeId);
468+
await gitHelper.markIssueAsPaid(event, issue.number, dbIssue.challengeId, labels);
468469
} catch (e) {
469470
await eventService.handleEventGracefully(event, issue, e);
470471
return;
@@ -500,7 +501,7 @@ async function handleIssueCreate(event, issue) {
500501
}
501502

502503
// create issue with challenge creation pending
503-
const issueObject = _.assign({}, issue, {
504+
const issueObject = _.assign({}, _.omit(issue, 'assignee'), {
504505
id: helper.generateIdentifier(),
505506
status: 'challenge_creation_pending'
506507
});
@@ -548,14 +549,13 @@ async function handleIssueCreate(event, issue) {
548549
const contestUrl = getUrlForChallengeId(issue.challengeId);
549550
const comment = `Contest ${contestUrl} has been created for this ticket.`;
550551
await gitHelper.createComment(event, issue.number, comment);
551-
if (event.provider === 'gitlab') {
552-
// if assignee is added during issue create then assign as well
553-
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
554-
event.data.assignee = {
555-
id: event.data.issue.assignees[0].id
556-
};
557-
await handleIssueAssignment(event, issue);
558-
}
552+
553+
// if assignee is added during issue create then assign as well
554+
if (event.data.issue.assignees && event.data.issue.assignees.length > 0 && event.data.issue.assignees[0].id) {
555+
event.data.assignee = {
556+
id: event.data.issue.assignees[0].id
557+
};
558+
await handleIssueAssignment(event, issue, true);
559559
}
560560

561561
logger.debug(`new challenge created with id ${issue.challengeId} for issue ${issue.number}`);
@@ -660,6 +660,50 @@ async function handleIssueUnAssignment(event, issue) {
660660
}
661661
}
662662

663+
/**
664+
* handles the issue recreate event
665+
* @param {Object} event the event
666+
* @param {Object} issue the issue
667+
* @private
668+
*/
669+
async function handleIssueRecreate(event, issue) {
670+
const dbIssue = await dbHelper.scanOne(models.Issue, {
671+
number: issue.number,
672+
provider: issue.provider,
673+
repositoryId: issue.repositoryId
674+
});
675+
try {
676+
const project = await getProjectDetail(event);
677+
678+
logger.debug(`Getting the billing account ID for project ID: ${project.tcDirectId}`);
679+
const accountId = await topcoderApiHelper.getProjectBillingAccountId(project.tcDirectId);
680+
681+
logger.debug(`Assigning the billing account id ${accountId} to challenge`);
682+
683+
// adding assignees as well if it is missed/failed during update
684+
// prize needs to be again set after adding billing account otherwise it won't let activate
685+
const updateBody = {
686+
billingAccountId: accountId,
687+
prizes: dbIssue.prizes
688+
};
689+
await topcoderApiHelper.updateChallenge(dbIssue.challengeId, updateBody);
690+
691+
// Activate the challenge
692+
await topcoderApiHelper.activateChallenge(dbIssue.challengeId);
693+
694+
// Cancel the challenge
695+
await topcoderApiHelper.cancelPrivateContent(dbIssue.challengeId);
696+
697+
await dbIssue.delete();
698+
} catch (err) {
699+
// Just log the error, keep the process go on.
700+
logger.error(`Error cleaning the old DB and its challenge.\n ${err}`);
701+
}
702+
703+
await handleIssueCreate(event, issue);
704+
// handleIssueLabelUpdated(event, issue);
705+
}
706+
663707
/**
664708
* Process issue event.
665709
* @param {Object} event the event
@@ -710,12 +754,14 @@ async function process(event) {
710754
await handleIssueLabelUpdated(event, issue);
711755
} else if (event.event === 'issue.unassigned') {
712756
await handleIssueUnAssignment(event, issue);
757+
} else if (event.event === 'issue.recreated') {
758+
await handleIssueRecreate(event, issue);
713759
}
714760
}
715761

716762
process.schema = Joi.object().keys({
717763
event: Joi.string().valid('issue.created', 'issue.updated', 'issue.closed', 'comment.created', 'comment.updated', 'issue.assigned',
718-
'issue.labelUpdated', 'issue.unassigned').required(),
764+
'issue.labelUpdated', 'issue.unassigned', 'issue.recreated').required(),
719765
provider: Joi.string().valid('github', 'gitlab').required(),
720766
data: Joi.object().keys({
721767
issue: Joi.object().keys({

utils/git-helper.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,13 @@ class GitHelper {
119119
* @param {Object} event the event
120120
* @param {Number} issueNumber the issue Number
121121
* @param {Number} challengeId the challenge id
122+
* @param {Array} existLabels the exist labels of the issue
122123
*/
123-
async markIssueAsPaid(event, issueNumber, challengeId) {
124+
async markIssueAsPaid(event, issueNumber, challengeId, existLabels) {
124125
if (event.provider === 'github') {
125-
await gitHubService.markIssueAsPaid(event.copilot, event.data.repository.full_name, issueNumber, challengeId);
126+
await gitHubService.markIssueAsPaid(event.copilot, event.data.repository.full_name, issueNumber, challengeId, existLabels);
126127
} else {
127-
await gitlabService.markIssueAsPaid(event.copilot, event.data.repository.id, issueNumber, challengeId);
128+
await gitlabService.markIssueAsPaid(event.copilot, event.data.repository.id, issueNumber, challengeId, existLabels);
128129
}
129130
}
130131

utils/kafka.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Kafka {
4747
return;
4848
}
4949

50-
if (event && _.includes(['issue.created', 'issue.updated', 'issue.closed',
50+
if (event && _.includes(['issue.created', 'issue.updated', 'issue.closed', 'issue.recreated',
5151
'comment.created', 'comment.updated', 'issue.assigned', 'issue.labelUpdated', 'issue.unassigned']
5252
, event.event)) {
5353
IssueService

utils/topcoder-api-helper.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ async function createChallenge(challenge) {
122122
Status Code:${statusCode}, Response: ${circularJSON.stringify(challengeResponse.result)}`);
123123
return _.get(challengeResponse, 'result.content.id');
124124
} catch (err) {
125+
console.log(err);
126+
125127
loggerFile.info(`EndPoint: POST /challenges, POST parameters: ${circularJSON.stringify(challengeBody)}, Status Code:null,
126128
Error: 'Failed to create challenge.', Details: ${circularJSON.stringify(err)}`);
127129
throw errors.convertTopcoderApiError(err, 'Failed to create challenge.');
@@ -203,7 +205,7 @@ async function getChallengeById(id) {
203205
const apiKey = await getM2Mtoken();
204206
logger.debug('Getting topcoder challenge details');
205207
try {
206-
const response = await axios.get(`${projectsClient.basePath}/challenges/${id}`, {
208+
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}`, {
207209
headers: {
208210
authorization: `Bearer ${apiKey}`
209211
},
@@ -227,7 +229,7 @@ async function closeChallenge(id, winnerId) {
227229
const apiKey = await getM2Mtoken();
228230
logger.debug(`Closing challenge ${id}`);
229231
try {
230-
const response = await axios.post(`${projectsClient.basePath}/challenges/${id}/close?winnerId=${winnerId}`, null, {
232+
const response = await axios.post(`${challengesClient.basePath}/challenges/${id}/close?winnerId=${winnerId}`, null, {
231233
headers: {
232234
authorization: `Bearer ${apiKey}`,
233235
'Content-Type': 'application/json'
@@ -344,7 +346,7 @@ async function getResourcesFromChallenge(id) {
344346
const apiKey = await getM2Mtoken();
345347
logger.debug(`fetch resource from challenge ${id}`);
346348
try {
347-
const response = await axios.get(`${projectsClient.basePath}/challenges/${id}/resources`, {
349+
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}/resources`, {
348350
headers: {
349351
authorization: `Bearer ${apiKey}`
350352
},
@@ -504,7 +506,7 @@ async function getChallengeResources(id) {
504506
const apiKey = await getM2Mtoken();
505507
logger.debug(`getting resource from challenge ${id}`);
506508
try {
507-
const response = await axios.get(`${projectsClient.basePath}/challenges/${id}/resources`, {
509+
const response = await axios.get(`${challengesClient.basePath}/challenges/${id}/resources`, {
508510
headers: {Authorization: `bearer ${apiKey}`}
509511
});
510512
const statusCode = response.data ? response.data.result.status : null;

0 commit comments

Comments
 (0)