diff --git a/services/AzureService.js b/services/AzureService.js index 878c894..e79a530 100644 --- a/services/AzureService.js +++ b/services/AzureService.js @@ -183,8 +183,10 @@ getUsernameById.schema = { * @param {Number} issueId the issue number * @param {Number} challengeId the challenge id * @param {Array} existLabels the issue labels + * @param {String} winner the winner topcoder handle + * @param {Boolean} createCopilotPayments the option to create copilot payments or not */ -async function markIssueAsPaid(copilot, repoFullName, issueId, challengeId, existLabels) { +async function markIssueAsPaid(copilot, repoFullName, issueId, challengeId, existLabels, winner, createCopilotPayments) { // eslint-disable-line max-params Joi.attempt({copilot, repoFullName, issueId, challengeId}, markIssueAsPaid.schema); const labels = _(existLabels).filter((i) => i !== config.FIX_ACCEPTED_ISSUE_LABEL) .push(config.FIX_ACCEPTED_ISSUE_LABEL, config.PAID_ISSUE_LABEL).value(); @@ -199,7 +201,16 @@ async function markIssueAsPaid(copilot, repoFullName, issueId, challengeId, exis .set('Authorization', `Bearer ${copilot.accessToken}`) .set('Content-Type', 'application/json-patch+json') .end(); - const body = helper.prepareAutomatedComment(`Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`, copilot); + + let commentMessage = '```\n'; + commentMessage += '*Payments Complete*\n'; + commentMessage += `Winner: ${winner}\n`; + if (createCopilotPayments) { + commentMessage += `Copilot: ${copilot.topcoderUsername}\n`; + } + commentMessage += '```\n'; + commentMessage += `Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`; + const body = helper.prepareAutomatedComment(commentMessage, copilot); await request .post(`${config.AZURE_DEVOPS_API_BASE_URL}/${repoFullName}/_apis/wit/workItems/${issueId}/comments?api-version=5.1-preview.3`) .send({ diff --git a/services/GithubService.js b/services/GithubService.js index 6023d4f..4837799 100644 --- a/services/GithubService.js +++ b/services/GithubService.js @@ -236,17 +236,28 @@ getUserIdByLogin.schema = { * @param {Number} number the issue number * @param {Number} challengeId the challenge id * @param {Array} existLabels the issue labels + * @param {String} winner the winner topcoder handle + * @param {Boolean} createCopilotPayments the option to create copilot payments or not * */ -async function markIssueAsPaid(copilot, repoFullName, number, challengeId, existLabels) { - Joi.attempt({copilot, repoFullName, number, challengeId, existLabels}, markIssueAsPaid.schema); +async function markIssueAsPaid(copilot, repoFullName, number, challengeId, existLabels, winner, createCopilotPayments) { // eslint-disable-line max-params + Joi.attempt({copilot, repoFullName, number, challengeId, existLabels, winner, createCopilotPayments}, markIssueAsPaid.schema); const github = await _authenticate(copilot.accessToken); const {owner, repo} = _parseRepoUrl(repoFullName); const labels = _(existLabels).filter((i) => i !== config.FIX_ACCEPTED_ISSUE_LABEL) .push(config.FIX_ACCEPTED_ISSUE_LABEL, config.PAID_ISSUE_LABEL).value(); try { await github.issues.edit({owner, repo, number, labels}); - const body = helper.prepareAutomatedComment(`Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`, copilot); + let commentMessage = '```\n'; + commentMessage += '*Payments Complete*\n'; + commentMessage += `Winner: ${winner}\n`; + if (createCopilotPayments) { + commentMessage += `Copilot: ${copilot.topcoderUsername}\n`; + } + commentMessage += '```\n'; + commentMessage += `Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`; + + const body = helper.prepareAutomatedComment(commentMessage, copilot); await github.issues.createComment({owner, repo, number, body}); } catch (err) { throw errors.convertGitHubError(err, 'Error occurred during updating issue as paid.'); @@ -259,7 +270,9 @@ markIssueAsPaid.schema = { repoFullName: Joi.string().required(), number: Joi.number().required(), challengeId: Joi.number().positive().required(), - existLabels: Joi.array().items(Joi.string()).required() + existLabels: Joi.array().items(Joi.string()).required(), + winner: Joi.string().required(), + createCopilotPayments: Joi.boolean().default(false).optional() }; /** diff --git a/services/GitlabService.js b/services/GitlabService.js index 249dc9a..d272465 100644 --- a/services/GitlabService.js +++ b/services/GitlabService.js @@ -199,15 +199,26 @@ getUserIdByLogin.schema = { * @param {Number} issueId the issue number * @param {Number} challengeId the challenge id * @param {Array} existLabels the issue labels + * @param {String} winner the winner topcoder handle + * @param {Boolean} createCopilotPayments the option to create copilot payments or not */ -async function markIssueAsPaid(copilot, projectId, issueId, challengeId, existLabels) { +async function markIssueAsPaid(copilot, projectId, issueId, challengeId, existLabels, winner, createCopilotPayments) { // eslint-disable-line max-params Joi.attempt({copilot, projectId, issueId, challengeId}, markIssueAsPaid.schema); const gitlab = await _authenticate(copilot.accessToken); const labels = _(existLabels).filter((i) => i !== config.FIX_ACCEPTED_ISSUE_LABEL) .push(config.FIX_ACCEPTED_ISSUE_LABEL, config.PAID_ISSUE_LABEL).value(); try { await gitlab.projects.issues.edit(projectId, issueId, {labels: labels.join(',')}); - const body = helper.prepareAutomatedComment(`Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`, copilot); + let commentMessage = '```\n'; + commentMessage += '*Payments Complete*\n'; + commentMessage += `Winner: ${winner}\n`; + if (createCopilotPayments) { + commentMessage += `Copilot: ${copilot.topcoderUsername}\n`; + } + commentMessage += '```\n'; + commentMessage += `Payment task has been updated: ${config.TC_OR_DETAIL_LINK}${challengeId}`; + + const body = helper.prepareAutomatedComment(commentMessage, copilot); await gitlab.projects.issues.notes.create(projectId, issueId, {body}); } catch (err) { throw errors.convertGitLabError(err, 'Error occurred during updating issue as paid.'); diff --git a/services/IssueService.js b/services/IssueService.js index 0664cf3..5e424e9 100755 --- a/services/IssueService.js +++ b/services/IssueService.js @@ -465,8 +465,11 @@ async function handleIssueClose(event, issue) { // eslint-disable-line await topcoderApiHelper.updateChallenge(dbIssue.challengeId, updateBody); const copilotAlreadySet = await topcoderApiHelper.roleAlreadySet(dbIssue.challengeId, 'Copilot'); + const createCopilotPayments = project.createCopilotPayments === 'true' && + event.copilot.topcoderUsername.toLowerCase() !== assigneeMember.topcoderUsername.toLowerCase(); + event.createCopilotPayments = createCopilotPayments; - if (!copilotAlreadySet && project.createCopilotPayments === 'true') { + if (!copilotAlreadySet && createCopilotPayments) { logger.debugWithContext(`Getting the topcoder member ID for copilot name : ${event.copilot.topcoderUsername}`, event, issue); // get copilot tc user id const copilotTopcoderUserId = await topcoderApiHelper.getTopcoderMemberId(event.copilot.topcoderUsername); @@ -530,7 +533,13 @@ async function handleIssueClose(event, issue) { // eslint-disable-line status: 'challenge_payment_successful', updatedAt: new Date() }); - await gitHelper.markIssueAsPaid(event, issue.number, dbIssue.challengeId, labels); + await gitHelper.markIssueAsPaid( + event, + issue.number, + dbIssue.challengeId, + labels, + event.assigneeMember.topcoderUsername, + event.createCopilotPayments); } catch (e) { await eventService.handleEventGracefully(event, issue, e); return; @@ -922,6 +931,7 @@ process.schema = Joi.object().keys({ }).required(), retryCount: Joi.number().integer().default(0).optional(), paymentSuccessful: Joi.boolean().default(false).optional(), + createCopilotPayments: Joi.boolean().default(false).optional(), challengeValid: Joi.boolean().default(false).optional(), dbIssue: Joi.object().optional(), assigneeMember: Joi.object().optional() diff --git a/utils/git-helper.js b/utils/git-helper.js index 686dae2..c9fab52 100644 --- a/utils/git-helper.js +++ b/utils/git-helper.js @@ -137,14 +137,37 @@ class GitHelper { * @param {Number} issueNumber the issue Number * @param {Number} challengeId the challenge id * @param {Array} existLabels the exist labels of the issue + * @param {String} winner the winner topcoder handle + * @param {Boolean} createCopilotPayments the option to create copilot payments or not */ - async markIssueAsPaid(event, issueNumber, challengeId, existLabels) { + async markIssueAsPaid(event, issueNumber, challengeId, existLabels, winner, createCopilotPayments = false) { // eslint-disable-line max-params if (event.provider === 'github') { - await gitHubService.markIssueAsPaid(event.copilot, event.data.repository.full_name, issueNumber, challengeId, existLabels); + await gitHubService.markIssueAsPaid( + event.copilot, + event.data.repository.full_name, + issueNumber, + challengeId, + existLabels, + winner, + createCopilotPayments); } else if (event.provider === 'gitlab') { - await gitlabService.markIssueAsPaid(event.copilot, event.data.repository.id, issueNumber, challengeId, existLabels); + await gitlabService.markIssueAsPaid( + event.copilot, + event.data.repository.id, + issueNumber, + challengeId, + existLabels, + winner, + createCopilotPayments); } else if (event.provider === 'azure') { - await azureService.markIssueAsPaid(event.copilot, event.data.repository.full_name, issueNumber, challengeId, existLabels); + await azureService.markIssueAsPaid( + event.copilot, + event.data.repository.full_name, + issueNumber, + challengeId, + existLabels, + winner, + createCopilotPayments); } }