Closed snlehton closed 4 years ago
Hi there. Thanks for your bug report. Can I ask which type of template you are creating?
If you're using 1.2.0 it will be one of:
I suspect a velocity template. I have a hunch that they might not be resolved correctly. Either that or I have a regression to find.
Also, I just realised you're using the preview option. Please set the webhook parameter and actually run a build with that parameter set.
Then when you preview choose that build as the build to base the preview from.
That preview feature pulls in all the state from that actual build run. If that parameter was not set in the build, the preview won't find it, and it will be null or UNRESOLVED.
Also. The webhook.foo -> foo
aren't really as crucial in the templates, as you can simply add ${webhook.foo}
to your template where required.
They were more useful in the legacy formats where you wanted foo
to be a top level item in the payload because your endpoint was expecting it to be there.
Thanks for the quick reply. Yes I'm using jsonVelocityTemplate (that's the reason I installed 1.2 as the TC plugin dictionary only has 1.1.x).
BTW; I would love to see some documentation about the more complex variables (I know this is a separate issue but I'll just put it out here). Example:
#macro( showchanges $mychanges)
#if ( $mychanges.size() > 0 )
#foreach( $change in $mychanges )
<$githubBaseURL/substr($change.version,0,7,32)|#substr($change.version,0,7,32)> :: $change.change.username :: #escapejson($change.change.comment)
#end
#else No Changes found #end
#end
For example how do I know what fields or methods are available like $change.version
or $mychanges.size()
?
I just tested the template on a new build (where the variable has been set), and I can confirm that it is working!
But I also noticed that my idea of passing the Github repo url does not really work, as the DSL repo I'm using for TeamCity configuration triggers new builds where the changelist is effectively from a different repo (the DSL repo).
Which leads to the question that how can I know what is available in the $change
variable? Could I actually figure out the Github repo address from there?
I think you should be able to. I can't remember what changes contains, but hopefully it's a SVcsModification. You can try calling the methods on that object or anything it extends.
Hmm, I tried that and VcsChange, but I guess the "change" is actually WebHooksChanges and "change.change" is WebHooksChange. From there I can get vcsRoot, and renaming the vcsRoot accordingly, I could get it to work (namely, removed the default #branch from it, to only have "https://github.com/username/repository.git" in there)
With this:
#macro( showchanges $mychanges)
#if ( $mychanges.size() > 0 )
#foreach( $change in $mychanges )
#set( $len = ${change.change.vcsRoot.length()} - 4 )
#set ( $message = $change.change.comment.substring(0, 64))
<#substr($change.change.vcsRoot,0,$len,32)/commit/$change.version|#substr($change.version,0,7,32)> :: $change.change.username :: #escapejson($message)
#end
#else No Changes found #end
#end
I was able to make it work so that I see commits and have links to them into Github. The name of the vcsRoot is expected to have ".git" in the end, hence that substring part.
I think this is now starting to be usable :) Case closed.
Oh dear. Sorry I lead you astray on that one. There was a good reason for implementing those classes. The default TeamCity implementations have circular references, and won't serialise to JSON correctly.
I think on 1.2.0 the build object is available to velocity and you could get the modifications from that, and then you'd have the TeamCity versions, not my JSON compatible webhook versions.
PS. Sorry to be so vague. I'm on vacation at the moment, and only have my phone on which to reply. Trawling through code is tricky.
build
will be of type SBuild
on case you want to find it on the javadoc page posted above.
I think on 1.2.0 the build object is available to velocity and you could get the modifications from that, and then you'd have the TeamCity versions, not my JSON compatible webhook versions.
That sounds cool!
PS. Sorry to be so vague. I'm on vacation at the moment, and only have my phone on which to reply. Trawling through code is tricky.
Don't worry, Internet will be there even after your vacation so just disable the notifications :)
For now I'm happy with how this works. Version 1.2 allows us to write pretty complex Slack messages
Great. If you can share a working solution I'd be grateful. There is a project for sharing templates at https://github.com/tcplugins/tcWebHooksTemplates
Exporting is now super easy in 1.2.0. There is a link in the actions menu (top right).
One thing I've been thinking about recently is a JsonToMap command in velocity. I'd hope it could be used to map git names to slack names, and then you could @ people in the slack message.
So you could define a map as JSON in a build parameter and then use it as a map in your velocity code.
BTW, JsonMap is now implemented (in alpha4). See : https://github.com/tcplugins/tcWebHooks/wiki/WebHook-Templates-:-The-Velocity-Templating-Engine
Expected Behavior
Based on the code here https://github.com/tcplugins/tcWebHooks/blob/a8ea5c10614406fb5a1a7d7b8321bca95fef1c9b/tcwebhooks-core/src/main/java/webhook/teamcity/WebHookContentBuilder.java#L291 I expect to be able to supply parameters from the project/build settings to tcWebHooks templates by adding "webhook." in the beginning of the parameter.
Current Behavior
Parameters starting with "webhook." are not available in the templates.
Steps to Reproduce (for bugs)
Your Environment
Example Configuration (xml)
(not relevant)