Implement Mustache templates for messages #32

Open
wants to merge 20 commits into
from

Projects

None yet

8 participants

@marcins
marcins commented Feb 9, 2013

A number of people in issue #12 have asked for the ability to customise the messages, such as adding build parameters etc. By using Mustache templates for build messages this can now be done.

This is quite a large change, and probably not as heavily tested as it should be. It probably needs some unit tests too. I am also not a Java developer, but "know" the language, so I might not have done everything optimally.

The README has been updates with information as to how the templates work, and the defaults should keep the messages the same as they were pre-Mustache.

This doesn't actually solve the original issue as it doesn't add the git branch name, and if the branch name isn't available through the accessible instance variables then it won't, I haven't checked.

It's probably a good start though!

marcins added some commits Feb 8, 2013
@marcins marcins Add IDEA files to .gitignore d19eb9b
@marcins marcins Update Jenkins version and add Mustache in POM
Updated to use Jenkins 1.500 and added the Java implementation
of Mustache from https://github.com/spullara/mustache.java
56e09bf
@marcins marcins Use Mustache templates to generate notifications
The plugin will now use Mustache template from configuration when
creating HipChat notifications. This allows you to use any values
available in the build / project configuration, including your own
custom parameters.
910a926
@marcins marcins Update version to 0.1.3 7eafa59
@marcins marcins Add link to completed message 2ad438f
@marcins marcins Update plugin version past what's on the plugin repo 313d5e8
@marcins marcins Update README with info about using Mustache 2e33605
@marcins marcins Fix empty message issues
When there is no config set there was an empty message being sent,
instead make sure the defaults are used.
333d376
@marcins marcins Update README with custom var info 3f0f322
@marcins marcins Remove MessageBuilder
MessageBuilder is no longer required since we now generate messages
using Mustache.
80ffae3
@marcins marcins Make sure "Cause" is available to Mustache
And some minor fixes to ensure there's no doubling up of "Started"
text.
e1bb907
@marcins marcins Fix notifier so that is uses job properties
Previous code was incorrectly getting message templates from
the global notifier instead of using the JobProperties object
77c2fe8
@marcins marcins Simplify the "started" template
Use a single "trigger" string that's the same as the non-Mustache
version's string instead of separate cause & changes strings which
could cause duplications.
f1ad64e
@marcins marcins Clean up config panels and add help files ed71e1f
@marcins marcins Readme update be9091c
@marcins marcins Fix README indentation d873c95
@marcins
marcins commented Feb 9, 2013

If anyone wants to try the plugin with Mustache notification templates I have a snapshot HPI up in Dropbox:

https://dl.dropbox.com/u/5336861/hipchat.hpi

YMMV, disclaimer, etc, etc :-)

Also check the updated README

marcins added some commits Feb 9, 2013
@marcins marcins Merge branch 'feature-mustache-messages' of https://github.com/marcin…
…s/jenkins-hipchat-plugin into feature-mustache-messages
7c013a6
@marcins marcins Update default completion msg to allow html
If the build fails then the completion message may
contain HTML. This means the default message needs to
triple 'stache "status" so that the correct output is generated.
16f705c
@rdohms
rdohms commented Mar 5, 2013

Any progress on accepting this? I would really like to add the description to my messages since i have "per PR" builds the number just becomes obsolete.

@marcins
marcins commented Mar 6, 2013

@rdohms you can always run my snapshot HPI instead of the "official" one if you want the functionality now? Actually I should build a new one with the last 2 fixes in there, don't have time right now though.

@Mange
Mange commented Mar 28, 2013

I am also interested in this.

@phil-schneider

Please use StringUtils.isBlank for checking if a string is .
I would also change the logger from warning to info.
Warning means: do something or it will might lead to a bug.

Owner

Thanks for the feedback - the warnings aren't there in the latest commit, but I've changed the way I check for blank strings.

@phil-schneider

Not really a nice solution.
The idea of the MessageBuilder was to have one StringBuffer to build to whole string.
You will create now one Stringbuffer and add a String which was the result of a 3part StringBuilder.
I would stick with the single MessageBuilder(Stringbuffer) which consumes less memory then your way

Owner

Thanks for the feedback - I've rewritten getOpenLink to just use a single StringBuilder.

ahh sorry, now I get it.
You create the message with params. Then you create a map (messageParams) with the key/value, which will be replaced with the mustache template engine.

