Get the Component Lead and add ScriptRunner Cloud

Daniel Alonso April 20, 2021

Hi,

I'm trying to implement a post-function in ScriptRunner Cloud that gets the Component Lead and set it as Approver, I have that script in SR Server:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.jira.user.util.Users
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

def sb = new StringBuffer() //sb is used to send string to logs
//Set the Project Roles readers/objects
ProjectRoleService projectRoleService = (ProjectRoleService) ComponentAccessor.getComponentOfType(ProjectRoleService.class);
ProjectRoleManager projectRoleManager = (ProjectRoleManager) ComponentAccessor.getComponentOfType(ProjectRoleManager.class);
ProjectManager projectManager = ComponentAccessor.getProjectManager();

ErrorCollection ec = new SimpleErrorCollection();
//Create the Global definitions
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def project
def userPropertyManager = ComponentAccessor.userPropertyManager
//Create the Specific definitions

def userList = new ArrayList();

def components = issue.componentObjects.toList()
if (components) {
userList.add(components?.first()?.componentLead)
}
//Prepare the field to set the values
CustomField customFieldApprovers = customFieldManager.getCustomFieldObjectByName("Approvers");
//set the value
issue.setCustomFieldValue(customFieldApprovers, userList)
//author of the change
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//Submit the change
issueManager.updateIssue(user,issue,EventDispatchOption.ISSUE_UPDATED,true);

But I don't have enough experience to implement it on Cloud.

Can someone help with the translation? Any suggestions?

1 answer

1 accepted

1 vote
Answer accepted
Kate Kabir
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.
April 21, 2021

Hi @Daniel Alonso 

Thank you for the question.

In order for you to get the Component from the issue and by call Get Component API as documented here. To do this you would need the ID of the component which you can get from the components filed on the issue. Then to set the approver field update. 

We in ScriptRunner for Jira Cloud have an example to get the component issue and update it with the Approver field value here. You can use the sample code as a guide to get the result/requirement that you want. 

Hope this will help you.

Thank you.

Kind Regards

Kate

Daniel Alonso April 22, 2021

Thanks Kate,

I will try that Snippet, is there a way I see more examples? The Library is very limited. Thanks.

Kate Kabir
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.
April 22, 2021

Hi @Daniel Alonso 

Yes, we do have other examples that you can refer to as guidance:

Hope this will help with your integration.

Thank you

Kind Regards

Kate

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events