Skip to content

Commit 3b6a100

Browse files
authored
Merge pull request #3181 from enriqueraso/bug/inherit-fail-on-azure-pullrequest
Fix issue with pullrequests in Azure DevOps
2 parents 8f85fab + 4b18753 commit 3b6a100

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class PullRequestInBuildAgentTest
1515
{
1616
"refs/pull-requests/5/merge",
1717
"refs/pull/5/merge",
18-
"refs/heads/pull/5/head"
18+
"refs/heads/pull/5/head",
19+
"refs/remotes/pull/5/merge",
20+
"refs/remotes/pull-requests/5/merge"
1921
};
2022

2123
[TestCaseSource(nameof(PrMergeRefs))]
@@ -49,7 +51,6 @@ public async Task VerifyContinuaCIPullRequest(string pullRequestRef)
4951
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
5052
}
5153

52-
5354
[TestCaseSource(nameof(PrMergeRefs))]
5455
public async Task VerifyDronePullRequest(string pullRequestRef)
5556
{
@@ -128,7 +129,6 @@ public async Task VerifyTravisCIPullRequest(string pullRequestRef)
128129
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
129130
}
130131

131-
132132
[TestCaseSource(nameof(PrMergeRefs))]
133133
public async Task VerifyBitBucketPipelinesPullRequest(string pullRequestRef)
134134
{
@@ -180,4 +180,23 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu
180180
// Cleanup repository files
181181
DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
182182
}
183+
184+
private static readonly object[] PrMergeRefInputs =
185+
{
186+
new object[] { "refs/pull-requests/5/merge", "refs/pull-requests/5/merge", false, true, false },
187+
new object[] { "refs/pull/5/merge", "refs/pull/5/merge", false, true, false},
188+
new object[] { "refs/heads/pull/5/head", "pull/5/head", true, false, false },
189+
new object[] { "refs/remotes/pull/5/merge", "pull/5/merge", false, true, true },
190+
};
191+
192+
[TestCaseSource(nameof(PrMergeRefInputs))]
193+
public void VerifyPullRequestInput(string pullRequestRef, string friendly, bool isBranch, bool isPullRequest, bool isRemote)
194+
{
195+
var refName = new ReferenceName(pullRequestRef);
196+
197+
Assert.AreEqual(friendly, refName.Friendly);
198+
Assert.AreEqual(isBranch, refName.IsBranch);
199+
Assert.AreEqual(isPullRequest, refName.IsPullRequest);
200+
Assert.AreEqual(isRemote, refName.IsRemoteBranch);
201+
}
183202
}

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,37 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
1111
private const string LocalBranchPrefix = "refs/heads/";
1212
private const string RemoteTrackingBranchPrefix = "refs/remotes/";
1313
private const string TagPrefix = "refs/tags/";
14-
private const string PullRequestPrefix1 = "refs/pull/";
15-
private const string PullRequestPrefix2 = "refs/pull-requests/";
14+
private static readonly string[] PullRequestPrefixes =
15+
{
16+
"refs/pull/",
17+
"refs/pull-requests/",
18+
"refs/remotes/pull/",
19+
"refs/remotes/pull-requests/"
20+
};
1621

1722
public ReferenceName(string canonical)
1823
{
1924
Canonical = canonical.NotNull();
20-
Friendly = Shorten();
21-
WithoutRemote = RemoveRemote();
2225

2326
IsBranch = IsPrefixedBy(Canonical, LocalBranchPrefix);
2427
IsRemoteBranch = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix);
2528
IsTag = IsPrefixedBy(Canonical, TagPrefix);
26-
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefix1) || IsPrefixedBy(Canonical, PullRequestPrefix2);
29+
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefixes);
30+
31+
Friendly = Shorten();
32+
WithoutRemote = RemoveRemote();
2733
}
2834

2935
public static ReferenceName Parse(string canonicalName)
3036
{
3137
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
3238
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
3339
|| IsPrefixedBy(canonicalName, TagPrefix)
34-
|| IsPrefixedBy(canonicalName, PullRequestPrefix1)
35-
|| IsPrefixedBy(canonicalName, PullRequestPrefix2))
40+
|| IsPrefixedBy(canonicalName, PullRequestPrefixes))
3641
{
3742
return new ReferenceName(canonicalName);
3843
}
44+
3945
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
4046
}
4147

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

6369
private string Shorten()
6470
{
65-
if (IsPrefixedBy(Canonical, LocalBranchPrefix))
71+
if (IsBranch)
6672
return Canonical.Substring(LocalBranchPrefix.Length);
67-
if (IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix))
73+
74+
if (IsRemoteBranch)
6875
return Canonical.Substring(RemoteTrackingBranchPrefix.Length);
69-
if (IsPrefixedBy(Canonical, TagPrefix))
76+
77+
if (IsTag)
7078
return Canonical.Substring(TagPrefix.Length);
79+
7180
return Canonical;
7281
}
7382

7483
private string RemoveRemote()
7584
{
76-
var isRemote = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix);
85+
if (IsRemoteBranch)
86+
{
87+
if (!IsPullRequest)
88+
return Friendly.Substring(Friendly.IndexOf("/", StringComparison.Ordinal) + 1);
89+
}
7790

78-
return isRemote
79-
? Friendly.Substring(Friendly.IndexOf("/", StringComparison.Ordinal) + 1)
80-
: Friendly;
91+
return Friendly;
8192
}
93+
8294
private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal);
95+
96+
private static bool IsPrefixedBy(string input, string[] prefixes) => prefixes.Any(prefix => IsPrefixedBy(input, prefix));
8397
}

0 commit comments

Comments
 (0)