Hey all,
I need to create a scripted field [free text] that will copy the last comment made to a ticket whenever comments are made.
Any ideas?
Thanks in advanced :)
Added.body and got what I needed.
import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (comments) {
comments.last().body
}
thx for the code it is working but now I am trying to reverse it so the comments section can copy the custom field... sorry for the question but i am a newbie thx
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Kathryn Allison, @Henning Tietgens (or anyone ) – do you know if there's a way to show this new scripted field in wiki markup? Some of our users have been using wiki markup within their comments and this new field is awesome as it allows us not have open each ticket to view the comments. However it's displaying the raw text, not sure if that can be configurable/scripted or not as my searches aren't coming up with much. TIA!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just managed to get this working based on frankensteining various other scripts here and there. Since this discussion is still on the first page of google results for searches on this topic, I'll add a comment here. Have a look at the current state of my scripted field:
import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def commentManager = ComponentAccessor.getCommentManager()
def rendererManager = ComponentAccessor.getRendererManager()
def comments = commentManager.getCommentsForUser(issue, user)
if (comments) {
rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)
}
Changing the returned value to one that uses the renderer manager gave me formatted results. It works just fine if you set the field type to "multi-line text", but you can set it to "HTML" if you want it to be more literally correct. The result is the same for both in JIRA 7.4.
Note, if you copy and use my version of the script, it also contains another modification to restrict which comment is returned based on what each user is permitted to see. Kind of defeats the point of comment visibility levels if this field circumvents them, after all. XD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Joshua DeClerck - Thanks for the code - it works GREAT! How would I add the commenter's name and date to the script? Thanks again!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Holan Shetlar - Oh man, I am not a developer and as a result, this is not guaranteed to be good quality code, but ok.
First, there are two sets of comment author/date info you can get: (1) creator/created, and (2) updater/updated. If a comment hasn't been modified, (2) will return the same values as (1). I'm opting to use (2) because of the extra versatility. I'm also opting to use the function to retrieve the author's "full name", rather than username.
It's possible to include logic to determine whether the comment has been updated or not, and print the name/date info differently depending on that, but I won't be doing that this time.
Using the same as my earlier example, I changed the if block this way:
if (comments) {
def commentAuthor = comments.last().getUpdateAuthorFullName() // alt = getAuthorFullName()
def commentDate = comments.last().getUpdated() // alt = getCreated()
def commentBody = rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)
return commentBody + "<p><span style=\"font-size:smaller;color:#a9a9a9;font-style:italic\">" + commentDate.format('YYYY-MM-dd') + " | " + commentAuthor + "</span></p>"
}
[Edited for a slightly nicer looking, less English-y outcome, but date only]
[More editing because I used generated HTML and even as a noob I could see it was ugly, oops - maybe a little better now?]
I dumped the original output into a commentBody variable, and made two more variables for the author and date. You can replace getUpdateAuthorFullName() and getUpdated() with other functions as documented in Atlassian's developer docs here.
The last line with the return command is for your template. It works with regular HTML, so add formatting to the text however you like. My example just inserts a simple line break after the body and concats the date and author.
I'm pretty sure my example is riddled with bad habits. I'm not sure if defining variables like that inside an if block is good code, or if using return like that is good code. I'm positive my template is bad code (not internationalized). And the date is just an ugly datetime string with no formatting. But I think this can get you started. I hope so, at least!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi All,
I am getting below error while I am trying to capture last comments into different custom field.
can someone please help me on fixing below issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, it seems like you're writing a listener. The variable "issue" is not directly available, you have to use "event.issue" instead.
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Joshua DeClerck - - this is awesome!! I cant figure out how to have everything print on one line (drop the line break). can you help?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Take a look at https://www.tutorialspoint.com/groovy/groovy_replaceall.htm and use this to replace all line breaks within the comment body with an empty string.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Joey Klein - Ah, yeah unfortunately Henning's suggestion might not work in this particular case. It comes down to the return body here:
return commentBody + "<p><span style=\"font-size:smaller;color:#a9a9a9;font-style:italic\">" + commentDate.format('YYYY-MM-dd') + " | " + commentAuthor + "</span></p>"
The key parts are commentBody, commentDate, and commentAuthor. What I shared made some subjective style choices, like the YYYY-MM-dd format (change this to your heart's content) and the layout. The fact that the date and author are both inside a paragraph tag (<p>...</p>) is what's putting them on a separate line, and why simply removing line break characters won't be enough.
You should be able to replace the "<p>..." part between commentBody and commentDate to whatever separator you want to use. Likewise for the " | " between the date and author. Just be sure to remove the closing /span and/or /p tags if you remove their opening counterparts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, my "line break" was more a general term, which could be translated to "whatever your line break is". I thought @Joey Klein was talking about the comment body itself, and I'm not sure what the line breaks within the formatted comment body are. Could be <p> or <br> or similar html tags...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi All,
first of all thanks for this nice scripted field!
Do you think that there is a possibility to script a field that will copy all comments?
Kind regards,
Nicole
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, you can use commentManager.getComments(issue) to get all comments of an issue.
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Henning,
thanks for the fast answer!
Now it works.
Kind regards,
Nicole
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Guys,
With the codes you have written, were any third party plug-ins used at all?
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use getComments() from CommentManager to get a list of all comments and then identify the last comment.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried using those 3 exact pieces, but couldn't get the script just right.
I'm not really groovy savvy, so my attempt may have just been a poor one.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.