Skip to content

Commit 63e43ca

Browse files
authored
Add support for aka.ms, github wiki and crates.io checking (Azure#37194)
1 parent a4f1839 commit 63e43ca

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

eng/common/scripts/Verify-Links.ps1

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,71 @@ param (
7878
Set-StrictMode -Version 3.0
7979

8080
$ProgressPreference = "SilentlyContinue"; # Disable invoke-webrequest progress dialog
81+
82+
function ProcessLink([System.Uri]$linkUri) {
83+
if ($linkUri -match '^https?://?github\.com/(?<account>)[^/]+/(?<repo>)[^/]+/wiki/.+') {
84+
# in an unauthenticated session, urls for missing pages will redirect to the wiki root
85+
return ProcessRedirectLink $linkUri -invalidStatusCodes 302
86+
}
87+
elseif ($linkUri -match '^https?://aka.ms/.+') {
88+
# aka.ms links are handled by a redirect service. Valid links return a 301
89+
# and invalid links return a 302 redirecting the user to a Bing search
90+
return ProcessRedirectLink $linkUri -invalidStatusCodes 302
91+
}
92+
elseif ($linkUri -match '^https?://crates\.io/(?<path>(crates|users|teams)/.+)') {
93+
return ProcessCratesIoLink $linkUri $matches.path
94+
}
95+
else {
96+
return ProcessStandardLink $linkUri
97+
}
98+
}
99+
100+
function ProcessRedirectLink([System.Uri]$linkUri, [int[]]$invalidStatusCodes) {
101+
# ProcessRedirectLink checks the status code of the initial response.
102+
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec -MaximumRedirection 0 -SkipHttpErrorCheck -ErrorAction SilentlyContinue
103+
$statusCode = $response.StatusCode
104+
105+
if ($statusCode -in $invalidStatusCodes) {
106+
Write-Host "[$statusCode] while requesting $linkUri"
107+
return $false
108+
}
109+
110+
# Because we've only tested the initial request for specific invalid status codes, we should still check that the
111+
# final destination is valid.
112+
return ProcessStandardLink $linkUri
113+
}
114+
115+
function ProcessCratesIoLink([System.Uri]$linkUri, $path) {
116+
# Crates.io links are handled by a SPA. Even for missing pages, the response will be a 200, and the spa will only
117+
# show a 404 page after it makes a request to the api. We can check the api to see if the page exists.
118+
$apiUri = "https://crates.io/api/v1/$path"
119+
120+
# Invoke-WebRequest will throw an exception for invalid status codes. They are handled in CheckLink
121+
Invoke-WebRequest -Uri $apiUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec | Out-Null
122+
123+
return $true
124+
}
125+
126+
function ProcessStandardLink([System.Uri]$linkUri) {
127+
$headRequestSucceeded = $true
128+
try {
129+
# Attempt HEAD request first
130+
$response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
131+
}
132+
catch {
133+
$headRequestSucceeded = $false
134+
}
135+
if (!$headRequestSucceeded) {
136+
# Attempt a GET request if the HEAD request failed.
137+
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
138+
}
139+
$statusCode = $response.StatusCode
140+
if ($statusCode -ne 200) {
141+
Write-Host "[$statusCode] while requesting $linkUri"
142+
}
143+
return $true
144+
}
145+
81146
# Regex of the locale keywords.
82147
$locale = "/en-us/"
83148
$emptyLinkMessage = "There is at least one empty link in the page. Please replace with absolute link. Check here for more information: https://aka.ms/azsdk/guideline/links"
@@ -228,22 +293,7 @@ function CheckLink ([System.Uri]$linkUri, $allowRetry=$true)
228293
}
229294
elseif ($linkUri.IsAbsoluteUri) {
230295
try {
231-
$headRequestSucceeded = $true
232-
try {
233-
# Attempt HEAD request first
234-
$response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
235-
}
236-
catch {
237-
$headRequestSucceeded = $false
238-
}
239-
if (!$headRequestSucceeded) {
240-
# Attempt a GET request if the HEAD request failed.
241-
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
242-
}
243-
$statusCode = $response.StatusCode
244-
if ($statusCode -ne 200) {
245-
Write-Host "[$statusCode] while requesting $linkUri"
246-
}
296+
$linkValid = ProcessLink $linkUri
247297
}
248298
catch {
249299

0 commit comments

Comments
 (0)