@@ -43,7 +43,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
4343 const string containerRegistryOAuthTokenUrlTemplate = "https://{0}/oauth2/token" ; // 0 - registry
4444 const string containerRegistryManifestUrlTemplate = "https://{0}/v2/{1}/manifests/{2}" ; // 0 - registry, 1 - repo(modulename), 2 - tag(version)
4545 const string containerRegistryBlobDownloadUrlTemplate = "https://{0}/v2/{1}/blobs/{2}" ; // 0 - registry, 1 - repo(modulename), 2 - layer digest
46- const string containerRegistryFindImageVersionUrlTemplate = "https://{0}/acr/v1/ {1}/_tags{2} " ; // 0 - registry, 1 - repo(modulename), 2 - /tag(version )
46+ const string containerRegistryFindImageVersionUrlTemplate = "https://{0}/v2/ {1}/tags/list " ; // 0 - registry, 1 - repo(modulename)
4747 const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/" ; // 0 - registry, 1 - packagename
4848 const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}" ; // 0 - registry, 1 - location, 2 - digest
4949
@@ -286,7 +286,8 @@ public override Stream InstallPackage(string packageName, string packageVersion,
286286 return results ;
287287 }
288288
289- results = InstallVersion ( packageName , packageVersion , out errRecord ) ;
289+ string packageNameForInstall = PrependMARPrefix ( packageName ) ;
290+ results = InstallVersion ( packageNameForInstall , packageVersion , out errRecord ) ;
290291 return results ;
291292 }
292293
@@ -413,6 +414,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
413414 else
414415 {
415416 _cmdletPassedIn . WriteVerbose ( "Repository is unauthenticated" ) ;
417+ return null ;
416418 }
417419 }
418420
@@ -572,27 +574,19 @@ internal async Task<HttpContent> GetContainerRegistryBlobAsync(string packageNam
572574 /// </summary>
573575 internal JObject FindContainerRegistryImageTags ( string packageName , string version , string containerRegistryAccessToken , out ErrorRecord errRecord )
574576 {
575- /* response returned looks something like:
576- * "registry": "myregistry.azurecr.io"
577- * "imageName": "hello-world"
578- * "tags": [
579- * {
580- * ""name"": ""1.0.0"",
581- * ""digest"": ""sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a"",
582- * ""createdTime"": ""2023-12-23T18:06:48.9975733Z"",
583- * ""lastUpdateTime"": ""2023-12-23T18:06:48.9975733Z"",
584- * ""signed"": false,
585- * ""changeableAttributes"": {
586- * ""deleteEnabled"": true,
587- * ""writeEnabled"": true,
588- * ""readEnabled"": true,
589- * ""listEnabled"": true
590- * }
591- * }]
592- */
577+ /*
578+ {
579+ "name": "<name>",
580+ "tags": [
581+ "<tag1>",
582+ "<tag2>",
583+ "<tag3>"
584+ ]
585+ }
586+ */
593587 _cmdletPassedIn . WriteDebug ( "In ContainerRegistryServerAPICalls::FindContainerRegistryImageTags()" ) ;
594588 string resolvedVersion = string . Equals ( version , "*" , StringComparison . OrdinalIgnoreCase ) ? null : $ "/{ version } ";
595- string findImageUrl = string . Format ( containerRegistryFindImageVersionUrlTemplate , Registry , packageName , resolvedVersion ) ;
589+ string findImageUrl = string . Format ( containerRegistryFindImageVersionUrlTemplate , Registry , packageName ) ;
596590 var defaultHeaders = GetDefaultHeaders ( containerRegistryAccessToken ) ;
597591 return GetHttpResponseJObjectUsingDefaultHeaders ( findImageUrl , HttpMethod . Get , defaultHeaders , out errRecord ) ;
598592 }
@@ -1608,13 +1602,14 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16081602 string registryUrl = Repository . Uri . ToString ( ) ;
16091603 string packageNameLowercase = packageName . ToLower ( ) ;
16101604
1605+ string packageNameForFind = PrependMARPrefix ( packageNameLowercase ) ;
16111606 string containerRegistryAccessToken = GetContainerRegistryAccessToken ( out errRecord ) ;
16121607 if ( errRecord != null )
16131608 {
16141609 return emptyHashResponses ;
16151610 }
16161611
1617- var foundTags = FindContainerRegistryImageTags ( packageNameLowercase , "*" , containerRegistryAccessToken , out errRecord ) ;
1612+ var foundTags = FindContainerRegistryImageTags ( packageNameForFind , "*" , containerRegistryAccessToken , out errRecord ) ;
16181613 if ( errRecord != null || foundTags == null )
16191614 {
16201615 return emptyHashResponses ;
@@ -1623,7 +1618,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16231618 List < Hashtable > latestVersionResponse = new List < Hashtable > ( ) ;
16241619 List < JToken > allVersionsList = foundTags [ "tags" ] . ToList ( ) ;
16251620
1626- SortedDictionary < NuGet . Versioning . SemanticVersion , string > sortedQualifyingPkgs = GetPackagesWithRequiredVersion ( allVersionsList , versionType , versionRange , requiredVersion , packageNameLowercase , includePrerelease , out errRecord ) ;
1621+ SortedDictionary < NuGet . Versioning . SemanticVersion , string > sortedQualifyingPkgs = GetPackagesWithRequiredVersion ( allVersionsList , versionType , versionRange , requiredVersion , packageNameForFind , includePrerelease , out errRecord ) ;
16271622 if ( errRecord != null )
16281623 {
16291624 return emptyHashResponses ;
@@ -1634,7 +1629,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16341629 foreach ( var pkgVersionTag in pkgsInDescendingOrder )
16351630 {
16361631 string exactTagVersion = pkgVersionTag . Value . ToString ( ) ;
1637- Hashtable metadata = GetContainerRegistryMetadata ( packageNameLowercase , exactTagVersion , containerRegistryAccessToken , out errRecord ) ;
1632+ Hashtable metadata = GetContainerRegistryMetadata ( packageNameForFind , exactTagVersion , containerRegistryAccessToken , out errRecord ) ;
16381633 if ( errRecord != null || metadata . Count == 0 )
16391634 {
16401635 return emptyHashResponses ;
@@ -1664,58 +1659,55 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16641659
16651660 foreach ( var pkgVersionTagInfo in allPkgVersions )
16661661 {
1667- using ( JsonDocument pkgVersionEntry = JsonDocument . Parse ( pkgVersionTagInfo . ToString ( ) ) )
1662+ string pkgVersionString = pkgVersionTagInfo . ToString ( ) ;
1663+ // determine if the package version that is a repository tag is a valid NuGetVersion
1664+ if ( ! NuGetVersion . TryParse ( pkgVersionString , out NuGetVersion pkgVersion ) )
16681665 {
1669- JsonElement rootDom = pkgVersionEntry . RootElement ;
1670- if ( ! rootDom . TryGetProperty ( "name" , out JsonElement pkgVersionElement ) )
1671- {
1672- errRecord = new ErrorRecord (
1673- new InvalidOrEmptyResponse ( $ "Response does not contain version element ('name') for package '{ packageName } ' in '{ Repository . Name } '.") ,
1674- "FindNameFailure" ,
1675- ErrorCategory . InvalidResult ,
1676- this ) ;
1677-
1678- return null ;
1679- }
1680-
1681- string pkgVersionString = pkgVersionElement . ToString ( ) ;
1682- // determine if the package version that is a repository tag is a valid NuGetVersion
1683- if ( ! NuGetVersion . TryParse ( pkgVersionString , out NuGetVersion pkgVersion ) )
1684- {
1685- errRecord = new ErrorRecord (
1686- new ArgumentException ( $ "Version { pkgVersionString } to be parsed from metadata is not a valid NuGet version.") ,
1687- "FindNameFailure" ,
1688- ErrorCategory . InvalidArgument ,
1689- this ) ;
1666+ errRecord = new ErrorRecord (
1667+ new ArgumentException ( $ "Version { pkgVersionString } to be parsed from metadata is not a valid NuGet version.") ,
1668+ "FindNameFailure" ,
1669+ ErrorCategory . InvalidArgument ,
1670+ this ) ;
16901671
1691- return null ;
1692- }
1672+ return null ;
1673+ }
16931674
1694- _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
1675+ _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
16951676
1696- if ( isSpecificVersionSearch )
1677+ if ( isSpecificVersionSearch )
1678+ {
1679+ if ( pkgVersion . ToNormalizedString ( ) == specificVersion . ToNormalizedString ( ) )
16971680 {
1698- if ( pkgVersion . ToNormalizedString ( ) == specificVersion . ToNormalizedString ( ) )
1699- {
1700- // accounts for FindVersion() scenario
1701- sortedPkgs . Add ( pkgVersion , pkgVersionString ) ;
1702- break ;
1703- }
1681+ // accounts for FindVersion() scenario
1682+ sortedPkgs . Add ( pkgVersion , pkgVersionString ) ;
1683+ break ;
17041684 }
1705- else
1685+ }
1686+ else
1687+ {
1688+ if ( versionRange . Satisfies ( pkgVersion ) && ( ! pkgVersion . IsPrerelease || includePrerelease ) )
17061689 {
1707- if ( versionRange . Satisfies ( pkgVersion ) && ( ! pkgVersion . IsPrerelease || includePrerelease ) )
1708- {
1709- // accounts for FindVersionGlobbing() and FindName() scenario
1710- sortedPkgs . Add ( pkgVersion , pkgVersionString ) ;
1711- }
1690+ // accounts for FindVersionGlobbing() and FindName() scenario
1691+ sortedPkgs . Add ( pkgVersion , pkgVersionString ) ;
17121692 }
17131693 }
17141694 }
17151695
17161696 return sortedPkgs ;
17171697 }
17181698
1699+ private string PrependMARPrefix ( string packageName )
1700+ {
1701+ string prefix = string . IsNullOrEmpty ( InternalHooks . MARPrefix ) ? PSRepositoryInfo . MARPrefix : InternalHooks . MARPrefix ;
1702+
1703+ // If the repostitory is MAR and its not a wildcard search, we need to prefix the package name with MAR prefix.
1704+ string updatedPackageName = Repository . IsMARRepository ( ) && packageName . Trim ( ) != "*"
1705+ ? string . Concat ( prefix , packageName )
1706+ : packageName ;
1707+
1708+ return updatedPackageName ;
1709+ }
1710+
17191711 #endregion
17201712 }
17211713}
0 commit comments