Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a content from The Confluence page to Jira comment body

Ivan Shkabara January 8, 2023

Hello!

How i can do convert a body of Confluence page to the Jira comment body?

Similar to JiraRendererPlugin but I need Jira style HTML.

 

e.g.:

Confluence
```<p>text</p>
<table>
<tr><th>Author</th><th>Comment</th></tr>
<tr><td>user1</td><td>this comment should be rendered as well on the Jira side</td></tr>
</table>
```

Should be for Jra
```text
||Author||Comment||
|user1|this comment should be rendered as well on the Jira side|
```

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 16, 2023

Hi @Ivan Shkabara

For your requirement, you will need to use the ScriptRunner console.

Below is a sample working code for your reference:-

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonSlurper
import org.jsoup.Jsoup

static ApplicationLink getPrimaryConfluenceLink() {
final def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
final def conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
conflLink
}

def issueManager = ComponentAccessor.issueManager
def commentManager = ComponentAccessor.commentManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//Set the Issue Key where you want to pass the table to
def issue = issueManager.getIssueObject('MOCK-2')

//Set the ID of the Confluence page
def urlId = '6094856'

def confluenceLink = primaryConfluenceLink
assert confluenceLink

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

def writer = new StringBuilder()

def responseHandler_GET = new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if (response.statusCode == HttpURLConnection.HTTP_OK) {
def output = new JsonSlurper().parseText(response.responseBodyAsString)['body']['view']['value']
writer.append("${output.toString()}\n")

} else {
throw new Exception(response.responseBodyAsString)
}
}
}

authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "/rest/api/content/${urlId}?type=page&expand=body.view,version.number")
.addHeader('Content-Type', 'application/json')
.execute(responseHandler_GET)

def output = tableToJson(writer.toString())

def result = new StringWriter()

output.each { Map.Entry entry ->
if (entry.key == 'Author' && entry.value == 'Comment') {
result.append("|||${entry.key}|||${entry.value}|||\n")
} else {
result.append("|${entry.key}|${entry.value}|\n")
}
}

commentManager.create(issue, loggedInUser, result.toString(), false)

static def tableToJson(String source) {
def doc = Jsoup.parse(source)
def jsonMap = [:] as Map<String, String>

doc.select('table').each { table ->
table.select('tr').each { row ->
def ths = row.select('th')
def tds = row.select('td')

if (ths) {
def author = ths.first().ownText()
def comment = ths.last().ownText()
jsonMap.put(author, comment)
}
if (tds) {
def author = tds.first().text()
def comment = tds.last().text()
jsonMap.put(author, comment)
}
}
}
jsonMap
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the ScriptRunner console configuration:-

config1.png

 

Below is a screenshot of the table on the confluence page:-

confluece_table.png

And below is a screenshot of the output that is produced in Jira:-

test1.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer