Skip to content

Commit 66a6d06

Browse files
committed
Added cmdlets to manipulate build tags
Add-TeamCityBuildTag - Adds a new tag to the current build Remove-TeamCityBuildTag - Removes a tag from the current build +semver: major
1 parent e1ae07d commit 66a6d06

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added cmdlets to manipulate build tags
12+
13+
`Add-TeamCityBuildTag` - Adds a new tag to the current build
14+
15+
`Remove-TeamCityBuildTag` - Removes a tag from the current build
16+
917
## [0.1.0] - 2023-04-23
1018

1119
### Added

src/public/Add-BuildTag.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Add-BuildTag {
2+
<#
3+
.SYNOPSIS
4+
Adds a new tag to the current build
5+
6+
.DESCRIPTION
7+
Adds a new tag to the current build using TeamCity service messages.
8+
9+
.EXAMPLE
10+
Add-TeamCityBuildTag "My Custom Tag"
11+
12+
##teamcity[addBuildTag 'My Custom Tag']
13+
14+
.NOTES
15+
Added in 2023.05
16+
17+
.LINK
18+
https://www.jetbrains.com/help/teamcity/2023.05/service-messages.html#Adding+and+Removing+Build+Tags
19+
#>
20+
[CmdletBinding()]
21+
[OutputType([System.Void])]
22+
param (
23+
[Parameter(Mandatory, Position = 0)]
24+
[string]
25+
# The tag to add
26+
$Tag
27+
)
28+
if (IsRunningInTeamCity) {
29+
Write-Host "##teamcity[addBuildTag '$(EscapeTeamCityBuildText $Tag)']"
30+
} else {
31+
Write-Host "Added build tag '$Tag'"
32+
}
33+
}

