Forums

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

How to Automatically create Sub - task using script runner post function?

Chander Korshika
Contributor
July 1, 2019

Can someone help me in modifying below sample custom script that you can use in order to create a number of subtasks .My requirement is to create as per the value of customfield.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def constantManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().get
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()

Issue parentIssue = issue

if (parentIssue.getIssueTypeObject().getName() == 'Sub-task')
return

if (parentIssue.getIssueTypeObject().name != 'Feature')
return

def summariesList = ["Summary 1", "Summary 2", "Summary 3"]

// code in order to create the values to update a CascadingSelect custom field
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("CascadingSelect")
def fieldConfig = cascadingSelect?.getRelevantConfig(parentIssue)
def listOfOptions = ComponentAccessor.getComponent(OptionsManager).getOptions(fieldConfig)
def parentOption = listOfOptions?.find {it.value == "AAA"}
def childOption = parentOption?.getChildOptions()?.find {it.value == "A2"}

def mapWithValues = [:]
mapWithValues.put(null, parentOption)
mapWithValues.put('1', childOption)

summariesList.each {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setSummary(it)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
// Add any other fields you want for the newly created sub task
newSubTask.setCustomFieldValue(cascadingSelect, mapWithValues)

def newIssueParams = ["issue" : newSubTask] as Map<String,Object>

//for JIRA v6.*
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
// for JIRA v7.*
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)

log.info "Issue with summary ${newSubTask.summary} created"
}

 

9 answers

2 votes
Chander Korshika
Contributor
April 11, 2023 edited

Its Resolved

1 vote
Chander Korshika
Contributor
September 7, 2024

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity.ShareType
import com.atlassian.jira.sharing.ShareType.Name
import com.atlassian.jira.sharing.ShareTypeFactory
import com.atlassian.jira.sharing.SharedEntityAccessor
import com.atlassian.jira.sharing.SharedEntity
import com.atlassian.jira.sharing.SharePermission
import com.atlassian.jira.sharing.SharedEntityManager
import com.atlassian.jira.sharing.search.SharedEntitySearchParameters
import com.atlassian.jira.dashboard.DashboardManager

// Get the DashboardManager
def dashboardManager = ComponentAccessor.getComponent(DashboardManager)
def sharedEntityManager = ComponentAccessor.getComponent(SharedEntityManager)
def shareTypeFactory = ComponentAccessor.getComponent(ShareTypeFactory)

// Replace with your dashboard ID and group names
def dashboardId = 10001L // The ID of the dashboard to which permissions will be added
def groups = ["jira-software-users", "jira-administrators"] // List of groups to grant permissions to

def dashboard = dashboardManager.getDashboard(dashboardId)

if (dashboard) {
groups.each { groupName ->
// Create a share permission for the group
def shareType = shareTypeFactory.getShareType(Name.GROUP)
def groupPermission = new SharePermissionImpl(shareType, groupName)

// Add the permission to the dashboard
def sharedEntity = sharedEntityManager.updateSharePermissions(dashboard.id, [groupPermission])
log.warn("Added permission for group: ${groupName} to dashboard: ${dashboard.getName()}")
}
} else {
log.warn("Dashboard with ID ${dashboardId} not found.")
}

0 votes
Chander Korshika
Contributor
September 8, 2024

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

// Replace with your dashboard ID and group names
def dashboardId = 10001L // The ID of the dashboard to which permissions will be added
def groups = ["jira-software-users", "jira-administrators"] // List of groups to grant permissions to

def baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
def restApiUrl = "${baseUrl}/rest/api/3/dashboard/${dashboardId}/permissions"

def httpClient = ComponentAccessor.getComponent(com.atlassian.sal.api.net.RequestFactory).createRequest(com.atlassian.sal.api.net.Request.Method.PUT, restApiUrl)

def authHeader = "Basic " + "YOUR_ENCODED_CREDENTIALS" // Replace with your base64-encoded credentials
httpClient.addHeader("Authorization", authHeader)
httpClient.addHeader("Content-Type", "application/json")