@phil-schneider
Contributor

Nice work. Please add to master branch

marcins added some commits Mar 31, 2013
@marcins marcins Use StringBuilder properly in getOpenLink
Inefficient use of concatenated strings with String builder when creating the link to the build job.
91c5489
@marcins marcins Use StringUtils to check for blank strings
Style fix to check for blank strings based on feedback.
81d3b1f
@marcins
marcins commented Mar 31, 2013

For all those that are interested, I've posted the latest snapshot HPI with these changes (based on at:

https://dl.dropbox.com/u/5336861/hipchat.hpi

I've been using this in production since I wrote it 2 months ago and haven't experienced any issues, although we do only have a small installation.

@phil-schneider
Contributor

I have another questions. Does your template replaces any parameter that are set by the build. Default parameter like $BUILD_ID and also custom parameter from a parameteriezed build?

@marcins
marcins commented Mar 31, 2013

@phil-schneider - have a look at the README in my feature branch, it gives examples of how to access stuff about the build from the mustache templates:

@iamamused

I installed this fork from the compiled https://dl.dropbox.com/u/5336861/hipchat.hpi link but for some reason I can't seem to customize the template. If I change either the global or project completion template I get an empty message (I just tried adding the word test into the template).

Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.HipChatNotifier$HipChatJobProperty prebuild
INFO: Invoking Started...
Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.ActiveNotifier getChanges
INFO: Empty change...
Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.StandardHipChatService publish
INFO: Posting: Jenkins to 172833: Feature Test - #21: Started by user anonymous (<a href='/job/Feature%20Test/21/'>Open</a>) green
Apr 8, 2013 12:59:45 PM hudson.model.Run execute
INFO: Feature Test #21 main build action completed: SUCCESS
Apr 8, 2013 1:00:00 PM jenkins.plugins.hipchat.StandardHipChatService publish
INFO: Posting: Jenkins to 172833:  green

Notice the last line is missing the message.

@marcins
marcins commented Apr 8, 2013

Interesting.. I'll look into it.

On Tue, Apr 9, 2013 at 3:14 AM, Jeffrey Sambells
notifications@github.comwrote:

