Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chamindac/0d2f121e4e2e43ce1827425bd2bbca84 to your computer and use it in GitHub Desktop.
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
$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