Skip to content

Commit

Permalink
Merge pull request #107 from ritchxu/ritchxu/ghae
Browse files Browse the repository at this point in the history
Support using private instance repo URL in upload
  • Loading branch information
jcansdale authored Apr 23, 2021
2 parents 996438c + 9345690 commit 274aa3b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Commands:
setApiKey Set GitHub API key/personal access token
Run 'gpr [command] --help' for more information about a command.
```
```
34 changes: 32 additions & 2 deletions src/GprTool/NuGetUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using NuGet.Packaging;
Expand All @@ -20,6 +22,7 @@ public class PackageFile

public string Filename { get; set; }
public string FilenameAbsolutePath { get; set; }
public string NugetPackageEndpoint { get; set; }
}

public class NuGetUtilities
Expand All @@ -39,7 +42,7 @@ public static bool BuildOwnerAndRepositoryFromUrl(PackageFile packageFile, strin
}

if (!Uri.TryCreate(repositoryUrl, UriKind.Absolute, out var repositoryUri)
|| repositoryUri.Host != "github.com")
|| !IsSupportedHost(repositoryUri.Host))
{
return false;
}
Expand All @@ -66,7 +69,15 @@ public static bool BuildOwnerAndRepositoryFromUrl(PackageFile packageFile, strin

packageFile.Owner = ownerAndRepositoryName[0];
packageFile.RepositoryName = ownerAndRepositoryName[1];
packageFile.RepositoryUrl = $"https://github.com/{packageFile.Owner}/{packageFile.RepositoryName}";
packageFile.RepositoryUrl = $"https://{repositoryUri.Host}/{packageFile.Owner}/{packageFile.RepositoryName}";
if (repositoryUri.Host != "github.com")
{
packageFile.NugetPackageEndpoint = $"nuget.{repositoryUri.Host}";
}
else
{
packageFile.NugetPackageEndpoint = $"nuget.pkg.github.com";
}

return true;
}
Expand Down Expand Up @@ -291,6 +302,25 @@ public static string GetDefaultConfigFile(Action<string> warning = null)
return Path.Combine(baseDir, "NuGet", "NuGet.Config");
}

private static bool IsSupportedHost(string host)
{
foreach (string supported in m_supportedHosts)
{
Regex rgx = new Regex(supported);
if (rgx.IsMatch(host))
{
return true;
}
}

return false;
}

private static readonly List<string> m_supportedHosts = new List<string>()
{
"github.com",
@".*\.githubenterprise\.com"
};
}

public class DisposableDirectory : IDisposable
Expand Down
2 changes: 1 addition & 1 deletion src/GprTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ static async Task<IRestResponse> UploadPackageAsyncImpl(PackageFile packageFile,
if (packageFile == null) throw new ArgumentNullException(nameof(packageFile));
if (packageStream == null) throw new ArgumentNullException(nameof(packageStream));

var client = WithRestClient($"https://nuget.pkg.github.com/{packageFile.Owner}/",
var client = WithRestClient($"https://{packageFile.NugetPackageEndpoint}/{packageFile.Owner}/",
x =>
{
x.Authenticator = new HttpBasicAuthenticator(user, token);
Expand Down
21 changes: 21 additions & 0 deletions test/GprTool.Tests/NuGetUtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ public void BuildOwnerAndRepositoryFromUrlFromNupkg(string repositoryUrl, string
Assert.That(packageFile.RepositoryUrl, Is.EqualTo(expectedGithubRepositoryUrl));
}

[TestCase("ritchxu/gpr", true, "ritchxu", "gpr", "https://github.com/ritchxu/gpr", "nuget.pkg.github.com")]
[TestCase("https://foo.githubenterprise.com/ritchxu/gpr", true, "ritchxu", "gpr", "https://foo.githubenterprise.com/ritchxu/gpr", "nuget.foo.githubenterprise.com")]
[TestCase("https://foo.bar.com/ritchxu/gpr", false, null, null, null, null)]
public void BuildOwnerAndRepositoryFromUrl(
string repositoryUrl,
bool expectedResult,
string expectedOwner,
string expectedRepositoryName,
string expectedGithubRepositoryUrl,
string expectedNugetPackageEndpoint)
{
var packageFile = new PackageFile();

Assert.That(NuGetUtilities.BuildOwnerAndRepositoryFromUrl(packageFile, repositoryUrl), Is.EqualTo(expectedResult));

Assert.That(packageFile.Owner, Is.EqualTo(expectedOwner));
Assert.That(packageFile.RepositoryName, Is.EqualTo(expectedRepositoryName));
Assert.That(packageFile.RepositoryUrl, Is.EqualTo(expectedGithubRepositoryUrl));
Assert.That(packageFile.NugetPackageEndpoint, Is.EqualTo(expectedNugetPackageEndpoint));
}

[Test]
public void ReadNupkgManifest()
{
Expand Down

0 comments on commit 274aa3b

Please sign in to comment.