Skip to content

Commit e93e70e

Browse files
authored
Sync eng/common directory with azure-sdk-tools repository for Tools PR Azure/azure-sdk-tools#903 (#14289)
1 parent 62b4ded commit e93e70e

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

eng/common/pipelines/templates/steps/verify-links.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ steps:
1414
-rootUrl "file://$(Build.SourcesDirectory)/${{ parameters.Directory }}"
1515
-recursive: $false
1616
-ignoreLinksFile ${{ parameters.IgnoreLinksFile }}
17-
-branchReplaceRegex "($env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI.*/blob/)master(/.*)"
17+
-branchReplaceRegex "^($env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI.*/(?:blob|tree)/)master(/.*)$"
1818
-branchReplacementName $env:SYSTEM_PULLREQUEST_SOURCECOMMITID
19+
-devOpsLogging: $true

eng/common/scripts/Verify-Links.ps1

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ param (
1414
# list of http status codes count as broken links. Defaults to 400, 401, 404, SocketError.HostNotFound = 11001, SocketError.NoData = 11004
1515
[array] $errorStatusCodes = @(400, 401, 404, 11001, 11004),
1616
# 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(/.*)$",
1818
# the substitute branch name or SHA commit
1919
[string] $branchReplacementName = "",
2020
# flag to allow checking against azure sdk link guidance.
@@ -62,6 +62,18 @@ function LogWarning
6262
}
6363
}
6464

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+
6577
function ResolveUri ([System.Uri]$referralUri, [string]$link)
6678
{
6779
# If the link is mailto, skip it.
@@ -123,6 +135,9 @@ function ParseLinks([string]$baseUri, [string]$htmlContent)
123135
function CheckLink ([System.Uri]$linkUri)
124136
{
125137
if ($checkedLinks.ContainsKey($linkUri)) {
138+
if (!$checkedLinks[$linkUri]) {
139+
LogWarning "broken link $linkUri"
140+
}
126141
return $checkedLinks[$linkUri]
127142
}
128143

@@ -245,7 +260,6 @@ if ($PSVersionTable.PSVersion.Major -lt 6)
245260
{
246261
LogWarning "Some web requests will not work in versions of PS earlier then 6. You are running version $($PSVersionTable.PSVersion)."
247262
}
248-
$badLinks = @();
249263
$ignoreLinks = @();
250264
if (Test-Path $ignoreLinksFile)
251265
{
@@ -254,6 +268,7 @@ if (Test-Path $ignoreLinksFile)
254268

255269
$checkedPages = @{};
256270
$checkedLinks = @{};
271+
$badLinks = @{};
257272
$pageUrisToCheck = new-object System.Collections.Queue
258273

259274
foreach ($url in $urls) {
@@ -269,23 +284,38 @@ while ($pageUrisToCheck.Count -ne 0)
269284

270285
$linkUris = GetLinks $pageUri
271286
Write-Host "Found $($linkUris.Count) links on page $pageUri";
272-
287+
$badLinksPerPage = @();
273288
foreach ($linkUri in $linkUris) {
274289
$linkUri = ReplaceGithubLink $linkUri
275-
276290
$isLinkValid = CheckLink $linkUri
277-
if (!$isLinkValid) {
278-
$script:badLinks += $linkUri
291+
if (!$isLinkValid -and !$badLinksPerPage.Contains($linkUri)) {
292+
$badLinksPerPage += $linkUri
279293
}
280294
if ($recursive -and $isLinkValid) {
281295
if ($linkUri.ToString().StartsWith($baseUrl) -and !$checkedPages.ContainsKey($linkUri)) {
282296
$pageUrisToCheck.Enqueue($linkUri);
283297
}
284298
}
285299
}
300+
if ($badLinksPerPage.Count -gt 0) {
301+
$badLinks[$pageUri] = $badLinksPerPage
302+
}
286303
}
287304

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+
}
290314

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+
}
291321
exit $badLinks.Count

0 commit comments

Comments
 (0)