Skip to content

Commit fe87da9

Browse files
authored
Create index.js
SVN web hook implementation for the bot
1 parent a98b6ef commit fe87da9

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

foxx-service/hooks-svn/index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const createRouter = require('@arangodb/foxx/router');
2+
const {db, query, time} = require('@arangodb');
3+
const joi = require('joi');
4+
5+
const r = createRouter();
6+
module.exports = r;
7+
8+
const collection = db._collection('hooks_svn');
9+
10+
r.get('/hooks-svn', (req, res) =>
11+
{
12+
res.send({
13+
result: 'ok'
14+
});
15+
});
16+
17+
r.get('/hooks-svn/log/last', (req, res) =>
18+
{
19+
const start = time();
20+
21+
if (req.queryParams && Object.prototype.hasOwnProperty.call(req.queryParams, 'repo'))
22+
{
23+
const queryResult = query`
24+
FOR doc in hooks_svn
25+
FILTER doc.repo==${req.queryParams.repo}
26+
sort doc.log.revision desc
27+
limit 1
28+
RETURN unset(doc, "_id","_rev")
29+
`.toArray();
30+
31+
res.send({
32+
result: queryResult.length > 0 ? queryResult[0] : null,
33+
execTime: time() - start
34+
});
35+
}
36+
else
37+
{
38+
res.throw(417, 'missing repo query');
39+
}
40+
})
41+
.response(['application/json'], 'SVN Revisions - Get last stored')
42+
.description('Gets a last stored SVN revision');
43+
44+
r.get('/hooks-svn/log/:id', (req, res) =>
45+
{
46+
const start = time();
47+
48+
const queryResult = query`
49+
FOR doc in hooks_svn
50+
FILTER doc._key==${req.pathParams.id}
51+
RETURN unset(doc, "_id","_rev")
52+
`.toArray();
53+
54+
res.send({
55+
result: queryResult.length > 0 ? queryResult[0] : null,
56+
execTime: time() - start
57+
});
58+
})
59+
.pathParam('id', joi.string().required(), 'Log id')
60+
.response(['application/json'], 'SVN Revisions - Get by Id')
61+
.description('Gets a SVN revision by its id');
62+
63+
r.post('/hooks-svn', (req, res) =>
64+
{
65+
const doc = req.body;
66+
67+
/* Object.assign(doc, {
68+
createdOn: new Date().getTime()
69+
});*/
70+
71+
//check if revision is already stored
72+
const queryResult = query`
73+
FOR doc in hooks_svn
74+
FILTER doc.log.revision==${doc.log.revision}
75+
RETURN clients
76+
`.toArray();
77+
78+
if (queryResult.length > 0)
79+
{
80+
res.throw(409, 'Already exists');
81+
}
82+
83+
const meta = collection.save(doc);
84+
res.send({result: meta});
85+
})
86+
.body(joi.object({
87+
repo: joi.string(),
88+
log: joi.object({
89+
revision: joi.number(),
90+
author: joi.string(),
91+
date: joi.number(),
92+
message: joi.string()
93+
})
94+
}))
95+
.response(['application/json'], 'SVN Hooks - Add record')
96+
.description('Adds new svn hook record');

0 commit comments

Comments
 (0)