diff --git a/docs/website/edge/badge.ts b/docs/website/edge/badge.ts new file mode 100644 index 0000000000..af039e1156 --- /dev/null +++ b/docs/website/edge/badge.ts @@ -0,0 +1,29 @@ +import { Context } from "netlify:edge"; + +const schemaVersion = "1"; +const label = "OPA"; +const releases = "https://api.github.com/repos/open-policy-agent/opa/releases/latest"; +const endpoint = "/badge-endpoint/"; + +// this is static/img/opa-logo.svg +const logoSvg = ''; + +export default async (req: Request, context: Context) => { + const url = new URL(req.url); + + const version = url.pathname.slice(endpoint.length); // "/badge-endpoint/v0.46.1" => "v0.46.1" + + const latest = await fetch(releases) + .then((response) => response.json()) + .then((data) => data.tag_name); + + const res = { + schemaVersion, + label, + logoSvg, + message: version, + color: latest == version ? "green" : "yellow", + }; + + return context.json(res); +}; diff --git a/docs/website/layouts/index.redirects b/docs/website/layouts/index.redirects index 911e9fde06..ad8cefc986 100644 --- a/docs/website/layouts/index.redirects +++ b/docs/website/layouts/index.redirects @@ -51,3 +51,6 @@ {{- end }} {{- end }} {{- end }} + +/badge/:tag style=:style https://img.shields.io/endpoint?url=https://openpolicyagent.org/badge-endpoint/:tag&style=:style +/badge/:tag https://img.shields.io/endpoint?url=https://openpolicyagent.org/badge-endpoint/:tag \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index ad1d2bcb2e..fdb0140274 100644 --- a/netlify.toml +++ b/netlify.toml @@ -2,7 +2,11 @@ publish = "docs/website/public" command = "make netlify-prod WASM_ENABLED=0 CGO_ENABLED=0" ignore = "./build/docs-build-needed.sh" +edge_functions = "docs/website/edge" +[[edge_functions]] +path = "/badge-endpoint/*" +function = "badge" [build.environment] HUGO_VERSION = "0.88.1"