Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner issue - fix version

ofir gutmacher September 23, 2019

Hi Community,

I have an issue with script runner:

I have 2 projects in JIRA which have the exact same version list (and component list).

Each time an issue is created in Project A, I have a script runner post function to create this issue in project B (if it answers some condition, obviously).

I also fill out some of the fields of the "B-Task" which I can take from the "A-task", including the fixversion field.

the last thing I made is a JQL filter to display me issues pf project B where the fixversion is "xxx". 

So, here is the information I know:

  • The values (including fix version) are added by ScriptRunner scripts.
  • In my JIRA instance, there are multiple projects, using version naming conventions that are identical.
    • Eg, project_A has a version called 20190605, and Project_B has another version called 20190605.
  • As far as Jira is concerned, 20190605 in Project_A is not identical to 20190605 in Project_B. Jira is giving them different id's.
  • the JQL appears to be returning some results, of tasks manually opened in project B. the JQL does not return tasks opened by script runner.
    I have a strong feeling this is because Jira cannot recognize the versions added by ScriptRunner correctly.
  • I am sure that ScriptRunner is adding the wrong version. It is adding 20190605 (from A), to an issue in project_B, and this is causing the problem.
  • We also know that if values are added using the Jira UI manually, JQL results are correct.

 

The goal, of this event is this:

- to continue having the automatino of task creation (eliminating some of the manual task creation saves time)

- to use the JQL in the PMO dashboard (but I must make it return correct results first, results where I have the true fixversion of project B)

Please let me know your thoughts and if you have further questions.

3 answers

0 votes
Müller_ Julian October 2, 2020

@ofir gutmacher 
Did you ever made the copy of the fixVersion work? I have the exact same problem, copying works well, but the fix version from project A is simply copied to the issue in project B and is displayed, but does not work in Release View.

Müller_ Julian October 5, 2020

@ofir gutmacher 

I found a solution. I added a loop were the fixversion is cut into the different versions, saves as a string and after that i searched for this string in the projekt versions of the linked issue.

The problem was that we have the same release in different projects and my script copies version from the project A to project B with the false ID.

This is my new code:

