How to use Scriptrunner for confluence to copy comments from one page to another

Sujatha M June 27, 2017

There is a scenario where i need to copy comments from one page to another when i copy the page. Adaptavist suggested to use scriptrunner plugin for confluence. Can anybody suggest the script to copy the comments. Also i cannot find the event Pagecopy in the scriptrunner event triggers.

1 answer

1 vote
Katy Kelly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 29, 2017

Hi Sujatha,

You can use the following script in Script Console. You just need to change the id of the pages you are referring to.

Here's how you can get the page id. 

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext

def pageManager = ComponentLocator.getComponent(PageManager)
Page initialPage = pageManager.getPage(98311)
Page targetPage = pageManager.getPage(98305)

for (Comment comment : initialPage.getComments()) {
targetPage.addComment(comment)
}
pageManager.saveContentEntity(targetPage, DefaultSaveContext.MINOR_EDIT)

Regards,

Katy

Adaptavist Product Support 

Sujatha M June 29, 2017

Hi Kelly,

This is a more generic requirement and not specific to a page. Every time i copy a page it should copy the comments.

I want to bind this to the pagecopy event actually, but i cannot find the event trigger to copy page event in the scriptrunner plugin, however i can find oher triggers like pagecreate, pagemove pageupdated etc.

Regards

Sujatha

 

 

 

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 6, 2017

Hi Sujatha,

If you use the ScriptEventHandlers you'll find the PageCopyEvent. With this event you can get the destination and origin.

Did you try this?

Regards,

Rafael

Sujatha M July 6, 2017

Yes i have tried this and I could not find the PageCopy event. That is when i reached out.

PageCopy_Notfound.png

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2017

That is very strange since it displays fine on mine as you can see:

Screen Shot 2017-07-07 at 09.49.20.pngWhich version of ScriptRunner and Confluence you have installed?

Sujatha M July 7, 2017

Confluence version 5.10.8 and scriptrunner version 5.0.8

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2017

Sujatha, the PageCopyEvent is only available starting from Confluence 6.x as it can be seen here:

https://docs.atlassian.com/confluence/latest-server/com/atlassian/confluence/event/events/content/page/PageCopyEvent.html

So unfortunately, it will not be available in your Confluence version, you would need to upgrade.

Sujatha M July 7, 2017

Thanks Rafael. We will upgrade and check. 

Also how do we get source and destination pages without giving IDS like below

Page initialPage = pageManager.getPage(98311) Page targetPage = pageManager.getPage(98305)

 I would want to bind the copyevent with the script which will copy the comments which are open( not resolvd) from source to destination. In this case we will not be able to specify the ID of the source and destination page as it is not known.

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2017
Sujatha M July 7, 2017

Is this how it has to be used? It shows error.

PageCopy_Event.png

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2017
def event = event as PageCopyEvent

def originalPage = event.getOrigin()

def destinationPage = event.getDestination()
Sujatha M July 7, 2017

Some event package has to be imported?

PageCopy_Event1.png

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2017

import com.atlassian.confluence.event.events.content.page.PageCopyEvent

Sujatha M July 7, 2017

Now the script is fine. But the comments are not being copied.

Please check the attachment. I have attached the original and destination document alsooriginal page.pngdestination pae.png.

code_working.png

Sujatha M July 11, 2017

Any update on this? Any reason why the comments are not being copied from one page to another when the page is copied even though we have enabled the script.

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2017

Hi Sujatha,

I just tested and Confluence does not trigger the PageCopyEvent for a single page, it will only trigger if you include the child pages. 

There is no way to make this work withou this option set.

Screen Shot 2017-07-12 at 16.07.38.png

Sujatha M July 27, 2017

We tried this option by including the child pages while copying. But what happens is, the comments are copied to the new page. However the same has been removed from the parent page. This is not the expectation. The expectation is to copy and not to move the comments, it should be available in both the parent and the child pages.

Regards

Sujatha

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 27, 2017

What you need is to create a new comment based on the the source one and add than one instead.

Sujatha M July 27, 2017

;;This is the script

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent

def pageManager = ComponentLocator.getComponent(PageManager)

def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()

for (Comment comment : originalPage.getComments()) {
    destinationPage.addComment(comment)  
}
pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 27, 2017
import com.atlassian.confluence.core.SaveContext
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def commentManager = ComponentLocator.getComponent(CommentManager)

def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()

public static final SaveContext NODATECHANGE_CONTEXT = new DefaultSaveContext(true, false, true)

for (Comment comment : originalPage.getComments()) {
final Comment newComment = new Comment()
newComment.bodyAsString = comment.getBodyAsString()
newComment.creationDate = comment.getCreationDate()
newComment.lastModificationDate = comment.getLastModificationDate()
newComment.creator = comment.getCreator()

commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)

destinationPage.addComment(newComment)
}

pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)

 

Like Dan Taube likes this
Sujatha M July 27, 2017

Script_Error.png

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 27, 2017

Just remove the public static final

Sujatha M July 27, 2017

Script_Error1.png

Sujatha M July 27, 2017

After removing the public static final, We are getting the above error. Also by the above solution will both inline and page comments be copied?

Sujatha M July 30, 2017

Any update on this?

Katy Kelly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2017

Hi Sujatha,

We cannot tell what your code is since your screenshot only shows half of it. 

Also, it shows an error in Line 23. Did you correct that?

Sujatha M July 31, 2017

#Below is the script after removing public static line

#error s thrown in the  below line

#commentManager.saveContentEntity(comment, #NODATECHANGE_CONTEXT)

variable - NODATECHANGE_CONTEXT is undeclared

#############################################

import com.atlassian.confluence.core.SaveContext
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def commentManager = ComponentLocator.getComponent(CommentManager)

def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()

for (Comment comment : originalPage.getComments()) {
    final Comment newComment = new Comment()
    newComment.bodyAsString = comment.getBodyAsString()
    newComment.creationDate = comment.getCreationDate()
    newComment.lastModificationDate = comment.getLastModificationDate()
    newComment.creator = comment.getCreator()

    commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)

    destinationPage.addComment(newComment)
}

pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events