Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get edit medta #59

Closed
wants to merge 5 commits into from
Closed
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
113 changes: 113 additions & 0 deletions PSJira/Internal/ConvertTo-JiraEditMetaField.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "ConvertTo-JiraEditMetaField" {
function defProp($obj, $propName, $propValue)
{
It "Defines the '$propName' property" {
$obj.$propName | Should Be $propValue
}
}

$sampleJson = @'
{
"fields": {
"summary": {
"required": true,
"schema": {
"type": "string",
"system": "summary"
},
"name": "Summary",
"hasDefaultValue": false,
"operations": [
"set"
]
},
"priority": {
"required": false,
"schema": {
"type": "priority",
"system": "priority"
},
"name": "Priority",
"hasDefaultValue": true,
"operations": [
"set"
],
"allowedValues": [
{
"self": "http://jiraserver.example.com/rest/api/2/priority/1",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/blocker.png",
"name": "Block",
"id": "1"
},
{
"self": "http://jiraserver.example.com/rest/api/2/priority/2",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/critical.png",
"name": "Critical",
"id": "2"
},
{
"self": "http://jiraserver.example.com/rest/api/2/priority/3",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/major.png",
"name": "Major",
"id": "3"
},
{
"self": "http://jiraserver.example.com/rest/api/2/priority/4",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/minor.png",
"name": "Minor",
"id": "4"
},
{
"self": "http://jiraserver.example.com/rest/api/2/priority/5",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/trivial.png",
"name": "Trivial",
"id": "5"
}
]
}
}
}
}
'@
$sampleObject = ConvertFrom-Json2 -InputObject $sampleJson

$r = ConvertTo-JiraEditMetaField $sampleObject

It "Creates PSObjects out of JSON input" {
$r | Should Not BeNullOrEmpty
$r.Count | Should Be 2
}

It "Sets the type name to PSJira.CreateMetaField" {
# Need to use the pipeline in this case, instead of directly using the
# -InputObject parameter. This is a quirk of PowerShell, arrays, and
# the pipeline.
($r | Get-Member).TypeName | Should Be 'PSJira.EditMetaField'
}

Context "Data validation" {
# Our sample JSON includes two fields: summary and priority.
$summary = ConvertTo-JiraEditMetaField $sampleObject | Where-Object -FilterScript {$_.Name -eq 'Summary'}
$priority = ConvertTo-JiraEditMetaField $sampleObject | Where-Object -FilterScript {$_.Name -eq 'Priority'}

defProp $summary 'Id' 'summary'
defProp $summary 'Name' 'Summary'
defProp $summary 'HasDefaultValue' $false
defProp $summary 'Required' $true
defProp $summary 'Operations' @('set')

It "Defines the 'Schema' property if available" {
$summary.Schema | Should Not BeNullOrEmpty
$priority.Schema | Should Not BeNullOrEmpty
}

It "Defines the 'AllowedValues' property if available" {
$summary.AllowedValues | Should BeNullOrEmpty
$priority.AllowedValues | Should Not BeNullOrEmpty
}
}
}
99 changes: 99 additions & 0 deletions PSJira/Internal/ConvertTo-JiraEditMetaField.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
function ConvertTo-JiraEditMetaField
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true)]
[PSObject[]] $InputObject,

[Switch] $ReturnError
)

