From abf043a27a641d50359e2c87a7d5f90f0c39a691 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 27 Nov 2018 22:36:29 +0530 Subject: [PATCH] [refactor] /util to /strategies --- bot/{index.js => main.js} | 0 server.js | 7 ++++--- server/index.js | 10 ---------- server/main.js | 10 ++++++++++ strategies/github.js | 35 +++++++++++++++++++++++++++++++++++ strategies/index.js | 5 +++++ util/convert/commit.js | 26 -------------------------- util/convert/event.js | 9 --------- util/convert/index.js | 4 ---- 9 files changed, 54 insertions(+), 52 deletions(-) rename bot/{index.js => main.js} (100%) delete mode 100644 server/index.js create mode 100644 server/main.js create mode 100644 strategies/github.js create mode 100644 strategies/index.js delete mode 100644 util/convert/commit.js delete mode 100644 util/convert/event.js delete mode 100644 util/convert/index.js diff --git a/bot/index.js b/bot/main.js similarity index 100% rename from bot/index.js rename to bot/main.js diff --git a/server.js b/server.js index 7e0e1a1..36a67ef 100644 --- a/server.js +++ b/server.js @@ -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(); }); diff --git a/server/index.js b/server/index.js deleted file mode 100644 index 0a58bff..0000000 --- a/server/index.js +++ /dev/null @@ -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); - - }); - -}; diff --git a/server/main.js b/server/main.js new file mode 100644 index 0000000..abe4946 --- /dev/null +++ b/server/main.js @@ -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); + + }); + +}; diff --git a/strategies/github.js b/strategies/github.js new file mode 100644 index 0000000..8fe06e9 --- /dev/null +++ b/strategies/github.js @@ -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 += `${curr.id.slice(0,6)}: ` + + `${curr.message}\n`; + + } + return acc; + + }, `🔨 ${data.commits.length} ${noun}\n`); + if (data.commits.length > 6) { + + text += `${data.commits.length - 6} more ${noun}.`; + + } + + return text; + + }, + +}; diff --git a/strategies/index.js b/strategies/index.js new file mode 100644 index 0000000..dee01b8 --- /dev/null +++ b/strategies/index.js @@ -0,0 +1,5 @@ +module.exports = { + + github: require("./github"), + +}; diff --git a/util/convert/commit.js b/util/convert/commit.js deleted file mode 100644 index c70b0ee..0000000 --- a/util/convert/commit.js +++ /dev/null @@ -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 += `${curr.id.slice(0,6)}: ` - + `${curr.message}\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 }; diff --git a/util/convert/event.js b/util/convert/event.js deleted file mode 100644 index 9039779..0000000 --- a/util/convert/event.js +++ /dev/null @@ -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}; diff --git a/util/convert/index.js b/util/convert/index.js deleted file mode 100644 index fd418a7..0000000 --- a/util/convert/index.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - getEvent: require("./event"), - commit: require("./commit"), -};