From 8467d653969f78369984adf3546feae0a59299b4 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 6 Jan 2021 15:14:50 -0800 Subject: [PATCH] Make the user agent configurable. --- .../pipelines/templates/steps/verify-links.yml | 3 ++- eng/common/scripts/Verify-Links.ps1 | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/eng/common/pipelines/templates/steps/verify-links.yml b/eng/common/pipelines/templates/steps/verify-links.yml index b23a266ccae..6c52f84969d 100644 --- a/eng/common/pipelines/templates/steps/verify-links.yml +++ b/eng/common/pipelines/templates/steps/verify-links.yml @@ -8,7 +8,7 @@ parameters: Urls: '(Get-ChildItem -Path ./ -Recurse -Include *.md)' BranchReplaceRegex: "^(${env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI}.*/(?:blob|tree)/)master(/.*)$" BranchReplacementName: "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}" - + UserAgent: ${env:USERAGENT} steps: - task: PowerShell@2 displayName: Link verification check @@ -25,3 +25,4 @@ steps: -branchReplacementName ${{ parameters.BranchReplacementName }} -devOpsLogging: $true -checkLinkGuidance: ${{ parameters.CheckLinkGuidance }} + -userAgent: ${{ parameters.UserAgent }} diff --git a/eng/common/scripts/Verify-Links.ps1 b/eng/common/scripts/Verify-Links.ps1 index 60065c98089..bd242f849b2 100644 --- a/eng/common/scripts/Verify-Links.ps1 +++ b/eng/common/scripts/Verify-Links.ps1 @@ -18,13 +18,18 @@ param ( # the substitute branch name or SHA commit [string] $branchReplacementName = "", # flag to allow checking against azure sdk link guidance. Check link guidance here: https://aka.ms/azsdk/guideline/links - [bool] $checkLinkGuidance = $false + [bool] $checkLinkGuidance = $false, + # UserAgent to be configured for web request. Default to PSUserAgent. + [string] $userAgent = "Microsoft.PowerShell.Commands.PSUserAgent" ) $ProgressPreference = "SilentlyContinue"; # Disable invoke-webrequest progress dialog # 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 = "Microsoft.PowerShell.Commands.PSUserAgent" +} function NormalizeUrl([string]$url){ if (Test-Path $url) { $url = "file://" + (Resolve-Path $url).ToString(); @@ -162,14 +167,14 @@ function CheckLink ([System.Uri]$linkUri) $headRequestSucceeded = $true try { # Attempt HEAD request first - $response = Invoke-WebRequest -Uri $linkUri -Method HEAD + $response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent } catch { $headRequestSucceeded = $false } if (!$headRequestSucceeded) { # Attempt a GET request if the HEAD request failed. - $response = Invoke-WebRequest -Uri $linkUri -Method GET + $response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent } $statusCode = $response.StatusCode if ($statusCode -ne 200) { @@ -239,7 +244,7 @@ function GetLinks([System.Uri]$pageUri) { if ($pageUri.Scheme.StartsWith("http")) { try { - $response = Invoke-WebRequest -Uri $pageUri + $response = Invoke-WebRequest -Uri $pageUri -UserAgent $userAgent $content = $response.Content } catch {