Subversion comment to email subject

89 views
Skip to first unread message

Philippe Payant

unread,
Oct 13, 2015, 10:55:36 AM10/13/15
to Jenkins Users
Given that a Subversion comment  contains a Bugzilla bug number, for example:

Fix Hoopla bug in the "easy" mode.  Bug 9413

I would like the "build succesful" email to be something like:

"MyProduct: build successful - bug 9413"

For bonus points, I would actually like the subject to contain the Bugzilla summary of bug 9413:

"MyProduct: build successful - bug 'Hoopla display is wrong in easy mode' "


-- Philippe

Slide

unread,
Oct 13, 2015, 11:07:55 AM10/13/15
to Jenkins Users
Are you using email-ext or Mailer? If using email-ext, you could use a SCRIPT token and have a groovy script that grabs anything you want from the build.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6aad9b4a-de29-4d0a-9879-6136dd055302%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Philippe Payant

unread,
Oct 14, 2015, 3:31:19 PM10/14/15
to Jenkins Users
Your suggestion launched me on a track that worked.  But I got it all wrong because I did not understand all I could do with a groovy script.

I upgraded my build script (Python based and commited in the sources) to use SVN_URL & SVN_REVISION to pull the commit log from Subversion.  The build script then parses the commit log to find the bug number.  The bug number is printed to stdout and it ends up in the Jenkins build log.

Then I wrote a pre-send groovy script for email-ext to grep the build log for the build number.  Once I have the build number, the email subject is updated.  Here is my script:

--snip--
import java.util.regex.Pattern;
import java.util.regex.Matcher;
try {
  log = build.getLog()
  def pattern = Pattern.compile(/7928DEC7-17B5-40a1-B094-282AE167178F\s+(\S+)\s+(.*)/)
  def matcher = pattern.matcher(log)
  def found = matcher.find()
  if ( found ) {
    def job = build.getParent().name
    def number = matcher.group(1)
    def summary = matcher.group(2)
    def subject = summary
    if ( number != 'N/A' ) {
      subject = "${summary} (bug ${number})"
    }
    msg.setSubject("[Build ${job}] ${subject}")
  }
} catch(e) {
  msg.setContent(e.toString(),'text/plain')
}
--end snip--

For people who are new to this, some pseudo-code and explanations:

The 'build' and 'msg' objects are prepopulated when the script is launched.  We get the log from 'build'.  Then we use a java.util.regex.Matcher to locate the build number in the log text.  Finally, the 'msg' object (a plain Java MimeMessage object) is modified with a new subject.

This is crazy.  Now my build script (commited along the sources) is performing a task it should not care about: parsing a SVN commit.  The better solution would be to have the pre-send groovy script do all that.  It has access to SVN_URL and SVN_REVISION.  My preferred language for my build system is Python, so I would probably shell out to an external Python script to parse the commit and contact Bugzilla for the summary.

I got some help from these:

Slide

unread,
Oct 14, 2015, 3:56:33 PM10/14/15
to Jenkins Users
I mean for you to change the Subject field in the email-ext configuration to have something like ${SCRIPT, script='get-commit-info.groovy'}, then you would have a groovy script that grabbed the info and returned it as a string. Email-ext would then take care of putting the correct text in. You don't need to do it via a pre-send script.

Philippe Payant

unread,
Oct 14, 2015, 4:08:46 PM10/14/15
to Jenkins Users
Yes, I see this is another alternative, and it is even simpler than a pre-send script.  I would like to do that.

Can you clarify some details for me?

1. Seems obvious from the doc that get-commit-info.groovy can/should be placed in JENKINS_HOME/email-templates right?
2. What are the 'bootstrap' variables (the ones like 'build' and 'msg' for pre-send scripts and 'manager' for groovy post-build script) ?
3. How is the result of that script to be returned?  The help for this says "only the last value in the script will be used in the expansion".  What does that mean?

I will google for an example.

Slide

unread,
Oct 15, 2015, 5:45:28 AM10/15/15
to jenkins...@googlegroups.com
See comments inline


On Wed, Oct 14, 2015 at 1:08 PM Philippe Payant <philth...@gmail.com> wrote:
Yes, I see this is another alternative, and it is even simpler than a pre-send script.  I would like to do that.

Can you clarify some details for me?

1. Seems obvious from the doc that get-commit-info.groovy can/should be placed in JENKINS_HOME/email-templates right?

Yes, this is correct. You can also install the config file provider plugin and put the script in there, then you would do 'managed:name of script' instead of the filename.

 
2. What are the 'bootstrap' variables (the ones like 'build' and 'msg' for pre-send scripts and 'manager' for groovy post-build script) ?

 
3. How is the result of that script to be returned?  The help for this says "only the last value in the script will be used in the expansion".  What does that mean?

This means that the result of the last line in the script is what is used to replace the token. ${SCRIPT, script='foo.groovy'} would be replaced by the result of the last line of foo.groovy.
 

Philippe Payant

unread,
Oct 15, 2015, 11:02:58 AM10/15/15
to Jenkins Users
I am so close now!  

Here is my problem.  My script (now a file in email-templates) starts with:

def env = build.getEnvironment(listener)

I placed this in the Default Subject of my project:

${SCRIPT, script="subject.groovy"}

The email I get is a Groovy error message:

Error in script or template: groovy.lang.MissingPropertyException: No such property: listener for class: Script1

Weird, the URL for the bootstrap variables you included clearly shows that 'listener' should be there. What's up?

Slide

unread,
Oct 17, 2015, 12:05:54 AM10/17/15
to Jenkins Users
It has a logger, not a listener: binding.put("logger", listener.getLogger());


If you need to get environment variables you can do it like this ENV(var: 'NAME_OF_VAR'). Any token-macro token can be used in this way.

Reply all
Reply to author
Forward
0 new messages