Skip to content

Commit 9f08955

Browse files
authored
[eng] Update the logic of resource repository in VersionController (#27382)
1 parent fd72b8d commit 9f08955

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

tools/PublishModules.psm1

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,38 @@ function Save-PackageLocally {
473473
$ModuleName = $module['ModuleName']
474474
$RequiredVersion = $module['RequiredVersion']
475475

476+
$AccessTokenSecureString = $env:SYSTEM_ACCESS_TOKEN | ConvertTo-SecureString -AsPlainText -Force
477+
$credentialsObject = [pscredential]::new("ONEBRANCH_TOKEN", $AccessTokenSecureString)
478+
479+
476480
# Only check for the modules that specifies = required exact dependency version
477481
if ($RequiredVersion -ne $null) {
478482
Write-Output "Checking for required module $ModuleName, $RequiredVersion"
479483
if (Find-Module -Name $ModuleName -RequiredVersion $RequiredVersion -Repository $TempRepo -ErrorAction SilentlyContinue) {
480484
Write-Output "Required dependency $ModuleName, $RequiredVersion found in the repo $TempRepo"
481485
} else {
486+
if (Test-Path Env:\DEFAULT_PS_REPOSITORY_URL) {
487+
$PSRepositoryUrl = $Env:DEFAULT_PS_REPOSITORY_URL
488+
$AccessTokenSecureString = $env:SYSTEM_ACCESS_TOKEN | ConvertTo-SecureString -AsPlainText -Force
489+
$credentialsObject = [pscredential]::new("ONEBRANCH_TOKEN", $AccessTokenSecureString)
490+
}
491+
else {
492+
$PSRepositoryUrl = "https://www.powershellgallery.com/api/v2"
493+
}
482494
Write-Warning "Required dependency $ModuleName, $RequiredVersion not found in the repo $TempRepo"
483-
Write-Output "Downloading the package from PsGallery to the path $TempRepoPath"
484-
# We try to download the package from the PsGallery as we are likely intending to use the existing version of the module.
485-
# If the module not found in psgallery, the following commnad would fail and hence publish to local repo process would fail as well
486-
Save-Package -Name $ModuleName -RequiredVersion $RequiredVersion -ProviderName Nuget -Path $TempRepoPath -Source https://www.powershellgallery.com/api/v2 | Out-Null
495+
Write-Output "Downloading the package from $PSRepositoryUrl to the path $TempRepoPath"
496+
# We try to download the package from the PSRepositoryUrl as we are likely intending to use the existing version of the module.
497+
# If the module not found in PSRepositoryUrl, the following command would fail and hence publish to local repo process would fail as well
498+
if (Test-Path Env:\DEFAULT_PS_REPOSITORY_URL) {
499+
Save-PSResource -Name $ModuleName -Version $RequiredVersion -Path $TempRepoPath -Repository $Env:DEFAULT_PS_REPOSITORY_NAME -Credential $credentialsObject -AsNupkg
500+
} else {
501+
Save-PSResource -Name $ModuleName -Version $RequiredVersion -Path $TempRepoPath -Repository PSGallery -AsNupkg
502+
}
487503
$NupkgFilePath = Join-Path -Path $TempRepoPath -ChildPath "$ModuleName.$RequiredVersion.nupkg"
488504
$ModulePaths = $env:PSModulePath -split ';'
489505
$DestinationModulePath = [System.IO.Path]::Combine($ModulePaths[0], $ModuleName, $RequiredVersion)
490506
Expand-Archive -Path $NupkgFilePath -DestinationPath $DestinationModulePath -Force
491-
Write-Output "Downloaded the package sucessfully"
507+
Write-Output "Downloaded the package successfully"
492508
}
493509
}
494510
}
@@ -583,7 +599,6 @@ function Add-AllModules {
583599
foreach ($module in $Keys) {
584600
$modulePath = $Modules[$module]
585601
Write-Output "Adding $module modules to local repo"
586-
587602
# Save missing dependencies locally from PS gallery.
588603
Save-PackagesFromPsGallery -TempRepo $TempRepo -TempRepoPath $TempRepoPath -ModulePaths $modulePath
589604

tools/VersionController/Models/ModuleHelper.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,32 @@ internal static string GetLatestVersionFromPSGallery(string moduleName, ReleaseT
3535
/// Get version from PSGallery and merge into one list.
3636
/// </summary>
3737
/// <returns>A list of version</returns>
38-
internal static List<AzurePSVersion> GetAllVersionsFromGallery(string moduleName, string psRepository)
38+
internal static List<AzurePSVersion> GetAllVersionsFromGallery(string moduleName)
3939
{
4040
HashSet<AzurePSVersion> galleryVersion = new HashSet<AzurePSVersion>();
4141
using (PowerShell powershell = PowerShell.Create())
4242
{
43-
powershell.AddScript($"Find-Module -Name {moduleName} -Repository {psRepository} -AllowPrerelease -AllVersions");
43+
string findModuleScript;
44+
45+
if (!string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("DEFAULT_PS_REPOSITORY_URL")))
46+
{
47+
string repository = System.Environment.GetEnvironmentVariable("DEFAULT_PS_REPOSITORY_NAME");
48+
findModuleScript = @"
49+
$secpasswd = ConvertTo-SecureString $Env:DEFAULT_PS_REPOSITORY_PASSWORD -AsPlainText -Force
50+
$credential = New-Object System.Management.Automation.PSCredential ($Env:DEFAULT_PS_REPOSITORY_USER, $secpasswd)
51+
Find-PSResource -Name " + moduleName + " -Repository " + repository + " -Credential $credential -Prerelease";
52+
}
53+
else
54+
{
55+
string repository = "PSGallery";
56+
findModuleScript = $"Find-PSResource -Name {moduleName} -Repository {repository} -Prerelease";
57+
}
58+
59+
powershell.AddScript(findModuleScript);
4460
var cmdletResult = powershell.Invoke();
4561
foreach (var versionInformation in cmdletResult)
4662
{
63+
System.Console.WriteLine(versionInformation);
4764
if (versionInformation.Properties["Version"]?.Value != null)
4865
{
4966
galleryVersion.Add(new AzurePSVersion(versionInformation.Properties["Version"]?.Value?.ToString()));

tools/VersionController/Models/VersionBumper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public class VersionBumper
4444
private AzurePSVersion _assignedVersion { get; set;}
4545

4646
public AzurePSVersion MinimalVersion { get; set; }
47-
public string PSRepositories { get; set; }
4847

4948
private ReleaseType _releaseType { get; set; }
5049

@@ -227,7 +226,7 @@ private string GetBumpedVersion()
227226
versionBump = Version.MINOR;
228227
}
229228

230-
List<AzurePSVersion> galleryVersion = ModuleHelper.GetAllVersionsFromGallery(_fileHelper.ModuleName, PSRepositories);
229+
List<AzurePSVersion> galleryVersion = ModuleHelper.GetAllVersionsFromGallery(_fileHelper.ModuleName);
231230
AzurePSVersion bumpedVersion = galleryVersion.Count == 0 ? new AzurePSVersion(0, 1, 0) : GetBumpedVersionByType(new AzurePSVersion(_oldVersion), versionBump);
232231
AzurePSVersion maxGAedVersionInGallery = ModuleHelper.GetLatestVersionFromGalleryUnderSameMajorVersion(bumpedVersion, galleryVersion, false);
233232
AzurePSVersion maxPreGAedVersionInGallery = ModuleHelper.GetLatestVersionFromGalleryUnderSameMajorVersion(bumpedVersion, galleryVersion, true);

tools/VersionController/Program.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,6 @@ private static bool GetModuleReadMe(string directory)
160160
/// </summary>
161161
private static void BumpVersions()
162162
{
163-
string targetRepositories = null;
164-
using (PowerShell powershell = PowerShell.Create())
165-
{
166-
powershell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;");
167-
powershell.AddScript("Register-PackageSource -Name PSGallery -Location https://www.powershellgallery.com/api/v2 -ProviderName PowerShellGet");
168-
powershell.AddScript("Get-PSRepository");
169-
var repositories = powershell.Invoke();
170-
string psgallery = null;
171-
foreach (var repo in repositories)
172-
{
173-
if ("https://www.powershellgallery.com/api/v2".Equals(repo.Properties["SourceLocation"]?.Value))
174-
{
175-
psgallery = repo.Properties["Name"]?.Value?.ToString();
176-
}
177-
}
178-
if (psgallery == null)
179-
{
180-
throw new Exception("Cannot calculate module version because PSGallery is not available.");
181-
}
182-
targetRepositories = psgallery;
183-
}
184-
185163
var changedModules = new List<string>();
186164
foreach (var directory in _projectDirectories)
187165
{
@@ -254,7 +232,6 @@ private static void BumpVersions()
254232
} else {
255233
_versionBumper = new VersionBumper(new VersionFileHelper(_rootDirectory, outputModuleManifestFile, projectModuleManifestPath), changedModules, _releaseType);
256234
}
257-
_versionBumper.PSRepositories = targetRepositories;
258235
if (_minimalVersion.ContainsKey(moduleName))
259236
{
260237
_versionBumper.MinimalVersion = _minimalVersion[moduleName];

0 commit comments

Comments
 (0)