From eb1c71fbb12f94153b5907feb729459b3dbb3157 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 9 Mar 2021 15:38:16 -0800 Subject: [PATCH] Address PR feedback --- .../templates/steps/set-default-branch.yml | 11 +++ .../templates/steps/verify-links.yml | 5 +- eng/common/scripts/Verify-Links.ps1 | 68 +++++++++++++++---- 3 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 eng/common/pipelines/templates/steps/set-default-branch.yml diff --git a/eng/common/pipelines/templates/steps/set-default-branch.yml b/eng/common/pipelines/templates/steps/set-default-branch.yml new file mode 100644 index 000000000000..b4949e93bbfa --- /dev/null +++ b/eng/common/pipelines/templates/steps/set-default-branch.yml @@ -0,0 +1,11 @@ +parameters: + WorkingDirectory: '$(System.DefaultWorkingDirectory)' + RemoteRepo: 'origin' +steps: +- pwsh: | + $setDefaultBranch = (git remote show ${{ parameters.RemoteRepo }} | Out-String) -replace "(?ms).*HEAD branch: (\w+).*", '$1' + Write-Host "Setting DefaultBranch=$setDefaultBranch" + echo "##vso[task.setvariable variable=DefaultBranch]$setDefaultBranch" + displayName: "Setup Default Branch" + workingDirectory: ${{ parameters.workingDirectory }} + condition: eq(variables['DefaultBranch'], '') diff --git a/eng/common/pipelines/templates/steps/verify-links.yml b/eng/common/pipelines/templates/steps/verify-links.yml index 1990ffe07dba..45c68c45091e 100644 --- a/eng/common/pipelines/templates/steps/verify-links.yml +++ b/eng/common/pipelines/templates/steps/verify-links.yml @@ -6,11 +6,12 @@ parameters: Recursive: $false CheckLinkGuidance: $true Urls: '(Get-ChildItem -Path ./ -Recurse -Include *.md)' - BranchReplaceRegex: "^(${env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI}.*/(?:blob|tree)/)master(/.*)$" + BranchReplaceRegex: "^(${env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI}.*/(?:blob|tree)/)$(DefaultBranch)(/.*)$" BranchReplacementName: "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}" Condition: succeeded() # If you want to run on failure for the link checker, set it to `Condition: succeededOrFailed()`. steps: + - template: /eng/common/pipelines/templates/steps/set-default-branch.yml - task: PowerShell@2 displayName: Link verification check condition: ${{ parameters.Condition }} @@ -26,4 +27,4 @@ steps: -branchReplaceRegex "${{ parameters.BranchReplaceRegex }}" -branchReplacementName ${{ parameters.BranchReplacementName }} -devOpsLogging: $true - -checkLinkGuidance: ${{ parameters.CheckLinkGuidance }} + -checkLinkGuidance: ${{ parameters.CheckLinkGuidance }} \ No newline at end of file diff --git a/eng/common/scripts/Verify-Links.ps1 b/eng/common/scripts/Verify-Links.ps1 index 18aedd447da9..afcd8793b664 100644 --- a/eng/common/scripts/Verify-Links.ps1 +++ b/eng/common/scripts/Verify-Links.ps1 @@ -1,25 +1,69 @@ +<# + .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 based on recursivePattern. + + .PARAMETER baseUrl + Recursively check links for all links verified that begin with this baseUrl, defaults to the folder the url is contained in. + + .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, 401, 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)/)master(/.*)$ + + .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. + + .INPUTS + None. No required inputs. + + .OUTPUTS + None. Verify-Links.ps1 does not generate any output. + + .EXAMPLE + PS> .\Verify-Links.ps1 + + .EXAMPLE + PS> .\Verify-Links.ps1 -urls C:\README.md + + .EXAMPLE + PS> .\Verify-Links -urls C:\README.md -checkLinkGuidance $true +#> param ( - # url list to verify links. Can either be a http address or a local file request. Local file paths support md and html files. [string[]] $urls, - # file that contains a set of links to ignore when verifying [string] $ignoreLinksFile = "$PSScriptRoot/ignore-links.txt", - # switch that will enable devops specific logging for warnings [switch] $devOpsLogging = $false, - # check the links recurisvely based on recursivePattern [switch] $recursive = $true, - # recusiving check links for all links verified that begin with this baseUrl, defaults to the folder the url is contained in [string] $baseUrl = "", - # path to the root of the site for resolving rooted relative links, defaults to host root for http and file directory for local files [string] $rootUrl = "", - # list of http status codes count as broken links. Defaults to 400, 401, 404, SocketError.HostNotFound = 11001, SocketError.NoData = 11004 [array] $errorStatusCodes = @(400, 401, 404, 11001, 11004), - # regex to check if the link needs to be replaced - [string] $branchReplaceRegex = "^(https://github.com/.*/(?:blob|tree)/)master(/.*)$", - # the substitute branch name or SHA commit + [string] $branchReplaceRegex = "", [string] $branchReplacementName = "", - # flag to allow checking against azure sdk link guidance. Check link guidance here: https://aka.ms/azsdk/guideline/links [bool] $checkLinkGuidance = $false, - # UserAgent to be configured for web request. Default to current Chrome version. [string] $userAgent )