Forums

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

if(newIssueproject.getVersions().contains(v)) always returns false although it should be true

Mouna Hammoudi
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 22, 2023

I am trying to create a new version only if a specific project has the same versions of the current issue. Here it is made in the following code block:

for( Version v: issue.getFixVersions()){

                        if(newIssueproject.getVersions().contains(v)){

                                 Version mynewVersion= ComponentAccessor.versionManager.createVersion(v.getName()+"-Inbox", startDate, releaseDate, description, newIssueproject.id, scheduleAfterVersion, released)

                                 mynewVersions.add(mynewVersion)

                        }

 

The problem is that               if(newIssueproject.getVersions().contains(v)) always returns false although it should be true sometimes. As a result, I always have versions that are null. So how can this be fixed?

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def authenticationContext = ComponentAccessor.jiraAuthenticationContext

def issueFactory = ComponentAccessor.getIssueFactory()

def issueManager = ComponentAccessor.getIssueManager()

def userManager = ComponentAccessor.getUserUtil()

 def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

long issueLinkType= 10070 as long

long sequence =1 as long

long newissueTypeID= 19 as long

long myissueID= issue.getId() as long

// the key of the project under which the version will get created

 final String projectKey = "SF"

// the start date - optional

final Date startDate = null

// the release date - optional

final Date releaseDate = null

// a description for the new version - optional

final String description = null

// id of the version to schedule after the given version object - optional

final Long scheduleAfterVersion = null

// true if this is a released version

final boolean released = false

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)

assert project : "Could not find project with key $projectKey"

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

         int counter=0

        for(ProjectComponent component: componentList){

                    MutableIssue newissue= ComponentAccessor.issueFactory.getIssue()

                                        //set values

                    newissue.setProjectObject(issue.projectObject)

                    newissue.setIssueTypeId("19")

                    newissue.setSummary("MOUNA CAMELIA")

                    newissue.setDescription("MOUNA CAMELIA")

                    newissue.reporter = issue.getReporter()

                    String componentName= component.getName()

                    log.warn("MOUNA CAMELIA HELLO 5 "+component)

                    def shortenedComponentName = componentName.substring(componentName.indexOf("-")+1)

                    def projectName = componentName.substring(0, componentName.indexOf("-"))

                    log.warn("MOUNA CAMELIA HELLO 6 projectName--"+projectName+"--" )

                    def newIssueproject = ComponentAccessor.projectManager.getProjectObjByKey(projectName)

                    newissue.setProjectObject(newIssueproject)

                    log.warn("MOUNA CAMELIA ISSUE GET VERSIONS "+newIssueproject.getVersions()+"-========= "+issue.getFixVersions())

                    def mynewVersions = new ArrayList <Version> ()

                    for( Version v: issue.getFixVersions()){

                        if(newIssueproject.getVersions().contains(v)){

                                 Version mynewVersion= ComponentAccessor.versionManager.createVersion(v.getName()+"-Inbox", startDate, releaseDate, description, newIssueproject.id, scheduleAfterVersion, released)

                                 mynewVersions.add(mynewVersion)

                        }

               

                    }

                    newissue.setFixVersions(mynewVersions)

 

1 answer

0 votes
Aron Gombas _Midori_
Community Champion
September 22, 2023

In Java, the Collection.contains() method relies on the equals() method of the item's class. It can lead to false results if the equality is not implemented correctly.

In this case, I think that the Version class does not implement equals() as you would expect.

Maybe you could just implement your own equality based on the ID. 

Mouna Hammoudi
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 22, 2023

When I use the ID, the result of the condition is always null, it seems that it is not possible for a project and an issue to have the same version ID so I ended up using the name instead, is this OK? It seems that it is impossible for a project and an issue to have the same versions IDs. I have tried many inputs and it is never working. 

 

Here is my new code: 

 log.warn("MOUNA CAMELIA ISSUE GET VERSIONS "+newIssueproject.getVersions()+"-========= ")

                    List<String> newIssueProjectVersionIDs= new ArrayList<String>()

                    for(Version myver: newIssueproject.getVersions()){

                            newIssueProjectVersionIDs.add(myver.getName())

                    }

                     log.warn("MOUNA CAMELIA newIssueProjectVersionIDs "+newIssueProjectVersionIDs+"-========= ")

                    def mynewVersions = new ArrayList <Version> ()

                    for( Version v: issue.getFixVersions()){

                        log.warn("MOUNA CAMELIA VERSION V "+v.getName())

                        if(newIssueProjectVersionIDs.contains(v.getName())){

                                 Version mynewVersion= ComponentAccessor.versionManager.createVersion(v.getName()+"-Inbox", startDate, releaseDate, description, newIssueproject.id, scheduleAfterVersion, released)

                                 mynewVersions.add(mynewVersion)

                                 log.warn("MOUNA CAMELIA TRUE")

                        }else{

                               log.warn("MOUNA CAMELIA FALSE")

                        }

               

                    }

Suggest an answer

Log in or Sign up to answer