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

Add warning if a site URL is not set #48

Merged
merged 10 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ runs:
run: echo "${{ inputs.config }}" >> hyde.yml
shell: bash

- name: Check site URL configuration
run: bash check-site-url.sh
shell: bash

# TODO: Add pre-build hook to run some bash commands?

- name: Build the site
Expand Down
8 changes: 8 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,11 @@ Sets the `SITE_URL` environment variable
> Deprecated: Use the `env` input instead with `TORCHLIGHT_TOKEN=value`

Sets the `TORCHLIGHT_TOKEN` environment variable

### Site URLs

It's highly recommended to set the site URL for your project so that sitemaps and meta tags are generated correctly.

You can do this by setting the `SITE_URL` environment variable, or by adding the `url` key to your `hyde.yml` file or `config/hyde.php` file.

If the site URL is not set, the action will output a warning and suggest a solution. An easy way to set the site URL is to use the `env` input as seen above.
37 changes: 37 additions & 0 deletions src/check-site-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Function to check if SITE_URL is effectively set
check_site_url() {
# Check if SITE_URL is set in .env file
if grep -q "^SITE_URL=" .env; then
return 0
fi

# Check for hyde.yml or hyde.yaml with any url setting
if [ -f "hyde.yml" ] && grep -q "^[[:space:]]*url:" hyde.yml; then
return 0
fi

if [ -f "hyde.yaml" ] && grep -q "^[[:space:]]*url:" hyde.yaml; then
return 0
fi

# If config/hyde.php exists, check if it has a non-localhost URL
if [ -f "config/hyde.php" ]; then
# Look for url configuration line and exclude localhost
if grep -q "'url' => env('SITE_URL'," config/hyde.php; then
line_number=$(grep -n "'url' => env('SITE_URL'," config/hyde.php | cut -d: -f1)
if ! grep -q "'url' => env('SITE_URL', 'http://localhost')," config/hyde.php; then
return 0
fi
echo "::warning file=config/hyde.php,line=$line_number,title=Missing Site URL::The site URL is set to localhost in your configuration file. Consider setting a production URL here or in using the 'env' input with 'SITE_URL=https://example.com'"
return 0
fi
fi

echo "::warning title=Missing Site URL::No SITE_URL environment variable found. It's recommended to set a production URL for your site. You can set it using the 'env' input with 'SITE_URL=https://example.com'"
return 0
}

# Show warning if SITE_URL is not properly set
check_site_url
97 changes: 97 additions & 0 deletions tests/unit/test-check-site-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash

test_site_url_warnings() {
base_dir="$(pwd)"
temp_dir=$(mktemp -d)
cd "$temp_dir"

# Test case 1: No SITE_URL set anywhere
touch .env
output=$(bash "$base_dir/src/check-site-url.sh")
expected="::warning title=Missing Site URL::No SITE_URL environment variable found. It's recommended to set a production URL for your site. You can set it using the 'env' input with 'SITE_URL=https://example.com'"

if [[ "$output" != "$expected" ]]; then
echo "Test 1 failed: Warning not shown when SITE_URL is missing"
echo "Expected: $expected"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

# Test case 2: SITE_URL is set in .env
echo "SITE_URL=https://example.com" > .env
output=$(bash "$base_dir/src/check-site-url.sh")

if [[ -n "$output" ]]; then
echo "Test 2 failed: Warning shown when SITE_URL is set"
echo "Expected empty output"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

# Test case 3: SITE_URL set in config with non-localhost value
rm .env
mkdir -p config
echo "'url' => env('SITE_URL', 'https://example.com')," > config/hyde.php
output=$(bash "$base_dir/src/check-site-url.sh")

if [[ -n "$output" ]]; then
echo "Test 3 failed: Warning shown when SITE_URL is set in config"
echo "Expected empty output"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

# Test case 4: SITE_URL in config but with localhost
echo "'url' => env('SITE_URL', 'http://localhost')," > config/hyde.php
output=$(bash "$base_dir/src/check-site-url.sh")
expected="::warning file=config/hyde.php,line=1,title=Missing Site URL::The site URL is set to localhost in your configuration file. Consider setting a production URL here or in using the 'env' input with 'SITE_URL=https://example.com'"

if [[ "$output" != "$expected" ]]; then
echo "Test 4 failed: Warning not shown when SITE_URL is localhost"
echo "Expected: $expected"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

# Test case 5: URL set in hyde.yml
rm config/hyde.php
echo "url: http://localhost" > hyde.yml
output=$(bash "$base_dir/src/check-site-url.sh")

if [[ -n "$output" ]]; then
echo "Test 5 failed: Warning shown when URL is set in hyde.yml"
echo "Expected empty output"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

# Test case 6: URL set in hyde.yaml with indentation
rm hyde.yml
echo " url: example.com" > hyde.yaml
output=$(bash "$base_dir/src/check-site-url.sh")

if [[ -n "$output" ]]; then
echo "Test 6 failed: Warning shown when URL is set in hyde.yaml"
echo "Expected empty output"
echo "Got: $output"
cd "$base_dir"
rm -rf "$temp_dir"
exit 1
fi

cd "$base_dir"
rm -rf "$temp_dir"
}

# Run the tests
test_site_url_warnings
Loading