daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package scriptConsole
  2.  
  3. import com.atlassian.jira.bc.issue.search.SearchService
  4. import com.atlassian.jira.component.ComponentAccessor
  5. import com.atlassian.jira.event.type.EventDispatchOption
  6. import com.atlassian.jira.issue.CustomFieldManager
  7. import com.atlassian.jira.issue.Issue
  8. import com.atlassian.jira.issue.IssueManager
  9. import com.atlassian.jira.issue.ModifiedValue
  10. import com.atlassian.jira.issue.MutableIssue
  11. import com.atlassian.jira.issue.fields.CustomField
  12. import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
  13. import com.atlassian.jira.issue.search.SearchResults
  14. import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
  15. import com.atlassian.jira.security.JiraAuthenticationContext
  16. import com.atlassian.jira.user.ApplicationUser
  17. import com.atlassian.jira.web.bean.PagerFilter
  18. import groovy.transform.Field
  19. import org.apache.log4j.Level
  20. import org.apache.log4j.Logger
  21.  
  22. /**
  23.  * Skript erstellt für Agile Hive
  24.  * Ticket:
  25.  * Falls bei der Migration Deutsch und Englisch vorliegen. Gibt es die Epic-Felder in beiden sprachen. Das Skript kopiert die deuteschen Feldwerte zu den englischen Feldwerten.
  26.  *
  27.  */
  28.  
  29. @Field Logger logger = (Logger) log;
  30. logger.setLevel(Level.DEBUG);
  31.  
  32. @Field String EPIC_JQL = "issuetype = Epos and key = WUAS-56"
  33.  
  34. @Field String CF_EPIC_NAME = "Epic Name"
  35. @Field String CF_EPIC_NAME_DE = "Epos-Name"
  36.  
  37. @Field String CF_EPIC_COLOR = "Epic Color"
  38. @Field String CF_EPIC_COLOR_DE = "Epos-Farbe"
  39.  
  40. @Field String CF_EPIC_STATUS = "Epic Status"
  41. @Field String CF_EPIC_STATUS_DE = "Epos-Status"
  42.  
  43. @Field String CF_EPIC_LINK = "Epic Link"
  44. @Field String CF_EPIC_LINK_DE = "Epos-Verknüpfung"
  45.  
  46.  
  47. @Field List<Issue> epics = getAllEpics();
  48.  
  49.  
  50. IssueManager issueManager = (IssueManager) ComponentAccessor.getComponent(IssueManager.class);
  51. for (Issue docIssue : epics) {
  52.     MutableIssue epic = issueManager.getIssueObject(docIssue.getKey());
  53.     handleIssue(epic);
  54.     updateIssue(epic);
  55. }
  56. logger.debug("Ich habe Fertig");
  57.  
  58. void handleIssue(Issue epic) {
  59.     logger.debug("epic ${epic.key}");
  60.     def epicNameDe = getCustomFieldValue(epic, CF_EPIC_NAME_DE)
  61.     updateCustomField(CF_EPIC_NAME, epicNameDe, epic);
  62.  
  63. }
  64.  
  65. List<Issue> getAllEpics() {
  66.     return getIssuesByJQL(EPIC_JQL)
  67. }
  68.  
  69. List<Issue> getIssuesByJQL(String jqlSearch) {
  70.     SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
  71.     ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
  72.     logger.info("Searching JQL: ${jqlSearch}");
  73.     SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
  74.     if (parseResult.isValid()) {
  75.         SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
  76.         return searchResult.getIssues();
  77.     }
  78.     logger.error("JQL '${jqlSearch}' is invalid. ${parseResult.getErrors()}");
  79.     return Collections.emptyList();
  80. }
  81.  
  82. def getCustomFieldValue(Issue issue, String cf_Name) {
  83.     CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
  84.     List<CustomField> customFields = customFieldManager.getCustomFieldObjects(issue);
  85.     return customFields.find { it.name == cf_Name }?.getValue(issue);
  86. }
  87.  
  88. void updateCustomField(String cfName, Object newValue, Issue issue) {
  89.     CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
  90.     CustomField customField = customFieldManager.getCustomFieldObjects(issue).find { it.name == cfName };
  91.     if (!customField) {
  92.         logger.warn("Did not find any CustomField with name ${cfName}");
  93.         return;
  94.     }
  95.     logger.debug("### setCustomFieldValue: ${newValue} to custom field ${customField.getName()}(${customField.getId()})");
  96.     FieldLayoutItem fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField);
  97.     Object oldValue = issue.getCustomFieldValue(customField);
  98.     customField.updateValue(fieldLayoutItem, issue, new ModifiedValue(oldValue, newValue), new DefaultIssueChangeHolder());
  99. }
  100.  
  101. void updateIssue(Issue issue) {
  102.     JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
  103.     ApplicationUser user = authenticationContext.getLoggedInUser();
  104.     IssueManager issueManager = (IssueManager) ComponentAccessor.getComponent(IssueManager.class);
  105.     logger.debug("throw Issue Updated Event for issue ${issue.key}")
  106.     issueManager.updateIssue(user, (MutableIssue) issue, EventDispatchOption.ISSUE_UPDATED, false);
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top