# Description:
#   integrazione con drone.io e funzioni annesse
#
# Dependencies:
#   None
#
# Configuration:
#   None
#
# Commands:
#  hubot (mostrami la) copertura (dei test)
#
# Author:
#   Enrico Fasoli (fazo96)

moment = require 'moment'

module.exports = (robot) ->
  robot.respond /(?:mostrami la )?(?:copertura|coverage)(?: dei test)?/i, (res) ->
    url = 'https://drone.io/github.com/fazo96/asjon/files/coverage/coverage.'
    robot.http(url+'json')
      .get() (err, resp, body) ->
        try
          report = JSON.parse body
        catch e
          if /^404/g.test body
            return res.send 'Coverage report non disponibile (404) forse \
              non รจ ancora stato generato?'
          return res.send 'Errore: '+e+'\n\nRisposta del server: '+body
        unless report?.files?.push? and report?.coverage?.toFixed?
          return res.send 'Errore: informazioni insufficienti'
        t = '=== Coverage: ' + report.coverage.toFixed(0) + '%'
        t += report.files.map (f) ->
          '\n - ' + f.filename + ' is covered at ' + f.coverage.toFixed(0) + '%'
        t += '\nHTML report: '+url+'html'
        t += '\nJSON report: '+url+'json'
        res.send t