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

Commit 4cf6b5d

Browse files
committed
Merge branch 'ch_30065364' into 'develop'
changes related to https://www.topcoder.com/challenges/30065364/?type=develop See merge request luettich/processor!10
2 parents ceb07b9 + 9783ed5 commit 4cf6b5d

File tree

9 files changed

+59
-82
lines changed

9 files changed

+59
-82
lines changed

README.md

100644100755
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following config parameters are supported, they are defined in `config/defau
2929
| TOPIC | the kafka subscribe topic name | tc-x-events |
3030
| PARTITION | the kafka partition | 0|
3131
| KAFKA_OPTIONS | the connection option for kafka | see below about KAFKA options |
32-
| MONGODB_URL | the MongoDB URL which must be same as Ragnar tool | mongodb://127.0.0.1:27017/ragnar |
32+
| MONGODB_URL | the MongoDB URL which must be same as Topcoder x tool | mongodb://127.0.0.1:27017/topcoderx |
3333
|TC_DEV_ENV| the flag whether to use topcoder development api or production| false|
3434
| TC_AUTHN_URL | the Topcoder authentication url | https://topcoder-dev.auth0.com/oauth/ro |
3535
| TC_AUTHN_REQUEST_BODY | the Topcoder authentication request body. This makes use of some environment variables: `TC_USERNAME`, `TC_PASSWORD`, `TC_CLIENT_ID`, `CLIENT_V2CONNECTION` | see `default.js` |
@@ -61,20 +61,17 @@ npm start
6161
```
6262

6363
## Setup for verification
64-
Before verifying the tool, 4 service needs be configured and run them
64+
Before verifying the tool, 3 service needs be configured and run them
6565
- processor
6666
- receiver
67-
- Ragnar Tool
68-
- Topcoder X (both backend and UI)
69-
70-
First login in Ragnar tool with admin and Add owner for which requires topcoder handle, git host's username and type of git host.
67+
- Topcoder X
7168

7269
Go to Topcoder X UI login with above used topcoder username and
7370
- go to settings and make sure git hosts are correctly setup, if not click setup and authorize to setup.
7471

75-
- Go to Topcoder X UI and go to project management and add a project from git account and click save, and edit the same project and click 'Add Webhooks' button (you need to add personnel access token), verify that webhooks are set up correctly on git host's project.
72+
- Go to Topcoder X UI and go to project management and add a project from git account and click save, and edit the same project and click 'Add Webhooks' button, verify that webhooks are set up correctly on git host's project.
7673

77-
Now, receiver service can receive the webhooks from git host's project and processor can processes the requests. Now you can verify this service by following the verfication steps below
74+
Now, receiver service can receive the webhooks from git host's project and processor can processes the requests. Now you can verify this service by following the verification steps below
7875

7976
## Verification
8077