I installed this fork from the compiled
https://dl.dropbox.com/u/5336861/hipchat.hpi link but for some reason I
can't seem to customize the template. If I change either the global or
project completion template I get an empty message (I just tried adding the
work test into the template.

Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.HipChatNotifier$HipChatJobProperty prebuild
INFO: Invoking Started...
Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.ActiveNotifier getChanges
INFO: Empty change...
Apr 8, 2013 12:59:44 PM jenkins.plugins.hipchat.StandardHipChatService publish
INFO: Posting: Jenkins to 172833: Feature Test - #21: Started by user anonymous (Open) green
Apr 8, 2013 12:59:45 PM hudson.model.Run execute
INFO: Feature Test #21 main build action completed: SUCCESS
Apr 8, 2013 1:00:00 PM jenkins.plugins.hipchat.StandardHipChatService publish
INFO: Posting: Jenkins to 172833: green

Notice the last line is messing the message.


Reply to this email directly or view it on GitHubhttps://github.com/jlewallen/jenkins-hipchat-plugin/pull/32#issuecomment-16064297
.

M.

@iamamused

Actually it only seem sto be blank if I modify the project specific template. If I modify the global one it seems to ignore my settings and uses the default one.

@iamamused

Also noticed that the global Build Server Url isn't applied in the Open link (everything lis linking as if it's a local file. It's like it's not reading my configuration. I'm running the latest version of Jenkins.

@marcins
marcins commented Apr 9, 2013

From all those reports it looks like there's just an issue with applying
the global config then? Although I'm pretty sure my production install is
only using global config and it is working.

Will try and reproduce when I have some time (maybe add some unit tests!)

On Tue, Apr 9, 2013 at 11:13 AM, Jeffrey Sambells
notifications@github.comwrote:

Also noticed that the global Build Server Url isn't applied in the Open
link (everything lis linking as if it's a local file. It's like it's not
reading my configuration. I'm running the latest version of Jenkins.


Reply to this email directly or view it on GitHubhttps://github.com/jlewallen/jenkins-hipchat-plugin/pull/32#issuecomment-16088805
.

M.

@iamamused

Ya, possibly. Playing with it some more it seems editing the global config doesn't actually update the configs in jobs/ until after I delete and then re-added the hipchat phase. That fixed the Build Server URL but the custom messages still didn't work.

@marcins
marcins commented Apr 9, 2013

I think this was an issue I ran into with the plugin before I made my
changes - the global config is only loaded once when the plugin is
initialised (so either when you first add it, or when the server starts
up), it's not reloaded when it's changed. Job config is reloaded everytime.

On Tue, Apr 9, 2013 at 11:45 AM, Jeffrey Sambells
notifications@github.comwrote:

Ya, possibly. Playing with it some more it seems editing the global config
doesn't actually update the configs in jobs/ until after I delete and then
re-added the hipchat phase. That fixed the Build Server URL but the custom
messages still didn't work.


Reply to this email directly or view it on GitHubhttps://github.com/jlewallen/jenkins-hipchat-plugin/pull/32#issuecomment-16089697
.

M.

@iamamused

ok, still can't figure out why the templates don't work though. The global one is ignored and the local changes just cause it to be blank. Here's my test job config if you can shed any insight:

</project>SpeakFeels-Mac-mini:Feature Test jenkins$ cat config.xml 
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description>This is just a test project to play with all the settings in Jenkins. Please ignore any output.</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty plugin="hipchat@0.1.5-SNAPSHOT">
      <room></room>
      <startNotification>true</startNotification>
      <notifySuccess>true</notifySuccess>
      <notifyAborted>true</notifyAborted>
      <notifyNotBuilt>true</notifyNotBuilt>
      <notifyUnstable>true</notifyUnstable>
      <notifyFailure>true</notifyFailure>
      <messageTemplateStarted>This is a test template for start</messageTemplateStarted>
      <messageTemplateCompleted>This is a test template for complete</messageTemplateCompleted>
      <messageTemplateSuffix></messageTemplateSuffix>
    </jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers class="vector"/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>env;</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers>
    <com.zanox.hudson.plugins.FTPPublisher plugin="ftppublisher@1.2">
      <siteName>example.com</siteName>
      <entries>
        <com.zanox.hudson.plugins.Entry>
          <filePath>$REMOTE_DIR/build</filePath>
          <sourceFile>upload/*/**</sourceFile>
        </com.zanox.hudson.plugins.Entry>
      </entries>
      <useTimestamps>false</useTimestamps>
      <flatten>true</flatten>
      <skip>false</skip>
    </com.zanox.hudson.plugins.FTPPublisher>
    <jenkins.plugins.hipchat.HipChatNotifier plugin="hipchat@0.1.5-SNAPSHOT">
      <authToken>...removed...</authToken>
      <buildServerUrl>http://example.com/</buildServerUrl>
      <room>172833</room>
      <sendAs>Jenkins</sendAs>
      <messageTemplateStarted>{{build.project.displayName}} - {{build.displayName}}: {{trigger}} {{{link}}} test</messageTemplateStarted>
      <messageTemplateCompleted>{{build.project.displayName}} - {{build.displayName}}: {{status}} after {{build.durationString}} test</messageTemplateCompleted>
      <messageTemplateSuffix></messageTemplateSuffix>
    </jenkins.plugins.hipchat.HipChatNotifier>
  </publishers>
  <buildWrappers>
    <EnvInjectBuildWrapper plugin="envinject@1.83">
      <info>
        <propertiesContent>REMOTE_DIR=feature-test</propertiesContent>
        <loadFilesFromMaster>false</loadFilesFromMaster>
      </info>
    </EnvInjectBuildWrapper>
  </buildWrappers>
</project>
@drubin
drubin commented Jun 16, 2013

@marcins I just tested this. It seems that it works perfectly except for setting a custom template per project. When doing that (even the default one, or just plain text) it seems to not send the notification.

Using the custom suffix seems to work.

Also I would suggest adding some sort of logging so we can see the output in the build script (currently there is NO way to figure out why the messages aren't being sent if the template is wrong)

@kellybyrd
Collaborator

There seem to be three (this one, #38, #42) pull requests all about customizing the sent message. This one seems most flexible and could probably cover the use cases of the other two, but I worry about it being able to cover the dead simple: "I just want to change the text string a little bit" case without someone having to read the Mustache docs. How do the other maintainers feel?

@leifcr
leifcr commented May 22, 2014

I think this is the best solution, as the readme is quite clear on how to do it. It's the most flexible solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment