easy-config first pass, #19

This commit is contained in:
Martin Klepsch 2018-02-18 23:00:05 +01:00
parent 8a388db71c
commit 00147b08a5
No known key found for this signature in database
GPG Key ID: 1A35E702AD48A9F6
4 changed files with 81 additions and 6 deletions

6
bot-config.yml Normal file
View File

@ -0,0 +1,6 @@
notify-reviewers-via-slack:
globallyEnabled: true
repoConfig:
status-im/status-react:
disabled-users: ["flexsurfer"]
disabled: true

View File

@ -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}`)
}
})
}

33
doc/README.md Normal file
View File

@ -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
```

View File

@ -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
}
}