tcplugins/tcWebHooks

Cannot format date

bsinou opened this issue · 10 comments

Hi,

First, thanks for the great job, it helps a lot! I yet have an issue while handling dates.

Expected Behavior

Start and End (retrieved with currentDate) timestamps should be displayed using iso format.

Current Behavior

Date are generated in the form of 8/31/23, 9:00 AM, both in previews and in the payload that is sent to the webhook.

Steps to Reproduce (for bugs)

  1. Define a new template with
    • Date Format: yyyy-MM-ddTHH:mm:ssZ
    • Payload Format: jsonVelocityTemplate
  2. Get the date in the json with eg:
{
        "TimeStarted": "${buildStartTime}", 
        "TimeEnd": "${currentTime}"
}
  1. Try both preview and webhook => dates are still wrongly formatted.

I have also tried to get access to the dateTool object [1] without success.

Your Environment

  • tcWebHooks Version: Initially tried with 1.2.2 (from the market place) and then also with 1.2.4 and 2.0.0-rc2 (directly from github)
  • TeamCity Version: 2023.05.3

Any hint would be greatly appreciated.

[1]: Seems like it has been added for 1.2.4 at least

Thanks for raising this bug. What you're doing should be working. I'll take a look when I get to a computer.

I can't see why setting the dateformat on the template is not working. I'll try to do some more investigation on the weekend.

I was able to use datetool (in version 1.2.4) with the following Velocity template...

{
        "startDate": "$dateTool.format('iso_tz', ${build.StartDate.Time})"
        "currentTime": "$dateTool.format('iso_tz', $dateTool)"
}

outputs...

{
        "startDate": "2023-08-07T22:57:38Z"
        "currentTime": "2023-09-01T10:23:28Z"
}

$dateTool appear to use some odd formatting style (not the normal one as you would expect).
See https://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/DateTool.html

I'll also patch 2.0.0 with the dateTool so that is same as 1.2.4 and release a new 2.0 RC. Hopefully will get that out on the weekend too.

You may have already seen this issue all about dates: #222
With some more examples on using dateTool with the objects in the template.

I was able to get $dateTool to use SimpleDateFormat format by setting a variable and then referencing...

#set($myDate = $dateTool.format("yyyy-MM-dd'T'HH:mm:ssZ",$dateTool))
{
        "iso_tz": "$dateTool.format('iso_tz',$dateTool)",
        "myDate": "$myDate"
}

It doesn't use Z for zulu time, but prints the offset (+0000)

{   
        "iso_tz": "2023-09-01T10:58:12Z",   
        "myDate": "2023-09-01T10:58:12+0000" 
}

Or you can set the format....

#set($myDate = $dateTool.format("yyyy-MM-dd'T'HH:mm:ssZ",$dateTool))
#set($myFormat = "yyyy-MM-dd'T'HH:mm:ssZ")

{
        "iso_tz": "$dateTool.format('iso_tz',$dateTool)",
        "myDate": "$myDate",
        "myFormat": "$dateTool.format($myFormat,$dateTool)"
}
{
        "iso_tz": "2023-09-01T11:03:15Z",
        "myDate": "2023-09-01T11:03:15+0000",
        "myFormat": "2023-09-01T11:03:15+0000"
}

Thanks a lot for all these explanations and tips.

This indeed fixes my problem until the date format issue is solved.

"endDate": "$dateTool.format('iso_tz',$dateTool)",

For the record yet, I could not have it work with v2-rc2: I had to revert both pluging (webhook and REST) to the 1.2.4

Yeah, sorry about that. The date tool is very new and I haven't released an updated 2.0.0: with it included yet.

No need to be sorry :)
You already saved my day with all your explanations and your prompt answer! Thanks again.

Hmm. There are lots of interesting bug in this behaviour.

  1. Previewing or executing in the template editor fails to pass the templateId or the preferredDateTime. Therefore the code has no way to determine how to resolve the date format you want.
  2. Previewing or executing the webhook from the webhook edit dialog box, throws a GSON exception.
    Update: My JSON was missing some commas, so it was not well defined. Once that was fixed the webhook preview worked.
  3. Executing a webhook from a build event does work (I tested with pinned event).

Result: Preview/test from the template editor does not support preferredDateTime. This is a bug.