diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml new file mode 100644 index 00000000..7dcb05e4 --- /dev/null +++ b/.github/workflows/pr-check.yml @@ -0,0 +1,40 @@ +name: PR Check Workflow + +on: + pull_request: + branches: + - main + +jobs: + check-PR: + runs-on: ubuntu-latest + + steps: + - name: Checkout base commit + uses: actions/checkout@v2 + with: + ref: ${{ github.base_ref }} + + - name: Generate routes + run: | + ./ci-scripts/gen_routes.sh > routes_to_test.txt + + - name: Upload routes file + uses: actions/upload-artifact@v2 + with: + name: routes-to-test + path: routes_to_test.txt + + - name: Checkout PR's head commit + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: Download routes file + uses: actions/download-artifact@v2 + with: + name: routes-to-test + + - name: Run redirect tests + run: | + ./ci-scripts/test_redirects.sh routes_to_test.txt diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..28bc0673 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: test-redirects + +test-redirects: + @./ci-scripts/test_redirects_local.sh diff --git a/README.md b/README.md index aaba2fa1..bddf21cb 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,13 @@ This website is built using [Docusaurus 2](https://docusaurus.io/), a modern sta ### Installation ``` -$ yarn +yarn ``` ### Local Development ``` -$ yarn start +yarn start ``` This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server. @@ -19,7 +19,7 @@ This command starts a local development server and opens up a browser window. Mo ### Build ``` -$ yarn build +yarn build ``` This command generates static content into the `build` directory and can be served using any static contents hosting service. @@ -29,13 +29,13 @@ This command generates static content into the `build` directory and can be serv Using SSH: ``` -$ USE_SSH=true yarn deploy +USE_SSH=true yarn deploy ``` Not using SSH: ``` -$ GIT_USER= yarn deploy +GIT_USER= yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/ci-scripts/gen_routes.sh b/ci-scripts/gen_routes.sh new file mode 100755 index 00000000..639677b1 --- /dev/null +++ b/ci-scripts/gen_routes.sh @@ -0,0 +1,8 @@ +#!/bin/bash -e + +echo "Generating routes..." >&2 +yarn build >/dev/null +sed 's/xmlns="[^"]*"//g' build/sitemap.xml | xmllint --xpath '//*[local-name()="loc"]/text()' - | while read -r url; do + echo "${url#https://docs.eigenlayer.xyz}" +done +echo "Done" >&2 diff --git a/ci-scripts/test_redirects.sh b/ci-scripts/test_redirects.sh new file mode 100755 index 00000000..ff4825c0 --- /dev/null +++ b/ci-scripts/test_redirects.sh @@ -0,0 +1,89 @@ +#!/bin/bash -e + +# Display usage information +usage() { + echo "Usage: $0 " + echo + echo "This script tests that each of the old pages redirects properly to a new page." + echo "It requires a file containing URLs to test, one per line." + echo + echo "Usage pattern:" + echo "1. Switch from new branch to old branch" + echo "2. Generate routes get_routes.sh > routes_to_test.txt" + echo "3. Switch from old branch to new branch (bringing routes_to_test.txt over)" + echo "4. Run $0 routes_to_test.txt" + echo "" + echo "This script tests that each of the old pages redirects properly to a new page." + echo "It does not test that this is the correct new page however." + exit 1 +} + +# Ensure server is killed on script exit +cleanup() { + echo "Stopping server..." + if [ ! -z "$SERVER_PID" ]; then + kill $SERVER_PID 2>/dev/null || echo "Server process not found." + else + echo "No server was started." + fi +} + +# Variable to track test failure +any_fail=0 + +# Check for help option or no argument +if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]] || [ "$#" -ne 1 ]; then + usage +fi + +# Absolute path to the filename +FILENAME=$(realpath "$1") + +# Ensure file exists +if [ ! -f "$FILENAME" ]; then + echo "File not found: $FILENAME" >&2 + exit 1 +fi + +# Build the project and start the server +echo "Building..." +yarn build > /dev/null +echo "Starting server..." +./node_modules/.bin/docusaurus serve --no-open & +SERVER_PID=$! +echo "Server started." + +# Wait a bit for the server to start +sleep 5 + +# Setup trap to clean up on script exit +trap cleanup EXIT + +echo "" +echo "Begining tests..." +echo "" +while IFS= read -r url; do + full_url="http://localhost:3000${url}" + # Perform a curl request and check both for "Page Not Found" and curl's exit status + if ! curl_output=$(curl -fs "$full_url" 2>&1); then + # Curl command failed + printf "\033[31mtest fail\033[0m: %s\n" "$full_url" >&2 + any_fail=1 + else + # If curl succeeded and "Page Not Found" not in output, mark test as passed + printf "\033[32mtest pass\033[0m: %s\n" "$full_url" + fi +done < "$FILENAME" + +# Exit with status code 1 if any test failed +if [ "$any_fail" -eq 1 ]; then + echo "" + printf "\033[31mOne or more tests failed!\033[0m\n" + echo "" + exit 1 +else + echo "" + printf "\033[32mAll tests passed!\033[0m\n" + echo "" + exit 0 +fi diff --git a/ci-scripts/test_redirects_local.sh b/ci-scripts/test_redirects_local.sh new file mode 100755 index 00000000..38a6fdee --- /dev/null +++ b/ci-scripts/test_redirects_local.sh @@ -0,0 +1,56 @@ +#!/bin/bash -e + +REPO_TOP_LEVEL=$(git rev-parse --show-toplevel) + +# Fetch the latest state of origin/main +git fetch origin main + +# Find the merge-base of the current branch and origin/main +MERGE_BASE=$(git merge-base origin/main @) + +echo "Merge base of origin/main and $CURRENT_BRANCH is $MERGE_BASE" + +# Generate a random worktree path +WORKTREE_PATH="/tmp/pr_worktree_$(date +%Y%m%d%H%M%S)_$RANDOM" + +# Ensure the worktree path is unique and does not already exist +if [ -d "$WORKTREE_PATH" ]; then + echo "Worktree directory already exists. Please try again." + exit 1 +fi + +# Create a new worktree for the merge-base in the temporary directory +git worktree add "$WORKTREE_PATH" $MERGE_BASE + +# Change directory to the worktree +cd "$WORKTREE_PATH" + +yarn + +# Run the script to generate the routes_to_test.txt +./ci-scripts/gen_routes.sh > routes_to_test.txt + +# Move the routes_to_test.txt back to the original repo directory +mv routes_to_test.txt "$REPO_TOP_LEVEL" + +# Clean up the worktree and return to the original repository directory +git worktree remove "$WORKTREE_PATH" +cd - + +# The current repo state is now the target for tests +# Run the tests with the routes file in the current state of the repository +set +e +./ci-scripts/test_redirects.sh routes_to_test.txt +TEST_REDIRECTS_EXIT_CODE=$? +set -e + +rm routes_to_test.txt + +# Check the exit status of the test script and exit accordingly +if [ $TEST_REDIRECTS_EXIT_CODE -ne 0 ]; then + echo "Redirect tests failed." + exit 1 +else + echo "All tests passed successfully." +fi +