pico-engine
pico-engine copied to clipboard
GitHub is a bit awkward as a code repository for installing rulesets
The title is odd, because GitHub is, above all else, a code repository. The awkwardness comes from:
- We have to use the Raw version of a
krl
file, and GitHub takes some time to make that available after a commit. - In private/protected repos, the Raw URL has a
token
with a relatively short TTL, and it is different from one press of the Raw button to another.
These considerations mean that the Flush button in the Rulesets tab isn't useful. The options are:
- flush using a pico engine api call from Postman, or a browser, or maybe with
http:get
(haven't tried that) No, that doesn't work because it is also based on the URL from which the ruleset was installed, and that will have changed! - install the ruleset instead of flushing it (beware that this could affect/re-initialize entity variables (use with care))
As a workaround, I am introducing this ruleset into a protected repo, having installed it in the root pico (which doesn't otherwise participate in the application). It then can serve up KRL files.
ruleset code-repo {
meta {
use module io.picolabs.wrangler alias wrangler
shares code
}
global {
code = function(rid){
ent:code >< rid => ent:code.get(rid) |
rid.match(valid_rid) => "ruleset "+rid+" {}" |
""
}
tags = ["code-repo"]
rs_event_domain = "code_repo"
valid_rid = re#(^\w[\w\d-.]+)$#
}
rule stashCode {
select when code_repo new_ruleset
rid re#(^\w[\w\d-.]+)$# setting(rid)
fired {
ent:code{rid} := event:attrs{"krl"}
}
}
rule initialize {
select when wrangler ruleset_installed where event:attrs{"rids"} >< meta:rid
pre {
chan = wrangler:channels(tags).head()
}
if chan.isnull() then
wrangler:createChannel(
tags,
{"allow":[{"domain":rs_event_domain,"name":"*"}],"deny":[]},
{"allow":[{"rid":meta:rid,"name":"*"}],"deny":[]}
)
}
}
Then a URL to get code would look like:
http://DOMAIN:PORT/c/ECI/query/code-repo/code.txt?rid=html
assuming one had raised the code_repo:new_ruleset
event with the corresponding event attributes previously.
I suppose one could use a GitHub post commit action to update by raising that same event.
Have tested this with a couple of rulesets, and it works. Waiting for a change to see how well Flush works.
The one proviso is that it cannot be used to hold/install a ruleset into itself. Hence using a pico that doesn't otherwise participate in any way in the application.
While the new ruleset was written in the context of a protected repo, it leaks no information from that project, and is general-purpose. It will be maintained in the PicoStack repo.