Creating calendar entry in Jenkins using pre email script
Asked Answered
M

1

6

I am currently using Jenkins version 1.617 with the latest Editable Email notification plugin.

What we are trying to do is take the changes from the build and put them into a meeting notice (Calendar Entry).

We are currently stuck using lotus notes 8.5.3FP6.

Here are our variables in the plugin:

Project Recipient List = $SERVER_GROUP
Content Type = HTML(text/html)
Default Subject = $DEFAULT_SUBJECT
Default Content = 

       $DEFAULT_CONTENT
    
       Repo: myRepo
       Install Location: S:\Build\VAT - Visual Authoring Tool\SERVICELOGIQ  \GM\VAT_${ENV, var="miniVersion"}\VAT_${ENV,  var="releaseversion"}_SERVICELOGIQ [${ENV, var="BUILD_NUMBER"}]
    
       Change Log: 
       ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, showPaths=true}

We are trying to use the following presend script:

import javax.mail.Message
import javax.mail.Message.RecipientType
import javax.mail.Address
import javax.mail.Multipart
import javax.mail.BodyPart
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.Session
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
import javax.mail.util.ByteArrayDataSource
import java.util.Date
import java.util.Calendar
import java.util.TimeZone
import java.text.DateFormat
import java.text.SimpleDateFormat
import javax.activation.DataHandler
msg.addHeaderLine("method=REQUEST");
msg.addHeaderLine("charset=UTF-8");
msg.addHeaderLine("component=VEVENT");
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
final Date start = cal.getTime();
cal.add(Calendar.HOUR, 1);
final Date end = cal.getTime();
SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd'T'hhmmssZ");
String fmtStartDate = dateFmt.format(start);
String fmtEndDate  = dateFmt.format(end);
 
String subject = msg.getSubject()
Multipart multi = (Multipart)msg.getContent()
BodyPart part = multi.getBodyPart(0)
String body = part.getContent().toString()
String from = "[email protected]"
Address[] toAddresses = msg.getAllRecipients()
String to = toAddresses.each{ it.toString() }.join(",")
 
String content =
            "BEGIN:VCALENDAR\n"+
            "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n"+ 
            "VERSION:2.0\n" +
            "METHOD:REQUEST\n" +
            "BEGIN:VEVENT\n" +
            "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:" + to + "\n" +
            "ORGANIZER:MAILTO:" + from + "\n" +
            "DTSTART:" + fmtStartDate + "\n" +
            "DTEND:" + fmtEndDate + "\n" +
            "LOCATION:Jenkins Build\n" +
            "TRANSP:OPAQUE\n" +
            "SEQUENCE:0\n" +
            "UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n" +
            " 000004377FE5C37984842BF9440448399EB02\n" +
            "DTSTAMP:20051206T120102Z\n" +
            "CATEGORIES:Meeting\n" +
            "DESCRIPTION:" + body + "\n\n" +
            "SUMMARY:" + subject + "\n" +
            "PRIORITY:5\n" +
            "CLASS:PUBLIC\n" +
            "BEGIN:VALARM\n" +
            "TRIGGER:PT1440M\n" +
            "ACTION:DISPLAY\n" +
            "DESCRIPTION:Reminder\n" +
            "END:VALARM\n" +
            "END:VEVENT\n" +
            "END:VCALENDAR";
 
// Create the message part 
MimeBodyPart messageBodyPart = new MimeBodyPart();
// Fill the message 
messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
messageBodyPart.setHeader("Content-ID","calendar_message");
messageBodyPart.setDataHandler(new DataHandler(
                new ByteArrayDataSource(content, "text/calendar")));//very important
 
// Create a Multipart 
MimeMultipart multipart = new MimeMultipart();
// Add part one 
multipart.addBodyPart(messageBodyPart);
// Put parts in message 
msg.setContent(multipart);

This is what we understand from this script, it looks like the script is taking a email that has already been created, parsing the values and changing it into a calendar entry. However, when we run this script, it does create the calendar entry, but only gets the first line of text in the default content section of the email.

Here is an example of the email that is created:

BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
BEGIN:VEVENT
ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:myemail
ORGANIZER:MAILTO:[email protected]
DTSTART:20150716T123802-0400
DTEND:20150716T013802-0400
LOCATION:Jenkins Build
TRANSP:OPAQUE
SEQUENCE:0
UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100
 000004377FE5C37984842BF9440448399EB02
DTSTAMP:20051206T120102Z
CATEGORIES:Meeting
DESCRIPTION:Check console output at http://localhost:8080/job/Test_Project_Sean2/77/ to view the results.

Repo: http://server/hg/hgweb.cgi/ServiceLogiq
Install Location: S:\Build\VAT - Visual Authoring Tool\SERVICELOGIQ\VAT_SERVICELOGIQ [77]


Change Log: 
Changes for Build #77
No changes



SUMMARY:Test_Project_Sean2 Build Successful!
PRIORITY:5
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:PT1440M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR

Our actual meeting notice is displayed like the following:

Check console output at http://servername to view the results. With the email file listed above attached.

Please note that the line that was output is the DEFAULT_CONTENT variable listed above.

We have already tried the following:

def env = System.getenv()
def version = env['CHANGES_SINCE_LAST_SUCCESS']

However, this returned a NULL value. I would appreciate any help we could get to solve our problem.

Maidy answered 16/7, 2015 at 16:0 Comment(0)
B
0

For multiple lines in a description in a calendar file, you need to put in an escaped \n instead of real newlines.

Browband answered 28/10, 2015 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.