Last active
March 31, 2020 05:56
-
-
Save chamindac/0d2f121e4e2e43ce1827425bd2bbca84 to your computer and use it in GitHub Desktop.
This script can be added as a task in pull request validation build with a branching pattern defined as a variable. For more information see http://chamindac.blogspot.com/2019/05/controlling-pull-request-source.html
This file contains 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
$sourceBranch = $env:System_PullRequest_SourceBranch | |
$targetBranch = $env:System_PullRequest_TargetBranch | |
$branchControlPattern = $(BranchControlPattern) | |
Write-Host ('PR Source Branch: ' + $sourceBranch) | |
Write-Host ('PR Target Branch: ' + $targetBranch) | |
$sourceBranchFilter = $sourceBranch -replace 'refs/heads/', '' | |
$targetBranchFilter = $targetBranch -replace 'refs/heads/', '' | |
if($sourceBranchFilter.Contains("/")) | |
{ | |
$sourceBranchFilter = $sourceBranchFilter -replace $sourceBranchFilter.Substring($sourceBranchFilter.IndexOf("/")),'/*' | |
} | |
if($targetBranchFilter.Contains("/")) | |
{ | |
$targetBranchFilter = $targetBranchFilter -replace $targetBranchFilter.Substring($targetBranchFilter.IndexOf("/")),'/*' | |
} | |
Write-Host ('PR Source Branch Filter: ' + $sourceBranchFilter) | |
Write-Host ('PR Target Branch Filter: ' + $targetBranchFilter) | |
$possibleSourceBranchPatterns = $branchControlPattern.Item($targetBranchFilter) | |
Write-Host ('PR Possible Source Branch Patterns: ' + $possibleSourceBranchPatterns) | |
if ($possibleSourceBranchPatterns -eq $null) | |
{ | |
Write-Error ('PR No possible source branch pattern found for target branch: ' + $targetBranch) | |
} | |
else | |
{ | |
$possibleSourceBranchPatternsArr = $possibleSourceBranchPatterns.Split(';', [System.StringSplitOptions]::RemoveEmptyEntries) | |
if ($possibleSourceBranchPatternsArr.Contains($sourceBranchFilter)) | |
{ | |
Write-Host ('PR can be allowed for target branch: ' + $targetBranch + ' from source branch: ' + $sourceBranch) | |
Write-Host ('Allowing PR validation build to proceed...') | |
} | |
else | |
{ | |
Write-Warning ('PR cannot be allowed for target branch: ' + $targetBranch + ' from source branch: ' + $sourceBranch) | |
Write-Error ('Stopping PR validation build from proceeding...') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment