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

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

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 

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

 

 

 

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

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

PageCopy_Notfound.png

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?

Confluence version 5.10.8 and scriptrunner version 5.0.8

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.

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.

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

PageCopy_Event.png

def event = event as PageCopyEvent

def originalPage = event.getOrigin()

def destinationPage = event.getDestination()

Some event package has to be imported?

PageCopy_Event1.png

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

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

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.

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

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

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

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

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)

 

Just remove the public static final

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?

Any update on this?

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?

#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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,691 views 25 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you