在 Youtrack Helpdesk 中,我们编写了一个用于通过电子邮件发送评论的小型工作流程。一切正常,只是图像不正确:
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var notification = require('@jetbrains/youtrack-scripting-api/notifications');
const resolveLinks = function (text) {
var baseUrl = 'https://test.com/images/';
var updatedText = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, function(match, altText, url) {
// Check if the URL is already absolute
if (url.startsWith('http://') || url.startsWith('https://')) {
return match;
}
// Replace with absolute URL
return `![${altText}](${baseUrl}${url})`;
});
if (text !== updatedText) {
text = updatedText;
}
return text;
};
exports.rule = entities.Issue.onChange({
title: 'Send Email Notification',
guard: function(ctx) {
return true;
},
action: function(ctx) {
var issue = ctx.issue;
if (issue.comments.added.isNotEmpty()) {
var authorName = issue.comments.added.first().author.fullName;
var emailReporter = issue.reporter.email;
var emailWorker = issue.fields.Bearbeiter.email;
var emailObserver = "";
issue.fields.Beobachten.forEach(function(user) {
emailObserver = emailObserver + user.email + ",";
});
if(emailObserver.endsWith(",")) {
emailObserver = emailObserver.substring(0,emailObserver.length-1);
}
var message = {
fromName: authorName,
to: [emailReporter],
cc: [emailWorker],
bcc: [emailObserver],
subject: 'New comment in ' + issue.id,
headers: {
'X-Custom-Header': 'Important value'
},
body: [
'<div style="font-family: sans-serif">',
' <div style="padding: 10px 10px; font-size: 13px; border-bottom: 1px solid #D4D5D6;">',
' New comment was added in issue <a href=https://service-desk.dhge.de/tickets/'+issue.id+'>'+issue.id+'</a> by ' + authorName + ': ',
' <br> <br>' + resolveLinks (issue.comments.last().text) ,
' </div>',
'<\div>'
].join('\n')
};
console.log("Send mail from "+authorName+" to: "+emailReporter);
console.log("Send mail from "+authorName+" to: "+emailWorker);
console.log("Send mail from "+authorName+" to: "+emailObserver);
console.log("Send mail text "+issue.comments.last().text);
notification.sendEmail(message, issue);
}
}
});
如何用正确的图像替换图像?
我希望看到评论工作流程发送的电子邮件中的附件。但电子邮件中只有标记语法,而不是正确的图像或附件。行
issue.comments.last().text
应该得到解析(wiki 标记为 HTML - 带有内联图像等)。
也许这段代码有帮助?
function createGeneralMessage(issue) {
const firstAddedComment = issue.comments.added.first();
let text = firstAddedComment.text;
const isMarkdown = firstAddedComment.isUsingMarkdown;
issue.attachments.added.forEach(function (attachment) {
if (isMarkdown) {
text = text + '\n[' + attachment.name + '](' + attachment.fileUrl + ')';
} else {
text = text + '\n[file:' + attachment.name + ']';
}
});
return issue.wikify(text, isMarkdown).trim();
}