process
{
foreach ($i in $InputObject)
{
Write-Debug "[ConvertTo-JiraEditMetaField] Processing object: '$i'"

if ($i.errorMessages)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Detected an errorMessages property. This is an error result."

if ($ReturnError)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Outputting details about error message"
$props = @{
'ErrorMessages' = $i.errorMessages;
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'PSJira.Error')

Write-Output $result
}
} else {
$fields = $i.fields
$fieldNames = (Get-Member -InputObject $fields -MemberType '*Property').Name
foreach ($f in $fieldNames)
{
Write-Debug "[ConvertTo-JiraEditMetaField] Processing field [$f]"
$item = $fields.$f

$props = @{
'Id' = $f;
'Name' = $item.name;
'HasDefaultValue' = [System.Convert]::ToBoolean($item.hasDefaultValue);
'Required' = [System.Convert]::ToBoolean($item.required);
'Schema' = $item.schema;
'Operations' = $item.operations;
}

if ($item.allowedValues)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Adding AllowedValues"
$props.AllowedValues = $item.allowedValues
}

if ($item.autoCompleteUrl)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Adding AutoCompleteURL"
$props.AutoCompleteUrl = $item.autoCompleteUrl
}

# Write-Debug "[ConvertTo-JiraEditMetaField] Checking for any additional properties"
foreach ($extraProperty in (Get-Member -InputObject $item -MemberType NoteProperty).Name)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Checking property $extraProperty"
if ($props.$extraProperty -eq $null)
{
# Write-Debug "[ConvertTo-JiraEditMetaField] - Adding property [$extraProperty]"
$props.$extraProperty = $item.$extraProperty
}
}

# Write-Debug "[ConvertTo-JiraEditMetaField] Creating PSObject out of properties"
$result = New-Object -TypeName PSObject -Property $props

# Write-Debug "[ConvertTo-JiraEditMetaField] Inserting type name information"
$result.PSObject.TypeNames.Insert(0, 'PSJira.EditMetaField')

# Write-Debug "[ConvertTo-JiraEditMetaField] Inserting custom toString() method"
$result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "$($this.Name)"
}

# Write-Debug "[ConvertTo-JiraEditMetaField] Outputting object"
Write-Output $result
}
}
}
}

end
{
# Write-Debug "[ConvertTo-JiraEditMetaField] Complete"
}
}


20 changes: 10 additions & 10 deletions PSJira/PSJira.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ FormatsToProcess = 'PSJira.format.ps1xml'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Add-JiraGroupMember', 'Add-JiraIssueComment', 'Format-Jira',
'Get-JiraConfigServer', 'Get-JiraField', 'Get-JiraFilter',
'Get-JiraGroup', 'Get-JiraGroupMember', 'Get-JiraIssue',
'Get-JiraIssueComment', 'Get-JiraIssueCreateMetadata',
'Get-JiraIssueType', 'Get-JiraPriority', 'Get-JiraProject',
'Get-JiraSession', 'Get-JiraUser', 'Invoke-JiraIssueTransition',
'New-JiraGroup', 'New-JiraIssue', 'New-JiraSession', 'New-JiraUser',
'Remove-JiraGroup', 'Remove-JiraGroupMember', 'Remove-JiraSession',
'Remove-JiraUser', 'Set-JiraConfigServer', 'Set-JiraIssue',
FunctionsToExport = 'Add-JiraGroupMember', 'Add-JiraIssueComment', 'Format-Jira',
'Get-JiraConfigServer', 'Get-JiraField', 'Get-JiraFilter',
'Get-JiraGroup', 'Get-JiraGroupMember', 'Get-JiraIssue',
'Get-JiraIssueComment', 'Get-JiraIssueCreateMetadata', 'Get-JiraIssueEditMetadata',
'Get-JiraIssueType', 'Get-JiraPriority', 'Get-JiraProject',
'Get-JiraSession', 'Get-JiraUser', 'Invoke-JiraIssueTransition',
'New-JiraGroup', 'New-JiraIssue', 'New-JiraSession', 'New-JiraUser',
'Remove-JiraGroup', 'Remove-JiraGroupMember', 'Remove-JiraSession',
'Remove-JiraUser', 'Set-JiraConfigServer', 'Set-JiraIssue',
'Set-JiraIssueLabel', 'Set-JiraUser'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down Expand Up @@ -122,7 +122,7 @@ PrivateData = @{
# ExternalModuleDependencies = ''

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
Expand Down
Loading