Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .docs/Set-VSTeamVariableGroupVariable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!-- #include "./common/header.md" -->

# Set-VSTeamVariableGroupVariable

## SYNOPSIS

<!-- #include "./synopsis/Set-VSTeamVariableGroupVariable.md" -->

## SYNTAX

## DESCRIPTION

<!-- #include "./synopsis/Set-VSTeamVariableGroupVariable.md" -->

## EXAMPLES

### Example 1

```powershell

# In project MyProject, variable group MyVarGroup, set the value of Foo to Hello. The variable
# called Foo will be created if it doesn't already exist.

Set-VSTeamVariableGroupVariable -ProjectName MyProject -GroupName MyVarGroup -Name Foo -Value Hello
```

## PARAMETERS

### GroupName

Name of the existing variable group

```yaml
Type: String
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### Name

Name of the variable to set

```yaml
Type: String
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### Value

The new value of the variable

```yaml
Type: String
Aliases:

Required: False
Position: Named
Default value: ""
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

<!-- #include "./params/projectName.md" -->

## INPUTS

## OUTPUTS

## NOTES

Secret variable updating or creation is not currently supported.

<!-- #include "./common/prerequisites.md" -->

## RELATED LINKS



[Add-VSTeamVariableGroup](Add-VSTeamVariableGroup.md)

[Get-VSTeamVariableGroup](Get-VSTeamVariableGroup.md)

[Remove-VSTeamVariableGroup](Remove-VSTeamVariableGroup.md)

[Update-VSTeamVariableGroup](Update-VSTeamVariableGroup.md)
1 change: 1 addition & 0 deletions .docs/synopsis/Set-VSTeamVariableGroupVariable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds a variable to an existing variable group, or changes its value if the variable is already present.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 7.12.0

Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/495) from [Seva Alekseyev](https://github.com/sevaa) the following:
- Added the command Set-VSTeamVariableGroupVariable, which assigns a value to a single variable in a variable group,
creating one if needed. [#423](https://github.com/MethodsAndPractices/vsteam/issues/423)

## 7.11.0

Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/516) from [Arturo Polanco](https://github.com/arturopolanco) the following:
Expand Down
53 changes: 53 additions & 0 deletions Source/Public/Set-VSTeamVariableGroupVariable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Set-VSTeamVariableGroupVariable {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Medium",
HelpUri='https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Set-VSTeamVariableGroupVariable')]
param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $GroupName,

[Parameter(Mandatory = $true)]
[string] $Name,

[string] $Value,

[switch] $Force,

[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[vsteam_lib.ProjectValidateAttribute($false)]
[ArgumentCompleter([vsteam_lib.ProjectCompleter])]
[string] $ProjectName
)

process {
if ($Force -or $pscmdlet.ShouldProcess("$GroupName.$Name", "Set Variable Value")) {
try {
$gl = Get-VSTeamVariableGroup -ProjectName $ProjectName
# Exception if a group with a name is not found is not particularly descriptive
$g = $gl | Where-Object -FilterScript {$_.Name -eq $GroupName}
if($g) {
if($g.variables | Get-Member -Name $Name) {
if(($g.variables.$Name | Get-Member -Name isSecret) -and $g.variables.$Name.isSecret) {
Write-Error "The variable $Name is a secret one. Updating secret variables is not currently supported."
return
}
else {
$g.variables.$Name.value = $Value
}
}
else {
Add-Member -InputObject $g.variables -MemberType NoteProperty -Name $Name -Value ([pscustomobject]@{value=$Value})
}
$body = $g | ConvertTo-Json -Depth 20 -Compress
Update-VSTeamVariableGroup -ProjectName $ProjectName -Id $g.Id -Body $body -Force | Out-Null
}
else {
Write-Error "Group $GroupName was not found."
return
}
}
catch {
_handleException $_
}
}
}
}
2 changes: 1 addition & 1 deletion Source/VSTeam.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'VSTeam.psm1'

# Version number of this module.
ModuleVersion = '7.11.0'
ModuleVersion = '7.12.0'

# Supported PSEditions
CompatiblePSEditions = @('Core', 'Desktop')
Expand Down
54 changes: 54 additions & 0 deletions Tests/function/tests/Set-VSTeamVariableGroupVariable.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Set-StrictMode -Version Latest

Describe 'VSTeamVariableGroup' {
BeforeAll {
. "$PSScriptRoot\_testInitialize.ps1" $PSCommandPath
}

Context 'Set-VSTeamVariableGroupVariable' {
Context 'Services' {
BeforeAll {
Mock _getApiVersion { return 'VSTS' }
Mock _getInstance { return 'https://dev.azure.com/test' }
Mock Get-VSTeamVariableGroup { @(@{
name = "TheGroup";
id = 101;
variables = @{
"x" = @{value="A"};
"y" = @{value="B"};
"s" = @{value=$null; isSecret=$True}
}
}) | ConvertTo-JSON | ConvertFrom-JSON}
Mock _getApiVersion { return '5.0-preview.1-unitTests' } -ParameterFilter { $Service -eq 'VariableGroups' }
Mock Update-VSTeamVariableGroup {}
Mock Write-Error {}
}

It 'should change the value of an exisiting variable' {
Set-VSTeamVariableGroupVariable -ProjectName project -GroupName TheGroup -Name x -Value NewValue -Force

Should -Invoke Update-VSTeamVariableGroup -Exactly -Scope It -Times 1 -ParameterFilter {
($Body | ConvertFrom-JSON).variables.x.value -eq "NewValue"
}
}

It 'should create a new variable' {
Set-VSTeamVariableGroupVariable -ProjectName project -GroupName TheGroup -Name z -Value ANewOne -Force

Should -Invoke Update-VSTeamVariableGroup -Exactly -Scope It -Times 1 -ParameterFilter {
($Body | ConvertFrom-JSON).variables.z.value -eq "ANewOne"
}
}

It 'should fail on nonexistent group' {
Set-VSTeamVariableGroupVariable -ProjectName project -GroupName NotAGroup -Name z -Value ANewOne -Force
Should -Invoke Write-Error -Exactly -Scope It -Times 1
}

It 'should fail on a secret var' {
Set-VSTeamVariableGroupVariable -ProjectName project -GroupName TheGroup -Name s -Value NewValue -Force
Should -Invoke Write-Error -Exactly -Scope It -Times 1
}
}
}
}
2 changes: 1 addition & 1 deletion Tests/function/tests/Update-VSTeamProject.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Describe 'VSTeamProject' {
## Assert
Should -Invoke Invoke-RestMethod -Exactly -Times 1 -Scope It -ParameterFilter {
$Uri -eq "https://dev.azure.com/test/_apis/projects/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Core)" -and
$Method -eq 'Patch' -and $Body -like '*"visibility"*"public"*'
$Method -eq 'Patch' -and ($Body | ConvertFrom-JSON).visibility -eq "public"
}
}
}
Expand Down