-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathget-updated-preview-urls.sh
executable file
·61 lines (53 loc) · 2.21 KB
/
get-updated-preview-urls.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# Returns a list of page preview URLs
# To run locally, clean the _preview folder and do a new asciibinder build before running the script
# To run in Prow CI, run with
# ./get-updated-preview-urls.sh $PULL_NUMBER
# Check if jq is installed
hash jq 2>/dev/null || { echo >&2 "Error: jq is not installed"; exit 1; }
# Set $pull_number if it is not passed as variable
if [ $# -eq 0 ]; then
commit_id=$(git log -n 1 --pretty=format:"%H")
pull_number="$(curl -s "https://api.github.com/search/issues?q=$commit_id" | jq '.items[0].number')"
pr_branch="$(git rev-parse --abbrev-ref HEAD)"
else
pull_number=$1
pr_branch="latest"
fi
preview_url_slug="ocpdocs-pr"
preview_url="https://${pull_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 HTML build 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
include_ref="include::$file"
found_file=$(grep -rl --include='*.adoc' --exclude-dir={modules,snippets,_unused_topics} "^$include_ref")
# Add the found updated assemblies, not directly included in PR
assemblies+=("$found_file")
# If not found, then it is a directly updated assembly file
if [ -z "$found_file" ]; then
assemblies+=("$file")
fi
done
# Make the HTML URL slugs
if [ ${#assemblies[@]} -gt 0 ]; then
for assembly in "${assemblies[@]}"; do
updated_pages+=$(echo "$assembly" | sed 's/\.adoc$/.html/' | tr '\n' ' ')
done
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
# sed s/$pr_branch/latest to match the Prow build URL
found_page=$(echo "${built_pages}" | grep "${updated_page}" | sed "s|/$pr_branch/|/latest/|")
pages+=("${found_page}")
done
#Echo the results, prepending the $preview_url to every line
echo "${pages[@]}" | tr ' ' '\n' | sort | uniq | sed "s|^|$preview_url/|"