Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 8, 2025

Summary

This PR prevents users from explicitly setting ApiVersion to Unknown when using the Set-PSResourceRepository and Register-PSResourceRepository cmdlets. The Unknown value is reserved for internal use when the system cannot automatically determine a repository's type, and allowing users to set it explicitly would create non-functional repositories.

Problem

Previously, users could set a repository's ApiVersion to "unknown":

Set-PSResourceRepository -Name "MyRepo" -ApiVersion "unknown"
# This would succeed but make the repository unusable

Register-PSResourceRepository -Name "MyRepo" -Uri "https://example.com" -ApiVersion "unknown"
# This would also succeed but make the repository unusable

When a repository has ApiVersion.Unknown, cmdlets like Find-PSResource and Install-PSResource detect this and throw errors indicating the repository type is not supported, making the repository completely non-functional.

Changes

Code Changes

  • src/code/SetPSResourceRepository.cs: Added [ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")] attribute to the ApiVersion parameter to restrict valid values at parameter binding time
  • src/code/RegisterPSResourceRepository.cs: Added [ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")] attribute to the ApiVersion parameter to restrict valid values at parameter binding time

Test Changes

  • test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1: Updated the test to verify that attempting to set ApiVersion to "unknown" now throws a ParameterArgumentValidationError and leaves the repository unchanged
  • test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1: Added a new test to verify that attempting to register with ApiVersion "unknown" throws a ParameterArgumentValidationError and the repository is not created

New Behavior

Set-PSResourceRepository -Name "MyRepo" -ApiVersion "unknown"
# Now throws: ParameterArgumentValidationError with message listing valid values

Register-PSResourceRepository -Name "MyRepo" -Uri "https://example.com" -ApiVersion "unknown"
# Now throws: ParameterArgumentValidationError with message listing valid values

The ValidateSet attribute provides automatic parameter validation at binding time, which is more idiomatic for PowerShell cmdlets. This approach:

  • Validates parameters before cmdlet execution begins
  • Provides built-in error messages from PowerShell listing all valid values
  • Enables tab completion with only valid values
  • Reduces the need for manual validation code

All other ApiVersion values continue to work as expected.

Fixes

Fixes #1419


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Remove ability to set API version to unknown Prevent users from setting ApiVersion to 'Unknown' in Set-PSResourceRepository Oct 8, 2025
Copilot AI requested a review from alerickson October 8, 2025 19:14
Copilot finished work on behalf of alerickson October 8, 2025 19:14
@alerickson
Copy link
Member

@copilot add [ValidateSet("V2", "V3". "Local", "NugetServer", "ContainerRegistry" )]
to Register-PSResourceRepository

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot add [ValidateSet("V2", "V3". "Local", "NugetServer", "ContainerRegistry" )]
to Register-PSResourceRepository

Added validation to Register-PSResourceRepository to prevent setting ApiVersion to 'Unknown'. Since we're using an enum type in C#, I applied the same validation pattern used in Set-PSResourceRepository rather than using ValidateSet. The validation throws a clear error message listing the valid values (V2, V3, Local, NugetServer, ContainerRegistry) when users attempt to use Unknown.

Changes in commit 4fe3725:

  • Added validation in RegisterPSResourceRepository.cs
  • Added test to verify the validation works correctly

Copilot AI changed the title Prevent users from setting ApiVersion to 'Unknown' in Set-PSResourceRepository Prevent users from setting ApiVersion to 'Unknown' in Set-PSResourceRepository and Register-PSResourceRepository Nov 21, 2025
Copilot finished work on behalf of alerickson November 21, 2025 20:46
@alerickson
Copy link
Member

@copilot In RegisterPSResourceRepository, add the following in the line before "public PSRepositoryInfo.APIVersion ApiVersion { get; set; }" :
" [ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry" )] "

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot In RegisterPSResourceRepository, add the following in the line before "public PSRepositoryInfo.APIVersion ApiVersion { get; set; }" :
" [ValidateSet("V2", "V3", "Local", "NugetServer", "Con...

Added [ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")] to the ApiVersion parameter in both RegisterPSResourceRepository and SetPSResourceRepository for consistency. This approach is more idiomatic as it validates at parameter binding time and provides better error messages. Also removed the now-redundant runtime validation code.

Changes in commit c0547b6.

Copilot AI requested a review from alerickson November 21, 2025 21:18
Copilot finished work on behalf of alerickson November 21, 2025 21:18
@alerickson
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@alerickson alerickson marked this pull request as ready for review November 21, 2025 21:50
@alerickson alerickson merged commit 6b9ddf4 into master Nov 21, 2025
14 checks passed
@alerickson alerickson deleted the copilot/remove-unknown-api-version branch November 21, 2025 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set-PSResourceRepository API version to unknown

3 participants