Skip to content

Instantly share code, notes, and snippets.

@reitermarkus
Last active April 30, 2020 21:10
Show Gist options
  • Save reitermarkus/fb56856a165569208eece79f1c18a953 to your computer and use it in GitHub Desktop.
Save reitermarkus/fb56856a165569208eece79f1c18a953 to your computer and use it in GitHub Desktop.
CodeCov Status for SinusBot
registerPlugin({
requiredModules: ['http'],
name: 'CodeCov Status',
version: '1.0.0',
engine: '>= 1.0.0',
description: 'Check and display CodeCov status.',
author: 'Markus Reiter <me@reitermark.us>',
vars: [{
name: 'interval',
indent: 0,
title: 'Check Interval in Minutes.',
type: 'number',
placeholder: '5',
}, {
name: 'conf',
indent: 0,
title: 'Repositories',
type: 'array',
vars: [{
name: 'repo',
indent: 1,
title: 'Repository:',
type: 'string'
}, {
name: 'branch',
indent: 1,
title: 'Branch:',
type: 'string',
placeholder: 'master',
}, {
name: 'channel',
indent: 1,
title: 'Channel:',
type: 'channel'
}, {
name: 'name',
indent: 1,
title: 'Channel Name: (%status - ✓/✗, %coverage)',
type: 'string',
placeholder: 'CI: %status, Code Coverage: %coverage'
}, {
name: 'description',
indent: 1,
title: 'Channel Description: (%status - passing/failing, %coverage)',
type: 'multiline',
placeholder: 'CI is %status and code coverage is at %coverage.'
}]
}]
}, function(sinusbot, config, pluginInfo) {
const engine = require('engine')
const backend = require('backend')
const http = require('http')
if (typeof config.interval == 'undefined' || config.interval == '' || config.interval == null) {
config.interval = 5
} else if (config.interval < 1) {
config.interval = 1
}
function startup() {
for (var i = 0; i < config.conf.length; i++) {
if (typeof config.conf[i].repo == 'undefined' || config.conf[i].repo == '') {
engine.log(`ERROR: No repo set by field ${i + 1}.`)
continue
}
if (typeof config.conf[i].branch == 'undefined' || config.conf[i].branch == '') {
config.conf[i].branch = 'master'
}
if (typeof config.conf[i].channel == 'undefined' || config.conf[i].channel == '') {
engine.log(`ERROR: No channel set by field ${i + 1}.`)
continue
}
}
setTimeout(run, 1000)
setInterval(run, config.interval * 60000)
}
function run() {
if (!backend.isConnected())
return
for (conf of config.conf) {
http.simpleRequest({
method: 'GET',
url: `https://codecov.io/api/gh/${conf.repo}/branch/${conf.branch}`,
timeout: 10000,
headers: {
'Accept': 'application/json'
}
}, function(error, response) {
if (error) {
engine.log(`ERROR: Error fetching code coverage: ${error}`)
return
}
const statusCode = response.statusCode
if (statusCode != 200) {
engine.log(`ERROR: Error fetching code coverage: ${statusCode}`)
return
}
const data = JSON.parse(response.data)
const commit = data.commits[0]
const ciPassed = commit.ci_passed
const coverage = Number(commit.totals.c)
const channel = backend.getChannelByID(conf.channel)
if (conf.name) {
const name = conf.name
.replace('%status', ciPassed ? '✓' : '✗')
.replace('%coverage', coverage.toFixed(1))
if (name !== channel.name()) {
engine.log(`INFO: Updating channel name to '${name}'.`)
channel.setName(name)
}
}
if (conf.description) {
const description = conf.description
.replace('%status', ciPassed ? 'passing' : 'failing')
.replace('%coverage', coverage.toFixed(1))
if (description !== channel.description()) {
engine.log(`INFO: Updating channel description to '${description}'.`)
channel.setDescription(description)
}
}
})
}
}
startup()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment