Skip to content

Commit

Permalink
Fix Add -PassThru Parameter to Export Cmdlets #79 (#92)
Browse files Browse the repository at this point in the history
* Fix Add -PassThru Parameter to Export Cmdlets #79
  • Loading branch information
webtonize authored Dec 28, 2023
1 parent bc6173a commit 2e8a1fb
Show file tree
Hide file tree
Showing 21 changed files with 588 additions and 75 deletions.
23 changes: 20 additions & 3 deletions src/PSRule.Rules.AzureDevOps/Functions/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ function Get-AzDevOpsProject {
.DESCRIPTION
Export the Azure DevOps Project using Azure DevOps Rest API to a JSON file
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER OutputPath
Output path for JSON files
.PARAMETER PassThru
Return the exported project as objects to the pipeline instead of writing to a file
.EXAMPLE
Export-AzDevOpsProject -Project $Project -OutputPath $OutputPath
Expand All @@ -170,9 +179,12 @@ function Export-AzDevOpsProject {
[Parameter(Mandatory=$true)]
[string]
$Project,
[Parameter(Mandatory=$true)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand All @@ -188,6 +200,11 @@ function Export-AzDevOpsProject {
catch {
throw "Failed to get project $Project from Azure DevOps"
}
$response | ConvertTo-Json | Out-File -FilePath "$OutputPath/$Project.prj.ado.json"
if($PassThru) {
Write-Output $response
} else {
Write-Verbose "Exporting project $Project as file $Project.prj.ado.json"
$response | ConvertTo-Json | Out-File -FilePath "$OutputPath/$Project.prj.ado.json"
}
}
# End of Function Export-AzDevOpsProject
16 changes: 13 additions & 3 deletions src/PSRule.Rules.AzureDevOps/Functions/DevOps.Groups.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ Export-ModuleMember -Function Get-AzDevOpsGroupDetails
.PARAMETER OutputPath
The folder path to the JSON file to export to
.PARAMETER PassThru
Return the group details as an object instead of exporting to a file
.EXAMPLE
Export-AzDevOpsGroups -Project 'MyProject' -OutputPath 'C:\Temp\'
Expand All @@ -134,9 +137,12 @@ Function Export-AzDevOpsGroups {
[Parameter(Mandatory=$true)]
[string]
$Project,
[Parameter(Mandatory=$true)]
[Parameter(ParameterSetName='JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName='PassThru')]
[switch]
$PassThru
)
if($null -eq $script:connection) {
throw 'Not connected to Azure DevOps. Run Connect-AzDevOps first.'
Expand All @@ -157,7 +163,11 @@ Function Export-AzDevOpsGroups {

$groupDetails += $thisGroup
}
$groupDetails | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\groups.ado.json"
if($PassThru) {
Write-Output $groupDetails
} else {
$groupDetails | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\groups.ado.json"
}
}
Export-ModuleMember -Function Export-AzDevOpsGroups
# End of Function Export-AzDevOpsGroups
54 changes: 39 additions & 15 deletions src/PSRule.Rules.AzureDevOps/Functions/DevOps.Pipelines.Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ Export-ModuleMember -Function Get-AzDevOpsPipelineYaml
.PARAMETER OutputPath
Output path for YAML file
.PARAMETER PassThru
Pass the YAML definition to the pipeline object
.EXAMPLE
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $PipelineId -OutputPath $OutputPath
#>
Expand All @@ -218,9 +221,12 @@ function Export-AzDevOpsPipelineYaml {
[Parameter(Mandatory)]
[string]
$PipelineName,
[Parameter(Mandatory)]
[Parameter(ParameterSetName = 'YamlFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand All @@ -236,8 +242,12 @@ function Export-AzDevOpsPipelineYaml {
return $null
} else {
$yaml += $yamlTemp}
Write-Verbose "Exporting YAML definition to $OutputPath\$PipelineName.yaml"
$yaml | Out-File "$OutputPath\$PipelineName.yaml"
if ($PassThru) {
Write-Output $yaml
} else {
Write-Verbose "Exporting YAML definition to $OutputPath\$PipelineName.yaml"
$yaml | Out-File "$OutputPath\$PipelineName.yaml"
}
}
Export-ModuleMember -Function Export-AzDevOpsPipelineYaml
# End of Function Export-AzDevOpsPipelineYaml
Expand All @@ -256,6 +266,9 @@ Export-ModuleMember -Function Export-AzDevOpsPipelineYaml
.PARAMETER OutputPath
Output path for JSON files
.PARAMETER PassThru
Pass the pipeline object to the pipeline object instead of exporting to a file
.EXAMPLE
Export-AzDevOpsPipelines -Project $Project -OutputPath $OutputPath
#>
Expand All @@ -265,9 +278,12 @@ function Export-AzDevOpsPipelines {
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand All @@ -290,16 +306,24 @@ function Export-AzDevOpsPipelines {
} else {
Write-Verbose "Token Type is set to ReadOnly, no pipeline ACLs will be returned"
}
Write-Verbose "Exporting pipeline $($pipeline.name) to JSON file"
Write-Verbose "Exporting pipeline as JSON file to $OutputPath\$($pipeline.name).ado.pl.json"
$pipeline | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$($pipeline.name).ado.pl.json"
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -OutputPath $OutputPath

If ($PassThru) {
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -PassThru
}
Write-Output $pipeline
} else {
Write-Verbose "Exporting pipeline $($pipeline.name) to JSON file"
Write-Verbose "Exporting pipeline as JSON file to $OutputPath\$($pipeline.name).ado.pl.json"
$pipeline | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$($pipeline.name).ado.pl.json"
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"

Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -OutputPath $OutputPath
}
}

}
}
Export-ModuleMember -Function Export-AzDevOpsPipelines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ Export-ModuleMember -Function Get-AzDevOpsEnvironmentChecks
.PARAMETER Project
Project name for Azure DevOps
.PARAMETER OutputPath
Path to output JSON files
.PARAMETER PassThru
Return the exported environments as objects to the pipeline instead of writing to a file
.EXAMPLE
Export-AzDevOpsEnvironmentChecks -Project $Project
#>
Expand All @@ -136,9 +142,12 @@ function Export-AzDevOpsEnvironmentChecks {
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru

)
if ($null -eq $script:connection) {
Expand All @@ -159,9 +168,13 @@ function Export-AzDevOpsEnvironmentChecks {
$environment | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.{2}" -f $script:connection.Organization,$Project,$environment.name)
$checks = @(Get-AzDevOpsEnvironmentChecks -Project $Project -Environment $environment.id)
$environment | Add-Member -MemberType NoteProperty -Name checks -Value $checks
Write-Verbose "Exporting environment $($environment.name) to JSON"
Write-Verbose "Output file: $OutputPath\$($environment.name).ado.env.json"
$environment | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($environment.name).ado.env.json"
if($PassThru) {
Write-Output $environment
} else {
Write-Verbose "Exporting environment $($environment.name) to JSON"
Write-Verbose "Output file: $OutputPath\$($environment.name).ado.env.json"
$environment | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($environment.name).ado.env.json"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ Export-ModuleMember -Function Get-AzDevOpsReleaseDefinitionAcls
.PARAMETER OutputPath
The path to the directory where the JSON files will be exported.
.PARAMETER PassThru
If set, the function will return the release definitions as objects instead of writing them to a file.
.EXAMPLE
Export-AzDevOpsReleaseDefinitions -Project 'myproject' -OutputPath 'C:\temp'
#>
Expand All @@ -131,8 +134,11 @@ Function Export-AzDevOpsReleaseDefinitions {
[Parameter(Mandatory)]
[string]$Project,

[Parameter(Mandatory)]
[string]$OutputPath
[Parameter(ParameterSetName = 'JsonFile')]
[string]$OutputPath,

[Parameter(ParameterSetName = 'PassThru')]
[switch]$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand All @@ -155,8 +161,6 @@ Function Export-AzDevOpsReleaseDefinitions {
throw $_.Exception.Message
}
$definitionName = $response.name
Write-Verbose "Exporting release definition $definitionName as file $definitionName.ado.rd.json"
$definitionPath = Join-Path -Path $OutputPath -ChildPath "$definitionName.ado.rd.json"
# Add an ObjectType of Azure.DevOps.Pipelines.Releases.Definition to the response
$response | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Azure.DevOps.Pipelines.Releases.Definition'
$response | Add-Member -MemberType NoteProperty -Name 'ObjectName' -Value ("{0}.{1}.{2}" -f $script:connection.Organization,$Project,$definitionName)
Expand All @@ -178,7 +182,14 @@ Function Export-AzDevOpsReleaseDefinitions {
} else {
Write-Warning "The ReadOnly token type is not supported for ACL export"
}
$response | ConvertTo-Json -Depth 100 | Out-File -FilePath $definitionPath
# If the PassThru switch is set, return the response object
if ($PassThru) {
Write-Output $response
} else {
Write-Verbose "Exporting release definition $definitionName as file $definitionName.ado.rd.json"
$definitionPath = Join-Path -Path $OutputPath -ChildPath "$definitionName.ado.rd.json"
$response | ConvertTo-Json -Depth 100 | Out-File -FilePath $definitionPath
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Export-ModuleMember -Function Get-AzDevOpsPipelinesSettings
.PARAMETER OutputPath
Output path for JSON files
.PARAMETER PassThru
Return the exported pipelines settings as objects to the pipeline instead of writing to a file
.EXAMPLE
Export-AzDevOpsPipelinesSettings -Project $Project -OutputPath $OutputPath
#>
Expand All @@ -62,9 +65,12 @@ function Export-AzDevOpsPipelinesSettings {
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand All @@ -74,7 +80,11 @@ function Export-AzDevOpsPipelinesSettings {
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name ObjectType -Value 'Azure.DevOps.Pipelines.Settings'
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.PipelineSettings" -f $script:connection.Organization,$Project)
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name Name -Value $Project
$pipelinesSettings | ConvertTo-Json -Depth 10 | Out-File (Join-Path -Path $OutputPath -ChildPath "$Project.ado.pls.json")
if ($PassThru) {
Write-Output $pipelinesSettings
} else {
$pipelinesSettings | ConvertTo-Json -Depth 10 | Out-File (Join-Path -Path $OutputPath -ChildPath "$Project.ado.pls.json")
}
}
Export-ModuleMember -Function Export-AzDevOpsPipelinesSettings
# End of Function Export-AzDevOpsPipelinesSettings
22 changes: 16 additions & 6 deletions src/PSRule.Rules.AzureDevOps/Functions/DevOps.Repos.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ Export-ModuleMember -Function Get-AzDevOpsRepositoryGhas
.PARAMETER OutputPath
Output path for JSON file
.PARAMETER PassThru
Return the exported repos as objects to the pipeline instead of writing to a file
.EXAMPLE
Export-AzDevOpsReposAndBranchPolicies -Project $Project -OutputPath $OutputPath
Expand All @@ -404,9 +407,12 @@ function Export-AzDevOpsReposAndBranchPolicies {
[Parameter(Mandatory)]
[string]
$Project,
[Parameter(Mandatory)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,
[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
if ($null -eq $script:connection) {
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
Expand Down Expand Up @@ -471,11 +477,15 @@ function Export-AzDevOpsReposAndBranchPolicies {
$repoAcls = Get-AzDevOpsRepositoryAcls -ProjectId $repo.project.id -RepositoryId $repo.id
$repo | Add-Member -MemberType NoteProperty -Name Acls -Value $repoAcls
}

# Export repo object to JSON file
Write-Verbose "Exporting repo $($repo.name) and its branches to JSON as file $($repo.name).ado.repo.json"
$branches += $repo
$branches | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($repo.name).ado.repo.json"
# If the PassThru switch is set, return the repo object
if ($PassThru) {
Write-Output $branches
} else {
# Export repo object to JSON file
Write-Verbose "Exporting repo $($repo.name) and its branches to JSON as file $($repo.name).ado.repo.json"
$branches | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($repo.name).ado.repo.json"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Export-ModuleMember -Function Get-AzDevOpsRetentionSettings
.PARAMETER OutputPath
The path to export the retention settings to.
.PARAMETER PassThru
If set, the function will return the retention settings as objects instead of writing them to a file.
.EXAMPLE
Get-AzDevOpsRetentionSettings -Project 'MyProject' -OutputPath 'C:\Temp\'
Expand All @@ -75,11 +78,19 @@ Function Export-AzDevOpsRetentionSettings {
[string]
$Project,

[Parameter(Mandatory=$true)]
[Parameter(ParameterSetName = 'JsonFile')]
[string]
$OutputPath
$OutputPath,

[Parameter(ParameterSetName = 'PassThru')]
[switch]
$PassThru
)
$settings = Get-AzDevOpsRetentionSettings -Project $Project
$settings | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($Project).ret.ado.json"
if($PassThru) {
Write-Output $settings
} else {
$settings | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($Project).ret.ado.json"
}
}
Export-ModuleMember -Function Export-AzDevOpsRetentionSettings
Loading

0 comments on commit 2e8a1fb

Please sign in to comment.