From b0840cbfd246ff3d3e14fea19d60436d44f4c0e1 Mon Sep 17 00:00:00 2001 From: Aidan Reilly <74046732+aireilly@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:51:27 +0000 Subject: [PATCH] Adding get updated preview URLs script updates --- scripts/get-updated-preview-urls.sh | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 scripts/get-updated-preview-urls.sh diff --git a/scripts/get-updated-preview-urls.sh b/scripts/get-updated-preview-urls.sh new file mode 100755 index 0000000000..e0c8ca5d8c --- /dev/null +++ b/scripts/get-updated-preview-urls.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# Returns a list of page preview URLs + +# Check if jq is installed +hash jq 2>/dev/null || { echo >&2 "Error: jq is not installed"; exit 1; } + +pr_branch="$(git rev-parse --abbrev-ref HEAD)" +commit_id=$(git log -n 1 --pretty=format:"%H") +pr_number="$(curl -s "https://api.github.com/search/issues?q=$commit_id" | jq '.items[0].number')" +preview_url_slug="ocpdocs-pr" +preview_url="https://${pr_number}--${preview_url_slug}.netlify.app" +assemblies=() +pages=() +files=$(git diff --name-only HEAD~1 HEAD --diff-filter=AMRD "*.adoc" ':(exclude)_unused_topics/*') + +# Get the full list of built files +if [ -e "_preview" ]; then + built_pages=$(find _preview -type f -name "*.html" -printf "%P\n") +else + echo "_preview output folder not found" + exit 1 +fi + +# Search for $file references in all *.adoc files that are not in a folder called modules/, snippets/, or _unused_topics/ +for file in $files; do + found_file=$(find . -name '*.adoc' -not -path "./modules/*" -not -path "./snippets/*" -not -path "./_unused_topics/*" -exec grep -rl "$file" {} +) + assemblies+=("$found_file") +done + +# Make the html URL slug +if [ ${#assemblies[@]} -gt 0 ]; then + updated_pages=$(echo "${assemblies[@]}" | xargs -n1 basename | sed 's/\.adoc$/.html/' | sort | uniq) +fi + +# Search built_pages for every entry in updated_pages and add to pages array when it is found +for updated_page in $updated_pages; do + # Note also need to sed $pr_branch > "latest" + found_page=$(echo "${built_pages}" | grep "${updated_page}" | sed "s/$pr_branch/latest/") + pages+=("${preview_url}/${found_page}") +done + +printf '%s\n' "${pages[@]}"