shayki5 / azure-devops-create-pr-task

Azure Pipelines task to create PR in Azure DevOps or GitHub during a build or release pipeline
https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest
MIT License
58 stars 35 forks source link

Check "refs/" prefix only at beginning of branch name #178

Closed ligz08 closed 1 year ago

ligz08 commented 1 year ago

Current logic breaks when a branch name happens to contain "refs/heads/" in the middle. Suppose a user (well, I mean myself) has $sourceBranch = "my/branch/contains/refs/heads/for/some/reason" After

if (!$sourceBranch.Contains("refs")) {
    $sourceBranch = "refs/heads/$sourceBranch"
}

no refs/heads/ is added at beginning since $sourceBranch.Contains("refs") is True, so $sourceBranch is still "my/branch/contains/refs/heads/for/some/reason"

Then after

# Remove the refs/heads/ or merge/pull from branches name (see issue #85)
if($sourceBranch.Contains("heads"))
{
   $sourceBranch = $sourceBranch.Remove(0, 11)
}
elseif($sourceBranch.Contains("refs/pull"))
{
   $sourceBranch = $sourceBranch.Remove(0, 10)
}

$sourceBranche becomes "ontains/refs/heads/for/some/reason" because first 10 chars are removed in the if($sourceBranch.Contains("heads")) block.

Eventually we'll find "Branch 'ontains/refs/heads/for/some/reason' not found in repository"

This PR makes sure to check refs/ prefix only at beginning of branch name.

shayki5 commented 1 year ago

Thank you @ligz08 for your contribution!