diff --git a/.github/workflows/update-example-index.yaml b/.github/workflows/update-example-index.yaml new file mode 100644 index 000000000..63856f6d3 --- /dev/null +++ b/.github/workflows/update-example-index.yaml @@ -0,0 +1,57 @@ +# 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 + pull_request: + +jobs: + update-examples-index: + 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 + + - name: Install xq + run: | + XQ_COMMIT=11673e94cabba145135092bb39200eafd9960cef + curl -sSL "https://raw.githubusercontent.com/sibprogrammer/xq/$XQ_COMMIT/scripts/install.sh" \ + | sudo bash + + - uses: open-policy-agent/setup-opa@34a30e8a924d1b03ce2cf7abe97250bbb1f332b5 # v2.2.0 + with: + version: edge + static: true + + - 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" + + xq . "$TEMP_DIR/sitemap.xml" > "$TEMP_DIR/output.json" + + echo "catting JSON" + cat "$TEMP_DIR/output.json" + + cat "$TEMP_DIR/output.json" | \ + opa eval 'data.process.symbols' \ + -d build/workflows/update-example-index/process.rego \ + --format=raw \ + --stdin-input \ + | jq \ + | tee internal/lsp/examples/index.json + +# - name: Commit and push changes +# uses: stefanzweifel/git-auto-commit-action@v4 +# with: +# commit_message: Update Imported Docs diff --git a/build/workflows/update-example-index/process.rego b/build/workflows/update-example-index/process.rego new file mode 100644 index 000000000..262fd9316 --- /dev/null +++ b/build/workflows/update-example-index/process.rego @@ -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] + + not count({p | + some p in _pages + p[0] == "builtins" + p[1] == name + }) > 1 + + 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), "/") +} diff --git a/internal/lsp/examples/index.json b/internal/lsp/examples/index.json index d752eb3a5..908ed45d2 100644 --- a/internal/lsp/examples/index.json +++ b/internal/lsp/examples/index.json @@ -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" + } }