src/public/Remove-BuildTag.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Remove-BuildTag {
2+
<#
3+
.SYNOPSIS
4+
Removes a tag from the current build
5+
6+
.DESCRIPTION
7+
Removes a tag from the current build using TeamCity service messages.
8+
9+
.EXAMPLE
10+
Remove-TeamCityBuildTag "My Custom Tag"
11+
12+
##teamcity[removeBuildTag 'My Custom Tag']
13+
14+
.NOTES
15+
Added in 2023.05
16+
17+
.LINK
18+
https://www.jetbrains.com/help/teamcity/2023.05/service-messages.html#Adding+and+Removing+Build+Tags
19+
#>
20+
[CmdletBinding()]
21+
[OutputType([System.Void])]
22+
param (
23+
[Parameter(Mandatory, Position = 0)]
24+
[string]
25+
# The tag to remove
26+
$Tag
27+
)
28+
if (IsRunningInTeamCity) {
29+
Write-Host "##teamcity[removeBuildTag '$(EscapeTeamCityBuildText $Tag)']"
30+
} else {
31+
Write-Host "Removed build tag '$Tag'"
32+
}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
BeforeAll {
2+
Import-Module "$PSScriptRoot/../../build/TeamCityLog/TeamCityLog.psd1" -Force
3+
}
4+
5+
Describe 'Add-BuildTag' {
6+
It 'Throws when an empty Tag is passed' {
7+
{
8+
Add-TeamCityBuildTag -Tag ''
9+
} | Should -Throw
10+
}
11+
12+
It "Doesn't throw when a non-empty Tag is passed" {
13+
{
14+
Add-TeamCityBuildTag -Tag 'My Tag' 6>&1 | Out-Null
15+
} | Should -Not -Throw
16+
}
17+
18+
It 'Allows Tag to be passed positionally' {
19+
$env:TEAMCITY_VERSION = '9999.99.9'
20+
$Res = Add-TeamCityBuildTag 'My Tag' 6>&1
21+
$Res | Should -Be "##teamcity[addBuildTag 'My Tag']"
22+
}
23+
24+
Context 'When Not Running in TeamCity' {
25+
BeforeEach {
26+
$env:TEAMCITY_VERSION = $null
27+
}
28+
It 'Outputs the tag but not in a service message' {
29+
$Res = Add-TeamCityBuildTag -Tag 'My Tag' 6>&1
30+
$Res | Should -Not -BeLike '##teamcity*'
31+
$Res | Should -Be "Added build tag 'My Tag'"
32+
}
33+
It 'Does not escape Tag text ' {
34+
$Res = Add-TeamCityBuildTag -Tag '😀' 6>&1
35+
$Res | Should -Be "Added build tag '😀'"
36+
}
37+
}
38+
39+
Context 'When Running in TeamCity' {
40+
BeforeEach {
41+
$env:TEAMCITY_VERSION = '9999.99.9'
42+
}
43+
It 'Outputs Text' {
44+
Mock Write-Host {} -ModuleName 'TeamCityLog'
45+
Add-TeamCityBuildTag -Tag 'My Tag'
46+
Should -Invoke -CommandName 'Write-Host' -Times 1 -Exactly -ModuleName 'TeamCityLog'
47+
}
48+
It 'Outputs the tag in a service message' {
49+
$Res = Add-TeamCityBuildTag -Tag 'My Tag' 6>&1
50+
$Res | Should -BeLike '##teamcity*'
51+
}
52+
It 'Escapes Tag Text' {
53+
$Res = Add-TeamCityBuildTag -Tag 'My Tag 😀' 6>&1
54+
$Res | Should -Be "##teamcity[addBuildTag 'My Tag |0xD83D|0xDE00']"
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
BeforeAll {
2+
Import-Module "$PSScriptRoot/../../build/TeamCityLog/TeamCityLog.psd1" -Force
3+
}
4+
5+
Describe 'Remove-BuildTag' {
6+
It 'Throws when an empty Tag is passed' {
7+
{
8+
Remove-TeamCityBuildTag -Tag ''
9+
} | Should -Throw
10+
}
11+
12+
It "Doesn't throw when a non-empty Tag is passed" {
13+
{
14+
Remove-TeamCityBuildTag -Tag 'My Tag' 6>&1 | Out-Null
15+
} | Should -Not -Throw
16+
}
17+
18+
It 'Allows Tag to be passed positionally' {
19+
$env:TEAMCITY_VERSION = '9999.99.9'
20+
$Res = Remove-TeamCityBuildTag 'My Tag' 6>&1
21+
$Res | Should -Be "##teamcity[removeBuildTag 'My Tag']"
22+
}
23+
24+
Context 'When Not Running in TeamCity' {
25+
BeforeEach {
26+
$env:TEAMCITY_VERSION = $null
27+
}
28+
It 'Outputs the tag but not in a service message' {
29+
$Res = Remove-TeamCityBuildTag -Tag 'My Tag' 6>&1
30+
$Res | Should -Not -BeLike '##teamcity*'
31+
$Res | Should -Be "Removed build tag 'My Tag'"
32+
}
33+
It 'Does not escape Tag text ' {
34+
$Res = Remove-TeamCityBuildTag -Tag '😀' 6>&1
35+
$Res | Should -Be "Removed build tag '😀'"
36+
}
37+
}
38+
39+
Context 'When Running in TeamCity' {
40+
BeforeEach {
41+
$env:TEAMCITY_VERSION = '9999.99.9'
42+
}
43+
It 'Outputs Text' {
44+
Mock Write-Host {} -ModuleName 'TeamCityLog'
45+
Remove-TeamCityBuildTag -Tag 'My Tag'
46+
Should -Invoke -CommandName 'Write-Host' -Times 1 -Exactly -ModuleName 'TeamCityLog'
47+
}
48+
It 'Outputs the tag in a service message' {
49+
$Res = Remove-TeamCityBuildTag -Tag 'My Tag' 6>&1
50+
$Res | Should -BeLike '##teamcity*'
51+
}
52+
It 'Escapes Tag Text' {
53+
$Res = Remove-TeamCityBuildTag -Tag 'My Tag 😀' 6>&1
54+
$Res | Should -Be "##teamcity[removeBuildTag 'My Tag |0xD83D|0xDE00']"
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)