jira-plugin-intellij /src/test/resources/examples/RollbackCompentsCockup.groovy

Language Groovy Lines 112
MD5 Hash 64708daf7a27bb19a74e1b4c4c11528e
Repository https://bitbucket.org/sorin/jira-plugin-intellij View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package examples

import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.core.ofbiz.CoreFactory
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.option.OptionSetManager
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.query.Query
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import org.apache.log4j.Level
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.issue.util.IssueUpdater
import com.atlassian.jira.issue.util.IssueUpdateBean
import com.atlassian.jira.event.type.EventType
import webwork.dispatcher.ActionResult
import com.atlassian.core.action.ActionUtils
import com.sun.deploy.net.HttpRequest

// CONFIG
String projectKey = "TITAN"
// END CONFIG

log.setLevel(Level.WARN)

ComponentManager componentManager = ComponentManager.getInstance()
OptionSetManager optionSetManager = ComponentManager.getComponentInstanceOfType(OptionSetManager.class)
IssueUpdater updater = componentManager.getIssueUpdater()
SearchProvider searchProvider = componentManager.getSearchProvider()
JiraAuthenticationContext authenticationContext = componentManager.getJiraAuthenticationContext()
SearchRequestService searchRequestService = componentManager.getSearchRequestService()
JiraServiceContext ctx = new JiraServiceContextImpl(authenticationContext.getUser());
ProjectComponentManager pcManager = componentManager.getProjectComponentManager()

IssueManager issueManager = componentManager.getIssueManager()
ProjectManager projectManager = componentManager.getProjectManager()
Project project = projectManager.getProjectObjByKey(projectKey)

JqlQueryBuilder jqlQueryBuilder = JqlQueryBuilder.newBuilder()
Query query = jqlQueryBuilder.where().project(project.id).buildQuery()
def sr = new SearchRequest(query)

SearchResults results

results = searchProvider.search(sr.getQuery(), authenticationContext.getUser(), PagerFilter.getUnlimitedFilter())
log.debug results

List issues = results.getIssues();
Map fixIssues = [:]

Set<String> allComponents = []

for (Issue issue in issues) {
    Collection<GenericValue> changeGroups = CoreFactory.getGenericDelegator().findByAnd("ChangeGroup", ["issue": issue.id]);

    GenericValue badChange = (changeGroups as List).reverse().find {GenericValue changeGroup ->
        changeGroup.getRelated("ChildChangeItem").find {GenericValue changeItem ->
            changeItem.get("field") == "Component" && changeItem.get("newstring") == null && changeItem.get("oldstring") != null
        }
    } as GenericValue

    if (badChange) {
        List comps = badChange.getRelated("ChildChangeItem").findAll {GenericValue changeItem ->
            changeItem.get("field") == "Component"
        }*.get("oldstring")

        comps.remove(null)
        log.debug("Issue ${issue.key} had commpoents $comps")
        fixIssues.put(issue.key, comps)
        allComponents.addAll(comps)
    }
}

log.debug("Create components $allComponents")
allComponents.each {String compName ->
    if (compName != null && !pcManager.findByComponentName(project.id, compName)) {
        pcManager.create(compName, "", null, AssigneeTypes.PROJECT_DEFAULT, project.id)
    }
}

fixIssues.each {String key, List comps ->
    MutableIssue issue = issueManager.getIssueObject(key)
    log.debug("Fix issue ${issue.key}")
    List<ProjectComponent> compObjects = comps.collect {pcManager.findByComponentName(project.id, it)}
    issue.setComponents(compObjects*.genericValue)

//    IssueUpdateBean iub = new IssueUpdateBean(issue, issueManager.getIssueObject(key), EventType.ISSUE_UPDATED_ID, authenticationContext.getUser(), false, false)
//    updater.doUpdate(iub, false)

    Map parameters = ["issueObject": issue, "issue" : issue.getGenericValue(), "remoteUser": authenticationContext.getUser(), "sendMail": false]
    ActionResult aResult = CoreFactory.getActionDispatcher().execute(com.atlassian.jira.action.ActionNames.ISSUE_UPDATE, parameters );
    ActionUtils.checkForErrors( aResult );


}
Back to Top