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

Commit 343f407

Browse files
committed
Preliminary Azure DevOps support
1 parent 72d1891 commit 343f407

10 files changed

+346
-8
lines changed

models/IssueClosedEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const IssueClosedEvent = {
1818
issue: issueSchema.required(),
1919
repository: repositorySchema.required(),
2020
assignee: Joi.object().keys({
21-
id: Joi.number().allow(null)
21+
id: Joi.alternatives().try(Joi.string(), Joi.number()).allow(null)
2222
}).required()
2323
})
2424
};

models/Project.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const schema = new Schema({
3030
}
3131
},
3232
repoUrl: {type: String, required: true},
33+
repoId: {type: String, required: false},
3334
rocketChatWebhook: {type: String, required: false},
3435
rocketChatChannelName: {type: String, required: false},
3536
archived: {type: String, required: true},

models/UserAssignedEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ UserAssignedEvent.schema = Joi.object().keys({
1919
issue: issueSchema.required(),
2020
repository: repositorySchema.required(),
2121
assignee: Joi.object().keys({
22-
id: Joi.number().required()
22+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
2323
}).required()
2424
});
2525

models/common.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Joi = require('joi');
1616

1717
// the repository schema
1818
const repositorySchema = Joi.object().keys({
19-
id: Joi.number().required(),
19+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
2020
name: Joi.string().required(),
2121
full_name: Joi.string().required()
2222
});
@@ -28,10 +28,10 @@ const issueSchema = Joi.object().keys({
2828
body: Joi.string().allow(''),
2929
labels: Joi.array().items(Joi.string()),
3030
assignees: Joi.array().items(Joi.object().keys({
31-
id: Joi.number().required()
31+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
3232
})),
3333
owner: Joi.object().keys({
34-
id: Joi.number().required()
34+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
3535
}).required()
3636
});
3737

@@ -43,10 +43,10 @@ const pullRequestSchema = Joi.object().keys({
4343
body: Joi.string().allow(''),
4444
title: Joi.string().required(),
4545
user: Joi.object().keys({
46-
id: Joi.number().required()
46+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
4747
}),
4848
assignees: Joi.array().items({
49-
id: Joi.number().required()
49+
id: Joi.alternatives().try(Joi.string(), Joi.number()).required()
5050
})
5151
});
5252

models/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dynamoose.setDefaults({
3131
if (process.env.CREATE_DB) {
3232
dynamoose.setDefaults({
3333
create: true,
34-
update: true,
34+
update: true
3535
});
3636
}
3737

routes/middlewares/RepositoryFilter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ module.exports = (provider) => async (req, res, next) => {
2222
} else if (provider === 'gitlab') {
2323
const repo = req.body.project || {};
2424
repoNames = [repo.homepage, repo.http_url, repo.url, repo.ssh_url, repo.web_url];
25+
} else if (provider === 'azure') {
26+
const projectId = req.body.resourceContainers.project.id;
27+
const projects = await dbHelper.scan(Project, {
28+
repoId: projectId,
29+
archived: 'false'
30+
});
31+
if (projects && projects.length > 0) {
32+
return next();
33+
}
2534
}
2635
const projects = await dbHelper.scan(Project, {
2736
archived: 'false'

routes/middlewares/SecurityChecker.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ module.exports = (provider) => async (req, res, next) => {
3636
isValid = true;
3737
}
3838
});
39+
} else if (provider === 'azure') {
40+
const projectId = req.body.resourceContainers.project.id;
41+
const project = await dbHelper.scanOne(Project, {
42+
repoId: projectId
43+
});
44+
const buff = Buffer.from(req.headers.authorization.split(' ')[1], 'base64'); // Ta-da
45+
const authorization = buff.toString('utf-8');
46+
const pwd = authorization.split(':')[1];
47+
if (pwd === project.secretWebhookKey) {
48+
isValid = true;
49+
}
3950
} else {
4051
// unknown provider
4152
return next();

routes/webhooks.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const logger = require('../utils/logger');
1414
const kafka = require('../utils/kafka');
1515
const GithubEventDetector = require('../utils/GithubEventDetector');
1616
const GitlabEventDetector = require('../utils/GitlabEventDetector');
17+
const AzureEventDetector = require('../utils/AzureEventDetector');
1718

1819
const RepositoryFilter = require('./middlewares/RepositoryFilter');
1920
const SecurityChecker = require('./middlewares/SecurityChecker');
@@ -47,4 +48,19 @@ router.post('/gitlab', SecurityChecker('gitlab'), RepositoryFilter('gitlab'), wr
4748
res.json({success: true});
4849
}));
4950

51+
router.post('/azure', SecurityChecker('azure'), RepositoryFilter('azure'), wrapper(async (req, res) => {
52+
const result = AzureEventDetector.detectMany(req.body);
53+
if (!result || result.length === 0) {
54+
logger.info('unknown event detected');
55+
logger.debug(req.body);
56+
} else {
57+
for (const event of result) { // eslint-disable-line no-restricted-syntax
58+
await kafka.send(JSON.stringify(event));
59+
logger.info(`successfully add event: ${event.event} to kafka queue`);
60+
logger.debug(`kafka message: ${JSON.stringify(event)}`);
61+
}
62+
}
63+
res.json({success: true});
64+
}));
65+
5066
module.exports = router;

0 commit comments

Comments
 (0)