在Bamboo中发送自定义通知

时间:2018-10-16 07:46:34

标签: bamboo

我正在尝试开发新的Bamboo通知插件。

根据official documentation-例如,如果要创建一个自定义通知,在其中要添加日志文件作为电子邮件附件,我需要实现以下接口ExtendedNotification。

public class MyNotification extends AbstractNotification implements ExtendedNotification {
    ...
}

此接口 ExtendedNotification 具有以下需要实现的方法:

@NotNull
@Override
public Email updateEmail (@NotNull Email email) {
   return null;
}

问题是在构建 Email 本身时-用这种方法从上下文获取构建版本不起作用(尝试附加正确的日志文件)。
例如,如果我有以下日志文​​件-\ BAMBOO \ tools \ logs \ $ {bamboo.planKey} \ health _ $ {bamboo.buildNumber} .txt)。 下面是full方法的代码。

@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;
}

我的问题是在Java的Bamboo中获取关键计划变量。在上面的代码中,我尝试使用以下方法获取它:

taskContext.getBuildContext ().getVariableContext ();

如此处StackOverflow question所述,但这不起作用。

我看到了一个在此处构建自定义通知的示例-enter link description here,但是代码很旧,并且不使用任何附件。

关于如何实现此目标的任何想法? 谢谢

1 个答案:

答案 0 :(得分:0)

最后,我找到了使其工作的方法。

我想要获取的那些变量 planKey buildNumber 来自以下内容:

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");
            ...
        }
    }
}

当我创建自定义通知- MyNotification 时,在 MyNotificationEventListener 中设置了 plan resultSummary

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);
            ...
        }
    }
}