Forums

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

Send MentionIssueEvent to external plugin using REAT API

Kate June 8, 2023

Dear All,

Could you please kindly help. I am trying to create  listner that is reacting on MentionIsuueCommentEvent and send infomation to external plugin via REST API.

Information that needs to be sent is:

  • person who added comment
  • people tagged in comment
  • issue link
  • comment body 

 

 

1 answer

0 votes
Graham Twine
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 8, 2023

Hello @Kate ,

 

This should get you started, The request format will need to conform with the service you are integrating with.

The script below can be added to a comment even in a listener

import java.util.Base64.Encoder

import org.apache.log4j.Level

import groovy.json.JsonSlurper
import groovy.json.JsonParserType

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.HttpResponseDecorator

import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.event.issue.IssueEvent

def apiEndpoint = "https://host:port/base/url"
def resource = "/invoke/this/method"
def token = "Bearer ${getAuthToken()}"

def http = new HTTPBuilder(apiEndpoint)
http.request(Method.POST, ContentType.JSON) { req ->
uri.path = resource
headers.'Authorization' = token
body = buildRequestBody()

response.success = { HttpResponseDecorator resp, data ->
log.info "Response Data: ${data}"
}
}


def getAuthToken() {
def credentials = "user1 user1"
def encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes())

return encodedCredentials.toString()
}


def Map<String, Object> buildRequest(IssueEvent event) {
String action = CommentHelper.getEventAction(event)
String commentId, username, commentBody
Comment originalComment = event.params.get('originalcomment') as Comment

if (COMMENT_DELETED == action) {
commentId = "${originalComment.id}"
username = "${originalComment.authorApplicationUser.username}"

} else {
if (COMMENT_CREATED == action) {
commentId = event.comment.id

} else {
commentId = originalComment.id
}

commentBody = event.comment.body
username = event.comment.authorApplicationUser.username
}

Map<String, Object> commentData = new HashMap<>()
commentData.put('id',commentId)
if (commentBody){
commentData.put('body',commentBody)
}
commentData.put('updateAuthor', ['username':username])

You will need to make this map conform with what the service expects.
Map<String, Object> body = new HashMap<>()
body.put('webhookEvent', action)
body.put('issue', ['key':event.issue.key])
body.put('comment', commentData)
// What issue links? issue.getLinks
// You would need to process the body to get people mentioned

return body
}

class CommentHelper {
public static final String COMMENT_CREATED = 'comment_created'
public static final String COMMENT_EDITED = 'comment_edited'
public static final String COMMENT_DELETED = 'comment_deleted'

public static String getEventAction(IssueEvent event) {
String action = ''

if (event.comment) {
if (event.params.originalcomment) {
action = COMMENT_EDITED

} else {
action = COMMENT_CREATED
}

} else {
action = COMMENT_DELETED
}

return action
}

Suggest an answer

Log in or Sign up to answer