[refactor] Add eslint and do dependency injection

This commit is contained in:
Muthu Kumar 2018-11-27 21:05:55 +05:30
parent dce4394ff6
commit 968f1a8e1e
14 changed files with 2157 additions and 2110 deletions

29
.eslintrc.js Normal file
View File

@ -0,0 +1,29 @@
module.exports = {
"parserOptions": {
"ecmaVersion": 2018
},
"env": {
"node": true,
"es6": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [ "error", "tab" ],
"operator-linebreak": [ "error", "before" ],
"semi": [ "error", "always" ],
"comma-dangle": [ "error", "only-multiline" ],
"quotes": [ "error", "double" ],
"no-tabs": [ "error", { "allowIndentationTabs": true } ],
"padded-blocks": 2,
"space-before-blocks": 2,
"comma-style": 2,
"no-console": 0,
"valid-typeof": 0,
"arrow-parens": 0,
"generator-star-spacing": 0,
"space-before-function-paren": 0,
"object-property-newline": 0,
"new-cap": 0,
"no-eval": 0
}
};

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules node_modules
config.js config.js
store

6
bot.js
View File

@ -1,5 +1,9 @@
const Telegraf = require("telegraf"); const Telegraf = require("telegraf");
const config = require("./config");
const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
const main = require("./bot");
const config = require("./config");
const db = require("./data")(Sequelize, config);
const bot = new Telegraf(config.botApiKey); const bot = new Telegraf(config.botApiKey);
Promise.all([ bot, db, config ]).then(main);

3
bot/index.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = ([ bot, db, config ]) => {
};

View File

@ -1,8 +1,8 @@
const Sequelize = require("sequelize"); module.exports = (Sequelize, config) => {
const config = require("../config");
const { database, username, password, options } = config.sequelize; const { database, username, password, options } = config.sequelize;
const sequelize = new Sequelize(database, username, password, options); const sequelize = new Sequelize(database, username, password, options);
const models = require("../models")(sequelize); const models = require("../models")(sequelize);
return sequelize.sync().then(() => models);
module.exports = sequelize.sync().then(() => models); };

View File

@ -1,3 +1,3 @@
module.exports = sequelize => ({ module.exports = sequelize => ({
integrations: require("./integrations")(sequelize), integrations: require("./integrations")(sequelize),
}); });

View File

@ -1,12 +1,10 @@
const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
module.exports = sequelize => { module.exports = sequelize =>
const integrations = sequelize.define("integrations", { sequelize.define("integrations", {
chatID: { type: Sequelize.INTEGER, allowNull: false, unique: "com"}, chatID: { type: Sequelize.INTEGER, allowNull: false, unique: "com"},
repoURL: {type: Sequelize.STRING, allowNull: false, unique: "com"}, repoURL: {type: Sequelize.STRING, allowNull: false, unique: "com"},
chatType: {type: Sequelize.STRING}, chatType: {type: Sequelize.STRING},
issues: {type: Sequelize.BOOLEAN, defaultValue: false}, issues: {type: Sequelize.BOOLEAN, defaultValue: false},
pullRequests: {type: Sequelize.BOOLEAN, defaultValue: false}, pullRequests: {type: Sequelize.BOOLEAN, defaultValue: false},
commits: {type: Sequelize.BOOLEAN, defaultValue: true}, commits: {type: Sequelize.BOOLEAN, defaultValue: true},
}); });
integrations.sync().then(return integration);
};

4060
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +1,29 @@
{ {
"name": "git-alerts-bot", "name": "git-alerts-bot",
"version": "1.0.0", "version": "0.1.0",
"description": "TG Bot for Gitlab/Github", "description": "TG Bot for Gitlab/Github",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+ssh://git@gitlab.com/ceda_ei/git-alerts-bot.git" "url": "git+ssh://git@gitlab.com/ceda_ei/git-alerts-bot.git"
}, },
"author": "ceda_ei", "author": "ceda_ei",
"license": "GPL-3.0", "license": "GPL-3.0",
"bugs": { "bugs": {
"url": "https://gitlab.com/ceda_ei/git-alerts-bot/issues" "url": "https://gitlab.com/ceda_ei/git-alerts-bot/issues"
}, },
"homepage": "https://gitlab.com/ceda_ei/git-alerts-bot#readme", "homepage": "https://gitlab.com/ceda_ei/git-alerts-bot#readme",
"dependencies": { "dependencies": {
"express": "^4.16.4", "express": "^4.16.4",
"sequelize": "^4.41.2", "sequelize": "^4.41.2",
"sqlite3": "^4.0.4", "sqlite3": "^4.0.4",
"telegraf": "^3.25.0" "telegraf": "^3.25.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^5.9.0", "eslint": "^5.9.0",
"eslint-config-google": "^0.11.0" "eslint-config-google": "^0.11.0"
} }
} }

View File

@ -1,18 +1,18 @@
module.exports = { module.exports = {
// Create a new bot by messaging @BotFather and follow the instructions // Create a new bot by messaging @BotFather and follow the instructions
// Replace the key by the actual token recieved from BotFather // Replace the key by the actual token recieved from BotFather
botApiKey: "key", botApiKey: "key",
// Connection information for database // Connection information for database
"sequelize": { "sequelize": {
database: "git-alerts-bot", database: "git-alerts-bot",
username: "git-bot", username: "git-bot",
password: "hopefully_a_secure_password", password: "hopefully_a_secure_password",
options: { options: {
host: "localhost", host: "localhost",
dialect: "sqlite", dialect: "sqlite",
// SQLite only // SQLite only
storage: "./git-alerts-bot.sqlite" storage: "./store/git-alerts-bot.sqlite"
} }
}, },
}; };

View File

@ -4,7 +4,9 @@ app.use(express.json());
const convert = require("./util/convert"); const convert = require("./util/convert");
app.get("/webhook/:provider", (req) => { app.get("/webhook/:provider", (req) => {
const strategy = req.params.provider;
const eventType = convert.getEvent[strategy](req.body); const strategy = req.params.provider;
const eventType = convert.getEvent[strategy](req.body);
}); });
app.listen(2000, () => console.log("Listening on port 2000")); app.listen(2000, () => console.log("Listening on port 2000"));

View File

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

View File

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

View File

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