|
1 | 1 | namespace GitVersion |
2 | 2 | { |
| 3 | + using LibGit2Sharp; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Linq; |
5 | | - using LibGit2Sharp; |
| 6 | + using System.Text.RegularExpressions; |
6 | 7 |
|
7 | 8 | public static class GitHelper |
8 | 9 | { |
| 10 | + private const string MergeMessageRegexPattern = "refs/heads/pull(-requests)?/(?<issuenumber>[0-9]*)/merge(-clean)?"; |
| 11 | + |
9 | 12 | public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, string branch = null) |
10 | 13 | { |
11 | 14 | using (var repo = new Repository(gitDirectory)) |
@@ -42,6 +45,32 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut |
42 | 45 | } |
43 | 46 | } |
44 | 47 |
|
| 48 | + public static bool LooksLikeAValidPullRequestNumber(string issueNumber) |
| 49 | + { |
| 50 | + if (string.IsNullOrEmpty(issueNumber)) |
| 51 | + { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + uint res; |
| 56 | + return uint.TryParse(issueNumber, out res); |
| 57 | + } |
| 58 | + |
| 59 | + public static string ExtractIssueNumber(string mergeMessage) |
| 60 | + { |
| 61 | + // Github Message: refs/heads/pull/5/merge |
| 62 | + // Stash Message: refs/heads/pull-requests/5/merge-clean |
| 63 | + |
| 64 | + var regex = new Regex(MergeMessageRegexPattern); |
| 65 | + var match = regex.Match(mergeMessage); |
| 66 | + |
| 67 | + string issueNumber = null; |
| 68 | + |
| 69 | + issueNumber = match.Groups["issuenumber"].Value; |
| 70 | + |
| 71 | + return issueNumber; |
| 72 | + } |
| 73 | + |
45 | 74 | static void AddMissingRefSpecs(Repository repo, Remote remote) |
46 | 75 | { |
47 | 76 | if (remote.FetchRefSpecs.Any(r => r.Source == "refs/heads/*")) |
@@ -99,13 +128,13 @@ static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Authent |
99 | 128 | var canonicalName = refs[0].CanonicalName; |
100 | 129 | Logger.WriteInfo(string.Format("Found remote tip '{0}' pointing at the commit '{1}'.", canonicalName, headTipSha)); |
101 | 130 |
|
102 | | - if (!canonicalName.StartsWith("refs/pull/")) |
| 131 | + if (!canonicalName.StartsWith("refs/pull/") && !canonicalName.StartsWith("refs/pull-requests/")) |
103 | 132 | { |
104 | 133 | var message = string.Format("Remote tip '{0}' from remote '{1}' doesn't look like a valid pull request.", canonicalName, remote.Url); |
105 | 134 | throw new WarningException(message); |
106 | 135 | } |
107 | 136 |
|
108 | | - var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/"); |
| 137 | + var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); |
109 | 138 |
|
110 | 139 | Logger.WriteInfo(string.Format("Creating fake local branch '{0}'.", fakeBranchName)); |
111 | 140 | repo.Refs.Add(fakeBranchName, new ObjectId(headTipSha)); |
|
0 commit comments