package example.SetFieldLastAssigneeWithRoleX

import com.atlassian.crowd.embedded.api.User
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.jira.ManagerFactory
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.util.JiraUtils;
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
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.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.security.roles.*
class TaskVersionListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskVersionListener.class)

@Override
void workflowEvent(IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)
ComponentManager componentManager=ComponentManager.getInstance()
CommentManager commentManager = componentManager.getCommentManager()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()

Issue issue = event.getIssue()

ProjectManager projectManager = componentManager.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class)
ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRoleManager.getProjectRole("ROLE XXX"), issue.getProjectObject())
if ( projectRoleActors.getUsers().contains(issue.getAssignee()) ) {
CustomField cf = customFieldManager.getCustomFieldObject("customfield_XXXXX")
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf),issue.getAssigneeId()), changeHolder)
}
//throw new RuntimeException("YEAH IT WORKS!");

}
}

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s