Skip to content

Commit

Permalink
Extended Test-VSTeamYamlPipeline parameter to validate YAML for a spe…
Browse files Browse the repository at this point in the history
…cific branch (MethodsAndPractices#437)

* added change to changelog
* added branch parameter
* added unit tests for Test-VSTeamYamlPipeline
* updated documentation for parameter 'Branch'
  • Loading branch information
SebastianSchuetze authored Jan 12, 2022
1 parent f2772aa commit 09b9b66
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 17 deletions.
33 changes: 27 additions & 6 deletions .docs/Test-VSTeamYamlPipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@

<!-- #include "./synopsis/Test-VSTeamYamlPipeline.md" -->

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.
Expand All @@ -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

Expand All @@ -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
```
<!-- #include "./params/projectName.md" -->
## INPUTS
Expand Down
2 changes: 1 addition & 1 deletion .docs/synopsis/Test-VSTeamYamlPipeline.md
Original file line number Diff line number Diff line change
@@ -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.
Validates the local YAML pipeline against Azure DevOps.
2 changes: 0 additions & 2 deletions .github/workflows/actions-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
push:
branches:
- trunk
- feature/*
- hotfix/*
pull_request:
branches:
- trunk
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 24 additions & 4 deletions Source/Public/Test-VSTeamYamlPipeline.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 `
Expand Down
71 changes: 67 additions & 4 deletions Tests/function/tests/Test-VSTeamYamlPipeline.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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''*'
}

}
}
}

0 comments on commit 09b9b66

Please sign in to comment.