Forums

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

Get component's attributes (Name, ID, etc)

Brenda August 20, 2019

Hi,

I need to set a description value if one specific component is selected, I'm using a behaviour for this (in Component field). I arrive to get the object "component", but I can't get its attributes separately, here is my code:

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager


def ComponentCF = getFieldById(getFieldChanged())
def desc = getFieldById("description")
def ComponentVal = ComponentCF.getValue() as String


if ( ComponentVal.name == "ALM - Services - User Management") // "ComponentVal.name" doesn't work

{

def defaultValue = """

---* Please provide following information in order to treat your access request *---

Email address:

IPN:

Business Unit(e.g. DEA-XXX, SWL):

Role:

Why you need this access?:

""".replaceAll(/ /, '')

 

if (! underlyingIssue?.description && !underlyingIssue?.created)
{
desc.setFormValue(defaultValue)
}
}

 

I've checked the value I have in "ComponentVal"  and I have this:

[ProjectComponentImpl

{ name='ALM - Services - User Management', description='Services related to user management across tools', lead='', assigneeType='3', projectId='10804', id='19328' }

]

3 answers

1 accepted

1 vote
Answer accepted
Andrew
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.
August 20, 2019

Hi @Brenda ,

def ComponentVal = ComponentCF.getValue() as String

That mean it is just string, sure You cannot get String.name property.

Maybe ComponentCF.getValue() it is Map. For check that could You please show ComponentCF.getClass() ?

Could You please check next?

def ComponentVal = ComponentCF.getValue()

if ( ComponentVal.name == "ALM - Services - User Management")

or

if ( ComponentVal[name] == "ALM - Services - User Management")

B.R.

Brenda August 20, 2019

Hi @Andrew  :)

it didn't work :(

Andrew
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.
August 20, 2019

Hi 

Next work for me. Set desc if component 'B' not exist in issue and we add it component.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.Issue

//check taht issue exist
if (underlyingIssue) {

def ComponentCF = getFieldById(getFieldChanged())
def desc = getFieldById("description")
def ComponentVal = ComponentCF.getValue()
def issueManager = ComponentAccessor.getIssueManager()
def originalIssue = issueManager.getIssueObject(underlyingIssue.id)

// if previous state not have B AND if added value have B
if ((!originalIssue.getComponents().find{ it.getName() == "B"}) && (ComponentCF.getValue().find{ it.getName() == "B"})){ // "ComponentVal.name" doesn't work
//log.error('tesasdfsadfdsg434t: Hurraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')


def defaultValue = """

---* Please provide following information in order to treat your access request *---

Email address:

IPN:

Business Unit(e.g. DEA-XXX, SWL):

Role:

Why you need this access?:

""".replaceAll(/ /, '')


desc.setFormValue(defaultValue)
}
/* I change and move that check to up for make test simpler
if ( !underlyingIssue?.description && !underlyingIssue?.created )
{
desc.setFormValue(defaultValue)
}*/
}

 

B.R.

Brenda August 21, 2019

Hi @Andrew  thx again for your help!

