Skip to content

Commit

Permalink
Createmeta update (#488)
Browse files Browse the repository at this point in the history
* Update Get-JiraIssueCreateMetadata.ps1

https://confluence.atlassian.com/jiracore/createmeta-rest-endpoint-to-be-removed-975040986.html

* Update ConvertTo-JiraCreateMetaField.ps1

https://confluence.atlassian.com/jiracore/createmeta-rest-endpoint-to-be-removed-975040986.html

* Update Get-JiraIssueCreateMetadata.ps1

https://confluence.atlassian.com/jiracore/createmeta-rest-endpoint-to-be-removed-975040986.html

* Update ConvertTo-JiraCreateMetaField.Unit.Tests.ps1

Altered test data to match what is returned by new createmeta call.
  • Loading branch information
robertmbaker authored Jun 13, 2024
1 parent 4f56b99 commit 4a64a1d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 121 deletions.
56 changes: 26 additions & 30 deletions JiraPS/Private/ConvertTo-JiraCreateMetaField.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,41 @@ function ConvertTo-JiraCreateMetaField {
)

process {
foreach ($i in $InputObject) {
foreach ($i in $InputObject.values) {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object"

$fields = $i.projects.issuetypes.fields
$fieldNames = (Get-Member -InputObject $fields -MemberType '*Property').Name
foreach ($f in $fieldNames) {
$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
}
$item = $i

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

if ($item.autoCompleteUrl) {
$props.AutoCompleteUrl = $item.autoCompleteUrl
}
if ($item.allowedValues) {
$props.AllowedValues = $item.allowedValues
}

foreach ($extraProperty in (Get-Member -InputObject $item -MemberType NoteProperty).Name) {
if ($null -eq $props.$extraProperty) {
$props.$extraProperty = $item.$extraProperty
}
}
if ($item.autoCompleteUrl) {
$props.AutoCompleteUrl = $item.autoCompleteUrl
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'JiraPS.CreateMetaField')
$result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "$($this.Name)"
foreach ($extraProperty in (Get-Member -InputObject $item -MemberType NoteProperty).Name) {
if ($null -eq $props.$extraProperty) {
$props.$extraProperty = $item.$extraProperty
}
}

Write-Output $result
$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'JiraPS.CreateMetaField')
$result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "$($this.Name)"
}

Write-Output $result
}
}
}
32 changes: 4 additions & 28 deletions JiraPS/Public/Get-JiraIssueCreateMetadata.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Get-JiraIssueCreateMetadata {

$server = Get-JiraConfigServer -ErrorAction Stop

$resourceURi = "$server/rest/api/2/issue/createmeta?projectIds={0}&issuetypeIds={1}&expand=projects.issuetypes.fields"
$resourceURi = "$server/rest/api/2/issue/createmeta/{0}/issuetypes/{1}"
}

process {
Expand All @@ -47,40 +47,16 @@ function Get-JiraIssueCreateMetadata {
Method = "GET"
Credential = $Credential
}

Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
$result = Invoke-JiraMethod @parameter

if ($result) {
if (@($result.projects).Count -eq 0) {
$errorMessage = @{
Category = "InvalidResult"
CategoryActivity = "Validating response"
Message = "No projects were found for the given project [$Project]. Use Get-JiraProject for more details."
}
Write-Error @errorMessage
}
elseif (@($result.projects).Count -gt 1) {
$errorMessage = @{
Category = "InvalidResult"
CategoryActivity = "Validating response"
Message = "Multiple projects were found for the given project [$Project]. Refine the parameters to return only one project."
}
Write-Error @errorMessage
}

if (@($result.projects.issuetypes) -eq 0) {
$errorMessage = @{
Category = "InvalidResult"
CategoryActivity = "Validating response"
Message = "No issue types were found for the given issue type [$IssueType]. Use Get-JiraIssueType for more details."
}
Write-Error @errorMessage
}
elseif (@($result.projects.issuetypes).Count -gt 1) {
if (@($result.values).Count -eq 0) {
$errorMessage = @{
Category = "InvalidResult"
CategoryActivity = "Validating response"
Message = "Multiple issue types were found for the given issue type [$IssueType]. Refine the parameters to return only one issue type."
Message = "No values were found for the given project [$Project]. Use Get-JiraProject for more details."
}
Write-Error @errorMessage
}
Expand Down
117 changes: 54 additions & 63 deletions Tests/Functions/ConvertTo-JiraCreateMetaField.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,71 +39,62 @@ Describe "ConvertTo-JiraCreateMetaField" -Tag 'Unit' {

$sampleJson = @'
{
"expand": "projects",
"projects": [
"values": [
{
"expand": "issuetypes",
"issuetypes": [
"required": true,
"schema": {
"type": "string",
"system": "summary"
},
"name": "Summary",
"fieldId": "summary",
"hasDefaultValue": false,
"operations": [
"set"
]
},
{
"required": false,
"schema": {
"type": "priority",
"system": "priority"
},
"name": "Priority",
"fieldId": "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"
},
{
"expand": "fields",
"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"
}
]
}
}
"self": "http://jiraserver.example.com/rest/api/2/priority/5",
"iconUrl": "http://jiraserver.example.com/images/icons/priorities/trivial.png",
"name": "Trivial",
"id": "5"
}
]
}
Expand Down

0 comments on commit 4a64a1d

Please sign in to comment.