|
1 | 1 | #!/bin/bash
|
2 |
| -# Returns a list of page preview URLs for use with Prow CI jobs |
| 2 | +# Returns a list of page preview URLs |
3 | 3 | # To run locally, clean the _preview folder and do a new asciibinder build before running the script
|
| 4 | +# To run in Prow CI, run with |
| 5 | +# ./get-updated-preview-urls.sh $PULL_NUMBER |
4 | 6 |
|
5 | 7 | # Check if jq is installed
|
6 | 8 | hash jq 2>/dev/null || { echo >&2 "Error: jq is not installed"; exit 1; }
|
7 | 9 |
|
8 |
| -pr_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 10 | +# Set $pull_number if it is not passed as variable |
| 11 | +if [ $# -eq 0 ]; then |
9 | 12 | commit_id=$(git log -n 1 --pretty=format:"%H")
|
10 |
| -pr_number="$(curl -s "https://api.github.com/search/issues?q=$commit_id" | jq '.items[0].number')" |
| 13 | +pull_number="$(curl -s "https://api.github.com/search/issues?q=$commit_id" | jq '.items[0].number')" |
| 14 | +pr_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 15 | +else |
| 16 | + pull_number=$1 |
| 17 | + pr_branch="latest" |
| 18 | +fi |
| 19 | + |
11 | 20 | preview_url_slug="ocpdocs-pr"
|
12 |
| -preview_url="https://${pr_number}--${preview_url_slug}.netlify.app" |
| 21 | +preview_url="https://${pull_number}--${preview_url_slug}.netlify.app" |
13 | 22 | assemblies=()
|
14 | 23 | pages=()
|
15 | 24 | files=$(git diff --name-only HEAD~1 HEAD --diff-filter=AMRD "*.adoc" ':(exclude)_unused_topics/*')
|
|
25 | 34 | # Search for $file references in all *.adoc files that are not in a folder called modules/, snippets/, or _unused_topics/
|
26 | 35 | for file in $files; do
|
27 | 36 | include_ref="include::$file"
|
28 |
| - found_file=$(find . -name '*.adoc' -not -path "./modules/*" -not -path "./snippets/*" -not -path "./_unused_topics/*" -exec grep -rl "^$include_ref" {} +) |
| 37 | + found_file=$(find . -name '*.adoc' -not -path "modules/*" -not -path "snippets/*" -not -path "_unused_topics/*" -exec grep -rl "^$include_ref" {} +) |
| 38 | + # Add the found updated assemblies, not directly included in PR |
| 39 | + assemblies+=("$found_file") |
| 40 | + # If not found, then it is a directly updated assembly file |
29 | 41 | if [ -z "$found_file" ]; then
|
30 | 42 | assemblies+=("$file")
|
31 | 43 | fi
|
32 | 44 | done
|
33 | 45 |
|
34 | 46 | # Make the HTML URL slug
|
35 | 47 | if [ ${#assemblies[@]} -gt 0 ]; then
|
36 |
| - updated_pages=$(echo "${assemblies[@]}" | xargs -n1 basename | sed 's/\.adoc$/.html/' | sort | uniq) |
| 48 | + updated_pages=$(echo "${assemblies[@]}" | sed 's/\.adoc$/.html/' | sort | uniq) |
37 | 49 | else
|
38 | 50 | # No updated pages, just add default URL
|
39 | 51 | pages+=("${preview_url}")
|
40 | 52 | fi
|
41 | 53 |
|
42 | 54 | # Search built_pages for every entry in updated_pages and add to pages array when it is found
|
43 | 55 | for updated_page in $updated_pages; do
|
44 |
| - # sed $pr_branch > "latest" to match the Prow build URL |
45 |
| - found_page=$(echo "${built_pages}" | grep "${updated_page}" | sed "s/$pr_branch/latest/") |
46 |
| - pages+=("${preview_url}/${found_page}") |
| 56 | + # sed s/$pr_branch/latest to match the Prow build URL |
| 57 | + found_page=$(echo "${built_pages}" | grep "${updated_page}" | sed "s|/$pr_branch/|/latest/|") |
| 58 | + pages+=("${found_page}") |
47 | 59 | done
|
48 | 60 |
|
49 |
| -printf '%s\n' "${pages[@]}" | sort | uniq |
| 61 | +#Echo the results, prepending the $preview_url to every line |
| 62 | +echo "${pages[@]}" | tr ' ' '\n' | sort | uniq | sed "s|^|$preview_url/|" |
0 commit comments