forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerify-Links.ps1
More file actions
593 lines (500 loc) · 18.9 KB
/
Verify-Links.ps1
File metadata and controls
593 lines (500 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<#
.SYNOPSIS
Check broken links.
.DESCRIPTION
The Verify-Links.ps1 script will check whether the files contain any broken links.
.PARAMETER urls
Specify url list to verify links. Can either be a http address or a local file request. Local file paths support md and html files.
.PARAMETER ignoreLinksFile
Specifies the file that contains a set of links to ignore when verifying.
.PARAMETER devOpsLogging
Switch that will enable devops specific logging for warnings.
.PARAMETER recursive
Check the links recurisvely. Applies to links starting with 'baseUrl' parameter. Defaults to true.
.PARAMETER baseUrl
Recursively check links for all links verified that begin with this baseUrl, defaults to the folder the url is contained in.
If 'recursive' parameter is set to false, this parameter has no effect.
.PARAMETER rootUrl
Path to the root of the site for resolving rooted relative links, defaults to host root for http and file directory for local files.
.PARAMETER errorStatusCodes
List of http status codes that count as broken links. Defaults to 400, 404, SocketError.HostNotFound = 11001, SocketError.NoData = 11004.
.PARAMETER branchReplaceRegex
Regex to check if the link needs to be replaced. E.g. ^(https://github.com/.*/(?:blob|tree)/)main(/.*)$
.PARAMETER branchReplacementName
The substitute branch name or SHA commit.
.PARAMETER checkLinkGuidance
Flag to allow checking against azure sdk link guidance. Check link guidance here: https://aka.ms/azsdk/guideline/links.
.PARAMETER userAgent
UserAgent to be configured for web requests. Defaults to current Chrome version.
.PARAMETER inputCacheFile
Path to a file that contains a list of links that are known valid so we can skip checking them.
.PARAMETER outputCacheFile
Path to a file that the script will output all the validated links after running all checks.
.PARAMETER requestTimeoutSec
The number of seconds before we timeout when sending an individual web request. Default is 15 seconds.
.EXAMPLE
PS> .\Verify-Links.ps1 C:\README.md
.EXAMPLE
PS> .\Verify-Links.ps1 https://azure.github.io/azure-sdk/index.html
.EXAMPLE
PS> .\Verify-Links C:\README.md -checkLinkGuidance $true
#>
[CmdletBinding()]
param (
[string[]] $urls,
[string] $ignoreLinksFile = "$PSScriptRoot/ignore-links.txt",
[switch] $devOpsLogging = $false,
[switch] $recursive = $true,
[string] $baseUrl = "",
[string] $rootUrl = "",
[array] $errorStatusCodes = @(400, 404, 11001, 11004),
[string] $branchReplaceRegex = "",
[string] $branchReplacementName = "",
[bool] $checkLinkGuidance = $false,
[string] $userAgent,
[string] $inputCacheFile,
[string] $outputCacheFile,
[string] $requestTimeoutSec = 15
)
Set-StrictMode -Version 3.0
$ProgressPreference = "SilentlyContinue"; # Disable invoke-webrequest progress dialog
function ProcessLink([System.Uri]$linkUri) {
if ($linkUri -match '^https?://?github\.com/(?<account>)[^/]+/(?<repo>)[^/]+/wiki/.+') {
# in an unauthenticated session, urls for missing pages will redirect to the wiki root
return ProcessRedirectLink $linkUri -invalidStatusCodes 302
}
elseif ($linkUri -match '^https?://aka.ms/.+') {
# aka.ms links are handled by a redirect service. Valid links return a 301
# and invalid links return a 302 redirecting the user to a Bing search
return ProcessRedirectLink $linkUri -invalidStatusCodes 302
}
elseif ($linkUri -match '^https?://crates\.io/(?<path>(crates|users|teams)/.+)') {
return ProcessCratesIoLink $linkUri $matches.path
}
else {
return ProcessStandardLink $linkUri
}
}
function ProcessRedirectLink([System.Uri]$linkUri, [int[]]$invalidStatusCodes) {
# ProcessRedirectLink checks the status code of the initial response.
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec -MaximumRedirection 0 -SkipHttpErrorCheck -ErrorAction SilentlyContinue
$statusCode = $response.StatusCode
if ($statusCode -in $invalidStatusCodes) {
Write-Host "[$statusCode] while requesting $linkUri"
return $false
}
# Because we've only tested the initial request for specific invalid status codes, we should still check that the
# final destination is valid.
return ProcessStandardLink $linkUri
}
function ProcessCratesIoLink([System.Uri]$linkUri, $path) {
# Crates.io links are handled by a SPA. Even for missing pages, the response will be a 200, and the spa will only
# show a 404 page after it makes a request to the api. We can check the api to see if the page exists.
$apiUri = "https://crates.io/api/v1/$path"
# Invoke-WebRequest will throw an exception for invalid status codes. They are handled in CheckLink
Invoke-WebRequest -Uri $apiUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec | Out-Null
return $true
}
function ProcessStandardLink([System.Uri]$linkUri) {
$headRequestSucceeded = $true
try {
# Attempt HEAD request first
$response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
}
catch {
$headRequestSucceeded = $false
}
if (!$headRequestSucceeded) {
# Attempt a GET request if the HEAD request failed.
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
}
$statusCode = $response.StatusCode
if ($statusCode -ne 200) {
Write-Host "[$statusCode] while requesting $linkUri"
}
return $true
}
# Regex of the locale keywords.
$locale = "/en-us/"
$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"
if (!$userAgent) {
$userAgent = "Chrome/87.0.4280.88"
}
function NormalizeUrl([string]$url){
if (Test-Path $url) {
$url = "file://" + (Resolve-Path $url).ToString();
}
Write-Verbose "The url to check against: $url."
$uri = [System.Uri]$url;
if ($script:baseUrl -eq "") {
# for base url default to containing directory
$script:baseUrl = (new-object System.Uri($uri, ".")).ToString();
}
if ($script:rootUrl -eq "") {
if ($uri.IsFile) {
# for files default to the containing directory
$script:rootUrl = $script:baseUrl;
}
else {
# for http links default to the root path
$script:rootUrl = new-object System.Uri($uri, "/");
}
}
return $uri
}
function LogWarning
{
if ($devOpsLogging)
{
Write-Host "##vso[task.LogIssue type=warning;]$args"
}
else
{
Write-Warning "$args"
}
}
function LogError
{
if ($devOpsLogging)
{
Write-Host "##vso[task.logissue type=error]$args"
}
else
{
Write-Error "$args"
}
}
function ResolveUri ([System.Uri]$referralUri, [string]$link)
{
# If the link is mailto, skip it.
if ($link.StartsWith("mailto:")) {
Write-Verbose "Skipping $link because it is a mailto link."
return
}
$linkUri = [System.Uri]$link;
# Our link guidelines do not allow relative links so only resolve them when we are not
# validating links against our link guidelines (i.e. !$checkLinkGuideance)
if ($checkLinkGuidance -and !$linkUri.IsAbsoluteUri) {
return $linkUri
}
if (!$linkUri.IsAbsoluteUri) {
# For rooted paths resolve from the baseUrl
if ($link.StartsWith("/")) {
Write-Verbose "rooturl = $rootUrl"
$linkUri = new-object System.Uri([System.Uri]$rootUrl, ".$link");
}
else {
$linkUri = new-object System.Uri($referralUri, $link);
}
}
$linkUri = [System.Uri]$linkUri.GetComponents([System.UriComponents]::HttpRequestUrl, [System.UriFormat]::SafeUnescaped)
Write-Verbose "ResolvedUri $link to $linkUri"
# If the link is not a web request, like mailto, skip it.
if (!$linkUri.Scheme.StartsWith("http") -and !$linkUri.IsFile) {
Write-Verbose "Skipping $linkUri because it is not http or file based."
return
}
if ($null -ne $ignoreLinks -and ($ignoreLinks.Contains($link) -or $ignoreLinks.Contains($linkUri.ToString()))) {
Write-Verbose "Ignoring invalid link $linkUri because it is in the ignore file."
return
}
return $linkUri;
}
function ParseLinks([string]$baseUri, [string]$htmlContent)
{
$hrefRegex = "<a[^>]+href\s*=\s*[""']?(?<href>[^""']*)[""']?"
$regexOptions = [System.Text.RegularExpressions.RegexOptions]"Singleline, IgnoreCase";
$hrefs = [RegEx]::Matches($htmlContent, $hrefRegex, $regexOptions);
#$hrefs | Foreach-Object { Write-Host $_ }
Write-Verbose "Found $($hrefs.Count) raw href's in page $baseUri";
[string[]] $links = $hrefs | ForEach-Object { ResolveUri $baseUri $_.Groups["href"].Value }
#$links | Foreach-Object { Write-Host $_ }
if ($null -eq $links) {
$links = @()
}
return ,$links
}
function CheckLink ([System.Uri]$linkUri, $allowRetry=$true)
{
if(!$linkUri.ToString().Trim()) {
LogWarning "Found Empty link. Please use absolute link instead. Check here for more information: https://aka.ms/azsdk/guideline/links"
return $false
}
$originalLinkUri = $linkUri
$linkUri = ReplaceGithubLink $linkUri
$link = $linkUri.ToString()
if ($checkedLinks.ContainsKey($link)) {
if (!$checkedLinks[$link]) {
LogWarning "broken link $link"
}
return $checkedLinks[$link]
}
$linkValid = $true
Write-Verbose "Checking link $linkUri..."
if ($linkUri.IsFile) {
if (!(Test-Path $linkUri.LocalPath)) {
LogWarning "Link to file does not exist $($linkUri.LocalPath)"
$linkValid = $false
}
}
elseif ($linkUri.IsAbsoluteUri) {
try {
$linkValid = ProcessLink $linkUri
}
catch {
$responsePresent = $_.Exception.psobject.Properties.name -contains "Response"
if ($responsePresent) {
$statusCode = $_.Exception.Response.StatusCode.value__
} else {
$statusCode = $null
}
if (!$statusCode) {
# Try to pull the error code from any inner SocketException we might hit
$innerExceptionPresent = $_.Exception.psobject.Properties.name -contains "InnerException"
$errorCodePresent = $false
if ($innerExceptionPresent -and $_.Exception.InnerException) {
$errorCodePresent = $_.Exception.InnerException.psobject.Properties.name -contains "ErrorCode"
}
if ($errorCodePresent) {
$statusCode = $_.Exception.InnerException.ErrorCode
}
}
if ($statusCode -in $errorStatusCodes) {
if ($originalLinkUri -ne $linkUri) {
LogError "[$statusCode] broken link $originalLinkUri (resolved to $linkUri)"
}
else {
LogError "[$statusCode] broken link $linkUri"
}
$linkValid = $false
}
else {
if ($null -ne $statusCode) {
# For 429 rate-limiting try to pause if possible
if ($allowRetry -and $responsePresent -and $statusCode -eq 429) {
$headersPresent = $_.Exception.psobject.Properties.name -contains "Headers"
$retryAfterPresent = $false
if ($headersPresent) {
$retryAfterPresent = $_.Exception.Headers.psobject.Properties.name -contains "RetryAfter"
}
$retryAfterDeltaPresent = $false
if ($retryAfterPresent) {
$retryAfterDeltaPresent = $_.Exception.Headers.RetryAfter.psobject.Properties.name -contains "Delta"
}
if ($retryAfterDeltaPresent) {
$retryAfter = $_.Exception.Response.Headers.RetryAfter.Delta.TotalSeconds
}
# Default retry after 60 (arbitrary) seconds if no header given
if (!$retryAfterDeltaPresent -or $retryAfter -gt 60) { $retryAfter = 60 }
Write-Host "Rate-Limited for $retryAfter seconds while requesting $linkUri"
Start-Sleep -Seconds $retryAfter
$linkValid = CheckLink $originalLinkUri -allowRetry $false
}
else {
Write-Host "[$statusCode] handled while requesting $linkUri"
# Override and set status code in the cache so it is truthy
# so we don't keep checking but we don't think it is valid either
$linkValid = $statusCode
}
}
else {
Write-Host "Exception while requesting $linkUri"
Write-Host $_.Exception.ToString()
# Override and set exception in the cache so it is truthy
# so we don't keep checking but we don't think it is valid either
$linkValid = "Exception"
}
}
}
}
elseif ($link.StartsWith("#")) {
# Ignore anchor links as we don't have a great way to check them.
}
else {
LogWarning "Link has invalid format $linkUri"
$linkValid = $false
}
if ($checkLinkGuidance) {
if ($linkUri.Scheme -eq 'http') {
LogWarning "DO NOT use 'http' in $linkUri. Please use secure link with https instead. Check here for more information: https://aka.ms/azsdk/guideline/links"
$linkValid = $false
}
# Check if the url is relative links, suppress the archor link validation.
if (!$linkUri.IsAbsoluteUri -and !$link.StartsWith("#")) {
LogWarning "DO NOT use relative link $linkUri. Please use absolute link instead. Check here for more information: https://aka.ms/azsdk/guideline/links"
$linkValid = $false
}
# Check if the url is anchor link has any uppercase.
if ($link -cmatch '#[^?]*[A-Z]') {
LogWarning "Please lower case your anchor tags (i.e. anything after '#' in your link '$linkUri'. Check here for more information: https://aka.ms/azsdk/guideline/links"
$linkValid = $false
}
# Check if link uri includes locale info.
if ($linkUri -match $locale) {
LogWarning "DO NOT include locale $locale information in links: $linkUri. Check here for more information: https://aka.ms/azsdk/guideline/links"
$linkValid = $false
}
}
$checkedLinks[$link] = $linkValid
return $linkValid
}
function ReplaceGithubLink([string]$originLink) {
if (!$branchReplacementName -or !$branchReplaceRegex) {
return $originLink
}
$ReplacementPattern = "`${1}$branchReplacementName`$2"
return $originLink -replace $branchReplaceRegex, $ReplacementPattern
}
function GetLinks([System.Uri]$pageUri)
{
if ($pageUri.Scheme.StartsWith("http")) {
try {
$response = Invoke-WebRequest -Uri $pageUri -UserAgent $userAgent -TimeoutSec $requestTimeoutSec -MaximumRetryCount 3
$content = $response.Content
if ($pageUri.ToString().EndsWith(".md")) {
$content = (ConvertFrom-MarkDown -InputObject $content).html
}
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
LogError "Invalid page [$statusCode] $pageUri"
}
}
elseif ($pageUri.IsFile -and (Test-Path $pageUri.LocalPath)) {
$file = $pageUri.LocalPath
if ($file.EndsWith(".md")) {
$content = (ConvertFrom-MarkDown $file).html
}
elseif ($file.EndsWith(".html")) {
$content = Get-Content $file
}
else {
if (Test-Path ($file + "index.html")) {
$content = Get-Content ($file + "index.html")
}
else {
# Fallback to just reading the content directly
$content = Get-Content $file
}
}
}
else {
LogError "Don't know how to process uri $pageUri"
}
[string[]] $links = ParseLinks $pageUri $content
return ,$links;
}
if ($urls) {
if ($urls.Count -eq 0) {
Write-Host "Usage $($MyInvocation.MyCommand.Name) <urls>";
exit 1;
}
}
if ($PSVersionTable.PSVersion.Major -lt 6)
{
LogWarning "Some web requests will not work in versions of PS earlier then 6. You are running version $($PSVersionTable.PSVersion)."
}
$ignoreLinks = @();
if (Test-Path $ignoreLinksFile) {
$ignoreLinks = (Get-Content $ignoreLinksFile).Where({ $_.Trim() -ne "" -and !$_.StartsWith("#") })
}
# Use default hashtable constructor instead of @{} because we need them to be case sensitive
$checkedPages = New-Object Hashtable
$checkedLinks = New-Object Hashtable
if ($inputCacheFile)
{
$cacheContent = ""
if ($inputCacheFile.StartsWith("http")) {
try {
$response = Invoke-WebRequest -Uri $inputCacheFile -TimeoutSec $requestTimeoutSec -MaximumRetryCount 3
$cacheContent = $response.Content
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
LogError "Failed to read cache file from page [$statusCode] $inputCacheFile"
}
}
elseif (Test-Path $inputCacheFile) {
$cacheContent = Get-Content $inputCacheFile -Raw
}
$goodLinks = $cacheContent.Split("`n").Where({ $_.Trim() -ne "" -and !$_.StartsWith("#") })
foreach ($goodLink in $goodLinks) {
$checkedLinks[$goodLink] = $true
}
}
$cachedLinksCount = $checkedLinks.Count
if ($cachedLinksCount) {
Write-Host "Skipping checks on $cachedLinksCount links found in the given cache of known good links."
}
$badLinks = New-Object Hashtable
$pageUrisToCheck = new-object System.Collections.Queue
foreach ($url in $urls) {
$uri = NormalizeUrl $url
$pageUrisToCheck.Enqueue($uri);
}
if ($devOpsLogging) {
Write-Host "##[group]Link checking details"
}
while ($pageUrisToCheck.Count -ne 0)
{
$pageUri = $pageUrisToCheck.Dequeue();
Write-Verbose "Processing pageUri $pageUri"
try {
if ($checkedPages.ContainsKey($pageUri)) { continue }
$checkedPages[$pageUri] = $true;
[string[]] $linkUris = GetLinks $pageUri
Write-Host "Checking $($linkUris.Count) links found on page $pageUri";
$badLinksPerPage = @();
foreach ($linkUri in $linkUris) {
$isLinkValid = CheckLink $linkUri
if (!$isLinkValid -and !$badLinksPerPage.Contains($linkUri)) {
if (!$linkUri.ToString().Trim()) {
$linkUri = $emptyLinkMessage
}
$badLinksPerPage += $linkUri
}
if ($recursive -and $isLinkValid) {
if ($linkUri.ToString().StartsWith($baseUrl) -and !$checkedPages.ContainsKey($linkUri)) {
$pageUrisToCheck.Enqueue($linkUri);
}
}
}
if ($badLinksPerPage.Count -gt 0) {
$badLinks[$pageUri] = $badLinksPerPage
}
} catch {
Write-Host "Exception encountered while processing pageUri $pageUri : $($_.Exception)"
throw
}
}
try {
if ($devOpsLogging) {
Write-Host "##[endgroup]"
}
if ($badLinks.Count -gt 0) {
Write-Host "Summary of broken links:"
}
foreach ($pageLink in $badLinks.Keys) {
Write-Host "'$pageLink' has $($badLinks[$pageLink].Count) broken link(s):"
foreach ($brokenLink in $badLinks[$pageLink]) {
Write-Host " $brokenLink"
}
}
$linksChecked = $checkedLinks.Count - $cachedLinksCount
if ($badLinks.Count -gt 0) {
Write-Host "Checked $linksChecked links with $($badLinks.Count) broken link(s) found."
}
else {
Write-Host "Checked $linksChecked links. No broken links found."
}
if ($outputCacheFile)
{
$goodLinks = $checkedLinks.Keys.Where({ "True" -eq $checkedLinks[$_].ToString() }) | Sort-Object
Write-Host "Writing the list of validated links to $outputCacheFile"
$goodLinks | Set-Content $outputCacheFile
}
} catch {
Write-Host "Exception encountered after all pageUris have been processed : $($_.Exception)"
throw
}
exit $badLinks.Count