Skip to content

Commit ce98bf5

Browse files
authored
Add -ApiVersion to Register-PSResourceRepository (#1431)
1 parent 7385230 commit ce98bf5

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/code/RegisterPSResourceRepository.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class RegisterPSResourceRepository : PSCmdlet
8585
[ValidateRange(0, 100)]
8686
public int Priority { get; set; } = DefaultPriority;
8787

88+
/// <summary>
89+
/// Specifies the Api version of the repository to be set.
90+
/// </sumamry>
91+
[Parameter(ParameterSetName = NameParameterSet)]
92+
public PSRepositoryInfo.APIVersion ApiVersion { get; set; }
93+
8894
/// <summary>
8995
/// Specifies vault and secret names as PSCredentialInfo for the repository.
9096
/// </summary>
@@ -115,6 +121,12 @@ protected override void ProcessRecord()
115121
{
116122
List<PSRepositoryInfo> items = new List<PSRepositoryInfo>();
117123

124+
PSRepositoryInfo.APIVersion? repoApiVersion = null;
125+
if (MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)))
126+
{
127+
repoApiVersion = ApiVersion;
128+
}
129+
118130
switch (ParameterSetName)
119131
{
120132
case NameParameterSet:
@@ -128,7 +140,7 @@ protected override void ProcessRecord()
128140

129141
try
130142
{
131-
items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, null, CredentialInfo, Force, this, out string errorMsg));
143+
items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, repoApiVersion, CredentialInfo, Force, this, out string errorMsg));
132144

133145
if (!string.IsNullOrEmpty(errorMsg))
134146
{
@@ -325,14 +337,28 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
325337
return null;
326338
}
327339

340+
if (repo.ContainsKey("ApiVersion") &&
341+
(repo["ApiVersion"] == null || String.IsNullOrEmpty(repo["ApiVersion"].ToString()) ||
342+
!(repo["ApiVersion"].ToString().Equals("local") || repo["ApiVersion"].ToString().Equals("v2") ||
343+
repo["ApiVersion"].ToString().Equals("v3") || repo["ApiVersion"].ToString().Equals("nugetServer") || repo["ApiVersion"].ToString().Equals("unknown"))))
344+
{
345+
WriteError(new ErrorRecord(
346+
new PSInvalidOperationException("Repository ApiVersion must be either 'local', 'v2', 'v3', 'nugetServer' or 'unknown'"),
347+
"IncorrectApiVersionForRepositoriesParameterSetRegistration",
348+
ErrorCategory.InvalidArgument,
349+
this));
350+
351+
return null;
352+
}
353+
328354
try
329355
{
330356
WriteDebug($"Registering repository '{repo["Name"]}' with uri '{repoUri}'");
331357
var addedRepo = RepositorySettings.AddRepository(repo["Name"].ToString(),
332358
repoUri,
333359
repo.ContainsKey("Priority") ? Convert.ToInt32(repo["Priority"].ToString()) : DefaultPriority,
334360
repo.ContainsKey("Trusted") ? Convert.ToBoolean(repo["Trusted"].ToString()) : DefaultTrusted,
335-
apiVersion: null,
361+
apiVersion: repo.ContainsKey("Trusted") ? (PSRepositoryInfo.APIVersion?) repo["ApiVersion"] : null,
336362
repoCredentialInfo,
337363
Force,
338364
this,

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,13 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
394394
$res.Uri.LocalPath | Should -Be "\"
395395
$res.ApiVersion | Should -Be "local"
396396
}
397+
398+
It "should register repository with explicitly provided ApiVersion" {
399+
Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion 'v2'
400+
$res = Get-PSResourceRepository -Name $TestRepoName1
401+
402+
$res.Name | Should -Be $TestRepoName1
403+
$res.Uri.LocalPath | Should -Contain $tmpDir1Path
404+
$res.ApiVersion | Should -Be 'v2'
405+
}
397406
}

0 commit comments

Comments
 (0)