// Fetch current permissions
def response = httpClient.execute()
def currentPermissions = new JsonSlurper().parseText(response.getResponseBodyAsString())

// Add new group permissions
def newPermissions = currentPermissions.findAll { it.type != "group" } // Exclude existing group permissions
groups.each { groupName ->
def sharePermission = [
type: "group",
group: groupName,
permissions: ["VIEW"]
]
newPermissions.add(sharePermission)
}

// Update permissions via REST API
def body = JsonOutput.toJson(newPermissions)
httpClient.setRequestBody(body)

response = httpClient.execute()
if (response.status >= 200 && response.status < 300) {
log.warn("Successfully updated permissions for dashboard ID ${dashboardId}.")
} else {
log.warn("Failed to update permissions for dashboard ID ${dashboardId}. Status: ${response.status}")
}

0 votes
Chander Korshika
Contributor
September 7, 2024

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortalPageManager
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity.SharePermissions
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.jira.portal.PortalPage

// Get the PortalPageManager for dashboard operations
def portalPageManager = ComponentAccessor.getComponent(PortalPageManager)

// Replace with your dashboard ID and group names
def dashboardId = 10001L // The ID of the dashboard to which permissions will be added
def groups = ["jira-software-users", "jira-administrators"] // List of groups to grant permissions to

// Retrieve the dashboard using its ID
def dashboard = portalPageManager.getPortalPageById(dashboardId)

if (dashboard) {
// Get current share permissions from the dashboard
def currentPermissions = dashboard.getPermissions()

// Convert current permissions to mutable set
def updatedPermissions = new HashSet<>(currentPermissions.getPermissions())

// Add new group permissions
groups.each { groupName ->
def sharePermission = new SharePermissionImpl(ShareType.Name.GROUP, groupName)
updatedPermissions.add(sharePermission)
log.warn("Adding permission for group: ${groupName} to dashboard: ${dashboard.getName()}")
}

// Set updated permissions
def newPermissions = new SharePermissions(updatedPermissions)
dashboard.setPermissions(newPermissions)

// Save the dashboard with updated permissions
portalPageManager.updatePortalPage(dashboard)

log.warn("Dashboard ${dashboard.getName()} updated with new permissions.")
} else {
log.warn("Dashboard with ID ${dashboardId} not found.")
}

0 votes
Chander Korshika
Contributor
September 7, 2024

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortalPageManager
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.type.ShareType

// Get the PortalPageManager for dashboard operations
def portalPageManager = ComponentAccessor.getComponent(PortalPageManager)

// Replace with your dashboard ID and group names
def dashboardId = 10001L // The ID of the dashboard to which permissions will be added
def groups = ["jira-software-users", "jira-administrators"] // List of groups to grant permissions to

// Retrieve the dashboard using its ID
def dashboard = portalPageManager.getPortalPageById(dashboardId)

if (dashboard) {
groups.each { groupName ->
// Create a share permission for the group
def sharePermission = new SharePermissionImpl(ShareType.Name.GROUP, groupName)

// Fetch the current permissions for the dashboard
def currentPermissions = portalPageManager.getSharePermissions(dashboard)

// Add the new permission to the list of current permissions
def updatedPermissions = new ArrayList<>(currentPermissions)
updatedPermissions.add(sharePermission)

// Update the dashboard's share permissions
portalPageManager.updateSharePermissions(dashboard, updatedPermissions)
log.warn("Added permission for group: ${groupName} to dashboard: ${dashboard.getName()}")
}
} else {
log.warn("Dashboard with ID ${dashboardId} not found.")
}

0 votes
Chander Korshika
Contributor
September 7, 2024

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharePermission
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.jira.sharing.SharedEntityAccessor
import com.atlassian.jira.sharing.SharedEntity

// Get the SharedEntityAccessor
def sharedEntityAccessor = ComponentAccessor.getComponent(SharedEntityAccessor)

