[refactor] /util to /strategies

This commit is contained in:
Muthu Kumar 2018-11-27 22:36:29 +05:30
parent 1199d4e20d
commit abf043a27a
9 changed files with 54 additions and 52 deletions

View File

@ -1,6 +1,6 @@
const express = require("express");
const main = require("./server/index");
const convert = require("./util/convert");
const main = require("./server/main");
const strategies = require("./strategies");
const { server: config } = require("./config");
const PORT = config.server.port;
@ -9,7 +9,8 @@ const app = express();
app.use(express.json());
app.use((req, res, next) => {
req.convert = convert;
// Add our strategies object to request
req.strategies = strategies;
next();
});

View File

@ -1,10 +0,0 @@
module.exports = app => {
app.get("/webhook/:provider", (req, res) => {
const strategy = req.params.provider;
const eventType = req.convert.getEvent[strategy](req.body);
});
};

10
server/main.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = app => {
app.get("/webhook/:provider", (req, res) => {
const { provider } = req.params;
const eventType = req.strategies[provider].getEvent(req.body);
});
};

35
strategies/github.js Normal file
View File

@ -0,0 +1,35 @@
module.exports = {
event (data) {
if (data.commits && data.ref.match("^/refs/heads/")) return "commit";
if (data.commits && data.ref.match("^/refs/tags/")) return "tag";
if (data.issue) return "issue";
},
commit (data) {
const noun = "commit" + data.commits.length > 1 ? "s" : "";
var text = data.commits.reduce((acc, curr, index) => {
if (index < 6) {
acc += `<a href='${curr.url}'>${curr.id.slice(0,6)}: `
+ `${curr.message}</a>\n`;
}
return acc;
}, `🔨 ${data.commits.length} ${noun}\n`);
if (data.commits.length > 6) {
text += `${data.commits.length - 6} more ${noun}.`;
}
return text;
},
};

5
strategies/index.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
github: require("./github"),
};

View File

@ -1,26 +0,0 @@
const github = function (data) {
const noun = "commit" + data.commits.length > 1 ? "s" : "";
var text = data.commits.reduce((acc, curr, index) => {
if (index < 6) {
acc += `<a href='${curr.url}'>${curr.id.slice(0,6)}: `
+ `${curr.message}</a>\n`;
}
return acc;
}, `🔨 ${data.commits.length} ${noun}\n`);
if (data.commits.length > 6) {
text += `${data.commits.length - 6} more ${noun}.`;
}
return text;
};
const gitlab = github;
module.exports = { github, gitlab };

View File

@ -1,9 +0,0 @@
const github = function (body) {
if (body.commits && body.ref.match("^/refs/heads/")) return "commit";
if (body.commits && body.ref.match("^/refs/tags/")) return "tag";
if (body.issue) return "issue";
};
module.exports = {github};

View File

@ -1,4 +0,0 @@
module.exports = {
getEvent: require("./event"),
commit: require("./commit"),
};