Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Added Bitbucket Server regex for urls. #249

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion SourceLink.Create.BitBucket/BitBucketUrlConverter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

using System.Text.RegularExpressions;

namespace SourceLink.Create.BitBucket
{
public static class UrlConverter
{
public static string Convert(string origin)
{
if (!origin.Contains("bitbucket.org")) return null;
// Process as Bitbucket Server url if not from bitbucket.org
if (!origin.Contains("bitbucket.org")) return ConvertServer(origin);
if (origin.StartsWith("git@"))
{
origin = origin.Replace(':', '/');
Expand All @@ -15,5 +18,26 @@ public static string Convert(string origin)
var uri = new System.Uri(origin);
return "https://bitbucket.org" + uri.LocalPath + "/raw/{commit}/*";
}

public static string ConvertServer(string origin)
{
// Try match https:// url.
var match = Regex.Match(origin, @"^https:\/\/(.*)\/scm\/(.*)\/(.*)\.git$", RegexOptions.IgnoreCase);
if (match.Success == false)
{
// Try match ssh://git url.
match = Regex.Match(origin, @"^ssh:\/\/git@(.*):\d+\/(.*)\/(.*)\.git$", RegexOptions.IgnoreCase);
}

// Any match?
if (match.Success)
{
return $"https://{match.Groups[1]}/projects/{match.Groups[2]}/repos/{match.Groups[3]}/raw/*?at={{commit}}";
}
else
{
return null;
}
}
}
}