@@ -106,7 +103,7 @@ Now, receiver service can receive the webhooks from git host's project and proce
106103
```
107104
debug: new challenge created with id 30051089 for issue 3
108105
```
109-
- Wait a minute or 2 for Topcoder internal systems to process the new challenge. You may get an error page `HTTP Status 404` if the internal processings haven't completed yet.
106+
- Wait a minute or 2 for Topcoder internal systems to process the new challenge. You may get an error page `HTTP Status 404` if the internal processing haven't completed yet.
110107
- Visit challenge url to verify the challenge:
111108
```
112109
https://www.topcoder-dev.com/challenge-details/30051089/?type=develop&noncache=true
@@ -157,6 +154,6 @@ When an user is assigned to an issue then 'issue.assigned' event will be capture
157154
- if user is not found in mapping
158155
- comment on github/gitlab issue will added as :
159156
```
160-
@username, please sign-up with Ragnar Self-service tool.
157+
@username, please sign-up with Topcoder x Self-service tool.
161158
```
162159
- user will be unassigned from issue

config/default.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ const fs = require('fs');
1414

1515
module.exports = {
1616
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
17-
TOPIC: process.env.TOPIC || 'tc-x-events',
1817
PARTITION: process.env.PARTITION || 0,
18+
TOPIC: process.env.TOPIC || 'tc-x-events',
1919
KAFKA_OPTIONS: {
2020
kafkaHost: process.env.KAFKA_HOST || 'localhost:9092',
2121
sslOptions: {
22-
cert: process.env.KAFKA_CLIENT_CERT || fs.readFileSync('./kafka_client.cer'),
23-
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key')
24-
}
22+
cert: process.env.KAFKA_CLIENT_CERT || fs.readFileSync('./kafka_client.cer'), // eslint-disable-line no-sync
23+
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key'), // eslint-disable-line no-sync
24+
}
2525
},
26-
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ragnar',
26+
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/topcoderx',
2727
TC_DEV_ENV: process.env.NODE_ENV === 'production' ? false : true,
2828
TC_AUTHN_URL: process.env.TC_AUTHN_URL || 'https://topcoder-dev.auth0.com/oauth/ro',
2929
TC_AUTHN_REQUEST_BODY: {

models/Challenge.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

models/Project.js

100644100755
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
const mongoose = require('mongoose');
1212

1313
const schema = new mongoose.Schema({
14-
provider: {type: String, required: true}, // github or gitlab
15-
repositoryId: {type: Number, required: true, unique: true},
16-
projectId: {type: Number, required: true, unique: true}
14+
title: {type: String, required: true},
15+
tcDirectId: {type: Number, required: true},
16+
repoUrl: {type: String, required: true},
17+
rocketChatWebhook: {type: String, required: true},
18+
rocketChatChannelName: {type: String, required: true},
19+
archived: {type: String, required: true},
20+
username: {type: String, required: true},
21+
secretWebhookKey: {type: String, required: true}
1722
});
1823

1924
// project id, provider, repositoryId must be unique
20-
schema.index({projectId: 1, provider: 1, repositoryId: 1}, {unique: true});
25+
schema.index({tcDirectId: 1}, {unique: true});
2126

2227

2328
module.exports = schema;

models/User.js

100644100755
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const constants = require('../constants');
1313
const Schema = mongoose.Schema;
1414

1515
const schema = new Schema({
16-
topcoderUsername: {type: String, required: true},
1716
userProviderId: {type: Number, required: true},
1817
username: {type: String, required: true},
1918
role: {type: String, required: true, enum: _.values(constants.USER_ROLES)},
@@ -24,11 +23,9 @@ const schema = new Schema({
2423
refreshToken: String
2524
});
2625

27-
schema.index({topcoderUsername: 1});
2826
schema.index({userProviderId: 1});
2927
schema.index({username: 1});
3028
schema.index({role: 1});
3129
schema.index({type: 1});
32-
schema.index({topcoderUsername: 1, type: 1}, {unique: true});
3330

3431
module.exports = schema;

models/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const models = {
2121
Issue: connection.model('Issue', require('./Issue')),
2222
Project: connection.model('Project', require('./Project')),
2323
User: connection.model('User', require('./User')),
24-
Challenge: connection.model('Challenge', require('./Challenge')),
2524
UserMapping: connection.model('UserMapping', require('./UserMapping'))
2625
};
2726
/* eslint-enable global-require */

services/GithubService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ async function createComment(copilot, repo, number, body) {
147147
const github = await _authenticate(copilot.accessToken);
148148
const owner = await _getUsernameById(github, copilot.userProviderId);
149149
await github.issues.createComment({owner, repo, number, body});
150-
logger.debug('Github comment is added on issue notifying user to assign using Ragnar tool');
150+
logger.debug('Github comment is added on issue notifying user to assign using Topcoder x tool');
151151
}
152152

153153
createComment.schema = {

services/IssueService.js

100644100755
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const ragnarService = require('./RagnarToolService');
2424
const gitlabService = require('./GitlabService');
2525

2626
const Issue = models.Issue;
27-
const Project = models.Project;
2827
const md = new MarkdownIt();
2928

3029
/**
@@ -120,7 +119,7 @@ async function handleIssueAssignment(event, issue) {
120119
}
121120

122121
// Update the challenge
123-
logger.debug(`Assinging user to challenge: ${userMapping.topcoderUsername}`);
122+
logger.debug(`Assigning user to challenge: ${userMapping.topcoderUsername}`);
124123
await topcoderApiHelper.updateChallenge(dbIssue.challengeId, {
125124
// task: true,
126125
assignees: [userMapping.topcoderUsername]
@@ -132,8 +131,8 @@ async function handleIssueAssignment(event, issue) {
132131

133132
logger.debug(`Member ${userMapping.topcoderUsername} is assigned to challenge with id ${dbIssue.challengeId}`);
134133
} else {
135-
// comment on the git ticket for the user to self-sign up with the Ragnar Self-Service tool
136-
const comment = `@${assigneeUsername}, please sign-up with Ragnar Self-service tool`;
134+
// comment on the git ticket for the user to self-sign up with the Topcoder x Self-Service tool
135+
const comment = `@${assigneeUsername}, please sign-up with Topcoder X tool`;
137136
if (event.provider === 'github') {
138137
await gitHubService.createComment(event.copilot, event.data.repository.name, issue.number, comment);
139138
// un-assign the user from the ticket
@@ -218,7 +217,7 @@ async function handleIssueUpdate(event, issue) {
218217
prizes: issue.prizes
219218
});
220219
await dbIssue.save();
221-
// comment on the git ticket for the user to self-sign up with the Ragnar Self-Service tool
220+
// comment on the git ticket for the user to self-sign up with the Topcoder x Self-Service tool
222221
const contestUrl = getUrlForChallengeId(dbIssue.challengeId);
223222
const comment = `Contest ${contestUrl} has been updated - the new changes has been updated for this ticket.`;
224223
if (event.provider === 'github') {
@@ -238,9 +237,15 @@ async function handleIssueUpdate(event, issue) {
238237
*/
239238
async function handleIssueCreate(event, issue) {
240239
// check if project for such repository is already created
241-
const project = await Project.findOne({
242-
provider: issue.provider,
243-
repositoryId: issue.repositoryId
240+
241+
let fullRepoUrl;
242+
if (issue.provider === 'github') {
243+
fullRepoUrl = `https://github.com/${event.data.repository.full_name}`;
244+
} else if (issue.provider === 'gitlab') {
245+
fullRepoUrl = `${config.GITLAB_API_BASE_URL}/${event.data.repository.full_name}`;
246+
}
247+
const project = await models.Project.findOne({
248+
repoUrl: fullRepoUrl
244249
});
245250