// Replace with your dashboard ID and group names
def dashboardId = 10001L // The ID of the dashboard to which permissions will be added
def groups = ["jira-software-users", "jira-administrators"] // List of groups to grant permissions to

// Retrieve the dashboard using its ID
def dashboard = sharedEntityAccessor.getSharedEntity(SharedEntity.Type.DASHBOARD, dashboardId)

if (dashboard) {
groups.each { groupName ->
// Create a share permission for the group
def sharePermission = new SharePermissionImpl(ShareType.Name.GROUP, groupName)

// Fetch the current permissions for the dashboard
def currentPermissions = sharedEntityAccessor.getSharePermissions(dashboard)

// Add the new permission to the list of current permissions
def updatedPermissions = new ArrayList<>(currentPermissions)
updatedPermissions.add(sharePermission)

// Update the dashboard's share permissions
sharedEntityAccessor.updateSharePermissions(dashboard, updatedPermissions)
log.warn("Added permission for group: ${groupName} to dashboard: ${dashboard.getName()}")
}
} else {
log.warn("Dashboard with ID ${dashboardId} not found.")
}

0 votes
Chander Korshika
Contributor
August 23, 2024

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueManager

import java.io.File
import java.io.FileWriter

// Get required managers
def projectManager = ComponentAccessor.getProjectManager()
def issueManager = ComponentAccessor.getIssueManager()

// Define the file path (update this to your desired path)
def filePath = "/path/to/your/downloads/jira_project_details.csv"

// Create a file and writer
File file = new File(filePath)
FileWriter writer = new FileWriter(file)

// Write the header row
writer.write("Project Name,Project Key,Project Description,Project Lead,Last Issue Update,Project Category,Issue Count\n")

// Fetch all projects
def projects = projectManager.getProjectObjects()

// Iterate through projects and write the details to the CSV file
projects.each { project ->
def projectName = project.getName() ?: "No Name"
def projectKey = project.getKey() ?: "No Key"
def projectDescription = project.getDescription() ?: "No Description"

// Get project lead
def projectLead = project.getProjectLead()?.getDisplayName() ?: "No Lead"

// Get the number of issues in the project
def issueCount = issueManager.getIssueIdsForProject(project.getId()).size()

// Get the last issue update
def lastIssueUpdate = issueManager.getIssueIdsForProject(project.getId())
.collect { issueManager.getIssueObject(it).getUpdated() }
.max() ?: "No Issues"

// Get project category
def projectCategoryName = project.getProjectCategory()?.getName() ?: "No Category"

// Write the project details to the CSV file
writer.write("${projectName},${projectKey},${projectDescription},${projectLead},${lastIssueUpdate},${projectCategoryName},${issueCount}\n")
}

// Close the writer
writer.close()

log.warn("Project details have been written to ${filePath}")

0 votes
Chander Korshika
Contributor
August 23, 2024

import com.atlassian.jira.component.ComponentAccessor

// Get the ProjectManager instance
def projectManager = ComponentAccessor.getProjectManager()

// Fetch all projects
def projects = projectManager.getProjectObjects()

// Iterate through projects and print the required details
projects.each { project ->
def projectName = project.getName() ?: "No Name"
def projectKey = project.getKey() ?: "No Key"
def projectDescription = project.getDescription() ?: "No Description"

// Print the project details
log.warn("Project Name: ${projectName}, Project Key: ${projectKey}, Project Description: ${projectDescription}")
}

0 votes
Chander Korshika
Contributor
August 22, 2024 edited

..

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira, marketing project management, jira for marketers, agile marketing, campaign planning, marketing workflows, jira templates, atlassian learning, marketing collaboration, project tracking, marketing teams, jira training

Streamline Marketing in Jira 🖼️

Plan, prioritize, and deliver marketing projects seamlessly. See how Jira supports your team's success. Start today!

Start now! 🚦
AUG Leaders

Atlassian Community Events