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

Commit f03c15b

Browse files
committed
Merge branch 'ch_30065364' into 'develop'
changes for https://www.topcoder.com/challenges/30065364/?type=develop See merge request luettich/receiver!9
2 parents 87a008c + 4a48dfb commit f03c15b

File tree

6 files changed

+128
-126
lines changed

6 files changed

+128
-126
lines changed

README.md

100644100755
Lines changed: 103 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,104 @@
1-
## Requirements
2-
3-
- Nodejs 8 is required
4-
- [Apache Kafka](https://kafka.apache.org/)
5-
- [Local installation guide](https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/)
6-
## Install dependencies
7-
8-
```shell
9-
npm install
10-
```
11-
12-
## Source code lint
13-
14-
eslint is used to lint the javascript source code:
15-
16-
```shell
17-
npm run lint
18-
```
19-
20-
## Endpoints
21-
22-
- POST /webhooks/github - The webhook handler for github
23-
- POST /webhooks/gitlab - The webhook handler for gitlab
24-
25-
26-
## Configuration
27-
28-
The following config parameters are supported, they are defined in `config/default.js` and can be configured in system environment:
29-
30-
31-
| Name | Description | Default |
32-
| :----------------------------- | :----------------------------------------: | :------------------------------: |
33-
| PORT | the port the application will listen on | 3000 |
34-
| LOG_LEVEL | the log level | info |
35-
| TOPIC | the kafka subscribe topic name | tc-x-events |
36-
| WEBHOOK_SECRET_TOKEN | the webhook security token for githost, it must be same as `WEBHOOK_SECRET_TOKEN` configured for Topcoder-X-backend| `ka75hsrq65cFEr61Hd4x`|
37-
|KAFKA_OPTIONS | the connection option for kafka | see below about KAFKA options |
38-
| MONGODB_URL | the MongoDB URL which must be same as Ragnar tool | mongodb://127.0.0.1:27017/ragnar|
39-
40-
KAFKA_OPTIONS should be object as described in https://github.com/SOHU-Co/kafka-node#kafkaclient
41-
For using with SSL, the options should be as
42-
```
43-
{
44-
kafkaHost: '<server>',
45-
sslOptions: {
46-
cert: '<certificate>',
47-
key: '<key>'
48-
}
49-
}
50-
```
51-
52-
`config/local.js` will not tracked by git.
53-
54-
## Local Setup
55-
56-
```shell
57-
npm start
58-
```
59-
60-
Server should be started at port 3002.
61-
62-
use `ngrok` to make your local deploy accessible by internet:
63-
```shell
64-
ngrok http 3002
65-
```
66-
67-
Copy the forwarding URL to set in `HOOK_BASE_URL` of topcoder-x-ui in config.json
68-
69-
## Setup for verification
70-
Before verifying the tool, 4 service needs be configured and run them
71-
- processor
72-
- receiver
73-
- Ragnar Tool
74-
- Topcoder X (both backend and UI)
75-
76-
First login in Ragnar tool with admin and Add owner for which requires topcoder handle, git host's username and type of git host.
77-
78-
Go to Topcoder X UI login with above used topcoder username and
79-
- go to settings and make sure git hosts are correctly setup, if not click setup and authorize to setup.
80-
81-
- 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.
82-
83-
Now, receiver service can receive the webhooks from git host's project. Now you can verify this service by following the verfication steps below
84-
85-
## GitHub Verification
86-
87-
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
88-
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
89-
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.
90-
- update a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.updated` event is generated.
91-
- assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.assigned` event is generated.
92-
- un-assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.unassigned` event is generated.
93-
- add/remove a label to an issue, you can see the logs in `receiver` and `processor`, the `issue.labelUpdated` event is generated.
94-
- create a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.created` event is generated.
95-
- close a pull request without merge, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `false`.
96-
- merge a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `true`.
97-
98-
## Gitlab Verification
99-
100-
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
101-
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
102-
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.
103-
- assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.assigned` event is generated.
104-
- un-assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.unassigned` event is generated.
105-
- add/remove a label to an issue, you can see the logs in `receiver` and `processor`, the `issue.labelUpdated` event is generated.
106-
- create a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.created` event is generated.
107-
- close a pull request without merge, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `false`.
1+
## Requirements
2+
3+
- Nodejs 8 is required
4+
- [Apache Kafka](https://kafka.apache.org/)
5+
- [Local installation guide](https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/)
6+
## Install dependencies
7+
8+
```shell
9+
npm install
10+
```
11+
12+
## Source code lint
13+
14+
eslint is used to lint the javascript source code:
15+
16+
```shell
17+
npm run lint
18+
```
19+
20+
## Endpoints
21+
22+
- POST /webhooks/github - The webhook handler for github
23+
- POST /webhooks/gitlab - The webhook handler for gitlab
24+
25+
26+
## Configuration
27+
28+
The following config parameters are supported, they are defined in `config/default.js` and can be configured in system environment:
29+
30+
31+
| Name | Description | Default |
32+
| :----------------------------- | :----------------------------------------: | :------------------------------: |
33+
| PORT | the port the application will listen on | 3000 |
34+
| LOG_LEVEL | the log level | info |
35+
| TOPIC | the kafka subscribe topic name | tc-x-events |
36+
| KAFKA_OPTIONS | the connection option for kafka | see below about KAFKA options |
37+
| MONGODB_URL | the MongoDB URL which must be same as Topcoder x tool | mongodb://127.0.0.1:27017/topcoderx|
38+
39+
KAFKA_OPTIONS should be object as described in https://github.com/SOHU-Co/kafka-node#kafkaclient
40+
For using with SSL, the options should be as
41+
```
42+
{
43+
kafkaHost: '<server>',
44+
sslOptions: {
45+
cert: '<certificate>',
46+
key: '<key>'
47+
}
48+
}
49+
```
50+
51+
`config/local.js` will not tracked by git.
52+
53+
## Local Setup
54+
55+
```shell
56+
npm start
57+
```
58+
59+
Server should be started at port 3002.
60+
61+
use `ngrok` to make your local deploy accessible by internet:
62+
```shell
63+
ngrok http 3002
64+
```
65+
66+
Copy the forwarding URL to set in `HOOK_BASE_URL` of topcoder-x in config.json
67+
68+
## Setup for verification
69+
Before verifying the tool, 3 service needs be configured and run them
70+
- processor
71+
- receiver
72+
- Topcoder X
73+
74+
Go to Topcoder X UI login with above used topcoder username and
75+
- go to settings and make sure git hosts are correctly setup, if not click setup and authorize to setup.
76+
77+
- 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.
78+
79+
Now, receiver service can receive the webhooks from git host's project. Now you can verify this service by following the verfication steps below
80+
81+
## GitHub Verification
82+
83+
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
84+
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
85+
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.
86+
- update a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.updated` event is generated.
87+
- assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.assigned` event is generated.
88+
- un-assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.unassigned` event is generated.
89+
- add/remove a label to an issue, you can see the logs in `receiver` and `processor`, the `issue.labelUpdated` event is generated.
90+
- create a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.created` event is generated.
91+
- close a pull request without merge, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `false`.
92+
- merge a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `true`.
93+
94+
## Gitlab Verification
95+
96+
- create an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.created` event is generated.
97+
- update an issue in the repo, you can see the logs in `receiver` and `processor`, the `issue.updated` event is generated.
98+
- create a comment on an issue, you can see the logs in `receiver` and `processor`, the `comment.created` event is generated.
99+
- assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.assigned` event is generated.
100+
- un-assigned a user to an issue, you can see the logs in `receiver` and `processor`, the `issue.unassigned` event is generated.
101+
- add/remove a label to an issue, you can see the logs in `receiver` and `processor`, the `issue.labelUpdated` event is generated.
102+
- create a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.created` event is generated.
103+
- close a pull request without merge, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `false`.
108104
- merge a pull request, you can see the logs in `receiver` and `processor`, the `pull_request.closed` event is generated and the `merged` property is `true`.

config/default.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ module.exports = {
1717
PORT: process.env.PORT || 3002, // eslint-disable-line no-magic-numbers
1818
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
1919
TOPIC: process.env.TOPIC || 'tc-x-events',
20-
WEBHOOK_SECRET_TOKEN: process.env.WEBHOOK_SECRET_TOKEN || 'ka75hsrq65cFEr61Hd4x',
2120
KAFKA_OPTIONS: {
2221
kafkaHost: process.env.KAFKA_HOST || 'localhost:9092',
2322
sslOptions: {
2423
cert: process.env.KAFKA_CLIENT_CERT || fs.readFileSync('./kafka_client.cer'), // eslint-disable-line no-sync
2524
key: process.env.KAFKA_CLIENT_CERT_KEY || fs.readFileSync('./kafka_client.key') // eslint-disable-line no-sync
2625
}
2726
},
28-
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ragnar'
27+
MONGODB_URL: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/topcoderx'
2928
};
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/*
22
* Copyright (c) 2018 TopCoder, Inc. All rights reserved.
33
*/
4+
'use strict';
45

56
/**
6-
* This module contains the schema of the Challenge.
7-
*
7+
* Schema for project and repository mapping.
88
* @author TCSCODER
99
* @version 1.0
1010
*/
11-
'use strict';
12-
1311
const mongoose = require('mongoose');
1412

1513
const schema = new mongoose.Schema({
@@ -18,7 +16,13 @@ const schema = new mongoose.Schema({
1816
repoUrl: {type: String, required: true},
1917
rocketChatWebhook: {type: String, required: true},
2018
rocketChatChannelName: {type: String, required: true},
21-
archieved: {type: String, required: true},
22-
username: {type: String, required: true}
19+
archived: {type: String, required: true},
20+
username: {type: String, required: true},
21+
secretWebhookKey: {type: String, required: true}
2322
});
23+
24+
// project id, provider, repositoryId must be unique
25+
schema.index({tcDirectId: 1}, {unique: true});
26+
27+
2428
module.exports = schema;

models/index.js

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const UserUnassignedEvent = require('./UserUnassignedEvent');
2424
const PullRequestCreatedEvent = require('./PullRequestCreatedEvent');
2525
const PullRequestClosedEvent = require('./PullRequestClosedEvent');
2626
const LabelUpdatedEvent = require('./LabelUpdatedEvent');
27-
const Challenge = require('./Challenge');
27+
const Project = require('./Project');
2828

2929
module.exports = {
3030
IssueCreatedEvent,
@@ -36,5 +36,5 @@ module.exports = {
3636
PullRequestCreatedEvent,
3737
PullRequestClosedEvent,
3838
LabelUpdatedEvent,
39-
Challenge: connection.model('Challenge', Challenge)
39+
Project: connection.model('Project', Project)
4040
};

routes/middlewares/RepositoryFilter.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
const _ = require('lodash');
1212

1313
const logger = require('../../utils/logger');
14-
const Challenge = require('../../models').Challenge;
14+
const Project = require('../../models').Project;
1515

1616
module.exports = (provider) => async (req, res, next) => {
1717
let repoNames = [];
1818
if (provider === 'github') {
1919
const repo = req.body.repository || {};
2020
repoNames = [repo.svn_url, repo.git_url, repo.ssh_url, repo.clone_url];
2121
} else if (provider === 'gitlab') {
22-
const repo = req.body.repository || {};
23-
repoNames = [repo.homepage, repo.http_url, repo.url, repo.ssh_url];
22+
const repo = req.body.project || {};
23+
repoNames = [repo.homepage, repo.http_url, repo.url, repo.ssh_url, repo.web_url];
2424
}
2525
let found = false;
26-
const challenges = await Challenge.find({archieved: false});
27-
found = _.some(challenges, (challenge) => _.includes(repoNames, challenge.repoUrl));
26+
const projects = await Project.find({archived: false});
27+
found = _.some(projects, (project) => _.includes(repoNames, project.repoUrl));
2828
if (found) {
2929
return next();
3030
}

routes/middlewares/SecurityChecker.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
*/
1010
'use strict';
1111
const crypto = require('crypto');
12-
const config = require('config');
1312
const logger = require('../../utils/logger');
13+
const Project = require('../../models').Project;
1414

15-
module.exports = (provider) => (req, res, next) => {
15+
module.exports = (provider) => async (req, res, next) => {
1616
let isValid = false;
17+
const params = req.body;
1718
if (provider === 'github') {
18-
const hash = crypto.createHmac('sha1', config.WEBHOOK_SECRET_TOKEN).update(req.rawBody).digest('hex');
19+
const projectDetail = await Project.findOne({repoUrl: params.repository.html_url});
20+
const hash = crypto.createHmac('sha1', projectDetail.secretWebhookKey).update(req.rawBody).digest('hex');
1921
isValid = `sha1=${hash}` === req.header('X-Hub-Signature');
2022
} else if (provider === 'gitlab') {
21-
isValid = config.WEBHOOK_SECRET_TOKEN === req.header('X-Gitlab-Token');
23+
const projectDetail = await Project.findOne({repoUrl: params.project.web_url});
24+
isValid = projectDetail.secretWebhookKey === req.header('X-Gitlab-Token');
2225
} else {
2326
// unknown provider
2427
return next();

0 commit comments

Comments
 (0)