246251
// Check if duplicated
@@ -254,23 +259,13 @@ async function handleIssueCreate(event, issue) {
254259
throw new Error(
255260
`challenge ${dbIssue.challengeId} existed already for the issue ${issue.number}`);
256261
}
257-
let projectId;
258-
if (project) { // if existing found don't create a project
259-
projectId = project.projectId;
260-
logger.debug(`existing project was found with id ${projectId} for repository ${event.data.repository.full_name}`);
261-
} else {
262-
// Create a new project
263-
projectId = await topcoderApiHelper.createProject(event.data.repository.full_name);
264-
265-
// save project and repository mapping in db
266-
await Project.create({
267-
projectId,
268-
provider: issue.provider,
269-
repositoryId: issue.repositoryId
270-
});
271262

272-
logger.debug(`new project created with id ${projectId} for issue ${issue.number}`);
273-
}
263+
if (!project) {
264+
throw new Error(
265+
'There is no project associated with this repository');
266+
}// if existing found don't create a project
267+
const projectId = project.tcDirectId;
268+
logger.debug(`existing project was found with id ${projectId} for repository ${event.data.repository.full_name}`);
274269

275270
// Create a new challenge
276271
issue.challengeId = await topcoderApiHelper.createChallenge({
@@ -279,7 +274,7 @@ async function handleIssueCreate(event, issue) {
279274
detailedRequirements: issue.body,
280275
prizes: issue.prizes,
281276
task: true
282-
});
277+
}, issue.provider, event.data.repository.full_name);
283278

284279
// Save
285280
await Issue.create(issue);

services/RagnarToolService.js

100644100755
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'use strict';
55

66
/**
7-
* This provides methods around Ragnar Self service tool.
7+
* This provides methods around Self service tool.
88
* @author TCSCODER
99
* @version 1.0
1010
*/
@@ -14,7 +14,7 @@ const config = require('config');
1414
const models = require('../models');
1515

1616
/**
17-
* gets the tc handle for given username from a provider captured by Ragnar tool
17+
* gets the tc handle for given username from a provider captured by Topcoder x tool
1818
* @param {String} provider the username provider
1919
* @param {String} username the username
2020
* @returns {Object} user mapping if found else null
@@ -36,7 +36,7 @@ getTCUserName.schema = {
3636
};
3737

3838
/**
39-
* gets the access token of repository's copilot captured by Ragnar tool
39+
* gets the access token of repository's copilot captured by Topcoder x tool
4040
* @param {String} provider the repo provider
4141
* @param {String} repoFullName the full name of repository
4242
* @returns {String} the copilot if exists
@@ -50,16 +50,25 @@ async function getRepositoryCopilot(provider, repoFullName) {
5050
fullRepoUrl = `${config.GITLAB_API_BASE_URL}/${repoFullName}`;
5151
}
5252

53-
const challenge = await models.Challenge.findOne({
53+
const project = await models.Project.findOne({
5454
repoUrl: fullRepoUrl
5555
});
5656

57-
if (!challenge || !challenge.username) {
58-
// throw this repo is not managed by Ragnar tool
59-
throw new Error(`This repository '${provider}' is not managed by Ragnar tool.`);
57+
if (!project || !project.username) {
58+
// throw this repo is not managed by Topcoder x tool
59+
throw new Error(`This repository '${repoFullName}' is not managed by Topcoder X tool.`);
6060
}
61+
62+
const userMapping = await models.UserMapping.findOne({
63+
topcoderUsername: project.username.toLowerCase()
64+
});
65+
66+
if (!userMapping || (provider === 'github' && !userMapping.githubUserId) || (provider === 'gitlab' && !userMapping.gitlabUserId)) {
67+
throw new Error(`Couldn't find githost username for '${provider}' for this repository '${repoFullName}'.`);
68+
}
69+
6170
const copilot = await models.User.findOne({
62-
topcoderUsername: challenge.username.toLowerCase(),
71+
username: provider === 'github' ? userMapping.githubUsername : userMapping.gitlabUsername,
6372
type: provider
6473
});
6574

0 commit comments

Comments
 (0)