Extra tricks for sending webhooks to HipChat from tcWebHooks

Posted on Updated on

Brian asked in the comments about setting the notify flag and changing the colour of messages sent to HipChat channels from tcWebHooks.

I emailed the wonderfully responsive HipChat support and Garret from HipChat replied that those extra options are already enabled on the TeamCity endpoint. Because of that you can make use of an undocumented feature of tcWebHooks. “ExtraParmameters”

Edit the ${HOME}/.BuildServer/config/ProjectName/plugin-settings.xml file and add the following before the closing tag of your webhook(s).

       <parameters>
        <param name="color" value="red" />
        <param name="notify" value="1" />
      </parameters>

This will set the message to have a red background and play a sound when the message is posted to the channel. Note the bug below though.

I would suggest splitting up your webhook into two seperate webhooks. One as a “success” webhook and one as a “failed” webhook. Then you can then set the correct background colour based on the result of your build.

Here is a full example. Remember to set your HipChat API URL correctly.

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <webhooks enabled="true">
    <webhook url="https://api.hipchat.com/.........." enabled="true" format="nvpairs">
      <states>
        <state type="buildStarted" enabled="false" />
        <state type="buildSuccessful" enabled="false" />
        <state type="buildFinished" enabled="true" />
        <state type="buildFixed" enabled="false" />
        <state type="buildBroken" enabled="false" />
        <state type="responsibilityChanged" enabled="false" />
        <state type="beforeBuildFinish" enabled="false" />
        <state type="buildInterrupted" enabled="false" />
        <state type="buildFailed" enabled="true" />
      </states>
       <parameters>
        <param name="color" value="red" />
        <param name="notify" value="1" />
      </parameters>
    </webhook>
    <webhook url="https://api.hipchat.com/.........." enabled="true" format="nvpairs">
      <states>
        <state type="buildStarted" enabled="false" />
        <state type="buildSuccessful" enabled="true" />
        <state type="buildFinished" enabled="false" />
        <state type="buildFixed" enabled="false" />
        <state type="buildBroken" enabled="false" />
        <state type="responsibilityChanged" enabled="false" />
        <state type="beforeBuildFinish" enabled="false" />
        <state type="buildInterrupted" enabled="false" />
        <state type="buildFailed" enabled="false" />
      </states>
       <parameters>
        <param name="color" value="green" />
        <param name="notify" value="1" />
      </parameters>
    </webhook>
  </webhooks>
</settings>


Beware of a bug. Editing webhooks in the UI does not properly retain the extra parameters. I’ll fix that in the next release. Until then, you need to add them back in manually.

This is now fixed in the 0.8.27.139 release.

Advertisements

24 thoughts on “Extra tricks for sending webhooks to HipChat from tcWebHooks

    Brian Craig said:
    April 16, 2013 at 19:03

    This worked perfectly for me. Thanks!

    perryjason said:
    May 22, 2013 at 18:59

    Very cool. Is there a way to pass the names of the committers for that failed build this way it could potentially cause their hipchat icon to bounce?

      netwolfuk responded:
      May 23, 2013 at 13:03

      I can’t think of a way to do this without some very hip chat specific code to build the list of usernames. What if the usernames need translating? Eg. Commiter’s username != hipchat username.

    perryjason said:
    May 22, 2013 at 19:23

    Or possibly add “@all” to the message so that everyone gets a notification.

      netwolfuk responded:
      May 23, 2013 at 13:05

      Yes. That’s possible now. I really should document custom message templates.

        perryjason said:
        May 23, 2013 at 13:10

        @netwolfuk – How do I got about adding @all to the beginning of the message that is already sent?

        netwolfuk responded:
        May 23, 2013 at 13:32

        I’ve forwarded you an email. Let me know how you get on and whether it’s good enough for general release. I can’t test it as I don’t have hipchat.

        Richard Green said:
        September 30, 2014 at 08:34

        Please can you help me with creating a custom message template. I had to download your source code to try and understand how to create the node but it’s just not working! I assume it’s because I need to give my custom-template a certain “type” but I don’t know what it should be!!

        netwolfuk responded:
        September 30, 2014 at 09:00

        Sure. Where are you trying to POST to? What would you like the template to say?
        The default template is here: https://github.com/tcplugins/tcWebHooks/blob/master/tcwebhooks-core/src/main/java/webhook/teamcity/payload/WebHookPayloadDefaultTemplates.java

        Richard Green said:
        September 30, 2014 at 09:06

        Thanks for your quick reply!

        I’m using it for hipchat integration, but what would be really useful would be to show the build branch, i.e. master or pull/959 or feature/MyFeature.

        I found the default template but couldn’t figure out how to override it – so I assume from the source code it needs to have a type of buildStatusHtml?

        netwolfuk responded:
        September 30, 2014 at 09:32

        Yes, you are exactly right. buildStatusHtml is a keyword that Garret (HipChat Support guy) and I agreed upon several years ago. So you need to create a custom parameter that templates that string. I did have docs for this, but sourceforge removed my wiki before I got them copied off (my fault, not theirs).
        I’ll quickly hack something together and post a link here shortly.

        Richard Green said:
        September 30, 2014 at 09:36

        Thanks! I have just literally this second got it working 😀

        I’ve done it like this (hope this encodes ok):

        Richard Green said:
        September 30, 2014 at 09:38
        netwolfuk responded:
        September 30, 2014 at 09:43

        Nice work! Well done. Do you mind if I copy this example for the docs?

        Richard Green said:
        September 30, 2014 at 09:44

        Absolutely, feel free – thanks for your help!

    Tad said:
    June 25, 2013 at 15:05

    Definitely interested in custom message templates as well. Could you shoot me over details on such? As @perryjason said – adding an @all to certain build failures would be quite helpful.

    Kyle said:
    July 22, 2013 at 23:55

    Is there something I’m doing wrong? I can’t seem to get the custom template to be used. The color seems to be working fine.

    Kyle said:
    July 22, 2013 at 23:57

    Wow my xml didn’t show. Here is the text within the template attribute of the custom-template tag: <span class="tcWebHooksMessage"><strong>Success</strong> ;<a href="${rootUrl}/project.html?projectId=${projectId}">${projectName}</a> :: <a href="${rootUrl}/viewType.html?buildTypeId=${buildTypeId}">${buildName}</a> <a href="${rootUrl}/viewLog.html?buildTypeId=${buildTypeId}&buildId=${buildId}"><strong>#${buildNumber}</strong></a> triggered by <strong>${triggeredBy}</strong></span>

    […] in Teamcity is explained on the TeamCity configuration properties wiki page. More info about using tcWebHooks custom parameters is also […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s