I can't test your code, getName() isn't recognized :( ...

I have the following error: "Cannot find matching method" 

Andrew
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.
August 21, 2019

Hi,

Really no matter I also have that message, You can just ignore that.

I resolve one part if modify:

import com.atlassian.jira.bc.project.component.ProjectComponent

Collection<ProjectComponent> comps = originalIssue.getComponents()

if ((!comps.find{ it.getName() == "B"}) && (ComponentCF.getValue().find{ it.getName() == "B"})){

For the 'comps' error has gone, but I cannot find for ComponentCF.getValue(). 

How I understand ComponentCF.getValue() should return List, but IDE think that return Object and I don't know why (

B.R.

Shannon Davis
Contributor
November 19, 2020

I think this is the solution to my problem but I read all this and it doesn't make sense to me.  I know components returns an object.   

 

I'm trying to do something similar - at this point, I know there will only be one component, so I know it will be the first one.  Feels like there has got to be an easier way to do something like this:

 

def componentField = getFieldById(getFieldChanged());


def comp = componentField.getValue();

//somehow get the one component out of  comp  but nothing is working

if (comp == 'Integrations') { defaultValue += integrationsDefaultValue}
if (comp == 'Maestro') { defaultValue += maestroDefaultValue;}
if (comp == 'Application') {defaultValue += appsdefaultValue;}
if (comp == 'MS-SQL Database') { defaultValue += dbDefaultValue;}
if (comp == 'Oracle Database') { defaultValue += dbDefaultValue;}
if (comp == 'SAP Transport' ) {defaultValue += sapDefaultValue;}

Shannon Davis
Contributor
November 19, 2020

Found a different way to do it.

 

import com.atlassian.jira.component.ComponentAccessor;
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.bc.project.component.ProjectComponent
@BaseScript FieldBehaviours fieldBehaviours

def defaultValue = """Background Justification:
""".replaceAll(/ /,'');
def integrationsDefaultValue = """ integrations fields here """.replaceAll(/ /,'');
def maestroDefaultValue= """
Server:
Schedule:
Job: """.replaceAll(/ /,'');
def sapDefaultValue = """
SAP System(s):
SAP Transport Numbers (In Sequence):""".replaceAll(/ /,'');
def dbDefaultValue = """
Database:
Database Server:
"""
def appsdefaultValue = """"
Server(s):
Code Repository:
""".replaceAll(/ /,'');

def componentField = getFieldById(getFieldChanged());
def components = componentField.getValue() as List<ProjectComponent>

def comp = components.first().name;

if (comp == 'Integrations') { defaultValue += integrationsDefaultValue}
if (comp == 'Maestro') { defaultValue += maestroDefaultValue;}
if (comp == 'Application') {defaultValue += appsdefaultValue;}
if (comp == 'MS-SQL Database') { defaultValue += dbDefaultValue;}
if (comp == 'Oracle Database') { defaultValue += dbDefaultValue;}
if (comp == 'SAP Transport' ) {defaultValue += sapDefaultValue;}


def descField = getFieldById("description");
if (! underlyingIssue?.description) {
descField.setFormValue(defaultValue);
}

2 votes
Brenda August 22, 2019

@Andrew  it woooooooorks!! ^^

Thank you =D

0 votes
Salim Hammar
Contributor
February 21, 2022

Hello @Brenda  @Shannon Davis  @Andrew  I need very help

This script will serve when i create , update and delete the component these features will apply in any project (with project key) 

 

Actually when i edit a name for the component it's working without issue but when i change the lead the component (edit) it goes on a loop and after i don't access the page component 

 

My opinion the problem it's : "destComponent = projectComponentManager.findByComponentName(project.id, oldComponent.name as String)" because he updated the name only but i don't know for add the other attributes 

that is to say (name , description, lead and assignee) 

You have idea ??

--------------------------------------------------------------------------------

def dest_prj = ['TS', 'TD', 'TK']
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.bc.project.component.ProjectComponentCreatedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentDeletedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentUpdatedEvent
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent

def projectManager = ComponentAccessor.projectManager
def projectComponentManager = ComponentAccessor.projectComponentManager

ProjectComponent component = event.projectComponent
log.info "Deletected ${event.getClass()} for $component.name ($component.id)"
dest_prj.each { String destProjectKey ->
log.info "Getting project object from dest_prj: $destProjectKey"
def project = projectManager.getProjectObjByKey(destProjectKey)
log.info "Project object found: $project"
def destComponent = projectComponentManager.findByComponentName(project.id, component.name)
log.info "Attempted to identify matching destination component in $project using name=$component.name : $destComponent"
if (event instanceof ProjectComponentCreatedEvent) {
if (!destComponent) {
log.info "Attempting to create $component.name into $project"

projectComponentManager.create(component.name, component.description, component.lead, component.assigneeType, project.id)
} else {
log.error "Can't create a copy of $component.name in $destProjectKey project. A component with that name alreay exist."
}
}
if (event instanceof ProjectComponentUpdatedEvent) {
def oldComponent = (event as ProjectComponentUpdatedEvent).oldProjectComponent as ProjectComponent
if (event instanceof ProjectComponentUpdatedEvent) {
destComponent = projectComponentManager.findByComponentName(project.id, oldComponent.name as String)
log.info "Attempted to identify matching destination component in $project using name=$oldComponent.name : $destComponent"
}

if (destComponent) {
log.info "Attempting to update $destComponent.name ($destComponent.id) from $project project using data from $component.name ($component.id)"
destComponent = MutableProjectComponent.copy(destComponent)
destComponent.with {
name = component.name
description = component.description
lead = component.lead
assigneeType = component.assigneeType //or use component.assigneeType
}
projectComponentManager.update(destComponent)
} else {
log.warn "No component found in $destProjectKey with name $oldComponent.name. Can't perform an update."
}
}

if (event instanceof ProjectComponentDeletedEvent) {
log.info "Detected Component Deletion event for $component.name"
if (destComponent) {
log.info "Attempting to detelete $destComponent.name ($destComponent.id) from $project project"
projectComponentManager.delete(destComponent.id)
} else {
log.warn "No component found in $destProjectKey with name $component.name. Can't delete it."
}
}
}

--------------------------------------------------------

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events