Hello Atlassian Community!
Today, I'll guide you through creating multiple projects and issue in Jira Cloud using ScriptRunner. These steps allow you to swiftly create projects.
def projects = [[
projectName: "Enter the Project Name",
projectKey: "TEST",
projectInfo: "Project info for Project",
storyAssignee: "39dcefe3652075690hb2", // it is an user's account Id
storyReporter: "39dcefe3652075690hb2",
leadAccountId: "39dcefe3652075690hb2"
],
[projectName: "Enter the Project Name 2",
projectKey: "TEST-2",
projectInfo: "Project info for Project 2",
storyAssignee: "39dcefe3652075690hb2",
storyReporter: "39dcefe3652075690hb2",
leadAccountId: "39dcefe3652075690hb2",
]
// ...other projects
]
projects.each { project ->
def payload = [ // Body for new project
key: project.projectKey,
leadAccountId: project.leadAccountId,
fieldConfigurationScheme: 10001,
issueTypeScheme: 10300,
issueTypeScreenScheme: 10001,
name: project.projectName,
workflowScheme: 10010,
projectTypeKey: "business",
assigneeType: "UNASSIGNED",
notificationScheme: 10001,
permissionScheme:10001,
categoryId:10001,
description: "This issue was created by ScriptRunner for Jira."
]
// API call to create project
def result = post("/rest/api/3/project")
.header('Content-Type', 'application/json')
.body(payload)
.asString()
// Get the ID of the created project
def projectId = new groovy.json.JsonSlurper().parseText(result.body).id
// Body content for "Story" within the new project
def issueData = [fields:
[ project: [ id: projectId ],
summary: "${payload.name}",
description: [ "type": "doc", "version": 1,
"content": [ [ "type": "paragraph",
"content": [ [ "type": "text",
"text": "${payload.description}" ] ]
]]],
issuetype:[ id: 10009 // Issue type ID for Story ],
reporter: [ id: "${projects.storyReporter}" ],
assignee: [ id: "${projects.storyAssignee}" ]
]]
// Creating a Story within the New Project using an API call
def createdStoryResponse = post("/rest/api/3/issue")
.header('Content-Type', 'application/json')
.body(issueData) .asString()
}
Firstly, Let's start by creating the project information in the projects array:
def projects = [[
projectName: "Enter the Project Name",
projectKey: "TEST",
projectInfo: "Project info for Project",
storyAssignee: "39dcefe3652075690hb2", // it is an user's account Id
storyReporter: "39dcefe3652075690hb2",
leadAccountId: "39dcefe3652075690hb2"
],
[projectName: "Enter the Project Name 2",
projectKey: "TEST-2",
projectInfo: "Project info for Project 2",
storyAssignee: "39dcefe3652075690hb2",
storyReporter: "39dcefe3652075690hb2",
leadAccountId: "39dcefe3652075690hb2",
]
// ...other projects
]
It iterates through the array, making necessary API calls for each project.
projects.each { project ->
// Body for new project
def payload = [
key: project.projectKey,
leadAccountId: project.leadAccountId,
fieldConfigurationScheme: 10001,
issueTypeScheme: 10300,
issueTypeScreenScheme: 10001,
name: project.projectName,
workflowScheme: 10010,
projectTypeKey: "business",
assigneeType: "UNASSIGNED",
notificationScheme: 10001,
permissionScheme:10001,
categoryId:10001,
description: "This issue was created by ScriptRunner for Jira."
]
// API call to create project
def result = post("/rest/api/3/project")
.header('Content-Type', 'application/json')
.body(payload)
.asString()
This code snippet serves two main purposes within the script:
// Get the ID of the created project
def projectId = new groovy.json.JsonSlurper().parseText(result.body).id
// Body content for "Story" within the new project
def issueData = [fields:
[ project: [ id: projectId ],
summary: "${payload.name}",
description: [ "type": "doc", "version": 1,
"content": [ [ "type": "paragraph",
"content": [ [ "type": "text",
"text": "${payload.description}" ] ]
]]],
issuetype:[ id: 10009 // Issue type ID for Story ],
reporter: [ id: "${projects.storyReporter}" ],
assignee: [ id: "${projects.storyAssignee}" ]
]]
Get the Project ID: After creating a project, this section retrieves the unique identifier (ID) to the newly created project. It uses the JsonSlurper() method to parse the response body obtained from the API call that creates the project.
Creating Story Issues: Here, the code prepares data for creating a "Story" issue within the newly created project. It structures the necessary information such as the project ID (projectId
), summary, description, issue type, reporter, and assignee. This data will be used to make an API call to create a "Story" issue associated with the newly created project.
These sections of the code are essential for linking the newly created project with a specific issue type "Story" and defining initial details for this issue within that project. This helps in organizing and tracking relevant information within the Jira environment.
This code snippet is responsible for making an API call to create a Story within a newly created project in Jira.
// Creating a Story within the New Project using an API call
def createdStoryResponse = post("/rest/api/3/issue")
.header('Content-Type', 'application/json')
.body(issueData) .asString()
I hope this guide assists you in managing multiple projects and issue efficiently in Jira Cloud using ScriptRunner! Feel free to ask any questions if you need further assistance.
Best regards,
Murat Seven
Atlassian Technical Consultant at @Snapbytes
Snapbytes
Ankara
27 accepted answers
0 comments