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|
```
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:-
Below is a screenshot of the table on the confluence page:-
And below is a screenshot of the output that is produced in Jira:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.