@@ -14,7 +14,7 @@ param (
14
14
# list of http status codes count as broken links. Defaults to 400, 401, 404, SocketError.HostNotFound = 11001, SocketError.NoData = 11004
15
15
[array ] $errorStatusCodes = @ (400 , 401 , 404 , 11001 , 11004 ),
16
16
# regex to check if the link needs to be replaced
17
- [string ] $branchReplaceRegex = " (https://github.com/.*/blob/)master(/.*)" ,
17
+ [string ] $branchReplaceRegex = " ^ (https://github.com/.*/(?: blob|tree) /)master(/.*)$ " ,
18
18
# the substitute branch name or SHA commit
19
19
[string ] $branchReplacementName = " " ,
20
20
# flag to allow checking against azure sdk link guidance.
@@ -62,6 +62,18 @@ function LogWarning
62
62
}
63
63
}
64
64
65
+ function LogError
66
+ {
67
+ if ($devOpsLogging )
68
+ {
69
+ Write-Host " ##vso[task.logissue type=error]$args "
70
+ }
71
+ else
72
+ {
73
+ Write-Error " $args "
74
+ }
75
+ }
76
+
65
77
function ResolveUri ([System.Uri ]$referralUri , [string ]$link )
66
78
{
67
79
# If the link is mailto, skip it.
@@ -123,6 +135,9 @@ function ParseLinks([string]$baseUri, [string]$htmlContent)
123
135
function CheckLink ([System.Uri ]$linkUri )
124
136
{
125
137
if ($checkedLinks.ContainsKey ($linkUri )) {
138
+ if (! $checkedLinks [$linkUri ]) {
139
+ LogWarning " broken link $linkUri "
140
+ }
126
141
return $checkedLinks [$linkUri ]
127
142
}
128
143
@@ -245,7 +260,6 @@ if ($PSVersionTable.PSVersion.Major -lt 6)
245
260
{
246
261
LogWarning " Some web requests will not work in versions of PS earlier then 6. You are running version $ ( $PSVersionTable.PSVersion ) ."
247
262
}
248
- $badLinks = @ ();
249
263
$ignoreLinks = @ ();
250
264
if (Test-Path $ignoreLinksFile )
251
265
{
@@ -254,6 +268,7 @@ if (Test-Path $ignoreLinksFile)
254
268
255
269
$checkedPages = @ {};
256
270
$checkedLinks = @ {};
271
+ $badLinks = @ {};
257
272
$pageUrisToCheck = new-object System.Collections.Queue
258
273
259
274
foreach ($url in $urls ) {
@@ -269,23 +284,38 @@ while ($pageUrisToCheck.Count -ne 0)
269
284
270
285
$linkUris = GetLinks $pageUri
271
286
Write-Host " Found $ ( $linkUris.Count ) links on page $pageUri " ;
272
-
287
+ $badLinksPerPage = @ ();
273
288
foreach ($linkUri in $linkUris ) {
274
289
$linkUri = ReplaceGithubLink $linkUri
275
-
276
290
$isLinkValid = CheckLink $linkUri
277
- if (! $isLinkValid ) {
278
- $script :badLinks += $linkUri
291
+ if (! $isLinkValid -and ! $badLinksPerPage .Contains ( $linkUri ) ) {
292
+ $badLinksPerPage += $linkUri
279
293
}
280
294
if ($recursive -and $isLinkValid ) {
281
295
if ($linkUri.ToString ().StartsWith($baseUrl ) -and ! $checkedPages.ContainsKey ($linkUri )) {
282
296
$pageUrisToCheck.Enqueue ($linkUri );
283
297
}
284
298
}
285
299
}
300
+ if ($badLinksPerPage.Count -gt 0 ) {
301
+ $badLinks [$pageUri ] = $badLinksPerPage
302
+ }
286
303
}
287
304
288
- Write-Host " Found $ ( $checkedLinks.Count ) links with $ ( $badLinks.Count ) broken"
289
- $badLinks | ForEach-Object { Write-Host " $_ " }
305
+ if ($badLinks.Count -gt 0 ) {
306
+ Write-Host " Summary of broken links:"
307
+ }
308
+ foreach ($pageLink in $badLinks.Keys ) {
309
+ Write-Host " '$pageLink ' has $ ( $badLinks [$pageLink ].Count) broken link(s):"
310
+ foreach ($brokenLink in $badLinks [$pageLink ]) {
311
+ Write-Host " $brokenLink "
312
+ }
313
+ }
290
314
315
+ if ($badLinks.Count -gt 0 ) {
316
+ LogError " Found $ ( $checkedLinks.Count ) links with $ ( $badLinks.Count ) page(s) broken."
317
+ }
318
+ else {
319
+ Write-Host " Found $ ( $checkedLinks.Count ) links. No broken links found."
320
+ }
291
321
exit $badLinks.Count
0 commit comments