diff --git a/.docs/Test-VSTeamYamlPipeline.md b/.docs/Test-VSTeamYamlPipeline.md index 0168b8d9..fada4774 100644 --- a/.docs/Test-VSTeamYamlPipeline.md +++ b/.docs/Test-VSTeamYamlPipeline.md @@ -12,16 +12,14 @@ + Now, you can try out a YAML pipeline without committing it to a repo or running it. Given an existing pipeline and an optional new YAML payload, this function will give you back the fully compiled YAML pipeline including templates. + ## EXAMPLES ### Example 1 ```powershell Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 -FilePath './azure-pipelines.yml' - -Name Id url state ----- -- --- ----- - -1 https://dev.azure.com/devsdb/3428bdd7-9fed-4c30-a6c9-fcb52f084ab9/_apis/pipelines/24/runs/-1 unknown ``` This example checks the YAML pipeline with ID 24 and the file './azure-pipelines.yml' for consistency on Azure DevOps to see if the changes still work. @@ -38,11 +36,26 @@ This example checks the YAML pipeline with ID 24 and the content of a yaml file ### Example 3 ```powershell -$yamlOverride = [string](Get-Content -raw $FilePath) +Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 - Branch 'main' +``` + +This example checks the YAML in the remote repository connected to the pipeline with ID 24 for consistency on Azure DevOps. It will take the file for the branch 'main'. + +### Example 4 + +```powershell +Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 - Branch 'refs/heads/feature/test' +``` + +This example checks the YAML in the remote repository connected to the pipeline with ID 24 for consistency on Azure DevOps. It will take the file for the branch ref 'refs/heads/feature/test'. + +### Example 5 + +```powershell Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 ``` -This example checks the YAML pipeline with ID 24 for consistency on Azure DevOps to see if the existing YAML of the pipeline works. +This example checks the YAML file pipeline with ID 24 for consistency on Azure DevOps to see if the existing YAML of the pipeline works. ## PARAMETERS @@ -63,6 +76,14 @@ Path to the file that should be checked Type: String ``` +### Branch + +branch name of the remote repository where the YAML file should be checked + +```yaml +Type: String +``` + ## INPUTS diff --git a/.docs/synopsis/Test-VSTeamYamlPipeline.md b/.docs/synopsis/Test-VSTeamYamlPipeline.md index 940e64dc..9dfeafca 100644 --- a/.docs/synopsis/Test-VSTeamYamlPipeline.md +++ b/.docs/synopsis/Test-VSTeamYamlPipeline.md @@ -1 +1 @@ -Tests the commited YAML pipeline files to check for inconsitencies like syntax, references etc.. Now, you can try out a YAML pipeline without committing it to a repo or running it. Given an existing pipeline and an optional new YAML payload, this function will give you back the full YAML pipeline. \ No newline at end of file +Validates the local YAML pipeline against Azure DevOps. \ No newline at end of file diff --git a/.github/workflows/actions-pipeline.yml b/.github/workflows/actions-pipeline.yml index 31110652..e00c599f 100644 --- a/.github/workflows/actions-pipeline.yml +++ b/.github/workflows/actions-pipeline.yml @@ -5,8 +5,6 @@ on: push: branches: - trunk - - feature/* - - hotfix/* pull_request: branches: - trunk diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc1b817..4a54e8ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/418) fr - Added `Get-VSTeamWorkItemTag`, `Update-VSTeamWorkItemTag` and `Remove-VSTeamWorkItemTag` cmdlets which allow you to [manage work item tags]([#419](https://github.com/MethodsAndPractices/vsteam/issues/419). +Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/437) from [Sebastian Schütze](https://github.com/SebastianSchuetze) which included the following: + +- Added Parameter `Branch` to `Test-VSTeamYamlPipeline` to test yaml for a specific branch [manage work item tags]([#428](https://github.com/MethodsAndPractices/vsteam/issues/428). + ## 7.5.0 Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/414) from [Guillermo Diaz](https://github.com/gm0d) which included the following: diff --git a/Source/Public/Test-VSTeamYamlPipeline.ps1 b/Source/Public/Test-VSTeamYamlPipeline.ps1 index 6bd4edd5..522d91b7 100644 --- a/Source/Public/Test-VSTeamYamlPipeline.ps1 +++ b/Source/Public/Test-VSTeamYamlPipeline.ps1 @@ -1,14 +1,15 @@ function Test-VSTeamYamlPipeline { [CmdletBinding(DefaultParameterSetName = 'WithFilePath', - HelpUri='https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Test-VSTeamYamlPipeline')] + HelpUri = 'https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Test-VSTeamYamlPipeline')] param( - [Parameter(ParameterSetName = 'WithFilePath', Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] - [Parameter(ParameterSetName = 'WithYamlOverride', Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] + [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] [Int32] $PipelineId, [Parameter(ParameterSetName = 'WithFilePath', Mandatory = $false)] [string] $FilePath, [Parameter(ParameterSetName = 'WithYamlOverride', Mandatory = $false)] - [string] $YamlOverride + [string] $YamlOverride, + [Parameter(Mandatory = $false)] + [string] $Branch ) DynamicParam { _buildProjectNameDynamicParam @@ -29,6 +30,25 @@ function Test-VSTeamYamlPipeline { $body.YamlOverride = $YamlOverride } + if ($Branch) { + # if full branch name is given then remove the first part + + $body.resources = @{ + pipelines = $null + repositories = @{ + self = @{ + refName = ($Branch -replace 'refs/heads/', '') + } + } + builds = $null + containers = $null + packages = $null + } + } + else { + Write-Warning "No branch specified, Azure DevOps api is using 'ref/heads/main' always as default. Specify a branch name to prevent errors (for details see: https://developercommunity.visualstudio.com/t/API-to-preview-pipeline-run-takes-non-ex/1635377)." + } + try { # Call the REST API $resp = _callAPI -Method POST -ProjectName $ProjectName ` diff --git a/Tests/function/tests/Test-VSTeamYamlPipeline.Tests.ps1 b/Tests/function/tests/Test-VSTeamYamlPipeline.Tests.ps1 index 4c082e39..1bbe1521 100644 --- a/Tests/function/tests/Test-VSTeamYamlPipeline.Tests.ps1 +++ b/Tests/function/tests/Test-VSTeamYamlPipeline.Tests.ps1 @@ -12,7 +12,7 @@ Describe 'VSTeamYamlPipeline' { Context 'Test-VSTeamYamlPipeline' { It 'With Pipeline with PipelineID and without extra YAML' { - Test-VSTeamYamlPipeline -projectName project -PipelineId 24 + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -WarningAction SilentlyContinue Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and @@ -23,7 +23,7 @@ Describe 'VSTeamYamlPipeline' { } It 'With Pipeline with PipelineID and YAML file path' { - Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath -WarningAction SilentlyContinue Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and @@ -33,23 +33,86 @@ Describe 'VSTeamYamlPipeline' { } } + It 'With Pipeline with PipelineID, Branch name and YAML file path' { + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath -Branch 'refs/heads/feature/myfeature' + + Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and + $Uri -like "*api-version=$(_getApiVersion Pipelines)*" -and + $Body -like '*"PreviewRun":*true*' -and + $Body -like '*"refName":"feature/myfeature"*' -and + $Body -like '*YamlOverride*' + } + } + + It 'With Pipeline with PipelineID, short Branch name' { + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -Branch 'feature/myfeature' + + Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and + $Uri -like "*api-version=$(_getApiVersion Pipelines)*" -and + $Body -like '*"PreviewRun":*true*' -and + $Body -like '*"refName":"feature/myfeature"*' + } + } + + It 'With Pipeline with PipelineID, with long "refs/heads" Branch name' { + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -Branch 'refs/heads/feature/myfeature' + + Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and + $Uri -like "*api-version=$(_getApiVersion Pipelines)*" -and + $Body -like '*"PreviewRun":*true*' -and + $Body -like '*"refName":"feature/myfeature"*' + } + } + It 'With Pipeline with PipelineID and YAML code' { $yamlOverride = [string](Get-Content -raw $testYamlPath) - Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -YamlOverride $yamlOverride + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -YamlOverride $yamlOverride -WarningAction SilentlyContinue + + Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and + $Uri -like "*api-version=$(_getApiVersion Pipelines)*" -and + $Body -like '*"PreviewRun":*true*' -and + $Body -like '*YamlOverride*' + } + + } + + It 'With Pipeline with PipelineID, Branch name and YAML file path' { + $yamlOverride = [string](Get-Content -raw $testYamlPath) + + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -YamlOverride $yamlOverride -Branch 'refs/heads/feature/myfeature' Should -Invoke Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { $Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and $Uri -like "*api-version=$(_getApiVersion Pipelines)*" -and $Body -like '*"PreviewRun":*true*' -and + $Body -like '*"refName":"feature/myfeature"*' -and $Body -like '*YamlOverride*' } } It 'Should create Yaml result' { - $yamlResult = Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath + $yamlResult = Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath -WarningAction SilentlyContinue $yamlResult | Should -Not -Be $null } + + It 'Should throw warning if no Branch specified' { + + Mock Write-Warning -MockWith { + return $Message + } + + Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -WarningAction SilentlyContinue + + Should -Invoke Write-Warning -Exactly -Scope It -Times 1 -ParameterFilter { + $Message -like '*No branch specified, Azure DevOps api is using ''ref/heads/main''*' + } + + } } } \ No newline at end of file