Skip to content

Commit

Permalink
Fix webhook processing
Browse files Browse the repository at this point in the history
  • Loading branch information
cmil committed Feb 10, 2024
1 parent 2e6eb07 commit f3c720f
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions jobs/process-webhook-delivery.xq
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ declare function local:update (
let $collection := substring(
$target, 1, string-length($target) - string-length($filename) - 1
)
let $l := util:log("info", "Fetching " || $source)
let $l := util:log-system-out("fetching " || $source)
let $response := local:gh-request("get", $source)
let $status := string($response[1]/@status)

Expand All @@ -60,17 +60,19 @@ declare function local:update (
(: FIXME: make sure $data is valid TEI :)

return if ($data) then
if (
util:log("info", "Updating " || $target),
xmldb:store(
$collection,
xmldb:encode-uri($filename),
$data
)
) then true() else false()
try {
util:log-system-out("webhook update: " || $target),
xmldb:create-collection("/", $collection) and
xmldb:store($collection, xmldb:encode-uri($filename), $data)
} catch * {
util:log-system-out($err:description),
false()
}
else false()
else (
util:log("warn", "Fetching " || $source || " failed. Status: " || $status),
util:log-system-out(
"Failed to fetch " || $source || "; status: " || $status
),
false()
)
};
Expand All @@ -82,9 +84,12 @@ declare function local:remove ($file as xs:string) as xs:boolean {
)
return
try {
(xmldb:remove($collection, $filename), true())
if ($filename = "tei.xml") then
(xmldb:remove($collection), true())
else
(xmldb:remove($collection, $filename), true())
} catch * {
util:log("warn", $err:description), false()
util:log-system-out($err:description), false()
}
};

Expand All @@ -93,6 +98,18 @@ declare function local:make-url ($template, $path) {
return $url
};

declare function local:make-target ($path, $corpusname) {
$config:corpora-root || '/' || $corpusname || '/' || (
if ($path = "corpus.xml")
then $path
else replace(
replace($path, '^' || $config:corpus-repo-prefix || '/', ''),
"\.xml$",
"/tei.xml"
)
)
};

declare function local:get-repo-contents ($url-template) {
let $url := local:make-url($url-template, $config:corpus-repo-prefix)
let $response := local:gh-request("get", $url)
Expand All @@ -104,7 +121,7 @@ declare function local:process-delivery () {
let $delivery := collection($config:webhook-root)
/delivery[@id = $local:delivery and not(@processed)]
let $repo := $delivery/@repo/string()
let $corpus := collection($config:data-root)//tei:teiCorpus[
let $corpus := collection($config:corpora-root)//tei:teiCorpus[
tei:teiHeader//tei:publicationStmt/tei:idno[@type="repo" and . = $repo]
]

Expand All @@ -127,9 +144,11 @@ declare function local:process-delivery () {
let $source := if ($path = "corpus.xml")
then local:make-url($contents-url, $path)
else $contents?*[?type = "file" and ?path = $path]?git_url
let $target := $config:data-root || '/' || $corpusname || '/'
|| replace($path, '^' || $config:corpus-repo-prefix || '/', '')
let $target := local:make-target($path, $corpusname)
let $action := $file/@action
let $log := util:log-system-out(
"action: "||$action||" target: "||$target||" source: "||$source
)

let $result := if ($action = "remove") then
local:remove($target)
Expand Down

0 comments on commit f3c720f

Please sign in to comment.