JQL RESULTS TO MAIL FOR SCRIPTRUNNER

Ufuk Uysal August 3, 2020

Hello ,
I want to run the following script every hour with the scriptrunner and send the mail with the url content, if the create time has not been assigned for 60 minutes and there are unresolved records. I can combine several scripts below and send mail. If there is no result, I want it not to send mail and the results returned in the mail content should return as urls. But I couldn't :) I would like your valuable help.

---

package dk.langhornweb
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.mail.Email
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def query = jqlQueryParser.parseQuery("PROJECT = zzz AND assignee is EMPTY AND status = Open AND resolution = Unresolved and created = -60m" )
def results = searchService.search(user,query, PagerFilter.getUnlimitedFilter())
results = results.getResults().each() { print "${it.key}"}
def filterId = 15902
def sendToEmail = "zzzzzz@zzzzz.zzzz"
def jiraGroups = [""]
def jiraAuthContext = ComponentAccessor.getJiraAuthenticationContext()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager.class)
def searchRequest = searchRequestManager.getSearchRequestById(filterId)
def searchResult = searchService.search(user, searchRequest.query, PagerFilter.unlimitedFilter)
if (searchResult.results.size() > 0) {
String cc = buildcc(sendToEmail, jiraGroups)
sendEmail(sendToEmail,
cc,
"| Atanmamış Kayıtlar Bulunmaktadır",
"Sayın Yönetici,\nEkibinizde atanmamış kayıtlar bulunmaktadır.\n\f$results\n\r Bu mail Yardım Masası tarafından Her gün saat 11:00'da otomatik olarak gönderilmektedir.")
}

// Build email cc
def buildcc(def defaultEmail, List<String> groups) {
def ccList = ["xxxx@xxx.xxx"]
def groupManager = ComponentAccessor.getGroupManager()
groups.each { group ->
Collection<ApplicationUser> memberList = groupManager.getUsersInGroup(group)
memberList.each { member ->
ccList.add(member.getEmailAddress())
}
}
return ccList.join(",")
}

// Create an email and send
def sendEmail(def emailAddr, def cc, def subject, def body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr as String)
email.setCc(cc as String)
email.setSubject(subject as String)
email.setBody(body as String)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

2 answers

0 votes
Boris Berenberg - Atlas Authority
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 4, 2020

If you can't get this working in ScriptRunner, you may want to try our app which is built to support this UX from the ground up: https://marketplace.atlassian.com/apps/1211069/notification-assistant-for-jira-email

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 3, 2020

Hi @Ufuk Uysal ,

Have you tried running this script, and have you found any error in the logs ? 

It seems you are not using the query in the script but rather the filter 15902. The mail will be sent if there this filters has results.

Ufuk Uysal August 3, 2020

hi antoine,

this script is running. The filter id and jql above are the same. I want him to mail if the result is frozen. Even if there is no result, mail is sent as []. If there is a result, the url does not come as follows. How can I fix this situation?

[Documentıssueımpl [issuekey = INFRASTRUCTURE-202]

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events