Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Comment on Jira Issue for Linked Confluence Pages

Created 1 year ago, Updated 1 month(s) ago
Apps in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.1 - 7.6)
compatibility bullet
ScriptRunner For Confluence (6.21.0)
Language |
groovy
@Grapes([
        @Grab("com.atlassian.jira:jira-api:8.0.0"),
        /*
        Many transitive dependencies of the Jira API will either be provided by the host Confluence application or simply aren't needed for this script. Since several of them won't be resolvable with the default configuration, we exclude them with the below annotations.
         */
        @GrabExclude("com.atlassian.annotations:atlassian-annotations"),
        @GrabExclude("jta:jta"),
        @GrabExclude("log4j:log4j"),
        @GrabExclude("webwork:pell-multipart-request"),
        @GrabExclude("org.codehaus.jackson:jackson-core-asl"),
        @GrabExclude("org.codehaus.jackson:jackson-mapper-asl"),
        @GrabExclude("com.atlassian.sal:sal-api"),
        @GrabExclude("com.atlassian.gadgets#atlassian-gadgets-api"),
])
import com.atlassian.confluence.user.AuthenticatedUserImpersonator
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal
import com.atlassian.jira.bc.issue.comment.CommentService
import com.atlassian.jira.bc.issue.comment.CommentService.CommentParameters.CommentParametersBuilder
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.UserManager
import com.onresolve.scriptrunner.remote.RemoteControl
import com.atlassian.jira.user.util.UserManager as JiraUserManager

def page = event.page
def (title, pageId, spaceKey) = [page.title, page.id, page.space.key]
def currentUserKey = AuthenticatedUserThreadLocal.get().name
log.debug "Current user is ${currentUserKey}"
def serviceAccountUser = ComponentLocator.getComponent(UserManager).getUser('serviceaccount') //change this to a user with admin permissions on your remote Jira instance

def messages = AuthenticatedUserImpersonator.REQUEST_AGNOSTIC.asUser({
    RemoteControl.forPrimaryJiraAppLink().exec {
        log.debug "Beginning attempt to create comment in Jira"
        def commentService = ComponentAccessor.getComponent(CommentService)
        def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
        def searchService = ComponentAccessor.getComponent(SearchService)
        def user = ComponentAccessor.getComponent(JiraUserManager).getUserByName(currentUserKey)
        log.debug "Will seearch & create comments as ${user.name}"
        def jql = "issueFunction in linkedIssuesOfRemote('query', 'pageId=${pageId}')"
        log.debug "Searching with JQL query ${jql}"
        def query = jqlQueryParser.parseQuery(jql)
        def results = searchService.search(user, query, PagerFilter.unlimitedFilter)
        def issues = results.results
        issues.collect { issue ->
            log.debug "Found issue ${issue.key}; attempting to create comment as ${user.name}"
            def commentParameters = new CommentParametersBuilder()
                    .author(user)
                    .body("Page ${title} has been updated") // Customize this comment to contain the information you need
                    .issue(issue)
                    .build()
            def commentCreateValidationResult = commentService.validateCommentCreate(user, commentParameters)
            if (commentCreateValidationResult.valid) {
                def comment = commentService.create(user, commentCreateValidationResult, true)
                def message = "Created comment ${comment.id} on issue ${issue.key} in repsonse to update of page '${title}' in the ${spaceKey} space"
                log.debug message
                return message
            }
            log.error "Could not create comment as ${user.name}"
            commentCreateValidationResult.errorCollection.errorMessages.each {
                log.error it
            }
            commentCreateValidationResult.warningCollection.warnings.each {
                log.warn it
            }
        }
    }
}, serviceAccountUser)

messages.each {
    log.debug messages
}
Having an issue with this script?
Report it here