Hi,
is there a way to use scriptrunner to give me a table with the correlation of 2 issues linked via a 3rd issue?
In practice I have:
issue1 -> link -> Issue2 -> link -> Issue3
I need a list of issues "issue1" and "issue3" in a table based on the links via issue2.
Is anyone had this need in the past?
Thanks so much!!
This looks more like a syntax error where it can't find the method you used which I suspect the culprit is this line:
Transition transition = new Transition(action.getId(), statusToId);
You are declaring the Transition with some invalid constructor. Not sure if this is your full code, but I don't see this variable was being used elsewhere.
Thank you very much for the answer! My initial post did not contain the whole code, it's a rather long string. I'll post it here so that there is no confusion. What baffles me in this whole sitauation is that the listenere worked fine for three months and nobody made any changes in the code before the problem accured.
@field Long linkTypeToProcess = 10400L;
@field ApplicationUser technicalUser = ComponentAccessor.getUserManager().getUserByName("admin");
@field MutableIssue issue = event.issue as MutableIssue;
ChangeHistoryManager historyManager = ComponentAccessor.getChangeHistoryManager();
def changeHistory = historyManager.getChangeHistoryById(event.getChangeLog().getLong("id"))
List<GenericValue> changeItems = changeHistory.getChangeItems()
//log.error(changeItems.toArray().toString())
for (GenericValue changeItem: changeItems){
def type = changeItem.getOrDefault("fieldtype","");
def field = changeItem.getOrDefault("field","");
if (type.equals("jira") && field.equals("status")){
IssueLinkManager linkManager = ComponentAccessor.getIssueLinkManager()
List<IssueLink> links = new ArrayList<>();
links.addAll(linkManager.getOutwardLinks(issue.getId()));
links.addAll(linkManager.getInwardLinks(issue.getId()));
for (IssueLink link : links){
if (!link.getLinkTypeId().equals(linkTypeToProcess)){
continue;
}
MutableIssue destinationIssue = (MutableIssue) link.getDestinationObject();
if (destinationIssue.getId().equals(issue.getId())){
destinationIssue = (MutableIssue) link.getSourceObject();
}
log.error("From " + issue.getKey())
log.error("To " + destinationIssue.getKey())
//log.error("Source status - " + issue.getStatus().getName() + " - " + issue.getStatus().getId())
//log.error("Target status - " + destinationIssue.getStatus().getName() + " - " + destinationIssue.getStatus().getId())
if (issue.getStatus().equals(destinationIssue.getStatus())){
continue;
}
try{
transitionIssue(destinationIssue, issue.getStatus().getId())
}
catch (Exception e){
log.error(e.getMessage())
}
}
}
}
def transitionIssue(Issue issue, String value) {
if (value.equals(issue.getStatusId())) {
return;
}
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
IssueService issueService = ComponentAccessor.getIssueService()
// JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
// ApplicationUser currentUser = jiraAuthenticationContext.getLoggedInUser();
try {
// jiraAuthenticationContext.setLoggedInUser(user);
String fromStatusId = issue.getStatus().getId();
//log.debug("Transitioning issue from " + issue.getStatus().getName() + "-" + fromStatusId + " to " + value);
JiraWorkflow workflow = workflowManager.getWorkflow(issue);
Map<String, Set<Transition>> transitionMap = new HashMap<>();
for (ActionDescriptor action : workflow.getAllActions()){
String statusToId = workflowManager.getNextStatusIdForAction(issue, action.getId());
//log.error("Action with id " +action.getId() + " leads to status " + statusToId);
if (!fromStatusId.equals(statusToId)) {
Transition transition = new Transition(action.getId(), statusToId);
for (StepDescriptor step: workflow.getStepsForTransition(action)){
transitionMap.compute((String) step.getMetaAttributes().get("jira.status.id"), new BiFunction<String, Set<Transition>, Set<Transition>>() {
@Override
public Set<Transition> apply(String k, Set<Transition> v) {
Set<Transition> transitionSet = v == null ? new HashSet<>() : v;
transitionSet.add(transition);
return transitionSet;
}
})
};
}
else {
log.error("The transition leads to itself");
}
};
Optional<List<Integer>> path = findPath(transitionMap, issue.getStatusId(), value);
if (path.isPresent()) {
for (int actionId : path.get()) {
//log.error("Performing action " + actionId);
IssueService.TransitionValidationResult transitionValidationResult = issueService.validateTransition(
technicalUser, issue.getId(), actionId, issueService.newIssueInputParameters()
);
if (transitionValidationResult.isValid()) {
issueService.transition(technicalUser, transitionValidationResult);
} else {
log.error("Error transitioning issue: " + transitionValidationResult.getErrorCollection());
break;
}
}
} else {
log.error("Could not find transition sequence from status " + issue.getStatusId() + " to " + value);
}
} catch (Exception e) {
log.error(e.getMessage());
}
// finally {
// jiraAuthenticationContext.setLoggedInUser(currentUser);
// }
}
def Optional<List<Integer>> findPath(Map<String, Set<Transition>> transitionMap,
String statusFromId, String statusToId) {
return findPath(transitionMap, Collections.emptySet(), statusFromId, statusToId);
}
def Optional<List<Integer>> findPath(Map<String, Set<Transition>> transitionMap, Set<String> visited,
String statusFromId, String statusToId) {
List<Integer> shortestPath = null;
Set<Transition> transitions = transitionMap.get(statusFromId);
if (transitions == null) {
return Optional.empty();
} else {
Set<String> visitedPrime = new HashSet<>(visited);
visitedPrime.add(statusFromId);
for (Transition transition : transitions) {
if (transition.statusToId.equals(statusToId)) {
return Optional.of(Collections.singletonList(transition.actionId));
} else if (!visited.contains(transition.statusToId)) {
Optional<List<Integer>> path = findPath(transitionMap, visitedPrime, transition.statusToId, statusToId);
if (path.isPresent() && (shortestPath == null || shortestPath.size() - 1 > path.get().size())) {
shortestPath = new ArrayList<>();
shortestPath.add(transition.actionId);
shortestPath.addAll(path.get());
}
}
}
}
return Optional.ofNullable(shortestPath);
}
class Transition {
private final int actionId;
private final String statusToId;
private Transition(int actionId, String statusToId) {
this.actionId = actionId;
this.statusToId = statusToId;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Transition that = (Transition) o;
return Objects.equals(statusToId, that.statusToId);
}
@Override
public int hashCode() {
return Objects.hash(statusToId);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your Transition class constructor has been set to private, causing it to not accessible.
private Transition(int actionId, String statusToId) {
this.actionId = actionId;
this.statusToId = statusToId;
}Please remove the private and try again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much for your help! This solved one error, but the other one persists and the listener still does not sync the issue statuses, unfortunetly.
This is the error:
[Static type checking] - Cannot call Script30#findPath(java.util.Map <java.lang.String, java.util.Set>, java.util.Set < java.lang.String>, java.lang.String, java.lang.String) with arguments [java.util.Map<String, Set>, java.util.Set ,T extends java.lang.Object>, java.lan.String, java.lang.String]
and this is the line of code, where the error pops (line 134, column 16):
return findPath(transitionMap, Collections.emptySet(), statusFromId, statusToId);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.