-
Notifications
You must be signed in to change notification settings - Fork 227
Fix verify-links regex to handle repository URIs with .git suffix #13560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e75412
Initial plan
Copilot 8ed5c20
Fix verify-links regex to handle optional .git suffix in repository URIs
Copilot 92bdf41
Refactor tests to use shared default regex variable
Copilot 6d57dda
Escape backslash in YAML regex pattern for Azure DevOps parsing
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
eng/common-tests/Verify-Links-ReplaceGithubLink.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| Import-Module Pester | ||
|
|
||
| BeforeAll { | ||
| # Default shared regex pattern with optional .git suffix | ||
| $script:defaultRegex = '^(https://github\.com/Azure/azure-sdk-for-java(?:\.git)?/(?:blob|tree)/)main(/.*)$' | ||
|
|
||
| # Helper function to test link replacement logic directly | ||
| # This mirrors the logic in ReplaceGithubLink function from Verify-Links.ps1 | ||
| function Test-ReplaceGithubLink { | ||
| param( | ||
| [string]$originLink, | ||
| [string]$branchReplaceRegex, | ||
| [string]$branchReplacementName | ||
| ) | ||
|
|
||
| if (!$branchReplacementName -or !$branchReplaceRegex) { | ||
| return $originLink | ||
| } | ||
| $ReplacementPattern = "`${1}$branchReplacementName`$2" | ||
| return $originLink -replace $branchReplaceRegex, $ReplacementPattern | ||
| } | ||
| } | ||
|
|
||
| Describe "ReplaceGithubLink" { | ||
| Context "When repository URI does not end with .git" { | ||
| It "Should replace branch name in GitHub link without .git suffix" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md" | ||
| $expectedLink = "https://github.com/Azure/azure-sdk-for-java/blob/abc123def/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def" | ||
| $result | Should -Be $expectedLink | ||
| } | ||
|
|
||
| It "Should replace branch name with tree path" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/core" | ||
| $expectedLink = "https://github.com/Azure/azure-sdk-for-java/tree/abc123def/sdk/core" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def" | ||
| $result | Should -Be $expectedLink | ||
| } | ||
| } | ||
|
|
||
| Context "When repository URI ends with .git" { | ||
| It "Should replace branch name in GitHub link with .git suffix" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java.git/blob/main/README.md" | ||
| $expectedLink = "https://github.com/Azure/azure-sdk-for-java.git/blob/abc123def/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java\.git/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def" | ||
| $result | Should -Be $expectedLink | ||
| } | ||
|
|
||
| It "Should replace branch name with tree path and .git suffix" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java.git/tree/main/sdk/core" | ||
| $expectedLink = "https://github.com/Azure/azure-sdk-for-java.git/tree/abc123def/sdk/core" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java\.git/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def" | ||
| $result | Should -Be $expectedLink | ||
| } | ||
| } | ||
|
|
||
| Context "When regex handles optional .git suffix" { | ||
| It "Should replace branch name whether .git is present or not" { | ||
| # Test without .git | ||
| $originalLinkWithoutGit = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md" | ||
| $expectedLink = "https://github.com/Azure/azure-sdk-for-java/blob/abc123def/README.md" | ||
|
|
||
| $resultWithoutGit = Test-ReplaceGithubLink -originLink $originalLinkWithoutGit -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def" | ||
| $resultWithoutGit | Should -Be $expectedLink | ||
|
|
||
| # Test with .git | ||
| $originalLinkWithGit = "https://github.com/Azure/azure-sdk-for-java.git/blob/main/README.md" | ||
| $expectedLinkWithGit = "https://github.com/Azure/azure-sdk-for-java.git/blob/abc123def/README.md" | ||
|
|
||
| $resultWithGit = Test-ReplaceGithubLink -originLink $originalLinkWithGit -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def" | ||
| $resultWithGit | Should -Be $expectedLinkWithGit | ||
| } | ||
| } | ||
|
|
||
| Context "When link does not match regex pattern" { | ||
| It "Should return original link unchanged" { | ||
| $originalLink = "https://github.com/SomeOrg/SomeRepo/blob/main/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def" | ||
| $result | Should -Be $originalLink | ||
| } | ||
|
|
||
| It "Should return original link when branch name doesn't match" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/feature-branch/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def" | ||
| $result | Should -Be $originalLink | ||
| } | ||
| } | ||
|
|
||
| Context "When replacement parameters are not set" { | ||
| It "Should return original link when branchReplacementName is empty" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName "" | ||
| $result | Should -Be $originalLink | ||
| } | ||
|
|
||
| It "Should return original link when branchReplaceRegex is empty" { | ||
| $originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md" | ||
|
|
||
| $result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex "" -branchReplacementName "abc123def" | ||
| $result | Should -Be $originalLink | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot default the shared regex here instead of duplicating it in all the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 92bdf41. The shared regex pattern with optional
.gitsuffix is now defined as$defaultRegexin theBeforeAllblock and reused throughout the tests.