// Check if fixVersion is updated
if (LastChangeDate != null){
LastChangeDateFormat = LastChangeDate.format("yyyy-MM-dd hh:mm:ss")
}
if (LastChangeDateFormat >= currentDate) {
log.debug("${issueKey} fixVersions changed ${LastChangeDate}")
def fixVersions = issue.getFixVersions()
log.debug("FixVersion DEV-Ticket lautet ${fixVersions}")

// Updating FixVersions in Customer Issue
if (issueLinkManager.getOutwardLinks(issue.getId()).findAll {it.issueLinkType.name == 'Customer Project'}) {
for (IssueLink link in issueLinkManager.getOutwardLinks(issue.getId()).findAll {it.issueLinkType.name == 'Customer Project'}) {
def destIssueCust = link.getDestinationObject()
def projectCust = destIssueCust.getProjectObject().getId();
def versionsCust = []
log.debug("Projekt = ${projectCust}")
for (version in fixVersions) {
String strVersion = version
versionsProject = versionManager.getVersion(projectCust, strVersion)
versionsCust.add(versionsProject)
}

destIssueCust.setFixVersions(versionsCust)
log.debug("FixVersion CUST-Ticket lautet ${destIssueCust.getFixVersions()}")
ComponentAccessor.getIssueManager().updateIssue(CurrentUser, destIssueCust, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_UPDATED, false);
log.debug("${issueKey} fixVersions changed in customer issue ${destIssueCust}")
}
}
ofir gutmacher December 3, 2020

nice

0 votes
ofir gutmacher September 25, 2019

here is the script listener, which is listeneing to issue update event on Project "A" called "Requirement".

So, If I am updating the "Project A req-issue" with a new fix version, then I am also updating a hidden fcustom field called "Requirement Version".

then, I am trying to update this hidden custom field called "Requirement Version" in all issues which have a link to this requirement in some way.

the comparison between the value in the "requirement version" field and the "fix version" field is what I have in a JQL FILTER-Dashboard gadget for the PMO TEAM.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.link.IssueLinkImpl;
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import groovy.xml.MarkupBuilder
import java.util.Collection
import org.apache.log4j.Category
import org.apache.log4j.Logger
import org.apache.log4j.Level

//def log = Logger.getLogger("com.acme.ListLinks")
def Category log = Category.getInstance("com.onresolve.jira.groovy")
//log.setLevel(Level.INFO)
log.info("Now adding version ")
def projectManager = ComponentAccessor.getProjectManager()
def versionManager = ComponentAccessor.getVersionManager()
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
def userManager = ComponentAccessor.getUserManager() as UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Requirement Version")
//Collection<Version> versionList = versionManager.getAllVersionsForProjects("REQ", false)
// get all requirmenets
def issue = (MutableIssue)event.issue
def version = issue.getFixVersions()
Collection<Version> IssueFixVersions = []

for (i in version) {
IssueFixVersions.add(i)
}
def SourceReqValue = event.issue.getCustomFieldValue(customField)
def userName="admin"
def adminuser = userManager.getUserByName(userName)
log.info("Now adding version " + version + " to issue " + issue.key + " old reqver " + SourceReqValue)
def changeHolder = new DefaultIssueChangeHolder();

log.info ("versions is "+version.toString())
log.info ("reqver is "+IssueFixVersions)
customField.updateValue(null, issue, new ModifiedValue(SourceReqValue, IssueFixVersions),changeHolder)
issueManager.updateIssue(adminuser, issue , EventDispatchOption.ISSUE_UPDATED, false)
SourceReqValue = event.issue.getCustomFieldValue(customField)
my_method(issue, adminuser, "Requirement", "Requirement", "Epic")




public String my_method(Issue issue, ApplicationUser adminuser, String linkTypeParam, String sourceIssueTypeParam, String destIssueTypeParam) {
def valid = false;
String results = "<br />";
//log.warn("Our issue is " + issue.id)
def linkMgr = ComponentAccessor.getIssueLinkManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Requirement Version")
def SourceReqValue = event.issue.getCustomFieldValue(customField)
def changeHolder = new DefaultIssueChangeHolder();
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
linkMgr.getOutwardLinks(issue.id)?.each {issueLink ->
//log.debug("In first clause")
def linkType = issueLink.getIssueLinkType() as IssueLinkType
def sourceObject = issueLink.sourceObject as MutableIssue
def sourceIssueType = sourceObject.getIssueType().getName()
def destinationObject = issueLink.destinationObject as MutableIssue
def destIssueType = destinationObject.getIssueType().getName()
if(linkType.name.contains(linkTypeParam) && sourceIssueType == sourceIssueTypeParam && destIssueType == destIssueTypeParam)
{
log.warn("link type: " + linkType.name +" between: " + sourceObject.key + " and " + destinationObject.key )
valid = true;
results += destinationObject.getKey() + "<br />"
log.debug("Results" + results)
def DestReqValue = destinationObject.getCustomFieldValue(customField)
log.warn("Source : " + SourceReqValue + " Destination: " + DestReqValue)
customField.updateValue(null, destinationObject, new ModifiedValue(DestReqValue, SourceReqValue),changeHolder)
issueManager.updateIssue(adminuser, destinationObject , EventDispatchOption.ISSUE_UPDATED, false)
my_method(destinationObject, adminuser, "Epic", "Epic", "Story")
my_method(destinationObject, adminuser, "Epic", "Epic", "Bug")
my_method(destinationObject, adminuser, "Epic", "Epic", "Task")
}
}
if(valid){
return results;
}
}
0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 23, 2019

You say

>I have a strong feeling this is because Jira cannot recognize the versions added by ScriptRunner correctly.

I suspect this is not quite what is happening.  I think it's actually because of your next thought:

>I am sure that ScriptRunner is adding the wrong version. It is adding 20190605 (from A), to an issue in project_B, and this is causing the problem.

I think this is the problem.  Versions in Jira are done by project, and you cannot use a version from one project in another.  Versions have distinct unique ids in the database, even if they have the same name, they are separate across projects.   I suspect your script is doing something like "find a version named 20190605", picking the first version object it finds, and stopping there, without checking for the same name in other projects, or it's going over the whole list and going with the last one it finds.  You're then using that version object to write to the issue.

Jira cannot handle that because you've ended up writing a version from one project on to an issue in another project.  That, by Jira's current design, is nonsense, so it fails.

I think you need to have a look at the code that is trying to identify the version to be written back.  Without seeing it, I am guessing there's a loop of some sort looking for it by name, which should be adjusted to search by name and project (of the issue you are going to write to). 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events