在 Bamboo 中发送自定义通知
Sending custom notifications in 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)。
下面是完整方法的代码。
@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 ();
如此处所述Whosebug question,但这不起作用。
我在这里看到了一个构建自定义通知的示例 - enter link description here,但是代码很旧并且没有使用任何附件。
知道如何实现吗?
谢谢
终于,我找到了让它工作的方法。
我想要获取的那些变量,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");
...
}
}
}
并且 plan 和 resultSummary 在 MyNotificationEventListener 中设置,当我创建我的自定义通知 - 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);
...
}
}
}
我正在尝试开发一个新的 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)。
下面是完整方法的代码。
@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 ();
如此处所述Whosebug question,但这不起作用。
我在这里看到了一个构建自定义通知的示例 - enter link description here,但是代码很旧并且没有使用任何附件。
知道如何实现吗? 谢谢
终于,我找到了让它工作的方法。
我想要获取的那些变量,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");
...
}
}
}
并且 plan 和 resultSummary 在 MyNotificationEventListener 中设置,当我创建我的自定义通知 - 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);
...
}
}
}