Skip to content

Commit dfe21e6

Browse files
authoredAug 5, 2024··
lsp: Auto update examples index (#956)
Signed-off-by: Charlie Egan <charlie@styra.com>
1 parent 7bd66d9 commit dfe21e6

File tree

4 files changed

+145
-15
lines changed

4 files changed

+145
-15
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# this workflow is used to update the internal/lsp/examples/index.json
2+
# file containing an index of the content available on
3+
# http://docs.styra.com/opa/rego-by-example
4+
name: Update Examples Index
5+
6+
on:
7+
workflow_dispatch:
8+
schedule:
9+
- cron: '0 1 * * *' # Run daily at 1 AM UTC
10+
11+
jobs:
12+
update-examples-index:
13+
env:
14+
RQ_VERSION: v0.0.9
15+
name: Update Examples Index
16+
runs-on: ubuntu-22.04
17+
permissions:
18+
contents: write # this workflow updates repo contents
19+
steps:
20+
- name: Check out code
21+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
22+
23+
- uses: open-policy-agent/setup-opa@34a30e8a924d1b03ce2cf7abe97250bbb1f332b5 # v2.2.0
24+
with:
25+
version: edge
26+
static: true
27+
28+
- name: Set up go for rq
29+
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
30+
with:
31+
go-version-file: go.mod
32+
33+
- name: Restore rq cache
34+
id: cache-rq
35+
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
36+
with:
37+
path: ~/go/bin/rq
38+
key: ${{ runner.os }}-${{ runner.arch }}-go-rq-${{ env.RQ_VERSION }}
39+
- run: go install git.sr.ht/~charles/rq/cmd/rq@${{ env.RQ_VERSION }}
40+
if: steps.cache-rq.outputs.cache-hit != 'true'
41+
- name: Cache rq binary
42+
if: steps.cache-rq.outputs.cache-hit != 'true'
43+
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
44+
with:
45+
path: ~/go/bin/rq
46+
key: ${{ runner.os }}-${{ runner.arch }}-go-rq-${{ env.RQ_VERSION }}
47+
48+
- name: Fetch sitemap and convert to JSON
49+
run: |
50+
set -o pipefail
51+
52+
TEMP_DIR=$(mktemp -d)
53+
54+
curl -L https://docs.styra.com/sitemap.xml -o "$TEMP_DIR/sitemap.xml"
55+
56+
cat "$TEMP_DIR/sitemap.xml" | \
57+
rq -i xml --indent " " | \
58+
opa eval 'data.process.symbols' \
59+
-d build/workflows/update-example-index/process.rego \
60+
--format=pretty \
61+
--stdin-input | \
62+
tee internal/lsp/examples/index.json
63+
64+
- name: Commit and push changes
65+
uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
66+
with:
67+
commit_message: Update Imported Docs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package process
2+
3+
import rego.v1
4+
5+
symbols := {"keywords": _keywords, "builtins": _builtins}
6+
7+
_keywords[name] := path if {
8+
some p in _pages
9+
p[0] == "keywords"
10+
11+
name := p[1]
12+
13+
path := concat("/", p)
14+
}
15+
16+
_builtins[name] := path if {
17+
some p in _pages
18+
p[0] == "builtins"
19+
20+
l := count(p)
21+
22+
l == 2
23+
24+
name := p[1]
25+
26+
count({p |
27+
some p in _pages
28+
p[0] == "builtins"
29+
p[1] == name
30+
}) < 2
31+
32+
path := concat("/", p)
33+
}
34+
35+
_builtins[name] := path if {
36+
some p in _pages
37+
p[0] == "builtins"
38+
39+
l := count(p)
40+
41+
l > 2
42+
43+
name := concat(
44+
".",
45+
[
46+
replace(p[1], "_", "."),
47+
concat(".", array.slice(p, 2, l)),
48+
],
49+
)
50+
51+
path := concat("/", p)
52+
}
53+
54+
_prefix := "https://docs.styra.com/opa/rego-by-example/"
55+
56+
_pages contains page if {
57+
some url in input.urlset.url
58+
59+
startswith(url.loc, _prefix)
60+
61+
page := split(trim_prefix(url.loc, _prefix), "/")
62+
}

‎foo/sitemap.xml

+1
Large diffs are not rendered by default.

‎internal/lsp/examples/index.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"builtins": {
3-
"contains": "builtins/contains",
4-
"io.jwt.decode_verify": "builtins/io_jwt/decode_verify",
5-
"regex.globs_match": "builtins/regex/globs_match",
6-
"regex.match": "builtins/regex/match",
7-
"regex.template_match": "builtins/regex/template_match",
8-
"time.clock": "builtins/time/clock",
9-
"time.format": "builtins/time/format",
10-
"time.now_ns": "builtins/time/now_ns",
11-
"time.parse_ns": "builtins/time/parse_ns"
12-
},
13-
"keywords": {
14-
"contains": "keywords/contains",
15-
"some": "keywords/some"
16-
}
2+
"builtins": {
3+
"contains": "builtins/contains",
4+
"io.jwt.decode_verify": "builtins/io_jwt/decode_verify",
5+
"regex.globs_match": "builtins/regex/globs_match",
6+
"regex.match": "builtins/regex/match",
7+
"regex.template_match": "builtins/regex/template_match",
8+
"time.clock": "builtins/time/clock",
9+
"time.format": "builtins/time/format",
10+
"time.now_ns": "builtins/time/now_ns",
11+
"time.parse_ns": "builtins/time/parse_ns"
12+
},
13+
"keywords": {
14+
"contains": "keywords/contains",
15+
"some": "keywords/some"
16+
}
1717
}

0 commit comments

Comments
 (0)
Please sign in to comment.