Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to access build variables from a bamboo Notification

User4 Test
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 15, 2018

Hi, 

I'm trying to develop a new Bamboo Notification Plugin.

According to the official documentation - if I want to create my special Notification, in which I want to add a log file as attachment to my email, for example, I need to implement the following interface ExtendedNotification.  

This interface has a method that need to be implemented. The problem is getting the build version from the context in this method to try to attach the correct log file (for example - \BAMBOO\tools\logs\${bamboo.planKey}\health_${bamboo.buildNumber}.txt). 

My code is below : 

@NotNull
@Override
public Email updateEmail (@NotNull Email email) {
try {

//VariableContext variables = taskContext.getBuildContext ().getVariableContext ();

// E:\Atlassian\BAMBOO\tools\logs\${bamboo.planKey}\health_${bamboo.buildNumber}.txt
File logFile = new File ("E:\\Atlassian\\BAMBOO\\tools\\logs\\" + taskContext.getBuildContext().getPlanKey () + "\\health_" + taskContext.getBuildContext().getBuildNumber() + ".txt");

//Message message = new MimeMessage(session);
Multipart multipart = new MimeMultipart ();

// creates body part for the message
MimeBodyPart messageBodyPart = new MimeBodyPart ();
messageBodyPart.setContent (getHtmlEmailContent (), "text/html");

// creates body part for the attachment
MimeBodyPart attachPart = new MimeBodyPart ();

// code to add attachment...will be revealed later
attachPart.attachFile (logFile);

// adds parts to the multipart
multipart.addBodyPart (messageBodyPart);
multipart.addBodyPart (attachPart);

// sets the multipart as message's content
email.setMultipart (multipart);
} catch (Exception e) {
log.error ("There was a problem composing the email", e);
return null;
}
return email;
}

I tried to follow the guidelines provided here : https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-access-build-variables-from-a-bamboo-task/qaq-p/572570,

taskContext.getBuildContext().getBuildNumber();

but there is no way of getting the taskContext. 

Any ideas ?

Thanks !

1 answer

0 votes
User4 Test
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 25, 2018

As I posted this question in stackoverflow, this is the answer : 

Finally, I found the way to make it work.

Those variable I want to get, the planKey and the buildNumber are coming from the following :

public class MyNotification extends AbstractCompletedNotification implements ExtendedNotification {
    private ResultsSummary resultsSummary;
    private ImmutablePlan plan;
    ...

    @NotNull
    @Override
    public Email updateEmail (@NotNull Email email) {
        try {

            File logFile = new File ("E:\\Atlassian\\BAMBOO\\tools\\logs\\" + 
                plan.getPlanKey () + 
                "\\health_" + 
                resultsSummary.getBuildNumber() + 
                ".txt");
            ...
        }
    }
}

And the plan and the resultSummary are set in the MyNotificationEventListener when I create my custom notification - MyNotification :

public class MyNotificationEventListener {
    ...
    @EventListener
    @HibernateEventListenerAspect
    public void handleEvent (@NotNull Object event) {
        if (event instanceof ChainCompletedEvent) { // for example
            ... 
            MyNotification myNotification = (MyNotification) BambooNotificationUtils.createNotification (MyNotification.class);
            ...
            myNotification.setResultsSummary (this.resultsSummaryManager.getResultsSummary (chainCompletedEvent.getPlanResultKey ()));
            ImmutablePlan immutablePlan = this.planManager.getPlanByKey (chainCompletedEvent.getPlanKey ());
            myNotification.setPlan (immutablePlan);
            ...
        }
    }
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events