Debian Codegraph @

vicious/contrib/buildbot_all.lua

raw symbols SCIP
1 -- contrib/buildbot_all.lua
2 -- Copyright (C) 2012 Andrzje Bieniek <[email protected]>
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
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 = {} --list of all buildbot builders
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 -- buildbot url
43 b.builder = builder -- builder name
44 b.lastChecked = 0 -- last checked build number
45 b.lastSuccessful = 0 -- last successful build number
46 b.lastResult = nil -- last json parsed result
47 b.lastError = nil -- last error string or nil if no error
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 --successful
71 return bs.OK
72 else
73 --failed
74 return bs.FAILED
75 end
76 else
77 --in progress
78 return bs.RUNNING
79 end
80 end
81
82 -- Function queries buildbot to refresh builds status.
83 -- * if build is successful or failed it will not be queried again, number is stored in lasteChecked
84 -- * up to 10 last builds will be checked to find last successful build
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 --- there is a gap to fill in, iterate all not checked builds starting from latest
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 --successful
113 self.lastSuccessful = nr
114 break;
115 end
116 nr = nr - 1
117 iter_counter = iter_counter + 1
118 if iter_counter > 10 then --check max last 10 builds when searching for successful build
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 --check if json library was loaded correctly
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 -- {{{ Buildbot widget type
180 local function worker(format, warg)
181 if #bb == 0 then --fill up bb with builders when worker function is run for the first time
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