diff --git a/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs b/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs index 7e12374d66..e443e726cf 100644 --- a/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs +++ b/src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs @@ -15,7 +15,9 @@ public class PullRequestInBuildAgentTest { "refs/pull-requests/5/merge", "refs/pull/5/merge", - "refs/heads/pull/5/head" + "refs/heads/pull/5/head", + "refs/remotes/pull/5/merge", + "refs/remotes/pull-requests/5/merge" }; [TestCaseSource(nameof(PrMergeRefs))] @@ -49,7 +51,6 @@ public async Task VerifyContinuaCIPullRequest(string pullRequestRef) await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); } - [TestCaseSource(nameof(PrMergeRefs))] public async Task VerifyDronePullRequest(string pullRequestRef) { @@ -128,7 +129,6 @@ public async Task VerifyTravisCIPullRequest(string pullRequestRef) await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); } - [TestCaseSource(nameof(PrMergeRefs))] public async Task VerifyBitBucketPipelinesPullRequest(string pullRequestRef) { @@ -180,4 +180,23 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu // Cleanup repository files DirectoryHelper.DeleteDirectory(remoteRepositoryPath); } + + private static readonly object[] PrMergeRefInputs = + { + new object[] { "refs/pull-requests/5/merge", "refs/pull-requests/5/merge", false, true, false }, + new object[] { "refs/pull/5/merge", "refs/pull/5/merge", false, true, false}, + new object[] { "refs/heads/pull/5/head", "pull/5/head", true, false, false }, + new object[] { "refs/remotes/pull/5/merge", "pull/5/merge", false, true, true }, + }; + + [TestCaseSource(nameof(PrMergeRefInputs))] + public void VerifyPullRequestInput(string pullRequestRef, string friendly, bool isBranch, bool isPullRequest, bool isRemote) + { + var refName = new ReferenceName(pullRequestRef); + + Assert.AreEqual(friendly, refName.Friendly); + Assert.AreEqual(isBranch, refName.IsBranch); + Assert.AreEqual(isPullRequest, refName.IsPullRequest); + Assert.AreEqual(isRemote, refName.IsRemoteBranch); + } } diff --git a/src/GitVersion.Core/Git/ReferenceName.cs b/src/GitVersion.Core/Git/ReferenceName.cs index 4e949e94fa..6787705285 100644 --- a/src/GitVersion.Core/Git/ReferenceName.cs +++ b/src/GitVersion.Core/Git/ReferenceName.cs @@ -11,19 +11,25 @@ public class ReferenceName : IEquatable, IComparable private string Shorten() { - if (IsPrefixedBy(Canonical, LocalBranchPrefix)) + if (IsBranch) return Canonical.Substring(LocalBranchPrefix.Length); - if (IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix)) + + if (IsRemoteBranch) return Canonical.Substring(RemoteTrackingBranchPrefix.Length); - if (IsPrefixedBy(Canonical, TagPrefix)) + + if (IsTag) return Canonical.Substring(TagPrefix.Length); + return Canonical; } private string RemoveRemote() { - var isRemote = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix); + if (IsRemoteBranch) + { + if (!IsPullRequest) + return Friendly.Substring(Friendly.IndexOf("/", StringComparison.Ordinal) + 1); + } - return isRemote - ? Friendly.Substring(Friendly.IndexOf("/", StringComparison.Ordinal) + 1) - : Friendly; + return Friendly; } + private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal); + + private static bool IsPrefixedBy(string input, string[] prefixes) => prefixes.Any(prefix => IsPrefixedBy(input, prefix)); }