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

Fixing Required Attachments #419

Open
wants to merge 3 commits into
base: release/2.15-alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions JiraPS/Private/Resolve-ErrorWebResponse.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,29 @@ function Resolve-ErrorWebResponse {
try {
$responseObject = ConvertFrom-Json -InputObject $responseBody -ErrorAction Stop

$errorArray = @()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use [System.Collections.ArrayList]

Reading material on why: https://gist.github.com/kevinblumenfeld/4a698dbc90272a336ed9367b11d91f1c

foreach ($_error in ($responseObject.errorMessages + $responseObject.errors)) {

foreach ($_err in $_error.PSObject.Properties.Value) {
if ($_err -like "*You must specify a summary of the issue." -Or $_err -like "*is required.") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't trust parsing text from the answer. What happens when the system is installed in another language?
(Unfortunately I can't test that)

Is there any other way we can identify this?

Write-Debug $_err
$errorArray += $_err
} else {
# unhandled value
}
}

if ($errorArray.count -gt 0) {
$errorParameter = @{
ExceptionType = "System.Net.Http.HttpRequestException"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same exception type as Invoke-WebRequest returns
(System.Net.WebException for PSVersion -lt 6 and Microsoft.PowerShell.Commands.HttpResponseException for PSVersion -ge 6)

Message = "Missing Required Fields: $errorArray"
ErrorId = "AuthenticationFailed"
Category = "AuthenticationError"
Cmdlet = $Cmdlet
}
ThrowError @errorParameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't use throw here. Throw will cause the entire script to fail when this happens (unless used with try {} catch {}.
Please use WriteError (can have the same behavior if wanted, by adding -ErrorAction Stop to the script)

}

# $_error is a PSCustomObject - therefore can't be $false
if ($_error -is [PSCustomObject]) {
[String]$_error = ($_error | Out-String)
Expand Down
24 changes: 0 additions & 24 deletions JiraPS/Public/New-JiraIssue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ function New-JiraIssue {
process {
$server = Get-JiraConfigServer -ErrorAction Stop -Debug:$false

$createmeta = Get-JiraIssueCreateMetadata -Project $Project -IssueType $IssueType -Credential $Credential -ErrorAction Stop -Debug:$false

$resourceURi = "$server/rest/api/2/issue"

Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
Expand Down Expand Up @@ -141,28 +139,6 @@ function New-JiraIssue {
}
}

Write-Verbose "[$($MyInvocation.MyCommand.Name)] Validating fields with metadata"
foreach ($c in $createmeta) {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Checking metadata for `$c [$c]"
if ($c.Required) {
if ($requestBody.ContainsKey($c.Id)) {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] Required field (id=[$($c.Id)], name=[$($c.Name)]) was provided (value=[$($requestBody.$($c.Id))])"
}
else {
$exception = ([System.ArgumentException]"Invalid or missing value Parameter")
$errorId = 'ParameterValue.CreateMetaFailure'
$errorCategory = 'InvalidArgument'
$errorTarget = $Fields
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "Jira's metadata for project [$Project] and issue type [$IssueType] specifies that a field is required that was not provided (name=[$($c.Name)], id=[$($c.Id)]). Use Get-JiraIssueCreateMetadata for more information."
$PSCmdlet.ThrowTerminatingError($errorItem)
}
}
else {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] Non-required field (id=[$($c.Id)], name=[$($c.Name)])"
}
}

$hashtable = @{
'fields' = ([PSCustomObject]$requestBody)
}
Expand Down