...
Code Block | ||
---|---|---|
| ||
/* Script to hide the "Notes" field if the current logged in user is not in "Project Owner" role and show it if the user is in "Project Owner" role. User can be in multiple roles but if he/she is not part of "Project Owner" role field should not be shown. */ import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.security.roles.ProjectRoleManager import com.atlassian.jira.security.roles.ProjectRole import com.atlassian.jira.project.ProjectManager import com.atlassian.jira.user.util.UserManager //define the project role final String PROJECT_OWNER = "Project Owner"; //define the project name final String PROJECT_NAME = "TnT Jira Support Services"; //set flags to show and hide field final boolean NOTES_VISIBLE = true; final boolean NOTES_NOT_VISIBLE = false; //define the field to be shown/hidden final String NOTES = "Notes"; //get the current logged in user def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); //get ProjectManager class def projectManagerClass = ComponentAccessor.projectManager; def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager); //get the project role object of logged in user ProjectRole newRole = projectRoleManager.getProjectRole(PROJECT_OWNER); //get the project object of the project against which logged in user roles are checked def project = projectManagerClass.getProjectObjByName(PROJECT_NAME); //get the custom field def notesField = getFieldByName(NOTES); //check if the logged in user has a project role in a project, return boolean def projectRoles = projectRoleManager.isUserInProjectRole(loggedInUser, newRole, project); //condition to set the custom field hidden or shown if(projectRoles.equals(NOTES_VISIBLE)) { notesField.setHidden(NOTES_NOT_VISIBLE); } else { notesField.setHidden(NOTES_VISIBLE); } |
12. Script to create issues of particular issue type when a classic project is created (Cloud)
Code Block | ||
---|---|---|
| ||
/*
Script to create issues of a particular issue type when
a new project of classic type is created in JIRA cloud
*/
//get the project key on creation of project
def projectKey = project.key;
logger.info("The key of the project is: " + projectKey);
//get call for project data based on project key
def getProjectStyle = get("/rest/api/3/project/" + projectKey)
.header('Content-Type', 'application/json')
.asObject(Map)
//project style
def projectStyle = getProjectStyle.body.style;
logger.info("Project Style is: " + projectStyle);
//project id
def projectId = getProjectStyle.body.id;
logger.info("Project Id is: " + projectId);
//variable used to assign rest call, later in the code
def postCreateIssue;
//issue type id
def issueTypeId = "10002";
//account id of assignee
def accountIdOfAssignee = "5b4f068a3675c92cbbded6a2";
//project style classic
def projectStyleClassic = "classic";
//condition to create issues only if it is classic project
if(projectStyle == projectStyleClassic) {
postCreateIssue = post("/rest/api/3/issue/bulk")
.header("Content-Type", "application/json")
.body(
[
issueUpdates: [
[
fields: [
summary : "Create user profiles",
description: [
type: "doc",
version: 1,
content: [
[
type: "paragraph",
content: [
[
text: "Create profiles for students in directory",
type: "text"
]
]
]
]
],
project : [
id: projectId
],
issuetype : [
id: issueTypeId
],
assignee: [
id: accountIdOfAssignee
]
]
],
[
fields: [
summary : "Create logo",
description: [
type: "doc",
version: 1,
content: [
[
type: "paragraph",
content: [
[
text: "Create logos for the customer",
type: "text"
]
]
]
]
],
project : [
id: projectId
],
issuetype : [
id: issueTypeId
],
assignee: [
id: accountIdOfAssignee
]
]
],
[
fields: [
summary : "Create accounts",
description: [
type: "doc",
version: 1,
content: [
[
type: "paragraph",
content: [
[
text: "Create accounts for each student n staff",
type: "text"
]
]
]
]
],
project : [
id: projectId
],
issuetype : [
id: issueTypeId
],
assignee: [
id: accountIdOfAssignee
]
]
],
[
fields: [
summary : "Create properties",
description: [
type: "doc",
version: 1,
content: [
[
type: "paragraph",
content: [
[
text: "Create properties for students and staff",
type: "text"
]
]
]
]
],
project : [
id: projectId
],
issuetype : [
id: issueTypeId
],
assignee: [
id: accountIdOfAssignee
]
]
],
[
fields: [
summary : "Assign and implement",
description: [
type: "doc",
version: 1,
content: [
[
type: "paragraph",
content: [
[
text: "Assign and implement the case",
type: "text"
]
]
]
]
],
project : [
id: projectId
],
issuetype : [
id: issueTypeId
],
assignee: [
id: accountIdOfAssignee
]
]
]
]
])
.asString()
} else {
logger.info("Project doesn't belong to classic style");
}
//success or failure of issue creation based on condition
if(projectStyle == projectStyleClassic && postCreateIssue.status == 201) {
logger.info("Successfully added issues to project with key: ", projectKey);
} else {
logger.error("Failed to bulk add issues to project with key: ", projectKey);
} |
Learning:
Knowledge on how script-runner listener works for both cloud and server
Got to understand different scenarios for Jira based on which scripts were written
Basic knowledge on groovy
While setting the value for multi-select system fields always set the values based on “id’s of values” and not “values itself“ in behaviors
While setting the value for single select system fields we can directly give “values” instead of “id’s of values”
script-runner behaviors
While setting the value for single select custom field we have to give “id’s of values” only (can also be directly configured in field setting as these are custom fields)
...