Can you execute a search filter using Script Runner?

Damon Morda September 14, 2014

I'd like to use Script Runner to execute a search filter periodically. Here's what I'm trying to do, if someone has another suggestion, I'm open.

  1. Search for issues that are:
    1. Marked as "Resolved"
    2. Have a value of EMPTY for "Survey Sent" field
    3. Have not been updated in 2 days
  2. For each issue, update it as follows:
    1. Set custom field named "Survey Sent" to "Yes"
    2. Send email to reporter

Is this possible using Script Runner?

 

3 answers

1 accepted

2 votes
Answer accepted
Geert Graat
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.
September 15, 2014

Hi, this definitely seems possible, see the following links for pointers:

https://answers.atlassian.com/questions/134531

https://answers.atlassian.com/questions/211505

https://answers.atlassian.com/questions/189532

I have created some scripts for the Script Runner to perform other tasks, it is relatively easy given all the information you can find. Good luck!

Geert

Damon Morda September 25, 2014

This got me on the right track. I have integrated all three concepts and have a functional solution now.

Like Alibek Malikov likes this
SWAPNIL SRIVASTAV May 18, 2020

Hello @Damon Morda ,

I have a similar requirement, could you please tell me what feature u used to trigger a script?

I mean, was it Script Listener, Send a custom email feature or what?

I have the code to hit the JQL and fetch results and traverse through them. 

But I dont know how to run it in the background in every 15 min or how to send an email using script.

1 vote
Damon Morda September 25, 2014

The code below will automatically send an email to the reporter once an issue meets certain criteria specified in a JQL query. It works by doing a JQL search for issues that don't have a value of "true" for a custom field we created called "Survey Sent". When the script runs, it will send emails to all the issues returned and then update that custom field to "true".

Implementation Steps

  1. Create a custom field called "Survey Sent" that is a single text field
  2. Take code below and put it in a file (e.g.,  /var/lib/jira/scripts/send-survey.groovy)
  3. Goto Admin > System > Services and add a new service:
    1. Name: My Auto Email Service
    2. Class: com.onresolve.jira.groovy.GroovyService
    3. Delay: 15
  4. Done.

You will need to modify some of the code below such as the JQL search. I put <> around anything to represent it's a variable you'll want to edit.

 

// This groovy script automatically sends a customer satisfaction survey
// to the reporter once an issue has been resolved and not updated for a 
// defined period of time.
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;

// Setup the various variables
jqlSearch = '&lt;project = "IT Help Desk" AND "Survey Sent" is EMPTY AND status in (Resolved,Closed) AND issuetype NOT IN (Task, Sub-task) AND updated &lt;= -2d&gt;'; // Create the JQL query to perform
theUser = "&lt;your cron user&gt;"; // Specify the user that should execute the query if not logged in
surveyField = "Survey Sent"; // The field used to determine if the survey was already sent
surveySentValue = "true"; // The value to stick in the custom field to indicate a survey was sent

SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueManager issueManager = ComponentAccessor.getIssueManager();
def componentManager = ComponentManager.instance
def customFieldManager = componentManager.getCustomFieldManager();
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager();

// If the user isn't specified, then set it to the default script running user
if (!user) {
	user = userUtil.getUserObject(theUser);
}
// Set the default list of issues to null just to be safe 
List&lt;Issue&gt; issues = null;

// Perform the search as a user and return the result so it can be validated. 
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);

