Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #212 from Ducatel/add-urlencode-filename-token
Browse files Browse the repository at this point in the history
Add urlencode filename token
  • Loading branch information
GeertvanHorrik authored Jun 6, 2019
2 parents 3ef11e0 + f17b9be commit 9628f44
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Please keep the list sorted.

Andrew Arnott <[email protected]>
David Ducatel <[email protected]>
Geert van Horrik <[email protected]>
Wesley Eledui <[email protected]>
Marek Fišera <[email protected]>
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ When working with a repository using uncommon URL you can use placeholders to sp

GitLink.exe <pdbfile> -u "https://host/projects/catel/repos/catel/browse/{filename}?at={revision}&raw"

Or if you require URL encoded filename you can use `urlencoded_filename` token

GitLink.exe <pdbfile> -u "http://host/api/v4/projects/42/repository/files/{urlencoded_filename}/raw?ref={revision}"

The custom url will be used to fill the placeholders with the relative file path and the revision hash.

### Git repository location
Expand Down
13 changes: 8 additions & 5 deletions src/GitLink.Tests/Providers/CustomUrlProviderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TheInitialization
[TestCase("https://example.com/repo", false)]
[TestCase("https://bitbucket.intra.company.com/projects/aaa/repos/a/browse/{filename}?raw", true)]
[TestCase("gopher://example.com/repo", false)]
[TestCase("http://gitlab.com/api/v4/projects/42/repository/files/{urlencoded_filename}/raw?ref={revision}&private_token=superToken", true)]
public void CorrectlyValidatesForUrls(string url, bool expectedValue)
{
var provider = new CustomUrlProvider();
Expand Down Expand Up @@ -73,14 +74,16 @@ public void ReturnsNullProjectUrl()
Assert.IsNull(provider.ProjectUrl);
}

[TestCase]
public void ReturnsValidRawGitUrl()
[TestCase(CorrectUrl)]
[TestCase("http://gitlab.com/api/v4/projects/42/repository/files/{urlencoded_filename}/raw?ref={revision}&private_token=superToken")]
public void ReturnsValidRawGitUrl(string url)
{
var provider = new CustomUrlProvider();
provider.Initialize(CorrectUrl);
provider.Initialize(url);

string correctReturnedUrl = CorrectUrl.Replace("{filename}", "%var2%");
correctReturnedUrl = correctReturnedUrl.Replace("{revision}", "{0}");
string correctReturnedUrl = url.Replace("{filename}", "%var2%")
.Replace("{revision}", "{0}")
.Replace("{urlencoded_filename}", "%var2%");

Assert.AreEqual(correctReturnedUrl, provider.RawGitUrl);
}
Expand Down
3 changes: 3 additions & 0 deletions src/GitLink/GitLink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
<ItemGroup>
<Content Include="Logo.ico" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>
</Project>
14 changes: 11 additions & 3 deletions src/GitLink/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace GitLink
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using Catel;
using Catel.Logging;
using GitTools;
Expand All @@ -28,6 +29,7 @@ public static class Linker
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

private static readonly string FilenamePlaceholder = Uri.EscapeUriString("{filename}");
private static readonly string UrlEncodedFileNamePlaceHolder = Uri.EscapeUriString("{urlencoded_filename}");
private static readonly string RevisionPlaceholder = Uri.EscapeUriString("{revision}");
private static readonly string PdbStrExePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "pdbstr.exe");
private static readonly string[] ExtensionsToIgnore = new string[] { ".g.cs" };
Expand Down Expand Up @@ -170,17 +172,18 @@ public static class Linker
}

string rawUrl = provider.RawGitUrl;
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder))
if (rawUrl.Contains(RevisionPlaceholder) || rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder))
{
if (!rawUrl.Contains(RevisionPlaceholder) || !rawUrl.Contains(FilenamePlaceholder))
if (!rawUrl.Contains(RevisionPlaceholder) || !(rawUrl.Contains(FilenamePlaceholder) || rawUrl.Contains(UrlEncodedFileNamePlaceHolder)))
{
Log.Error("Supplied custom URL pattern must contain both a revision and a filename placeholder.");
return false;
}

rawUrl = rawUrl
.Replace(RevisionPlaceholder, "{0}")
.Replace(FilenamePlaceholder, "%var2%");
.Replace(FilenamePlaceholder, "%var2%")
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
}
else
{
Expand Down Expand Up @@ -217,6 +220,11 @@ public static class Linker
if (sourceFile.Value != null)
{
var relativePathForUrl = ReplaceSlashes(provider, sourceFile.Value);
if (provider.RawGitUrl.Contains(UrlEncodedFileNamePlaceHolder))
{
relativePathForUrl = HttpUtility.UrlEncode(relativePathForUrl);
}

srcSrvContext.Paths.Add(Tuple.Create(sourceFile.Key, relativePathForUrl));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/GitLink/Providers/CustomUrlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace GitLink.Providers
public sealed class CustomUrlProvider : ProviderBase
{
private const string FileNamePlaceHolder = "{filename}";
private const string UrlEncodedFileNamePlaceHolder = "{urlencoded_filename}";
private const string RevisionPlaceHolder = "{revision}";
private static readonly Regex HostingUrlPattern = new Regex(@"https?://.+");

Expand All @@ -27,14 +28,16 @@ public CustomUrlProvider()
public override bool Initialize(string url)
{
if (string.IsNullOrEmpty(url) || !HostingUrlPattern.IsMatch(url) ||
(!url.Contains(FileNamePlaceHolder) && !url.Contains(RevisionPlaceHolder)))
(!(url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder)) &&
!url.Contains(RevisionPlaceHolder)))
{
return false;
}

if (url.Contains(FileNamePlaceHolder))
if (url.Contains(FileNamePlaceHolder) || url.Contains(UrlEncodedFileNamePlaceHolder))
{
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%");
_rawUrl = url.Replace(FileNamePlaceHolder, "%var2%")
.Replace(UrlEncodedFileNamePlaceHolder, "%var2%");
}

if (url.Contains(RevisionPlaceHolder))
Expand Down

0 comments on commit 9628f44

Please sign in to comment.