Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(na): add manual link checker scripts #1769

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/scripts/check.urls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# Function to get HTTP response code of a URL
get_response_code() {
local response_code
local url=$1
response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
echo "$response_code"
}

# Function to check for meta refresh tag in HTML content
check_meta_refresh() {
local html_content=$1
url=$2
if grep -q '<meta http-equiv="refresh"' <<< "$html_content"; then
local redirect_url
local redirect_response_code
redirect_url=$(grep -oP 'url=[^"]+' <<< "$html_content" | cut -d'=' -f2)
redirect_response_code=$(get_response_code "$redirect_url")
echo "${url} Is redirected! Result is:"
echo " -> $redirect_url $redirect_response_code "
fi
}

run_checks(){
echo
prod_urls=$1
# Loop through each URL in the file
while IFS= read -r url; do
# Get HTTP response code, if it's not 200, print it so they know
response_code=$(get_response_code "$url")
if [ "$response_code" -ne 200 ]; then
echo "$url $response_code"
fi

# If response code is 200, check for meta refresh tag
if [ "$response_code" -eq 200 ]; then
html_content=$(curl -s "$url")
check_meta_refresh "$html_content" "$url"
fi
done <<< "$prod_urls"
}

get_urls_from_prod_site_map(){
local urls
# thanks https://aruljohn.com/blog/download-extract-urls-sitemaps/
urls=$(curl -qs https://docs.communityhealthtoolkit.org/sitemap.xml 2>&1 | grep -o "<loc>[^<]*" | sed -e 's/<[^>]*>//g')
urls="${urls//https:\/\/docs.communityhealthtoolkit.org/http:\/\/localhost:1313}"
echo "$urls"
}

echo;echo "Are you on a test branch and is hugo running on http://localhost:1313 ?";echo
read -p "\"y\" to proceed, \"n\" to cancel " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[nN]$ ]]
then
prod_urls=$(get_urls_from_prod_site_map)
url_count=$(echo "$prod_urls" | wc -l | cut -d' ' -f1)
run_checks "$prod_urls"
echo;echo "Successfully checked ${url_count} URLs!"
fi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tech-doc-hugo
.DS_Store
.idea
.hugo_build.lock
urls.txt