Forums

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

I am trying to update Component Lead using Groovy, but its not working as expected.

Mt4
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 7, 2022
components.each { eachComponent ->
    def componentLead = eachComponent.getComponentLead()
        def lead = eachComponent.getLead()
        if ( componentLead != null) {
        def userDirectory = componentLead.getDirectoryId()
        
         def componentName = eachComponent.getName()
         def user = componentLead.getUsername()
        Long lastLoginTime = loginManager.getLoginInfo(componentLead.username).getLastLoginTime()
       
                  
       
        if (!excludeListUsers.contains(componentLead.getUsername())
        && userDirectory != 1
       && componentLead.getUsername()!= "") {
       
                //log.error("user " + user)
         if(lastLoginTime!=null && lastLoginTime!= 0 )
        {
             
            Date lastLoginDate=new Date(lastLoginTime);
            Date today = new Date();
            def daysDifference= today - lastLoginDate;
            //log.error("Last login days" + daysDifference)
   //Last login date is more than 90 days
             if(daysDifference > 90)
                {
                   
                def complead = componentLead.getName()
                    // update Component
                    MutableProjectComponent newProjectComponent = MutableProjectComponent.copy(eachComponent);
                newProjectComponent.setLead("Component Lead Not Identified")
                    log.error("Last login days " + daysDifference)
                    log.error("Component " + newProjectComponent)
                //log.error("user " + user)
                //log.error("project "+ eachProject)

                 }
            }
 

3 answers

0 votes
Rinaldi James Michael
Contributor
March 21, 2023

(COMMENT DELETED)

0 votes
Christoph Beer June 15, 2022

Hi @Mt4 ,

your code brought me forward and I finished it updating the Component. Using the ProjectComponentManager to update the component is required at the end:

def projectComponentManager = ComponentAccessor.projectComponentManager
projectComponentManager.update(newProjectComponent)

 

But there are two additional points to take into account: 

1. The Component you copy must be the new component which shall be updated. Dependant on what components.each loops through, you maybe need to change it. If eachComponent is the source, then you will update the source component.  

MutableProjectComponent newProjectComponent = MutableProjectComponent.copy(eachComponent); 

 

2. the Parameter for setLead is a String but the String should match an existing username. 

newProjectComponent.setLead("username")

 

following this it worked for me.

 

regards

Christoph

Rinaldi Michael March 19, 2023

I have tried replacing a component with the below code. But nothing works:

This code tries to fetch a component from a user and replace the lead. Instead the field is left blank in the component.

//variable declaration for adding groups and projects

def userManager = ComponentAccessor.userManager

def projectManager = ComponentAccessor.projectManager

def userService = ComponentAccessor.getComponent(UserService)

def projectComponentManager = ComponentAccessor.projectComponentManager

String printtext



 

@UserPicker(label = 'New project lead', description = 'Enter a project lead to set for this project')

ApplicationUser user


def components = projectComponentManager.findComponentsByLead(user.getUsername())

for(int c=0;c<components.size();c++)

{

  MutableProjectComponent newProjectComponent = MutableProjectComponent.copy(components[c]);

  newProjectComponent.setName(components[c].getName())

  newProjectComponent.setLead("dummy_username")

  newProjectComponent.setDescription(components[c].getDescription())

  newProjectComponent.setAssigneeType(components[c].getAssigneeType())

  projectComponentManager.update(newProjectComponent)

}

return components[0].getComponentLead()
Rinaldi Michael March 20, 2023

Never mind. Got it to work. I had to pass the User Key instead of user name. 

Not sure what's going on. It works, so I'll take it XD

Shared the script that works for me in a new comment.

0 votes
Fabio Racobaldo _Herzum_
Community Champion
April 8, 2022

Hi @Mt4 ,

welcome to the Atllassian community!

https://docs.atlassian.com/software/jira/docs/api/7.0.5/com/atlassian/jira/bc/project/component/MutableProjectComponent.html#setLead-java.lang.String-

setLead should have a username as parameter and not a string like "Component Lead Not Identified". A component lead is a user.

Hope this helps,

Fabio

Mt4
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 14, 2022

Hi Fabio,

 

I tried with passing users as well, but still its not working.

 

Thanks!

Suggest an answer

Log in or Sign up to answer