[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
config.js
store

6
bot.js
View File

@ -1,5 +1,9 @@
const Telegraf = require("telegraf");
const config = require("./config");
const Sequelize = require("sequelize");
const main = require("./bot");
const config = require("./config");
const db = require("./data")(Sequelize, config);
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");
const config = require("../config");
module.exports = (Sequelize, config) => {
const { database, username, password, options } = config.sequelize;
const sequelize = new Sequelize(database, username, password, options);
const models = require("../models")(sequelize);
const { database, username, password, options } = config.sequelize;
const sequelize = new Sequelize(database, username, password, options);
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 => ({
integrations: require("./integrations")(sequelize),
integrations: require("./integrations")(sequelize),
});

View File

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

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

View File

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

View File

@ -4,7 +4,9 @@ app.use(express.json());
const convert = require("./util/convert");
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"));

View File

@ -1,16 +1,24 @@
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 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;

View File

@ -1,7 +1,9 @@
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";
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 +1,4 @@
module.exports = {
getEvent: require("./event"),
commit: require("./commit"),
getEvent: require("./event"),
commit: require("./commit"),
};