diff --git a/docs/Makefile b/docs/Makefile index a24d3c16bb..740e7a6f0d 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -35,9 +35,10 @@ install-deps: live-blocks-install-deps # The live-blocks-% pattern target will shim to the # npm scripts in ./website/scripts/live-blocks +# NOTE(sr): we'll avoid an API call if we make use of releases.yaml to determine the latest release .PHONY: live-blocks-% live-blocks-%: - cd $(CURDIR)/website/scripts/live-blocks && npm run $* + cd $(CURDIR)/website/scripts/live-blocks && LATEST=$(shell sed -n '2s/- //p' $(CURDIR)/website/data/releases.yaml) npm run $* .PHONY: serve-local serve-local: dev-build @@ -69,7 +70,8 @@ hugo-production-build: --minify .PHONY: production-build -production-build: clean generate hugo-production-build live-blocks-inject +production-build: clean generate hugo-production-build + make live-blocks-inject .PHONY: preview-build preview-build: diff --git a/docs/website/scripts/live-blocks/src/preprocess/acquireOPAVersion.js b/docs/website/scripts/live-blocks/src/preprocess/acquireOPAVersion.js index a9d606626f..e3caa97b18 100644 --- a/docs/website/scripts/live-blocks/src/preprocess/acquireOPAVersion.js +++ b/docs/website/scripts/live-blocks/src/preprocess/acquireOPAVersion.js @@ -59,7 +59,7 @@ function createAcquirer(version) { throw new Error(`binary for ${VERSION_EDGE} is not available at path ${path}, ensure it has been built for the current platform`) } - const assetURL = await getReleaseAssetURL(version) + const assetURL = getReleaseAssetURL(version); let file try { @@ -89,30 +89,13 @@ function pathToVersion(version) { return path.resolve(OPA_CACHE_PATH, `${version}-${PLATFORM}${PLATFORM === PLATFORMS.WINDOWS ? '.exe' : ''}`) } -// Gets the URL that can be used to download a given version of OPA for the current platform. May error with a user-friendly message. -async function getReleaseAssetURL(version) { - // Releases are on GitHub - let releaseURL; +// Gets the URL that can be used to download a given version of OPA for the current platform. +// Releases are on GitHub, URLs are predictable +function getReleaseAssetURL(version) { if (version === VERSION_LATEST) { - releaseURL = `https://api.github.com/repos/open-policy-agent/opa/releases/latest` - } else { - releaseURL = `https://api.github.com/repos/open-policy-agent/opa/releases/tags/${version}`; + version = process.env.LATEST; } - - try { - const release = await (await fetch(releaseURL)).json() - for (let asset of Object.values(release.assets)) { - if (asset.name.indexOf(PLATFORM) != -1) { - if (asset.browser_download_url) { // If it's actually set, return it - return asset.browser_download_url - } - // Implicit else, throw error below. - } - } - } catch (e) { - throw new ChainedError(`error occurred while getting the OPA release asset URL for ${version} on ${PLATFORM} from ${releaseURL}`, e) - } - throw new Error(`unable to get the OPA release asset URL for ${version} on ${PLATFORM} from ${releaseURL}`) + return `https://github.com/open-policy-agent/opa/releases/download/${version}/opa_${PLATFORM}_${GOARCH}${PLATFORM === PLATFORMS.WINDOWS ? '.exe' : ''}`; } // Determines, based on the path to where it should be downloaded, whether it needs to be downloaded. Will not throw an error. diff --git a/docs/website/scripts/live-blocks/src/preprocess/index.js b/docs/website/scripts/live-blocks/src/preprocess/index.js index 2cd9716094..d163638903 100644 --- a/docs/website/scripts/live-blocks/src/preprocess/index.js +++ b/docs/website/scripts/live-blocks/src/preprocess/index.js @@ -79,7 +79,10 @@ async function processFile(path) { function getVersion(path) { const versionMatch = (new RegExp(`/(v[^/]*|${VERSION_EDGE}|${VERSION_LATEST})/`)).exec(filepath.resolve(path)) if (!versionMatch) { - return VERSION_LATEST + if (process.env.LATEST) { + return VERSION_LATEST + } + return VERSION_EDGE } return versionMatch[1]