253
社区成员
发帖
与我相关
我的任务
分享import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.td.jira.plugin.utils.LogFileOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/***
* @auth baopuzi
* @date 7/25/18
* @desc 描述信息
*/
public class IssuseUpdateListener extends AbstractIssueEventListener {
private static final Logger log = LoggerFactory.getLogger(IssuseUpdateListener.class);
private JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
@Override
public void workflowEvent(IssueEvent issueEvent) {
log.debug("_+_+_+_+_+_+_+_+_+_+_+_登录的用户信息:" + authContext.getLoggedInUser().toString());
Issue issue;
MutableIssue mutableIssue = null;
Long eventTypeId; //事件类型
Object customFieldValue; //自定义字段的值
CustomField customField = null; //自定义字段
eventTypeId = issueEvent.getEventTypeId();
if (EventType.ISSUE_UPDATED_ID.equals(eventTypeId)) {
try {
issue = issueEvent.getIssue();
mutableIssue = (MutableIssue) issue;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
List<CustomField> customFieldList = customFieldManager.getCustomFieldObjects(issue);
if (customFieldList != null && customFieldList.size() > 0) {
String nameKey = null;
for (CustomField field : customFieldList) {
nameKey = field.getNameKey();
LogFileOutput.log("****************desc:" + field.getDescription() + ",fieldName:" + field.getFieldName() + ",id:" + field.getId() + "name:" + field.getName() + ",nameKey:" + field.getNameKey() + ",untransLasedDesc:" + field.getUntranslatedDescription() + ",valueFormIssue:" + field.getValueFromIssue(issue) + ",getValue::" + field.getValue(issue) + ",valueFromMu:" + field.getValueFromIssue(mutableIssue) + ",getValueMu:" + field.getValue(mutableIssue));
if (nameKey.equals("顺延")) {
customField = field;
break;
}
}
}
customFieldValue = mutableIssue.getCustomFieldValue(customField);
LogFileOutput.log("*****************::customFieldValue:" + customFieldValue);
//设置时间
if (customFieldValue == null) {
return;
}
Integer days = Integer.valueOf(customFieldValue + "");
if (days != 0) {
Timestamp dueDate = issue.getDueDate();
int intDays = Math.abs(days.intValue());
//
Calendar calendar = Calendar.getInstance();
if (dueDate == null) {
log.info("++++++++++++该issue没有指定due Date ,跳过并且不执行");
return;
} else {
calendar.setTime(dueDate);
}
for (int i = 0; i < intDays; ) {
if (days < 0) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
} else {
calendar.add(Calendar.DAY_OF_MONTH, -1);
}
if (calendar.get(Calendar.DAY_OF_WEEK) - 1 == 6 || calendar.get(Calendar.DAY_OF_WEEK) - 1 == 0) {
days++;
continue;
}
i++;
}
Date newDueDate = new Date(System.currentTimeMillis() + (1000 * 3600 * 24 * days));
if (dueDate != null) {
newDueDate = new Date(dueDate.getTime() + (1000 * 3600 * 24 * days));
}
LogFileOutput.log("dueDate:" + dueDate + ",newDueDate:" + new Timestamp(newDueDate.getTime()));
mutableIssue.setDueDate(new Timestamp(newDueDate.getTime()));
mutableIssue.setCustomFieldValue(customField, null);
ComponentAccessor.getIssueManager().updateIssue(authContext.getLoggedInUser(), mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
}
} catch (Exception ex) {
LogFileOutput.log("!@#$$$$$$%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^error*******************:" + ex.getMessage());
mutableIssue.setCustomFieldValue(customField, null);
ComponentAccessor.getIssueManager().updateIssue(authContext.getLoggedInUser(), mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
//置空自定义字段
LogFileOutput.log("******************************置空自定义字段:ok,issueKey:" + mutableIssue.getKey());
}
if (log.isTraceEnabled()) {
log.trace("**********************Exit event " + issueEvent.getEventTypeId() + " on issue " + issueEvent.getIssue());
}
} else {
super.workflowEvent(issueEvent);
}
}
}