-
Notifications
You must be signed in to change notification settings - Fork 1
/
higanworks-travis-build-restart.coffee
63 lines (56 loc) · 2.18 KB
/
higanworks-travis-build-restart.coffee
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
# Description:
# Travis latest build restart
#
# Dependencies:
# "githubot": "0.4.x"
#
# Configuration:
# HUBOT_TRAVIS_ACCESS_TOKEN
# HUBOT_IDOBATA_HOOK_URL
# HUBOT_GITHUB_TOKEN
# HUBOT_GITHUB_USER
# HUBOT_GITHUB_API
#
# Commands:
# hubot travis build restart :user/:repo - latest build restart
#
# Notes:
# HUBOT_GITHUB_API allows you to set a custom URL path (for Github enterprise users)
#
# Author:
# MATSUMOTO, Ryosuke
QS = require "querystring"
module.exports = (robot) ->
success = "<span class='label label-success'>Success</span>"
failure = "<span class='label label-important'>Failure</span>"
github = require("githubot")(robot)
robot.respond /travis build restart (.*)$/i, (msg) ->
repo = github.qualified_repo msg.match[1]
travis_url = "https://api.travis-ci.org/repositories/#{repo}/builds.json"
msg.send "HiganBot restart Travis latest build of #{repo}"
msg.http(travis_url)
.header('User-Agent', 'higan-hubot')
.get() (err, res, body) ->
data = body
json = JSON.parse body
if data.length > 4 and data[0] == '['
msg.http("https://api.travis-ci.org/builds/#{json[0].id}/restart")
.header("Authorization", "token #{process.env.HUBOT_TRAVIS_ACCESS_TOKEN}")
.post() (err, res, body) ->
if res.statusCode == 200
json = JSON.parse body
result = if json.result then success else failure
if json.flash[0].error
message = "#{repo} build restart. result: #{failure} : #{json.flash[0].error}"
else
message = "#{repo} build restart. result: #{result} : #{json.flash[0].notice}"
post_data = QS.stringify source: message
msg.http(process.env.HUBOT_IDOBATA_HOOK_URL)
.query(format: "html")
.header("Content-Type", "application/x-www-form-urlencoded")
.post(post_data) (err, res, body) ->
data = body
else
msg.send "Travis API return error. status:#{res.statusCode} body:#{body}"
else
msg.send "HiganBot can not find #{repo}"