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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to access build variables from a bamboo Notification Edited

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

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
Community showcase
Published in Bamboo

Bamboo Data Center Apps Program coming soon

G’day Bamboo customers, As we approach GA for Bamboo Data Center , we would like to inform you that the Data Center Apps Program for Bamboo starts this quarter. How long does it take? We are g...

77 views 0 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you