Sometimes we need to create new tickets in Jira programmatically each first of month… (yes, in other words, we will create a recurring task each month) and a scheduled process stored in Jira is called “Service”.
With Adaptavist Scriptrunner or MyGroovy plugins for Jira Server and a little groovy script we can create programmatically complex recurring tasks!
You only need to adapt this script to your situation
//REST clases import groovyx.net.http.HTTPBuilder import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpRequest; import org.apache.http.protocol.HttpContext; import groovy.json.JsonSlurper; import java.text.SimpleDateFormat import java.sql.Timestamp import java.text.DateFormat import java.util.Date import com.atlassian.jira.event.issue.AbstractIssueEventListener import com.atlassian.jira.event.issue.IssueEvent import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.customfields.CustomFieldType import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.mail.Email import com.atlassian.mail.server.MailServerManager import com.atlassian.mail.server.SMTPMailServer import org.apache.log4j.Category import com.atlassian.jira.user.util.UserUtil import com.atlassian.jira.component.ComponentAccessor; import java.text.SimpleDateFormat import java.sql.Timestamp import java.text.DateFormat import java.util.Date import com.atlassian.jira.project.Project import org.apache.log4j.Category import com.atlassian.jira.bc.project.component.* import groovyx.net.http.HTTPBuilder import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpRequest; import org.apache.http.protocol.HttpContext; import groovy.json.JsonSlurper; import com.atlassian.jira.issue.IssueManager; import groovyx.net.http.ContentType; import groovyx.net.http.Method.*; import com.atlassian.jira.issue.label.LabelManager import com.atlassian.jira.issue.watchers.WatcherManager MailServerManager mailServerManager = ComponentAccessor.getMailServerManager() SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer() Category log = Category.getInstance("com.onresolve.jira.groovy") log.setLevel(org.apache.log4j.Level.DEBUG) log.debug "---------------------------RECURRING TASKS INIT-------------------------" def today = new Date().format('yyyy-MM-dd'); def month = new Date().format('MM').toInteger(); if ( month == 0 ) month = 11 month = month-1 def month_txt = new java.text.DateFormatSymbols().months[month] def userManager = ComponentAccessor.getUserManager() def currentUserObj = userManager.getUserByName("ADMIN") def issueMgr = ComponentAccessor.getIssueManager() def projectMgr = ComponentAccessor.getProjectManager() def issueFactory = ComponentAccessor.getIssueFactory() WatcherManager watcherManager = ComponentAccessor.getWatcherManager() //RECURRENT INVOICE TASK EXAMPLE... //---------------------------------------------CREATE NEW ISSUE EACH FIRST OF MONTH def newissue = issueFactory.getIssue(); newissue.setSummary ("INVOICE OF MONTH " + month_txt) newissue.setProjectObject (projectMgr.getProjectByCurrentKey("PRJ")) newissue.setIssueTypeId("XX") //IssueType Id def project = projectMgr.getProjectByCurrentKey("PRJ") newissue.setReporterId("ADMIN") newissue.setAssigneeId(project.getLeadUserKey()) newissue.setDescription("INVOICE...") //"Saving Issue" def params = ["issue":newissue] def subTask = issueMgr.createIssue(currentUserObj, params) def Key = subTask.get("key") log.debug "Key = " + Key //Adding labels LabelManager labelManager = ComponentAccessor.getComponent(LabelManager) def labels = labelManager.getLabels(newissue.id).collect{it.getLabel()} labels += 'Invoice' labelManager.setLabels(currentUserObj,newissue.id,labels.toSet(),false,false) //Adding watchers watcherManager.startWatching(userManager.getUserByName("USER1"), newissue) watcherManager.startWatching(userManager.getUserByName("USER2"), newissue) //------------------------------------------------------------ //OTHER EXAMPLE RECURRING TASK newissue = issueFactory.getIssue(); newissue.setSummary ("EXAMPLE") newissue.setProjectObject (projectMgr.getProjectByCurrentKey("PRJ")) newissue.setIssueTypeId("XX") //IssueType Id project = projectMgr.getProjectByCurrentKey("PRJ") newissue.setReporterId("ADMIN") newissue.setAssigneeId("ADMIN") newissue.setDescription("EXAMPLE...") //"Saving Issue" params = ["issue":newissue] subTask = issueMgr.createIssue(currentUserObj, params) Key = subTask.get("key") log.debug "Key = " + Key //Adding labels labelManager = ComponentAccessor.getComponent(LabelManager) labels = labelManager.getLabels(newissue.id).collect{it.getLabel()} labels += 'Example' labelManager.setLabels(currentUserObj,newissue.id,labels.toSet(),false,false) //Adding watchers watcherManager.startWatching(userManager.getUserByName("USER1"), newissue) watcherManager.startWatching(userManager.getUserByName("USER2"), newissue) watcherManager.startWatching(userManager.getUserByName("USER3"), newissue) watcherManager.startWatching(userManager.getUserByName("USER4"), newissue) watcherManager.startWatching(userManager.getUserByName("USER5"), newissue) log.debug "---------------------------RECURRING TASKS END-------------------------"
Save it as a file and place it as a Jira Service (and configure the execution time).
In the marketplace exists Jira plugin solutions for more complex situations:
see more (link)
By MrAddon