// Loop through each issue returned in the JQL and set a value for the custom field "Survey Sent"
if (parseResult.isValid()) {

	def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
	// Setup mail server
	MailServerManager mailServerManager = componentManager.getMailServerManager();
	SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer();

	// Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps)
	issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}

	// Loop through each issue an update the Survey Sent cusotm field to indicate the survey has been sent
	issues.each() { Issue issue -&gt;
		MutableIssue myIssue = issueManager.getIssueObject(issue.id);
		def tgtField = customFieldManager.getCustomFieldObjects(myIssue).find {it.name == surveyField}
		def changeHolder = new DefaultIssueChangeHolder();
		tgtField.updateValue(null, myIssue, new ModifiedValue(issue.getCustomFieldValue(tgtField), surveySentValue),changeHolder);

		// Reindex the field because if you don't, when the JQL search runs again it might not pick up on the
		// fact that you put "true" in the "Survey Sent" custom field.
		boolean wasIndexing = ImportUtils.isIndexIssues();
		ImportUtils.setIndexIssues(true);
		ComponentAccessor.getIssueIndexManager().reIndex(issue);
		ImportUtils.setIndexIssues(wasIndexing);

		// Setup the reporter variables
		reporter = userUtil.getUserByName(issue.reporter.name); // Get the reporter object
		reporterEmail = reporter.getEmailAddress(); // Get the reporter email address
		reporterFirstName = reporter.displayName; // Get the reporter full name

		// If the mail server configuration is valid, send an email.
		if (mailServer) {
			Email email = new Email(reporterEmail); // Set the TO address, optionally CC and BCC
			email.setSubject("(${issue.key}) Customer Service Survey"); // Set the SUBJECT
			email.setFrom("IT Help Desk &lt;ithelpdesk@yourdomain.com&gt;"); // Set the FROM address
			String content = """\
Hello ${reporterFirstName},
Ticket: ${issue.key}
Summary: ${issue.getSummary()}

IT recently resolved your ticket and we hope you were happy with our support. We would appreciate it if you could take a quick 6 question survey to tell is about your experience.

&lt;some url&gt;

Your feedback is important as we continuously strive to improve the services provided to our user community.
Thanks in advance for helping us help you.

IT Help Desk
ithelpdesk@mydomain.com
(555) 555-1212""";

			email.setBody(content)
			mailServer.send(email)
		}
		else {
			log.error "[Send Survey Cron] No SMTP mail server defined.";
		}
	}
} 
else {
	// Log errors if invalid JQL is used so we can fix it
	log.error("[Send Survey Cron] Invalid JQL: " + jqlSearch);
}
0 votes
Vijay Sridhar March 13, 2016

Hi ,

After adding the service in JIRA , i am getting the below error in the logs.

JIRA Version = 7.0.9

 

2016-03-14 13:11:00,025 Caesium-1-1 ERROR anonymous Email_Esclation [c.o.jira.groovy.GroovyService] Script service failed: /var/atlassian/application-data/jira/scripts/sendsurvey.groovy
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.ComponentManager.getCustomFieldManager() is applicable for argument types: () values: []
at sendsurvey.run(sendsurvey.groovy:34)
2016-03-14 13:11:00,427 Caesium-1-4 ERROR vijay.sridhar [c.a.p.automation.scheduler.RuleCallable] Exception while executing the rule
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
at com.tuncaysenturk.jira.plugins.enhancer.customfield.sla.c.a(SlaIndicatorCFType.java:85)
at com.tuncaysenturk.jira.plugins.enhancer.customfield.a.getValueFromIssue(AbstractJepCFType.java:136)
at com.atlassian.jira.issue.fields.CustomFieldImpl.getValue(CustomFieldImpl.java:386)
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.IssueImpl.setCustomFieldValue(IssueImpl.java:906)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateIssue(CustomFieldImpl.java:1074)
at com.atlassian.jira.bc.issue.DefaultIssueService.updateIssueFromFieldsWithoutScreenCheck(DefaultIssueService.java:702)
at com.atlassian.jira.bc.issue.DefaultIssueService.updateIssueFromFields(DefaultIssueService.java:661)
at com.atlassian.jira.bc.issue.DefaultIssue

Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

The first error is because you use ComponentManager instead of ComponentAccessor. The right way to get the customFieldManager is

import com.atlassian.jira.component.ComponentAccessor


def customFieldManager = ComponentAccessor.getCustomFieldManager()

Now for your second error I will need more information (the code in the line that exception is thrown). But I think it depends on the type of your custom field. 

Vijay Sridhar March 21, 2016

Hi,

After changing to ComponentAccessor. it's working fine .

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.util.DefaultUserManager

def  tester= userUtil.getUserByName(issue.tester.name);  // Code Executing Successfully , 

Getting Below Error if changed to  UserManager.getUserByName(issue.tester.name);

Error.png

No signature of method: static com.atlassian.jira.user.util.UserManager.getUserByName() is applicable for argument types: (java.lang.String) values: [vijay.sridhar]

Any other things needs to changed for using UserManager to get the name ?

 

 

 

 

Thanos Batagiannis [Adaptavist]
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.
March 22, 2016

Can I ask you why you need this ? I mean, 

ApplicationUser getReporter = issue.reporter will return to you exactly the same object with 

ApplicationUser getReporter = userManager.getUserByName(issue.reporter.username)

Vijay Sridhar March 27, 2016

Hi,
Was using UserManager to get the reporter name which shows warning symbol during compile  ,after using "" ApplicationUser getReporter = issue.reporter " the warning has gone

Suggest an answer

Log in or Sign up to answer