Issue in Scriptrunner jira cloud

Divya Priya
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 23, 2024

Hi ,

I tried creating a last comment script which will return the last comment added in the issue.

I have added a scripted field to get the details , but in that when i tag people in the comment , We are getting atlassian id instead of name.

image.png

Code :

 

def comments = issue.fields.comment.comments;

def lastComment = comments.last();

def lastCommentBody = lastComment ? lastComment.body : "";
return lastCommentBody as String;


1 answer

0 votes
Marcelo Ulloa June 25, 2024

You can take the ID Account that is showing you and pass it to the Displayname to add it to the comment

 

import groovy.json.JsonSlurper
import org.slf4j.LoggerFactory

def logger = LoggerFactory.getLogger(this.class)

def issueKey = "XX-123"
def accountId = "634..............."

// Get user information
def userResponse = get("/rest/api/3/user?accountId=${accountId}")
    .header('Content-Type', 'application/json')
    .asObject(Map)

if (userResponse.status == 200) {
    def user = userResponse.body
    def displayName = user.displayName

    // ADF format for mentioning a user
    def adfComment = [
        type: "doc",
        version: 1,
        content: [
            [
                type: "paragraph",
                content: [
                    [
                        type: "text",
                        text: "This ticket was handled by "
                    ],
                    [
                        type: "mention",
                        attrs: [
                            id: accountId,
                            text: "@${displayName}"
                        ]
                    ],
                    [
                        type: "text",
                        text: "."
                    ]
                ]
            ]
        ]
    ]

    def commentResponse = post("/rest/api/3/issue/${issueKey}/comment")
        .header("Content-Type", "application/json")
        .body([body: adfComment])
        .asString()
    if (commentResponse.status == 201) {
        logger.info("Comment added successfully to issue ${issueKey}.")
    } else {
        logger.error("Error adding comment to issue ${issueKey}. Status: ${commentResponse.status}, Message: ${commentResponse.body}")
    }
} else {
    logger.error("Error retrieving user details for accountId ${accountId}. Status: ${userResponse.status}, Message: ${userResponse.body}")
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events