summaryrefslogtreecommitdiffstats
path: root/usr/share/build-errors/save-build-error.lua
blob: 80b2040c1a166166573e2c373d88f994ed528eb6 (plain)
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
#!/usr/bin/lua

local db = '/var/spool/build-errors/sqlite/build-errors.db'

cjson = require "cjson" 
sqlite3 = require "lsqlite3"

local input = io.stdin:read()
-- temporary
-- input = '{"hostname":"build-edge-armhf","pkgname":"msmtp","logurl":"http://build.alpinelinux.org/buildlogs/build-edge-armhf/main/msmtp/msmtp-1.6.3-r0.log"}'

local function try_decoding_json(json)
	return pcall(function () return cjson.decode(json) end)
end

local function splitOnFirstSpace(str)
	local space = str:find(' ') or (#str + 1)
	return str:sub(1, space-1), str:sub(space+1)
end

topic,msg = splitOnFirstSpace(input)
if msg ~= "" then 
	ok,d = try_decoding_json(msg)
end
if not ok then d = {comment=input} end
if not d.pkgname then d.pkgname = "?" end
if not d.hostname then d.hostname = "?" end
if not d.reponame then d.reponame = "?" end
if not d.logurl then d.logurl = "?" end
if not d.comment then d.comment = "" end

for k, v in pairs(d) do
	d[k] = string.gsub(d[k], "'", "''")
end

-- for k, v in pairs(d) do print(string.format("%s=%s", tostring(k), tostring(v))) end

db = sqlite3.open(db)
local query = "\
	insert into build_errors (\
		hostname,\
		pkgname,\
		reponame,\
		logurl,\
		comment\
	)\
	values (\
		'"..d.hostname.."',\
		'"..d.pkgname.."',\
		'"..d.reponame.."',\
		'"..d.logurl.."',\
		'"..d.comment.."'\
	);\
"
db:exec(query)
db:close()