See this code…


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.customfields.*
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.fugue.Option
import org.apache.log4j.Category
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.plugin.PluginAccessor
import com.atlassian.fugue.Option
ComponentManager componentManager=ComponentManager.getInstance()
def userPropertyManager = ComponentAccessor.getUserPropertyManager();
def userManager = ComponentAccessor.getUserManager();
def Users = userManager.getAllUsers();
def issueFactory = ComponentManager.getInstance().getIssueFactory()
def projectMgr = ComponentManager.getInstance().getProjectManager()
User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def issueMgr = ComponentManager.getInstance().getIssueManager()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
UserUtil userUtil = ComponentAccessor.getUserUtil()
def user2 = userUtil.getUserByKey("admin_user_to_do_the_jql_query")
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
String jqlSearch = ''
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def newissue = null;

int i = 0;
int j = 0;
int last = Users.size();

String firstName = "";
String lastName = "";
String displayName = "";
String username = "";
String email = "";
String isActive = "";
String directory = "";

def projectName = "TARGET PROJECT NAME TO CREATE THE USER EPICS"
Issue epic = null;
String Key = null;
def epicLink = null;
def cf2 = null;

while ( i < last ) {
//We can get now the user parameters
firstName = Users[i].getFirstName();
lastName = Users[i].getLastName();
displayName = Users[i].getDisplayName();
username = Users[i].getName();
email = Users[i].getEmailAddress();
isActive = Users[i].isActive();
directory = Users[i].getDirectoryId();
if ( isActive == "true" && directory == "XXXXX") {
//Create the user as EPIC in a new project
newissue = issueFactory.getIssue();
newissue.setSummary ("User: " + username)
newissue.setProject (projectMgr.getProjectByName(projectName)) //User Epics
newissue.setIssueTypeId("5") //Epic

def project = projectMgr.getProjectObjByName(projectName)
newissue.setAssigneeId(project.getLeadUserKey())

//Fill the Epic Name
def cf = null;
cf = customFieldManager.getCustomFieldObjects(newissue).find {it.name == 'Epic Name'}
newissue.setCustomFieldValue(cf,"User: " + username)

def params = ["issue":newissue]
def subTask = issueMgr.createIssue(currentUserObj, params)
Key = subTask.get("key")

//Search in XXXX project for tickets with the user name referenced... example query
jqlSearch = " project = 'XXX' and (( 'First Name' ~ '"+firstName+"' and 'Last Name' ~ '"+lastName+"') or ('Username' ~ '"+username+"' ))"
SearchService.ParseResult parseResult = searchService.parseQuery(user2.getDirectoryUser(), jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user2.getDirectoryUser(), parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect {issueMgr.getIssueObject(it.id)}
def issueSet = new HashSet<Issue>();
for ( issue in issues ){
issueSet.add(issue);
}

epic = issueMgr.getIssueObject(Key);
def epicLinkManagerImpl = getBean("epicLinkManagerImpl");
def epicoption = (Option)Option.option(epic)
epicLinkManagerImpl.associateIssuesWithEpic((com.atlassian.jira.user.ApplicationUser) user2, epicoption, issueSet);


}

j++;
}
i++;
}

def getBean(String beanId)
{
def ghPlugin = ComponentAccessor.getPluginAccessor().getEnabledPlugin("com.pyxis.greenhopper.jira")
def descriptor = ghPlugin.getModuleDescriptor("greenhopper-launcher")
def applicationContext = descriptor.getModule().greenHopperCacheManager.applicationContext
def beanInstance = applicationContext.getBean(beanId)
return beanInstance;
}

throw new Exception("END SCRIPT ");

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