Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to add a watcher(user) only to those tickets that were created from the mail ?

Alex
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.
August 1, 2022

how to add a user only to those tickets that were created from the mail "mypost@gmail.com" ? I think to do it through POST-function , but I only have part of the code

import com.atlassian.jira.component.ComponentAccessor 

def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()

def watcher = ["username1","username2"]

watcher.each{userName->
def user=ComponentAccessor. userManager.getUserByName(userName)
watcherManager.startWatching(user, issue)
}

 

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 28, 2023

Hi @Alex

For your requirement, it would be better to handle this in the Mail Handler.

Below is a sample code for your reference:-

import com.adaptavist.hapi.jira.projects.Projects
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.service.util.ServiceUtils
import com.atlassian.jira.service.util.handler.MessageUserProcessor
import com.atlassian.mail.MailUtils

//This requires the name of the user that will trigger the Mail Handler
final def username = 'admin'

//This requires the Project's Key that will be used for the Mail Handler
final def projectKey = 'MOCK'

//This requires the name of the Issue Type that will be used
final def issueTypeName = 'Bug'

def userManager = ComponentAccessor.userManager
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def messageUserProcessor = ComponentAccessor.getComponent(MessageUserProcessor)

def user = userManager.getUserByName(username)

def issueReporter = messageUserProcessor.getAuthorFromSender(message) ?: user
def project = Projects.getByKey(projectKey)
def subject = message.subject as String
def from = message.from as String
def messageBody = MailUtils.getBody(message)

def issue = ServiceUtils.findIssueObjectInString(subject) as MutableIssue

if (issue) {
return
}

def issueObject = issueFactory.issue
issueObject.setProjectObject(project)
issueObject.setSummary(subject)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == issueTypeName }.id)
issueObject.setReporter(issueReporter)
issue = messageHandlerContext.createIssue(user, issueObject) as MutableIssue

if (from == 'mypost@gmail.com') {
issue.addWatcher()
}

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH,false)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

I hope this helps to answer your question. :-)

Thank you and Kind regards,
Ram 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events