Skip to content

Commit

Permalink
lsp: Auto update examples index
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed Aug 5, 2024
1 parent 7bd66d9 commit b236cc6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 15 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/update-example-index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# this workflow is used to update the internal/lsp/examples/index.json
# file containing an index of the content available on
# http://docs.styra.com/opa/rego-by-example
name: Update Examples Index

on:
workflow_dispatch:
schedule:
- cron: '0 1 * * *' # Run daily at 1 AM UTC

jobs:
update-examples-index:
env:
RQ_VERSION: v0.0.9
name: Update Examples Index
runs-on: ubuntu-22.04
permissions:
contents: write # this workflow updates repo contents
steps:
- name: Check out code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- uses: open-policy-agent/setup-opa@34a30e8a924d1b03ce2cf7abe97250bbb1f332b5 # v2.2.0
with:
version: edge
static: true

- name: Set up go for rq
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version-file: go.mod

- name: Restore rq cache
id: cache-rq
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: ~/go/bin/rq
key: ${{ runner.os }}-${{ runner.arch }}-go-rq-${{ env.RQ_VERSION }}
- run: go install git.sr.ht/~charles/rq/cmd/rq@${{ env.RQ_VERSION }}
if: steps.cache-rq.outputs.cache-hit != 'true'
- name: Cache rq binary
if: steps.cache-rq.outputs.cache-hit != 'true'
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: ~/go/bin/rq
key: ${{ runner.os }}-${{ runner.arch }}-go-rq-${{ env.RQ_VERSION }}

- name: Fetch sitemap and convert to JSON
run: |
set -o pipefail
TEMP_DIR=$(mktemp -d)
curl -L https://docs.styra.com/sitemap.xml -o "$TEMP_DIR/sitemap.xml"
cat "$TEMP_DIR/sitemap.xml" | \
rq -i xml --indent " " | \
opa eval 'data.process.symbols' \
-d build/workflows/update-example-index/process.rego \
--format=pretty \
--stdin-input | \
tee internal/lsp/examples/index.json
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
with:
commit_message: Update Imported Docs
62 changes: 62 additions & 0 deletions build/workflows/update-example-index/process.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package process

import rego.v1

symbols := {"keywords": _keywords, "builtins": _builtins}

_keywords[name] := path if {
some p in _pages
p[0] == "keywords"

name := p[1]

path := concat("/", p)
}

_builtins[name] := path if {
some p in _pages
p[0] == "builtins"

l := count(p)

l == 2

name := p[1]

count({p |
some p in _pages
p[0] == "builtins"
p[1] == name
}) < 2

path := concat("/", p)
}

_builtins[name] := path if {
some p in _pages
p[0] == "builtins"

l := count(p)

l > 2

name := concat(
".",
[
replace(p[1], "_", "."),
concat(".", array.slice(p, 2, l)),
],
)

path := concat("/", p)
}

_prefix := "https://docs.styra.com/opa/rego-by-example/"

_pages contains page if {
some url in input.urlset.url

startswith(url.loc, _prefix)

page := split(trim_prefix(url.loc, _prefix), "/")
}
1 change: 1 addition & 0 deletions foo/sitemap.xml

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions internal/lsp/examples/index.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"builtins": {
"contains": "builtins/contains",
"io.jwt.decode_verify": "builtins/io_jwt/decode_verify",
"regex.globs_match": "builtins/regex/globs_match",
"regex.match": "builtins/regex/match",
"regex.template_match": "builtins/regex/template_match",
"time.clock": "builtins/time/clock",
"time.format": "builtins/time/format",
"time.now_ns": "builtins/time/now_ns",
"time.parse_ns": "builtins/time/parse_ns"
},
"keywords": {
"contains": "keywords/contains",
"some": "keywords/some"
}
"builtins": {
"contains": "builtins/contains",
"io.jwt.decode_verify": "builtins/io_jwt/decode_verify",
"regex.globs_match": "builtins/regex/globs_match",
"regex.match": "builtins/regex/match",
"regex.template_match": "builtins/regex/template_match",
"time.clock": "builtins/time/clock",
"time.format": "builtins/time/format",
"time.now_ns": "builtins/time/now_ns",
"time.parse_ns": "builtins/time/parse_ns"
},
"keywords": {
"contains": "keywords/contains",
"some": "keywords/some"
}
}

0 comments on commit b236cc6

Please sign in to comment.