diff --git a/bot-config.yml b/bot-config.yml new file mode 100644 index 0000000..6f88e5f --- /dev/null +++ b/bot-config.yml @@ -0,0 +1,6 @@ +notify-reviewers-via-slack: + globallyEnabled: true + repoConfig: + status-im/status-react: + disabled-users: ["flexsurfer"] + disabled: true diff --git a/bot_scripts/notify-reviewers-via-slack.js b/bot_scripts/notify-reviewers-via-slack.js index a4c38f4..4283c51 100644 --- a/bot_scripts/notify-reviewers-via-slack.js +++ b/bot_scripts/notify-reviewers-via-slack.js @@ -8,6 +8,7 @@ // Martin Klepsch (martinklepsch) const slackHelper = require('../lib/slack') +const config = require('../lib/config') const botName = 'notify-reviewers-via-slack' @@ -21,7 +22,13 @@ function registerForNewReviewRequests (robot) { // Make sure we don't listen to our own messages if (context.isBot) return null - await notifyReviewer(context, robot) + const repoName = `${context.payload.repository.owner.login}/${context.payload.repository.name}` + + if (config.enabledForRepo(botName, repoName)) { + await notifyReviewer(context, robot) + } else { + robot.log.info(`${botName} not runing for repo: ${repoName}`) + } }) } diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..8e33a1e --- /dev/null +++ b/doc/README.md @@ -0,0 +1,33 @@ +# Status Bot Documentation + +### Configuration + +All bots should have a global identifier (string). Configuration for each Bot and repository pairing will be stored in `bot-config.yml`. + +#### Recipe — Getting configuration in bots: + +```js +TODO +``` + +#### Recipe — Disabling a bot for a specific repository: + +Edit the configuration for the bot in [`bot-config.yml`](/bot-config.yml): + +```yml +notify-reviewers-via-slack: + globallyEnabled: true + repoConfig: + status-im/status-react: + # disables bot for status-im/status-react + disabled: true +``` + +#### Recipe — Disabling a bot globally: + +Edit the configuration for the bot in [`bot-config.yml`](/bot-config.yml): + +```yml +notify-reviewers-via-slack: + globallyEnabled: false +``` \ No newline at end of file diff --git a/lib/config.js b/lib/config.js index 7a9eb67..98d2014 100644 --- a/lib/config.js +++ b/lib/config.js @@ -5,18 +5,47 @@ // js-yaml: "^3.10.0" // // Author: -// PombeirP +// PombeirP + martinklepsch + +function readYaml (fileName) { + const yaml = require('js-yaml') + const fs = require('fs') + + return yaml.safeLoad(fs.readFileSync(fileName, 'utf8')) +} + +function getConfig () { + return readYaml('config.yml') +} module.exports = (robot, fileName) => { // Get document, or throw exception on error try { - const yaml = require('js-yaml') - const fs = require('fs') - - return yaml.safeLoad(fs.readFileSync(fileName, 'utf8')) + return readYaml(fileName) } catch (e) { robot.log.error(e) } return null } + +module.exports.enabled = (botName) => { + return getConfig()[botName]['defaultEnabled'] +} + +module.exports.forRepo = (botName, repoName) => { + return getConfig()[botName]['repoConfig'][repoName] +} + +module.exports.enabledForRepo = (botName, repoName) => { + let config = getConfig() + + // TODO: how to use `forRepo` here? + if (config[botName]['repoConfig'][repoName]['disabled']) { + return false + } else if (config[botName]['defaultEnabled']) { + return true + } else { + return false + } +}