Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 206 lines (172 sloc) 6.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
# Description:
# Janky API integration. https://github.com/github/janky
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_JANKY_URL
#
# Commands:
# hubot ci - show usage
#
# Author:
# sr

URL = require "url"
url = URL.parse(process.env.HUBOT_JANKY_URL)
HTTP = require(url.protocol.replace(/:$/, ""))

defaultOptions = () ->
  auth = new Buffer(url.auth).toString("base64")
  template =
    host: url.hostname
    port: url.port || 80
    path: url.pathname
    headers:
      "Authorization": "Basic #{auth}"

buildsStatus = (builds) ->
  buildsLength = builds.length
  response = ""

  if buildsLength > 0
    builds.forEach (build) ->
      response += buildStatusMessage(build)
      response += "\n" if buildsLength > 1

  response

buildStatusMessage = (build) ->
  response = ""
  response += "Build ##{build.number} (#{build.sha1}) of #{build.repo}/#{build.branch} #{build.status}"
  response += "(#{build.duration}s) #{build.compare}"
  response += " [Log: #{build.web_url}]"


get = (path, params, cb) ->
  options = defaultOptions()
  options.path += path
  console.log(options)
  req = HTTP.request options, (res) ->
    body = ""
    res.setEncoding("utf8")
    res.on "data", (chunk) ->
      body += chunk
    res.on "end", () ->
      cb null, res.statusCode, body
  req.on "error", (e) ->
    console.log(e)
    cb e, 500, "Client Error"
  req.end()

put = (path, params, cb) ->
  post path, params, cb, 'PUT'

post = (path, params, cb, method='POST') ->
  bodyParams = JSON.stringify params

  options = defaultOptions()
  options.path = "/_hubot/#{path}"
  options.method = method
  options.headers['Content-Length'] = bodyParams.length

  req = HTTP.request options, (res) ->
    body = ""
    res.setEncoding("utf8")
    res.on "data", (chunk) ->
      body += chunk
    res.on "end", () ->
      cb null, res.statusCode, body
  req.on "error", (e) ->
    console.log(e)
    cb e, 500, "Client Error"
  req.end(bodyParams)

del = (path, params, cb) ->
  post path, params, cb, 'DELETE'

module.exports = (robot) ->
  robot.respond /ci\??$/i, (msg) ->
    get "help", { }, (err, statusCode, body) ->
      if statusCode == 200
        msg.send body
      else
        msg.reply "Unable to fetch help. Got HTTP status #{statusCode}"

  robot.respond /ci build ([-_\.0-9a-zA-Z]+)(\/([-_\+\.a-zA-z0-9\/]+))?/i, (msg) ->
    app = msg.match[1]
    branch = msg.match[3] || "master"
    room_id = msg.message.user.room
    user = msg.message.user.name.replace(/\ /g, "+")

    post "#{app}/#{branch}?room_id=#{room_id}&user=#{user}", {}, (err, statusCode, body) ->
      if statusCode == 201 or statusCode == 404
        response = body
      else
        console.log body
        response = "Can't go HAM on #{app}/#{branch}, shit's being weird. Got HTTP status #{statusCode}"

      msg.send response

  robot.respond /ci setup ([\.\-\/_a-z0-9]+)(\s?([\.\-_a-z0-9]+))?/i, (msg) ->
    nwo = msg.match[1]
    params = "?nwo=#{nwo}"
    if msg.match[3] != undefined
      params += "&name=#{msg.match[3]}"
      if msg.match[5] != undefined
        params += "&template=#{msg.match[5]}"

    post "setup#{params}", {}, (err, statusCode, body) ->
      if statusCode == 201
        msg.reply body
      else
        msg.reply "Can't Setup. Make sure I have access to it. Expected HTTP status 201, got #{statusCode}"

  robot.respond /ci toggle ([-_\.0-9a-zA-Z]+)/i, (msg) ->
    app = msg.match[1]

    post "toggle/#{app}", { }, (err, statusCode, body) ->
      if statusCode == 200
        msg.send body
      else
        msg.reply "Failed to flip the flag. Sorry. Got HTTP status #{statusCode}"

  robot.respond /ci set room ([-_0-9a-zA-Z\.]+) (.*)$/i, (msg) ->
    repo = msg.match[1]
    room = encodeURIComponent(msg.match[2])
    put "#{repo}?room=#{room}", {}, (err, statusCode, body) ->
      if [404, 403, 200].indexOf(statusCode) > -1
        msg.send body
      else
        msg.send "I couldn't update the room. Got HTTP status #{statusCode}"

  robot.respond /ci rooms$/i, (msg) ->
    get "rooms", { }, (err, statusCode, body) ->
      if statusCode == 200
        rooms = JSON.parse body
        msg.reply rooms.join ", "
      else
        msg.reply "can't predict rooms now."

  robot.respond /ci builds ([0-9]+) ?(building)?$/i, (msg) ->
    limit = msg.match[1]
    building = msg.match[2]?
    get "builds?limit=#{limit}&building=#{building}", {}, (err, statusCode, body) ->
      builds = JSON.parse(body)
      response = buildsStatus(builds) || "Builds? Sorry, there's no builds here"

      msg.send response

  robot.respond /ci status( (\*\/[-_\+\.a-zA-z0-9\/]+))?$/i, (msg) ->
    path = if msg.match[2] then "/#{msg.match[2]}" else ""
    get path, {}, (err, statusCode, body) ->
      if statusCode == 200
        msg.send(body)
      else
        msg.send("Couldn't get status. Got HTTP status #{statusCode}")

  robot.respond /ci status (-v )?([-_\.0-9a-zA-Z]+)(\/([-_\+\.a-zA-z0-9\/]+))?/i, (msg) ->
    app = msg.match[2]
    count = 5
    branch = msg.match[4] || 'master'

    unless msg.match[1]?
      count = 1

    get "#{app}/#{branch}?limit=#{count}", { }, (err, statusCode, body) ->
      builds = JSON.parse(body)
      response = buildsStatus(builds) || "Sorry, no builds found for #{app}/#{branch}"

      msg.send response

  robot.router.post "/janky", (req, res) ->
    robot.messageRoom req.body.room, req.body.message
    res.end "ok"

  robot.respond /ci show ([-_\.0-9a-zA-Z]+)/i, (msg) ->
    app = msg.match[1]
    get "show/#{app}", { }, (err, statusCode, body) ->
      if statusCode == 200
        repo = JSON.parse(body)
        lines = for key, val of repo
          "#{key}: #{val}"
        response = lines.join("\n")
        msg.send response
      else
        replyMsg = "Error F7U12: Can't show: #{statusCode}: #{body}"
        msg.reply replyMsg

  robot.respond /ci delete ([-_\.0-9a-zA-Z]+)/i, (msg) ->
    app = msg.match[1]
    del "#{app}", {}, (err, statusCode, body) ->
      if statusCode != 200
        msg.reply "I got an error removing #{app}; sometimes deleting all the old commits/branches times out the unicorn. Maybe try again?"
      msg.send body
Something went wrong with that request. Please try again.