PagerDuty curl examples do not translate
Closed this issue · 4 comments
curl
command
curl --request PUT \
--url https://api.pagerduty.com/incidents/id/alerts/alert_id \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Token token=y_NbAkKc66ryYTWUXYEu' \
--header 'Content-Type: application/json' \
--header 'From: ' \
--data '{
"alert": {
"type": "alert",
"status": "resolved",
"incident": {
"id": "PEYSGVF",
"type": "incident_reference"
},
"body": {
"type": "alert_body",
"contexts": [
{
"type": "link"
}
],
"details": {
"customKey": "Server is on fire!",
"customKey2": "Other stuff!"
}
}
}
}'
Expected Behavior
Header and Body values are populated
Current Behavior
When running with this example the header values are dropped and the body is blank.
Possible Solution
I haven't had time to look into it, but it appears to be an issue with how PagerDuty is formatting. I don't know if they are doing something strange or if this format is used elsewhere.
Steps to Reproduce (for bugs)
$CurlString = @'
curl --request PUT \
--url https://api.pagerduty.com/incidents/id/alerts/alert_id \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Token token=y_NbAkKc66ryYTWUXYEu' \
--header 'Content-Type: application/json' \
--header 'From: ' \
--data '{
"alert": {
"type": "alert",
"status": "resolved",
"incident": {
"id": "PEYSGVF",
"type": "incident_reference"
},
"body": {
"type": "alert_body",
"contexts": [
{
"type": "link"
}
],
"details": {
"customKey": "Server is on fire!",
"customKey2": "Other stuff!"
}
}
}
}'
'@
ConvertTo-IRM -CurlCommand $CurlString -CommandAsString
Context
You can view this specific one and all other example in their API reference. https://developer.pagerduty.com/api-reference/4e932ba0c1989-update-an-alert
Your Environment
> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.5
PSEdition Core
GitCommitId 7.2.5
OS Microsoft Windows 10.0.19044
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> Get-Module Curl2PS
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 0.1.0 Curl2PS {ConvertTo-IRM, Get-CurlCommand}
At first glance it appears that the command line parser is not correctly parsing the values inside of single quotes. I'll do some further testing to see what's going on.
Thank you for the detailed report!
Ok, I figured out a better way to support quotes by using PowerShell's native parsing rather than an outside library.
Right now the code leverages Microsoft.CodeAnalysis
, an independent library, to do the command line parsing by:
[Microsoft.CodeAnalysis.CommandLineParser]::SplitCommandLineIntoArguments($curlString, $true)
And after it parses the command line, it looks for curl parameters and values and converts the ones that it is aware of.
However, I just had a eureka moment and realized that PowerShell can actually parse parameters for you with the $args
variable. So what I'm going to test is:
function parse {
$args
}
Invoke-Command -ScriptBlock ([scriptblock]::create("parse $curlString"))
And the output looks much better:
curl
--request
PUT
--url
https://api.pagerduty.com/incidents/id/alerts/alert_id
--header
Accept: application/vnd.pagerduty+json;version=2
--header
Authorization: Token token=y_NbAkKc66ryYTWUXYEu
--header
Content-Type: application/json
--header
From:
--data
{ "alert": { "type": "alert", "status": "resolved", "incident": { "id": "PEYSGVF", "type": "incident_reference" }, "body": { "type": "alert_body", "contexts": [ { "type": "link" } ], "details": { "customKey": "Server is on fire!", "customKey2": "Other stuff!" } } } }
I could probably also clean up the json body... I'll see if I can get that in there as well.
I'll update the code, test, and post back when I publish the changes.
Thanks again for reporting this!
Ok, this has been fixed in v0.1.1! Thank you for reporting it. The parsing is greatly improved with such a simple fix and I also added a -CompressJSON
parameter that will attempt to use ConvertFrom-JSON | ConvertTo-JSON -Compress
to clean up the body. If you have any issues with the new version related to these changes, post pack on this issue and I'll take a look.
Thanks!