tcWebHooks
tcWebHooks copied to clipboard
Variables not populated in the Started event
I am working on TeamCity templates for tcWebHooks and I have a branch build template defined like so:
{
"markdown": "[${projectName}](${rootUrl}/project.html?projectId=${projectExternalId}) :: [${buildName}]( ${rootUrl}/viewType.html?buildTypeId=${buildExternalTypeId}) | [${branchDisplayName} ${substr(build.vcs.number,0,7,32)}](${buildStatusUrl}&tab=buildChangesDiv) | [build #${buildNumber}](${buildStatusUrl}) | **${capitalise(buildStateDescription)}**"
}
The variable ${substr(build.vcs.number,0,7,32)}
gets defined in the Started hook as ??? but is correctly defined in later hooks:
[2018-04-18 16:40:54,698] INFO - jetbrains.buildServer.SERVER - WebHookListener :: :: WebHook triggered : https://blahblah using format jsontemplate returned 204 No Content
[2018-04-18 16:40:54,698] DEBUG - jetbrains.buildServer.SERVER - WebHookListener :: :doPost :: content dump: {
"markdown": "[test proj](http://localhost:8111/project.html?projectId=TestProj) :: [test branch proj](http://localhost:8111/viewType.html?buildTypeId=TestProj_TestBranchProj) | [imaci5 ???](http://localhost:8111/viewLog.html?buildTypeId=TestProj_TestBranchProj&buildId=54&tab=buildChangesDiv) | [build #36](http://localhost:8111/viewLog.html?buildTypeId=TestProj_TestBranchProj&buildId=54) | **Started**"
}
[2018-04-18 16:40:56,054] DEBUG - jetbrains.buildServer.SERVER - WebHookListener :: :doPost :: content dump: {
"markdown": "[test proj](http://localhost:8111/project.html?projectId=TestProj) :: [test branch proj](http://localhost:8111/viewType.html?buildTypeId=TestProj_TestBranchProj) | [imaci5 72123ab](http://localhost:8111/viewLog.html?buildTypeId=TestProj_TestBranchProj&buildId=54&tab=buildChangesDiv) | [build #36](http://localhost:8111/viewLog.html?buildTypeId=TestProj_TestBranchProj&buildId=54) | **Loaded Changes**"
}
I'm pretty much just copying the Slack templates so I am surprised to see this. Is this expected behavior and is the Slack template doing the same thing?
Hi @dgilman
Yes I noticed that with the slack one this week. It would be related to #26.
I just checked the slack messages. The loaded changes event does have the commit and branch name correctly populated (eg, master
rather than <default>
)
I've created a loaded changes hook that says "Started" as a workaround which will work and I'm ok with upstreaming (if you're ok with merging).
I'm coming to Spark from Slack and the tcSlackBuildNotifier plugin which fired a Started message as soon as the build started (it seems) and was able to correctly figure out the branch and commit ID. I don't really know the TeamCity API at all but it looks like it just listens to buildStarted and not changesLoaded.
I'll have a look at Pete's code and see what he's doing. It's actually a fork of mine from about 5 years ago.
BTW. Have a look at the tcWebHooksTemplates project on GitHub which I've be working on this week for a way to share templates.
Yes. He's manually loading the changes and mapping them to a slack username. https://github.com/PeteGoo/tcSlackBuildNotifier/blob/master/tcslackbuildnotifier-core/src/main/java/slacknotifications/teamcity/payload/content/SlackNotificationPayloadContent.java#L138
I installed the tcSlackNotifier plugin and have noticed the following behaviour:
If there are no changes, the start event does not list anything.
If there are changes, then it builds a nice commit message
This behaviour is consistent with the tcWebHooks Legacy JSON payload format. To test this, I just created a legacy json webhook and pointed it at http://localhost:8111/webhooks/endpoint.html
No changes:
Changes:
However, when I built the Slack template, I used the TeamCity defined parameter because:
- The webhook template templating language has no support for conditionals.
- The changes object does not serialise to JSON correctly (See #98)
- If a build ran with no changes, it wouldn't give any indication of which revision it was built against anyway. This is consistent with the behaviour of the Slack Notifier on start.
I will add some more payload variables to the standard set to give us a bit more info at start. However, in the case where there is a build run without any changes, this will still be blank.
Off the top of my head the following would be useful.
- buildHasChanges: boolean
- buildChangesComment: string
- buildChangesUsername: string
- buildChangesVersion: string
- buildChangesFileCount: int
These would give the most recent change, although from screenshots, it looks like the tcSlackNotifier lists all contributing changes, not the latest.
However, without conditionals in the template it wouldn't solve our problem yet.
So to get around that, we could have a macro (like the substring one), at which point it starts to get messy, and I should really just finish the work to support the Velocity template language.
I need to revisit this in 1.2, as most of these issues should be mitigated with conditionals in velocity templates.
Hi @dgilman. Please see comments on the end of #26 I've added a feature to resolve variables early in 1.2.0-alpha8
1.2.0-alpha8 finally released.