| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local setmetatable = setmetatable |
| 22 |
local pcall = pcall |
| 23 |
local json_status, json = pcall(require, "json") |
| 24 |
local io = { popen = io.popen } |
| 25 |
local pairs = pairs |
| 26 |
local assert = assert |
| 27 |
|
| 28 |
|
| 29 |
local bb = {} |
| 30 |
|
| 31 |
local bs = {OK=1, FAILED=2, RUNNING=3} |
| 32 |
local bc = {"green", "red", "yellow"} |
| 33 |
|
| 34 |
local buildbot_all = {} |
| 35 |
|
| 36 |
BB = {} |
| 37 |
BB.__index = BB |
| 38 |
|
| 39 |
function BB.create(url, builder) |
| 40 |
local b = {} |
| 41 |
setmetatable(b,BB) |
| 42 |
b.url = url |
| 43 |
b.builder = builder |
| 44 |
b.lastChecked = 0 |
| 45 |
b.lastSuccessful = 0 |
| 46 |
b.lastResult = nil |
| 47 |
b.lastError = nil |
| 48 |
return b |
| 49 |
end |
| 50 |
|
| 51 |
function BB:_queryBuildbot(build_number) |
| 52 |
local f = io.popen("curl --connect-timeout 1 "..self.url.."/json/builders/"..self.builder.."/builds/"..build_number) |
| 53 |
local jsbuilder = f:read("*all") |
| 54 |
f:close() |
| 55 |
if #jsbuilder == 0 then |
| 56 |
return false, "can't read from url" |
| 57 |
end |
| 58 |
|
| 59 |
local result_status, result = pcall(json.decode, jsbuilder, false) |
| 60 |
if not result_status then |
| 61 |
return false, "can't parse json data" |
| 62 |
end |
| 63 |
return true, result |
| 64 |
end |
| 65 |
|
| 66 |
function BB:_getBuildStatus(result) |
| 67 |
if #result['text'] > 0 then |
| 68 |
local text = result['text'] |
| 69 |
if text[1] == "build" and text[2] == "successful" and #text == 2 then |
| 70 |
|
| 71 |
return bs.OK |
| 72 |
else |
| 73 |
|
| 74 |
return bs.FAILED |
| 75 |
end |
| 76 |
else |
| 77 |
|
| 78 |
return bs.RUNNING |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
function BB:refresh() |
| 86 |
local last_pass_fail = 0 |
| 87 |
local nr = -1 |
| 88 |
local last_result |
| 89 |
local iter_counter = 0 |
| 90 |
|
| 91 |
self.lastError = nil |
| 92 |
self.lastResult = nil |
| 93 |
|
| 94 |
while nr > self.lastChecked or nr == -1 do |
| 95 |
local r_status, r = self:_queryBuildbot(nr) |
| 96 |
local s |
| 97 |
|
| 98 |
if not r_status then |
| 99 |
self.lastError = r |
| 100 |
return |
| 101 |
end |
| 102 |
|
| 103 |
s = self:_getBuildStatus(r) |
| 104 |
if not last_result then |
| 105 |
last_result = r |
| 106 |
end |
| 107 |
nr = r['number'] |
| 108 |
assert(nr > 0) |
| 109 |
if last_pass_fail == 0 and (s == bs.OK or s == bs.FAILED) then |
| 110 |
last_pass_fail = nr |
| 111 |
end |
| 112 |
if s == bs.OK then |
| 113 |
self.lastSuccessful = nr |
| 114 |
break; |
| 115 |
end |
| 116 |
nr = nr - 1 |
| 117 |
iter_counter = iter_counter + 1 |
| 118 |
if iter_counter > 10 then |
| 119 |
break; |
| 120 |
end |
| 121 |
end |
| 122 |
if last_pass_fail ~= 0 then |
| 123 |
self.lastChecked = last_pass_fail |
| 124 |
end |
| 125 |
if last_result then |
| 126 |
self.lastResult = last_result |
| 127 |
end |
| 128 |
end |
| 129 |
|
| 130 |
function BB:getLastSuccessful() |
| 131 |
return self.lastSuccessful |
| 132 |
end |
| 133 |
|
| 134 |
function BB:getCurrent() |
| 135 |
return self.lastResult['number'] |
| 136 |
end |
| 137 |
|
| 138 |
function BB:getCurrentStatus() |
| 139 |
return self:_getBuildStatus(self.lastResult) |
| 140 |
end |
| 141 |
|
| 142 |
function BB:getBuilder() |
| 143 |
return self.builder |
| 144 |
end |
| 145 |
|
| 146 |
function BB:getError() |
| 147 |
return self.lastError |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
local function getBuilderStatus(b) |
| 152 |
local s = "[" .. b:getBuilder() |
| 153 |
|
| 154 |
if not json_status then |
| 155 |
return s .. ".<span color=\"orange\">can't find libluaX.X-json</span>]" |
| 156 |
end |
| 157 |
|
| 158 |
local err = b:getError() |
| 159 |
if err then |
| 160 |
return s .. ".<span color=\"orange\">" .. err .. "</span>]" |
| 161 |
end |
| 162 |
|
| 163 |
if b:getLastSuccessful() ~= 0 then |
| 164 |
success_build_nr_str = "<span color=\"green\">".. b:getLastSuccessful() .."</span>" |
| 165 |
else |
| 166 |
success_build_nr_str = "-" |
| 167 |
end |
| 168 |
|
| 169 |
local current_build_color = bc[b:getCurrentStatus()] |
| 170 |
current_build_nr_str = "<span color=\""..current_build_color.."\">"..b:getCurrent().."</span>" |
| 171 |
|
| 172 |
if current_build_color ~= "green" then |
| 173 |
s = s .. "." .. current_build_nr_str |
| 174 |
end |
| 175 |
return s .. "." .. success_build_nr_str .. "]" |
| 176 |
end |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
local function worker(format, warg) |
| 181 |
if #bb == 0 then |
| 182 |
for i,v in pairs(warg) do |
| 183 |
bb[#bb+1] = BB.create(v["url"], v["builder"]) |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
local str = "" |
| 188 |
for i,v in pairs(bb) do |
| 189 |
v:refresh() |
| 190 |
str = str .. " " .. getBuilderStatus(v) |
| 191 |
end |
| 192 |
return {str .. " "} |
| 193 |
end |
| 194 |
|
| 195 |
|
| 196 |
setmetatable(buildbot_all, { __call = function(_, ...) return worker(...) end }) |
| 197 |
|