Skip to content

Commit 9663990

Browse files
authored
Lint xrefs and urls (#10507)
1 parent df75088 commit 9663990

File tree

5 files changed

+118
-75
lines changed

5 files changed

+118
-75
lines changed

.github/workflows/doc-build.yml

-14
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ on:
1414
- cron: '0 0 * * *'
1515

1616
jobs:
17-
check-urls:
18-
runs-on: ubuntu-latest
19-
steps:
20-
- uses: actions/checkout@v3
21-
- name: Check URLs
22-
run: bash ./scripts/check_urls.sh
23-
24-
check-xrefs:
25-
runs-on: ubuntu-latest
26-
steps:
27-
- uses: actions/checkout@v3
28-
- name: Check Links
29-
run: bash ./scripts/check_xrefs.sh
30-
3117
build:
3218
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
3319
permissions:

.github/workflows/lint.yml

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ jobs:
6464
6565
exit $RC
6666
67+
lint-urls:
68+
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
69+
with:
70+
runner: linux.2xlarge
71+
docker-image: executorch-ubuntu-22.04-linter
72+
submodules: 'none'
73+
fetch-depth: 0
74+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
75+
timeout: 90
76+
script: |
77+
./scripts/lint_urls.sh
78+
79+
lint-xrefs:
80+
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
81+
with:
82+
runner: linux.2xlarge
83+
docker-image: executorch-ubuntu-22.04-linter
84+
submodules: 'none'
85+
fetch-depth: 0
86+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
87+
timeout: 90
88+
script: |
89+
./scripts/lint_xrefs.sh
90+
6791
android-java-format:
6892
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
6993
permissions:

scripts/check_urls.sh

-58
This file was deleted.

scripts/lint_urls.sh

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
user_agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
13+
max_jobs=10
14+
pids=()
15+
16+
running_jobs() {
17+
jobs -rp | wc -l
18+
}
19+
20+
while IFS=: read -r filepath url; do
21+
fpath="$filepath"
22+
(
23+
code=$(curl -k -gsLm30 --retry 3 --retry-delay 3 --retry-connrefused -o /dev/null -w "%{http_code}" -I "$url") || code=000
24+
if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then
25+
code=$(curl -k -gsLm30 --retry 3 --retry-delay 3 --retry-connrefused -o /dev/null -w "%{http_code}" -r 0-0 -A "$user_agent" "$url") || code=000
26+
fi
27+
if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then
28+
request_id=$(curl -sS -G -H 'Accept: application/json' \
29+
--data-urlencode "host=$url" \
30+
--data-urlencode "max_nodes=1" \
31+
--data-urlencode "node=us3.node.check-host.net" \
32+
https://check-host.net/check-http \
33+
| jq -r .request_id) || request_id=""
34+
if [ -n "$request_id" ]; then
35+
sleep 5
36+
for _ in {1..5}; do
37+
new_code=$(curl -sS -H 'Accept: application/json' \
38+
"https://check-host.net/check-result/$request_id" \
39+
| jq -r -e '.[][0][3]') || new_code=000
40+
[[ "$new_code" =~ ^[0-9]+$ ]] || new_code=000
41+
if [ "$new_code" -ge 200 ] && [ "$new_code" -lt 400 ]; then
42+
code=$new_code
43+
break
44+
fi
45+
sleep 5
46+
done
47+
fi
48+
fi
49+
if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then
50+
printf "${red}%s${reset} ${yellow}%s${reset} %s\n" "$code" "$url" "$fpath" >&2
51+
exit 1
52+
else
53+
printf "${green}%s${reset} ${cyan}%s${reset} %s\n" "$code" "$url" "$fpath"
54+
exit 0
55+
fi
56+
) &
57+
pids+=($!)
58+
while [ "$(running_jobs)" -ge "$max_jobs" ]; do
59+
sleep 1
60+
done
61+
done < <(
62+
git --no-pager grep --no-color -I -P -o \
63+
'(?!.*@lint-ignore)(?<!git\+)(?<!\$\{)https?://(?![^\s<>\")]*[<>\{\}\$])[^[:space:]<>\")\[\]\(\\]+' \
64+
-- '*' \
65+
':(exclude).*' \
66+
':(exclude,glob)**/.*' \
67+
':(exclude,glob)**/*.lock' \
68+
':(exclude,glob)**/*.svg' \
69+
':(exclude,glob)**/*.xml' \
70+
':(exclude,glob)**/*.gradle*' \
71+
':(exclude,glob)**/*gradle*' \
72+
':(exclude,glob)**/third-party/**' \
73+
':(exclude,glob)**/third_party/**' \
74+
| sed -E 's/[^/[:alnum:]]+$//' \
75+
| grep -Ev '://(0\.0\.0\.0|127\.0\.0\.1|localhost)([:/])' \
76+
| grep -Ev 'fwdproxy:8080' \
77+
|| true
78+
)
79+
80+
for pid in "${pids[@]}"; do
81+
wait "$pid" 2>/dev/null || {
82+
case $? in
83+
1) status=1 ;;
84+
127) ;; # ignore "not a child" noise
85+
*) exit $? ;;
86+
esac
87+
}
88+
done
89+
90+
exit $status

scripts/check_xrefs.sh renamed to scripts/lint_xrefs.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ while IFS=: read -r filepath link; do
2323
status=1
2424
fi
2525
done < <(
26-
git --no-pager grep --no-color -I -o -E \
27-
'\[[^]]+\]\([^[:space:])]*/[^[:space:])]*\)|href="[^"]*/[^"]*"|src="[^"]*/[^"]*"' \
26+
git --no-pager grep --no-color -I -P -o \
27+
'(?!.*@lint-ignore)(?:\[[^]]+\]\([^[:space:])]*/[^[:space:])]*\)|href="[^"]*/[^"]*"|src="[^"]*/[^"]*")' \
2828
-- '*' \
2929
':(exclude).*' \
3030
':(exclude)**/.*' \
3131
':(exclude)**/*.lock' \
3232
':(exclude)**/*.svg' \
3333
':(exclude)**/*.xml' \
34-
':(exclude)**/third-party/**' \
34+
':(exclude,glob)**/third-party/**' \
35+
':(exclude,glob)**/third_party/**' \
3536
| grep -Ev 'https?://' \
3637
| sed -E \
3738
-e 's#([^:]+):\[[^]]+\]\(([^)]+)\)#\1:\2#' \

0 commit comments

Comments
 (0)