Skip to content

Commit 19abab7

Browse files
authored
Fix JFrog's NuGet Artifactory (v2 endpoint) bug (#1428)
1 parent 389c1a5 commit 19abab7

File tree

8 files changed

+378
-268
lines changed

8 files changed

+378
-268
lines changed

src/code/IServerAPICalls.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,7 @@ public interface IServerAPICalls
8383
/// Name: no wildcard support.
8484
/// Examples: Install "PowerShellGet"
8585
/// </summary>
86-
Stream InstallName(string packageName, bool includePrerelease, out ErrorRecord errRecord);
87-
88-
/// <summary>
89-
/// Installs package with specific name and version.
90-
/// Name: no wildcard support.
91-
/// Version: no wildcard support.
92-
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
93-
/// Install "PowerShellGet" -Version "3.0.0-beta16"
94-
/// </summary>
95-
Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord);
86+
Stream InstallPackage(string packageName, string packageVersion, bool includePrerelease, out ErrorRecord errRecord);
9687

9788
#endregion
9889
}

src/code/InstallHelper.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ private List<PSResourceInfo> InstallPackages(
549549
// and value as a Hashtable of specific package info:
550550
// packageName, { version = "", isScript = "", isModule = "", pkg = "", etc. }
551551
// Install parent package to the temp directory.
552-
Hashtable packagesHash = InstallPackage(
552+
Hashtable packagesHash = BeginPackageInstall(
553553
searchVersionType: _versionType,
554554
specificVersion: _nugetVersion,
555555
versionRange: _versionRange,
@@ -617,7 +617,7 @@ private List<PSResourceInfo> InstallPackages(
617617
}
618618
}
619619

620-
packagesHash = InstallPackage(
620+
packagesHash = BeginPackageInstall(
621621
searchVersionType: VersionType.SpecificVersion,
622622
specificVersion: depVersion,
623623
versionRange: null,
@@ -683,7 +683,7 @@ private List<PSResourceInfo> InstallPackages(
683683
/// <summary>
684684
/// Installs a single package to the temporary path.
685685
/// </summary>
686-
private Hashtable InstallPackage(
686+
private Hashtable BeginPackageInstall(
687687
VersionType searchVersionType,
688688
NuGetVersion specificVersion,
689689
VersionRange versionRange,
@@ -839,25 +839,11 @@ private Hashtable InstallPackage(
839839
{
840840
// Download the package.
841841
string pkgName = pkgToInstall.Name;
842-
Stream responseStream;
843-
844-
if (searchVersionType == VersionType.NoVersion && !_prerelease)
842+
Stream responseStream = currentServer.InstallPackage(pkgName, pkgVersion, _prerelease, out ErrorRecord installNameErrRecord);
843+
if (installNameErrRecord != null)
845844
{
846-
responseStream = currentServer.InstallName(pkgName, _prerelease, out ErrorRecord installNameErrRecord);
847-
if (installNameErrRecord != null)
848-
{
849-
errRecord = installNameErrRecord;
850-
return packagesHash;
851-
}
852-
}
853-
else
854-
{
855-
responseStream = currentServer.InstallVersion(pkgName, pkgVersion, out ErrorRecord installVersionErrRecord);
856-
if (installVersionErrRecord != null)
857-
{
858-
errRecord = installVersionErrRecord;
859-
return packagesHash;
860-
}
845+
errRecord = installNameErrRecord;
846+
return packagesHash;
861847
}
862848

863849
bool installedToTempPathSuccessfully = _asNupkg ? TrySaveNupkgToTempPath(responseStream, tempInstallPath, pkgName, pkgVersion, pkgToInstall, packagesHash, out updatedPackagesHash, out errRecord) :

src/code/LocalServerApiCalls.cs

Lines changed: 159 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -217,157 +217,28 @@ public override FindResults FindVersionWithTag(string packageName, string versio
217217
/** INSTALL APIS **/
218218

219219
/// <summary>
220-
/// Installs specific package.
220+
/// Installs a specific package.
221221
/// Name: no wildcard support.
222222
/// Examples: Install "PowerShellGet"
223-
/// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable)
224-
/// if prerelease, call into InstallVersion instead.
225-
/// </summary>
226-
public override Stream InstallName(string packageName, bool includePrerelease, out ErrorRecord errRecord)
227-
{
228-
_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallName()");
229-
FileStream fs = null;
230-
errRecord = null;
231-
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.*", WildcardOptions.IgnoreCase);
232-
NuGetVersion latestVersion = new NuGetVersion("0.0.0.0");
233-
String latestVersionPath = String.Empty;
234-
235-
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
236-
{
237-
string packageFullName = Path.GetFileName(path);
238-
239-
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
240-
{
241-
_cmdletPassedIn.WriteDebug($"'{packageName}' found in '{path}'");
242-
string[] packageWithoutName = packageFullName.ToLower().Split(new string[]{ $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
243-
string packageVersionAndExtension = packageWithoutName[0];
244-
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
245-
string version = packageVersionAndExtension.Substring(0, extensionDot);
246-
_cmdletPassedIn.WriteDebug($"Parsing version '{version}' of package '{packageName}'");
247-
NuGetVersion.TryParse(version, out NuGetVersion nugetVersion);
248-
249-
if ((!nugetVersion.IsPrerelease || includePrerelease) && (nugetVersion > latestVersion))
250-
{
251-
latestVersion = nugetVersion;
252-
latestVersionPath = path;
253-
}
254-
}
255-
}
256-
257-
if (String.IsNullOrEmpty(latestVersionPath))
258-
{
259-
errRecord = new ErrorRecord(
260-
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
261-
"InstallNameFailure",
262-
ErrorCategory.ResourceUnavailable,
263-
this);
264-
265-
return fs;
266-
}
267-
268-
try
269-
{
270-
_cmdletPassedIn.WriteDebug($"Reading file '{latestVersionPath}'");
271-
fs = new FileStream(latestVersionPath, FileMode.Open, FileAccess.Read);
272-
if (fs == null)
273-
{
274-
errRecord = new ErrorRecord(
275-
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
276-
"InstallNameFailure",
277-
ErrorCategory.ResourceUnavailable,
278-
this);
279-
}
280-
}
281-
catch (Exception e)
282-
{
283-
errRecord = new ErrorRecord(
284-
exception: e,
285-
"InstallNameFailure",
286-
ErrorCategory.ReadError,
287-
this);
288-
}
289-
290-
return fs;
291-
}
292-
293-
/// <summary>
294-
/// Installs package with specific name and version.
295-
/// Name: no wildcard support.
296-
/// Version: no wildcard support.
297-
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
298-
/// Install "PowerShellGet" -Version "3.0.0-beta16"
299-
/// API Call: https://www.powershellgallery.com/api/v2/package/Id/version (version can be prerelease)
223+
/// Install "PowerShellGet" -Version "3.0.0"
300224
/// </summary>
301-
public override Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord)
225+
public override Stream InstallPackage(string packageName, string packageVersion, bool includePrerelease, out ErrorRecord errRecord)
302226
{
303-
_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallVersion()");
304-
errRecord = null;
305-
FileStream fs = null;
306-
307-
// if 4 digits and last is 0, create 3 digit equiv string
308-
// 4 digit version (where last is 0) is always passed in.
309-
NuGetVersion.TryParse(version, out NuGetVersion pkgVersion);
310-
_cmdletPassedIn.WriteDebug($"Version parsed as '{pkgVersion}'");
311-
312-
if (pkgVersion.Revision == 0)
227+
Stream results = new MemoryStream();
228+
if (string.IsNullOrEmpty(packageVersion))
313229
{
314-
version = pkgVersion.ToNormalizedString();
230+
results = InstallName(packageName, includePrerelease, out errRecord);
315231
}
316-
317-
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.{version}.nupkg*", WildcardOptions.IgnoreCase);
318-
String pkgVersionPath = String.Empty;
319-
320-
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
321-
{
322-
string packageFullName = Path.GetFileName(path);
323-
324-
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
325-
{
326-
_cmdletPassedIn.WriteDebug($"Found match with '{path}'");
327-
pkgVersionPath = path;
328-
}
232+
else {
233+
results = InstallVersion(packageName, packageVersion, out errRecord);
329234
}
330235

331-
if (String.IsNullOrEmpty(pkgVersionPath))
332-
{
333-
errRecord = new ErrorRecord(
334-
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
335-
"InstallNameFailure",
336-
ErrorCategory.ResourceUnavailable,
337-
this);
338-
339-
return fs;
340-
}
341-
342-
try
343-
{
344-
_cmdletPassedIn.WriteDebug($"Reading file '{pkgVersionPath}'");
345-
fs = new FileStream(pkgVersionPath, FileMode.Open, FileAccess.Read);
346-
347-
if (fs == null)
348-
{
349-
errRecord = new ErrorRecord(
350-
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
351-
"InstallNameFailure",
352-
ErrorCategory.ResourceUnavailable,
353-
this);
354-
}
355-
}
356-
catch (Exception e)
357-
{
358-
errRecord = new ErrorRecord(
359-
exception: e,
360-
"InstallVersionFailure",
361-
ErrorCategory.ReadError,
362-
this);
363-
}
364-
365-
return fs;
236+
return results;
366237
}
367238

368239
#endregion
369240

370-
#region LocalRepo Specific Methods
241+
#region Private Methods
371242

372243
/// <summary>
373244
/// Helper method called by FindName() and FindNameWithTag()
@@ -592,6 +463,155 @@ private FindResults FindTagsHelper(string[] tags, bool includePrerelease, out Er
592463
return findResponse;
593464
}
594465

466+
/// <summary>
467+
/// Installs specific package.
468+
/// Name: no wildcard support.
469+
/// Examples: Install "PowerShellGet"
470+
/// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable)
471+
/// if prerelease, call into InstallVersion instead.
472+
/// </summary>
473+
private Stream InstallName(string packageName, bool includePrerelease, out ErrorRecord errRecord)
474+
{
475+
_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallName()");
476+
FileStream fs = null;
477+
errRecord = null;
478+
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.*", WildcardOptions.IgnoreCase);
479+
NuGetVersion latestVersion = new NuGetVersion("0.0.0.0");
480+
String latestVersionPath = String.Empty;
481+
482+
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
483+
{
484+
string packageFullName = Path.GetFileName(path);
485+
486+
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
487+
{
488+
_cmdletPassedIn.WriteDebug($"'{packageName}' found in '{path}'");
489+
string[] packageWithoutName = packageFullName.ToLower().Split(new string[] { $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
490+
string packageVersionAndExtension = packageWithoutName[0];
491+
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
492+
string version = packageVersionAndExtension.Substring(0, extensionDot);
493+
_cmdletPassedIn.WriteDebug($"Parsing version '{version}' of package '{packageName}'");
494+
NuGetVersion.TryParse(version, out NuGetVersion nugetVersion);
495+
496+
if ((!nugetVersion.IsPrerelease || includePrerelease) && (nugetVersion > latestVersion))
497+
{
498+
latestVersion = nugetVersion;
499+
latestVersionPath = path;
500+
}
501+
}
502+
}
503+
504+
if (String.IsNullOrEmpty(latestVersionPath))
505+
{
506+
errRecord = new ErrorRecord(
507+
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
508+
"InstallNameFailure",
509+
ErrorCategory.ResourceUnavailable,
510+
this);
511+
512+
return fs;
513+
}
514+
515+
try
516+
{
517+
_cmdletPassedIn.WriteDebug($"Reading file '{latestVersionPath}'");
518+
fs = new FileStream(latestVersionPath, FileMode.Open, FileAccess.Read);
519+
if (fs == null)
520+
{
521+
errRecord = new ErrorRecord(
522+
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
523+
"InstallNameFailure",
524+
ErrorCategory.ResourceUnavailable,
525+
this);
526+
}
527+
}
528+
catch (Exception e)
529+
{
530+
errRecord = new ErrorRecord(
531+
exception: e,
532+
"InstallNameFailure",
533+
ErrorCategory.ReadError,
534+
this);
535+
}
536+
537+
return fs;
538+
}
539+
540+
/// <summary>
541+
/// Installs package with specific name and version.
542+
/// Name: no wildcard support.
543+
/// Version: no wildcard support.
544+
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
545+
/// Install "PowerShellGet" -Version "3.0.0-beta16"
546+
/// API Call: https://www.powershellgallery.com/api/v2/package/Id/version (version can be prerelease)
547+
/// </summary>
548+
private Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord)
549+
{
550+
_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallVersion()");
551+
errRecord = null;
552+
FileStream fs = null;
553+
554+
// if 4 digits and last is 0, create 3 digit equiv string
555+
// 4 digit version (where last is 0) is always passed in.
556+
NuGetVersion.TryParse(version, out NuGetVersion pkgVersion);
557+
_cmdletPassedIn.WriteDebug($"Version parsed as '{pkgVersion}'");
558+
559+
if (pkgVersion.Revision == 0)
560+
{
561+
version = pkgVersion.ToNormalizedString();
562+
}
563+
564+
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.{version}.nupkg*", WildcardOptions.IgnoreCase);
565+
String pkgVersionPath = String.Empty;
566+
567+
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
568+
{
569+
string packageFullName = Path.GetFileName(path);
570+
571+
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
572+
{
573+
_cmdletPassedIn.WriteDebug($"Found match with '{path}'");
574+
pkgVersionPath = path;
575+
}
576+
}
577+
578+
if (String.IsNullOrEmpty(pkgVersionPath))
579+
{
580+
errRecord = new ErrorRecord(
581+
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
582+
"InstallNameFailure",
583+
ErrorCategory.ResourceUnavailable,
584+
this);
585+
586+
return fs;
587+
}
588+
589+
try
590+
{
591+
_cmdletPassedIn.WriteDebug($"Reading file '{pkgVersionPath}'");
592+
fs = new FileStream(pkgVersionPath, FileMode.Open, FileAccess.Read);
593+
594+
if (fs == null)
595+
{
596+
errRecord = new ErrorRecord(
597+
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
598+
"InstallNameFailure",
599+
ErrorCategory.ResourceUnavailable,
600+
this);
601+
}
602+
}
603+
catch (Exception e)
604+
{
605+
errRecord = new ErrorRecord(
606+
exception: e,
607+
"InstallVersionFailure",
608+
ErrorCategory.ReadError,
609+
this);
610+
}
611+
612+
return fs;
613+
}
614+
595615
/// <summary>
596616
/// Extract metadata from .nupkg package file.
597617
/// This is called only for packages that are ascertained to be a match for our search criteria.

0 commit comments

Comments
 (0)