Skip to content

Commit 06a4944

Browse files
authored
Script to validate URLs (#10289)
1 parent b3963e6 commit 06a4944

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

scripts/check_urls.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -euo pipefail
9+
10+
status=0
11+
green='\e[1;32m'; red='\e[1;31m'; cyan='\e[1;36m'; yellow='\e[1;33m'; reset='\e[0m'
12+
last_filepath=
13+
14+
while IFS=: read -r filepath url; do
15+
if [ "$filepath" != "$last_filepath" ]; then
16+
printf '\n%s:\n' "$filepath"
17+
last_filepath=$filepath
18+
fi
19+
code=$(curl -gsLm30 -o /dev/null -w "%{http_code}" -I "$url") || code=000
20+
if [ "$code" -ge 400 ]; then
21+
code=$(curl -gsLm30 -o /dev/null -w "%{http_code}" -r 0-0 -A "Mozilla/5.0" "$url") || code=000
22+
fi
23+
if [ "$code" -ge 200 ] && [ "$code" -lt 400 ]; then
24+
printf "${green}%s${reset} ${cyan}%s${reset}\n" "$code" "$url"
25+
else
26+
printf "${red}%s${reset} ${yellow}%s${reset}\n" "$code" "$url" >&2
27+
status=1
28+
fi
29+
done < <(
30+
git --no-pager grep --no-color -I -o -E \
31+
'https?://[^[:space:]<>\")\{\(\$]+' \
32+
-- '*' \
33+
':(exclude).*' \
34+
':(exclude)**/.*' \
35+
':(exclude)**/*.lock' \
36+
':(exclude)**/*.svg' \
37+
':(exclude)**/*.xml' \
38+
':(exclude)**/third-party/**' \
39+
| sed 's/[[:punct:]]*$//' \
40+
| grep -Ev '://(0\.0\.0\.0|127\.0\.0\.1|localhost)([:/])' \
41+
|| true
42+
)
43+
44+
exit $status

0 commit comments

Comments
 (0)