aboutsummaryrefslogtreecommitdiffstats
path: root/updates/web/chromium/check-chromium-extension-updates.lua
diff options
context:
space:
mode:
Diffstat (limited to 'updates/web/chromium/check-chromium-extension-updates.lua')
-rw-r--r--updates/web/chromium/check-chromium-extension-updates.lua82
1 files changed, 57 insertions, 25 deletions
diff --git a/updates/web/chromium/check-chromium-extension-updates.lua b/updates/web/chromium/check-chromium-extension-updates.lua
index 815e8a4..0312283 100644
--- a/updates/web/chromium/check-chromium-extension-updates.lua
+++ b/updates/web/chromium/check-chromium-extension-updates.lua
@@ -1,16 +1,48 @@
#!/usr/bin/env luajit
-- Checks for updated Chromium extensions against their GitHub releases/tags.
-local extensions = {
- -- NOTE: replaced by the derivation build step, with the
- -- detailed list of all extensions that we need to check
- --@@EXTENSIONS@@--
-}
+local cjson = require("cjson")
+local http_request = require("http.request")
+
+local rawExtensionsString = "@@EXTENSIONS@@"
+local extensions = cjson.decode(rawExtensionsString)
local function printf(fmt, ...)
io.write(string.format(fmt, ...))
end
+-- Performs a GET request against the GitHub API and returns the
+-- decoded JSON body, or nil plus an error message.
+local function githubGetJson(url)
+ local req = http_request.new_from_uri(url)
+ req.headers:upsert("user-agent", "check-chromium-extension-updates")
+ req.headers:upsert("accept", "application/vnd.github+json")
+
+ local headers, stream = req:go(10) -- 10s timeout
+ if not headers then
+ -- on failure, `stream` actually holds the error message
+ return nil, tostring(stream)
+ end
+
+ local body, err = stream:get_body_as_string()
+ stream:shutdown()
+ if not body then
+ return nil, err or "no response body"
+ end
+
+ local status = headers:get(":status")
+ if status ~= "200" then
+ return nil, string.format("HTTP %s", tostring(status))
+ end
+
+ local ok, decoded = pcall(cjson.decode, body)
+ if not ok then
+ return nil, "failed to decode JSON response"
+ end
+
+ return decoded, nil
+end
+
print("Checking for Chromium extension updates...")
print("----------------------------------------------")
print("")
@@ -18,36 +50,36 @@ print("")
for _, ext in ipairs(extensions) do
local isTag = ext.updateType == "tag"
local endpoint
- local jqFilter
if isTag then
endpoint = string.format("https://api.github.com/repos/%s/%s/tags", ext.owner, ext.repo)
- jqFilter = ".[0].name"
else
endpoint = string.format("https://api.github.com/repos/%s/%s/releases/latest", ext.owner, ext.repo)
- jqFilter = ".tag_name"
end
- local proc = io.popen(string.format("curl -sL --fail %q | jq -r %q", endpoint, jqFilter))
- local latestRaw = (proc and proc:read("*a") or ""):gsub("%s+$", "")
- if proc then
- proc:close()
- end
+ local data, err = githubGetJson(endpoint)
+ local latestRaw
- if latestRaw == "" or latestRaw == "null" then
- printf("FAIL [%s] Failed to fetch data from GitHub API.\n", ext.pname)
+ if not data then
+ printf("FAIL [%s] Failed to fetch data from GitHub API (%s).\n", ext.pname, err)
else
- local prefix = ext.tagPrefix
- local latest = latestRaw
- if prefix ~= "" and latestRaw:sub(1, #prefix) == prefix then
- latest = latestRaw:sub(#prefix + 1)
- end
+ latestRaw = isTag and data[1] and data[1].name or data.tag_name
- if latest ~= ext.version then
- printf("UPDATE [%s] %s -> %s\n", ext.pname, ext.version, latest)
- printf(" Repo: https://github.com/%s/%s\n", ext.owner, ext.repo)
- print("")
+ if latestRaw == nil or latestRaw == "" then
+ printf("FAIL [%s] Failed to fetch data from GitHub API.\n", ext.pname)
else
- printf("OK [%s] %s\n", ext.pname, ext.version)
+ local prefix = ext.tagPrefix
+ local latest = latestRaw
+ if prefix ~= "" and latestRaw:sub(1, #prefix) == prefix then
+ latest = latestRaw:sub(#prefix + 1)
+ end
+
+ if latest ~= ext.version then
+ printf("UPDATE [%s] %s -> %s\n", ext.pname, ext.version, latest)
+ printf(" Repo: https://github.com/%s/%s\n", ext.owner, ext.repo)
+ print("")
+ else
+ printf("OK [%s] %s\n", ext.pname, ext.version)
+ end
end
end
end