Skip to content

Commit

Permalink
Updated Suggestion providers
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
Richard Hessinger committed May 26, 2022
1 parent c306d70 commit 3e513c8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Chocolatey/InedoExtension/InedoExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<LangVersion>Latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Inedo.SDK" Version="2.0.0" ExcludeAssets="runtime" />
<PackageReference Include="NuGet.Protocol" Version="6.1.0" />
<PackageReference Include="NuGet.Packaging" Version="6.1.0" />
<PackageReference Include="Inedo.SDK" Version="2.0.1" ExcludeAssets="runtime" />
<PackageReference Include="NuGet.Protocol" Version="6.2.0" />
<PackageReference Include="NuGet.Packaging" Version="6.2.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using System.Threading;
using System.Threading.Tasks;
using Inedo.Extensibility;
using Inedo.Extensibility.Credentials;
using Inedo.Extensibility.SecureResources;
using Inedo.Extensions.Chocolatey.Credentials;
using Inedo.Extensions.Credentials;
using Inedo.Web;
using NuGet.Common;
using NuGet.Configuration;
Expand All @@ -13,23 +17,60 @@ namespace Inedo.Extensions.Chocolatey.SuggestionProviders
{
internal sealed class PackageNameSuggestionProvider : ISuggestionProvider
{
public Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfiguration config)
public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfiguration config)
{
return Task.FromResult(GetSuggestions(AH.CoalesceString(config["PackageName"], config["Name"]), AH.CoalesceString(config["Source"], "https://chocolatey.org/api/v2")));
}

private static IEnumerable<string> GetSuggestions(string packageName, string source)
{
if (SpecialSourceSuggestionProvider.SpecialSources.Contains(source))

if (SpecialSourceSuggestionProvider.SpecialSources.Contains(config["Source"]))
return Enumerable.Empty<string>();
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV2(new PackageSource(source) { ProtocolVersion = 2 });


var packageSource = string.IsNullOrWhiteSpace(config["ResourceName"]) ? null : (ChocolateySourceSecureResource)SecureResource.Create(config["ResourceName"], config.EditorContext as IResourceResolutionContext);
var sourceUrl = AH.CoalesceString(config["Source"], packageSource?.SourceUrl, "https://chocolatey.org/api/v2");

PackageSourceCredential credentials = null;
if (!string.IsNullOrWhiteSpace(config["UserName"]) && !string.IsNullOrWhiteSpace(config["Password"]))
{
credentials = new PackageSourceCredential(
sourceUrl,
config["UserName"],
config["Password"],
true,
null
);
}
else
{
var packageCredentials = packageSource?.GetCredentials(config.EditorContext as ICredentialResolutionContext);
if (packageCredentials != null && packageCredentials is UsernamePasswordCredentials usernamePassword)
{
credentials = new PackageSourceCredential(
sourceUrl,
usernamePassword.UserName,
AH.Unprotect(usernamePassword.Password),
true,
null
);
}
else if (packageCredentials != null && packageCredentials is TokenCredentials token)
{
credentials = new PackageSourceCredential(
sourceUrl,
"API",
AH.Unprotect(token.Token),
true,
null
);
}
}


SourceRepository repository = Repository.Factory.GetCoreV2(new PackageSource(sourceUrl) { ProtocolVersion = 2, Credentials = credentials });
var resource = repository.GetResource<AutoCompleteResource>();
var resultsTask = resource.IdStartsWith(packageName, false, logger, cancellationToken);
resultsTask.Wait();
return resultsTask.Result;
return await resource.IdStartsWith(config["PackageName"], false, logger, cancellationToken);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,68 @@ namespace Inedo.Extensions.Chocolatey.SuggestionProviders
{
internal sealed class VersionSuggestionProvider : ISuggestionProvider
{
public Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfiguration config)
public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfiguration config)
{
return Task.FromResult(GetSuggestions(config, AH.CoalesceString(config["PackageName"], config["Name"]), config["Version"], config["Source"], AH.CoalesceString(config["ResourceName"], config["From"]), config["UserName"], AH.CreateSecureString(config["Password"])));
}

internal static IEnumerable<string> GetSuggestions(IComponentConfiguration config, string packageName, string partialVersion, string source, string resourceName, string userName, SecureString password)
{
if (SpecialSourceSuggestionProvider.SpecialSources.Contains(source))

if (SpecialSourceSuggestionProvider.SpecialSources.Contains(config["Source"]))
return Enumerable.Empty<string>();

ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceCacheContext cache = new SourceCacheContext { NoCache = true };


var packageSource = string.IsNullOrWhiteSpace(resourceName) ? null : (ChocolateySourceSecureResource)SecureResource.Create(resourceName, config.EditorContext as IResourceResolutionContext);
var sourceUrl = AH.CoalesceString(source, packageSource?.SourceUrl, "https://chocolatey.org/api/v2");
var packageSource = string.IsNullOrWhiteSpace(config["ResourceName"]) ? null : (ChocolateySourceSecureResource)SecureResource.Create(config["ResourceName"], config.EditorContext as IResourceResolutionContext);
var sourceUrl = AH.CoalesceString(config["Source"], packageSource?.SourceUrl, "https://chocolatey.org/api/v2");

SourceRepository repository = Repository.Factory.GetCoreV2(
new PackageSource(sourceUrl)
PackageSourceCredential credentials = null;
if (!string.IsNullOrWhiteSpace(config["UserName"]) && !string.IsNullOrWhiteSpace(config["Password"]))
{
credentials = new PackageSourceCredential(
sourceUrl,
config["UserName"],
config["Password"],
true,
null
);
}
else
{
var packageCredentials = packageSource?.GetCredentials(config.EditorContext as ICredentialResolutionContext);
if(packageCredentials != null && packageCredentials is UsernamePasswordCredentials usernamePassword)
{
ProtocolVersion = 2,
Credentials = packageSource?.GetCredentials(config.EditorContext as ICredentialResolutionContext) is not UsernamePasswordCredentials credentials
? null
: new PackageSourceCredential(
credentials = new PackageSourceCredential(
sourceUrl,
usernamePassword.UserName,
AH.Unprotect(usernamePassword.Password),
true,
null
);
}
else if(packageCredentials != null && packageCredentials is TokenCredentials token)
{
credentials = new PackageSourceCredential(
sourceUrl,
AH.CoalesceString(userName, credentials.UserName),
AH.CoalesceString(AH.Unprotect(password), AH.Unprotect(credentials.Password)),
"API",
AH.Unprotect(token.Token),
true,
null
)
);
}
}

SourceRepository repository = Repository.Factory.GetCoreV2(
new PackageSource(sourceUrl)
{
ProtocolVersion = 2,
Credentials = credentials,
IsMachineWide = false
}
);
var resource = repository.GetResource<AutoCompleteResource>();
var versionsTask = resource.VersionStartsWith(packageName, partialVersion, false, cache, logger, cancellationToken);
versionsTask.Wait();
return versionsTask.Result.OrderByDescending(v => v).Select(v => v.ToFullString());
var versions = await resource.VersionStartsWith(config["PackageName"], config["Version"], false, cache, logger, cancellationToken);

return versions.OrderByDescending(v => v).Select(v => v.ToFullString());

}
}
Expand Down

0 comments on commit 3e513c8

Please sign in to comment.