Skip to content

PR body parsing improvement for test websites #84

PR body parsing improvement for test websites

PR body parsing improvement for test websites #84

Workflow file for this run

name: Tests
on:
pull_request_target:
branches:
- main
paths-ignore:
- "**/*.md"
jobs:
test:
name: WebPageTest Test Cases
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Install dependencies
run: |
npm install webpagetest
npm install jest
- name: Run WebPageTest with unit tests
run: |
echo "::group::Unit tests"
npm test
echo "::endgroup::"
env:
WPT_SERVER: "webpagetest.httparchive.org"
WPT_API_KEY: ${{ secrets.HA_API_KEY }}
- name: Run WebPageTest for more websites
run: |
METRICS=$(git diff --name-only --diff-filter=ACMRT ${GITHUB_BASE_SHA} ${GITHUB_HEAD_SHA} | grep -E "^dist/.*\.js$" | xargs -I {} basename {} | cut -d. -f1 | sort | uniq)
PR_BODY="$(cat <<'EOF'
${{ github.event.pull_request.body }}
EOF
)"
# Read PR body into an array, removing line breaks
declare -a lines
while IFS= read -r line; do
lines+=("${line//[$'\r\n']}")
done <<< "$PR_BODY"
# Find the start index of the section you're interested in
start_index=-1
for ((i=0; i<${#lines[@]}; i++)); do
if [[ "${lines[$i]}" == *"**Test websites**:"* ]]; then
start_index=$((i + 1))
break
fi
done
if [ $start_index -gt -1 ]; then
# Initialize an array for URLs
declare -a URLS
url_pattern="^(http|https|ftp)://[a-zA-Z0-9.-]+(\.[a-zA-Z]{2,4})(/[a-zA-Z0-9_.-]+)*(\/?)(\?[a-zA-Z0-9_.-]+=[a-zA-Z0-9%_.-]+)*$"
for ((i=start_index; i<${#lines[@]}; i++)); do
url="${lines[$i]}"
# Remove any extra characters or spaces
url="${url//[- ]/}"
if [[ $url =~ $url_pattern ]]; then
URLS+=("$url")
fi
done
for TEST_WEBSITE in "${URLS[@]}"; do
echo "::group::Custom metrics for $TEST_WEBSITE"
node tests/wpt.js "$TEST_WEBSITE" "$METRICS"
echo "::endgroup::"
done
else
echo "No websites found."
fi
env:
WPT_SERVER: "webpagetest.httparchive.org"
WPT_API_KEY: ${{ secrets.HA_API_KEY }}
- name: Add comment with results
uses: mshick/add-pr-comment@v2
if: always()
with:
refresh-message-position: true
message-path: test-results.md