tcplugins / tcWebHooks

WebHooks plugin for Teamcity. Supports many build states and payload formats.
https://netwolfuk.wordpress.com/category/teamcity/tcplugins/tcwebhooks/
157 stars 30 forks source link

Supplying parameters from project/build settings #151

Closed snlehton closed 4 years ago

snlehton commented 4 years ago

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)

  1. Add parameter with "webhook.githubBaseURL" in project settings with pointing to your Github project root
  2. Write a template with "${githubBaseURL}"
  3. Preview the template: ${githubBaseURL} is not expanded and reads as "${githubBaseURL}" and not the actual URL

Your Environment

Example Configuration (xml)

(not relevant)

netwolfuk commented 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.

netwolfuk commented 4 years ago

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.

netwolfuk commented 4 years ago

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.

snlehton commented 4 years ago

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() ?

snlehton commented 4 years ago

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?

netwolfuk commented 4 years ago

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.

http://javadoc.jetbrains.net/teamcity/openapi/current/jetbrains/buildServer/vcs/SVcsModification.html

snlehton commented 4 years ago

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.

netwolfuk commented 4 years ago

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.

netwolfuk commented 4 years ago

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.

netwolfuk commented 4 years ago

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.

netwolfuk commented 4 years ago

build will be of type SBuild on case you want to find it on the javadoc page posted above.

snlehton commented 4 years ago

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

netwolfuk commented 4 years ago

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.

netwolfuk commented 4 years ago

BTW, JsonMap is now implemented (in alpha4). See : https://github.com/tcplugins/tcWebHooks/wiki/WebHook-Templates-:-The-Velocity-Templating-Engine