Skip to main content
Book your demo
Example scripts
To homepage
Jira
Data centre icon
Data Center

Copy a Table From a Confluence Page and Add it to a Jira Comment

Created 2 year(s) ago, Updated 26 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.20 - 9.4)
compatibility bullet
ScriptRunner For Jira (7.8.0)
Language |
groovy
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

//Set the Issue Key where you want to pass the table to
final def issueKey = '<ISSUE_KEY>'

//Set the ID of the Confluence page
final def confluencePageID = '<CONFLUENCE_PAGE_ID>'

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

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

def issue = issueManager.getIssueObject(issueKey)
def authenticatedRequestFactory = primaryConfluenceLink.createImpersonatingAuthenticatedRequestFactory()

def writer = new StringBuilder()
def result = 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}\n")
        } else {
            throw new Exception(response.responseBodyAsString)
        }
    }
}

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

def output = tableToJson(writer.toString())

output.each { Map.Entry entry ->
    if (entry.key == 'Head') {
        def head = output[entry.key.toString()] as List
        result.append("|||${head[0]}|||${head[1]}|||${head[2]}|||\n")
    } else {
        def body = output[entry.key.toString()] as List
        result.append("|${body[0]}|${body[1]}|${body[2]}|\n")
    }
}

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

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

    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.get(1).ownText()
                def group = ths.last().ownText()
                jsonMap.put('Head', [author, comment, group])
            }
            if (tds) {
                def author = tds.first().ownText()
                def comment = tds.get(1).ownText()
                def group = tds.last().ownText()
                jsonMap.put("Body${++count}".toString(), [author, comment, group])
            }
        }
    }
    jsonMap
}
Having an issue with this script?
Report it here