Skip to content

Fix issue with pullrequests in Azure DevOps #3181

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -49,7 +51,6 @@ public async Task VerifyContinuaCIPullRequest(string pullRequestRef)
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}


[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyDronePullRequest(string pullRequestRef)
{
Expand Down Expand Up @@ -128,7 +129,6 @@ public async Task VerifyTravisCIPullRequest(string pullRequestRef)
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}


[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyBitBucketPipelinesPullRequest(string pullRequestRef)
{
Expand Down Expand Up @@ -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);
}
}
42 changes: 28 additions & 14 deletions src/GitVersion.Core/Git/ReferenceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,37 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
private const string LocalBranchPrefix = "refs/heads/";
private const string RemoteTrackingBranchPrefix = "refs/remotes/";
private const string TagPrefix = "refs/tags/";
private const string PullRequestPrefix1 = "refs/pull/";
private const string PullRequestPrefix2 = "refs/pull-requests/";
private static readonly string[] PullRequestPrefixes =
{
"refs/pull/",
"refs/pull-requests/",
"refs/remotes/pull/",
"refs/remotes/pull-requests/"
};

public ReferenceName(string canonical)
{
Canonical = canonical.NotNull();
Friendly = Shorten();
WithoutRemote = RemoveRemote();

IsBranch = IsPrefixedBy(Canonical, LocalBranchPrefix);
IsRemoteBranch = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix);
IsTag = IsPrefixedBy(Canonical, TagPrefix);
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefix1) || IsPrefixedBy(Canonical, PullRequestPrefix2);
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefixes);

Friendly = Shorten();
WithoutRemote = RemoveRemote();
}

public static ReferenceName Parse(string canonicalName)
{
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
|| IsPrefixedBy(canonicalName, TagPrefix)
|| IsPrefixedBy(canonicalName, PullRequestPrefix1)
|| IsPrefixedBy(canonicalName, PullRequestPrefix2))
|| IsPrefixedBy(canonicalName, PullRequestPrefixes))
{
return new ReferenceName(canonicalName);
}

throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
}

Expand All @@ -62,22 +68,30 @@ public bool EquivalentTo(string? name) =>

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));
}