summaryrefslogtreecommitdiffstats
path: root/var/lib/build-errors/scripts/save-build-error.lua
diff options
context:
space:
mode:
Diffstat (limited to 'var/lib/build-errors/scripts/save-build-error.lua')
-rw-r--r--var/lib/build-errors/scripts/save-build-error.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/var/lib/build-errors/scripts/save-build-error.lua b/var/lib/build-errors/scripts/save-build-error.lua
new file mode 100644
index 0000000..12d489a
--- /dev/null
+++ b/var/lib/build-errors/scripts/save-build-error.lua
@@ -0,0 +1,56 @@
+#!/usr/bin/lua
+
+local dbfile = '/var/lib/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
+
+local db = sqlite3.open(dbfile)
+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()