I'm trying to set an issues DateTime field at issue creation time based on priority. I've started by using Mizan's script in this question: https://answers.atlassian.com/questions/27052/customize-priority-based-on-custom-field
However I want the datetime to be set more granularly. 1 hour for Critical, 2 Hours for high etc. I've modified the script sucessfully to use my own priorties but I can't figure out how I can set the datetime field to be set with the time as well as the date. Note I haven't even figured out how to call the custom field yet!
My version of the script is here:
import java.sql.Timestamp import com.atlassian.jira.issue.MutableIssue // initializing the priority def CRITICAL = 1; def HIGH = 3; def MEDIUM = 9; def LOW = 30; // calender which returns the date according to the priority defined private GregorianCalendar getDate(double roll) { Calendar cal = Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.HOUR_OF_DAY,0); cal.set(Calendar.MINUTE,0); cal.set(Calendar.SECOND,0); cal.set(Calendar.MILLISECOND,0); for (int x=0;x<roll;x++) { cal.add(Calendar.DAY_OF_MONTH,1); } return cal; } MutableIssue mutableIssue = (MutableIssue) issue; def priority = mutableIssue.getPriority().getString("name"); def setDueDate = mutableIssue.getDueDate(); //onlY set the dueDate if the date isn't already set (i.e. if it == null). GregorianCalendar cal; if(priority.equals("Critical")) { cal = getDate(CRITICAL); } else if(priority.equals("High")) { cal = getDate(HIGH); } else if(priority.equals("Medium")) { cal = getDate(MEDIUM); } Timestamp dueDate = new Timestamp(cal.getTimeInMillis()); mutableIssue.getPriorityObject(); mutableIssue.setDueDate(dueDate);
Hey Graham,
Haven't tested this code but maybe you can get some ideas from it.
import com.atlassian.event.api.EventListener; import com.atlassian.event.api.EventPublisher; import com.atlassian.jira.bc.issue.IssueService; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.datetime.DateTimeFormatter; import com.atlassian.jira.event.issue.IssueEvent; import com.atlassian.jira.event.type.EventType; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.IssueInputParameters; import com.atlassian.jira.issue.priority.Priority; import com.atlassian.jira.security.JiraAuthenticationContext; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import java.util.Calendar; public class DueDateBasedOnPriority implements InitializingBean, DisposableBean { private static final Logger log = LoggerFactory.getLogger(DueDateBasedOnPriority.class); private final EventPublisher eventPublisher; private final IssueService issueService; private final JiraAuthenticationContext jiraAuthenticationContext; /** * Constructor. * @param eventPublisher injected {@code EventPublisher} implementation. */ public DueDateBasedOnPriority(EventPublisher eventPublisher, IssueService issueService, JiraAuthenticationContext jiraAuthenticationContext) { this.eventPublisher = eventPublisher; this.issueService = issueService; this.jiraAuthenticationContext = jiraAuthenticationContext; } /** * Called when the plugin has been enabled. * @throws Exception */ @Override public void afterPropertiesSet() throws Exception { // register ourselves with the EventPublisher eventPublisher.register(this); } /** * Called when the plugin is being disabled or removed. * @throws Exception */ @Override public void destroy() throws Exception { // unregister ourselves with the EventPublisher eventPublisher.unregister(this); } /** * Receives any {@code IssueEvent}s sent by JIRA. * @param issueEvent the IssueEvent passed to us */ @EventListener public void onIssueEvent(IssueEvent issueEvent) { Long eventTypeId = issueEvent.getEventTypeId(); DateTimeFormatter dateTimeFormatter = ComponentAccessor.getComponent(DateTimeFormatter.class); Issue issue = issueEvent.getIssue(); Priority priority = issue.getPriorityObject(); // if it's an event we're interested in, log it if (eventTypeId.equals(EventType.ISSUE_CREATED_ID)) { IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(); issueInputParameters.setDueDate(getDueDateOnPriority(priority.getName()).toString()); IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(jiraAuthenticationContext.getUser().getDirectoryUser(), issue.getId(), issueInputParameters); if(updateValidationResult.isValid()){ IssueService.IssueResult updateResult = issueService.update(jiraAuthenticationContext.getUser().getDirectoryUser(), updateValidationResult); if(!updateResult.isValid()){ //log your errors } } } } private DateTime getDueDateOnPriority(String priority){ DateTime now = new DateTime(); if(priority.equals("CRITICAL")) return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 1); if(priority.equals("HIGH")) return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 3); if(priority.equals("MEDIUM")) return now.withHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + 9); if(priority.equals("LOW")) return now.withDayOfYear(Calendar.getInstance().get(Calendar.DAY_OF_YEAR) + 3); else return now; } }
Hi Bhushan,
Thanks very much for this. It'll take me a while to work my way through it but I'll have a play and see how I get on!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry about the bounty being added and cancelled. I'm trying to assign 5 karma but it keeps setting it to 30 which is a bit steep for me :/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.