2

I would like to clone an existing issue in JIRA using REST APIs in Python, but also would like to change the project and issue type when I clone it. How could I accomplish this?

Any help is much appreciated.

Thanks!

2 Answers2

5

I do not believe clone is an available endpoint, however even if it was I know "moving" an issue to another project and/or issue type is not an endpoint. The way you can get around this is by doing the following.

1: Read in the values of a Jira ticket and store the important fields you want to retain as variables

2: Send a request to Jira to create a new ticket and pass in the values you wanted to retain which would likely be Summary, Description, Assignee, Reporter, and any other fields you use.

I use the Jira library to assist with the requests https://jira.readthedocs.io/en/master/

Example code:

import configuration #.py file that I store my username/password/token/server domain
from jira import JIRA
    
# Initialize Jira
jira = JIRA(
    basic_auth=(configuration.JIRAProdUsername, configuration.JIRAProdToken),
    options = {'server':'https://'+configuration.JIRAProdDomain+'.atlassian.net'}
)
    
# Search for issues to be updated
issue_search_convert = jira.search_issues("SOME JQL FILTER THAT FINDS THE ISSUES YOU WANT TO CLONE", maxResults=75)

# Create New Jira Tickets
for key in issue_search_convert:
    issue = jira.issue(key)
    issue_description = issue.fields.description
    issue_summary = issue.fields.summary    
    jira_dict_convert = {
        'project': {'key': 'ITS'},
        'summary': issue_summary,
        'assignee': {'name': 'User1@example.com'},
        'reporter': {'name': 'User2@example.com'},
        'issuetype': {'name': 'NameOfIssueType'},
        'description': issue_description,
        'components': [{'name': 'Component'}],
        'customfield_12761': SomeCustomFieldValue
    }
    jira.create_issue(jira_dict_convert)
igneus
  • 963
  • 10
  • 25
guitarhero23
  • 1,065
  • 9
  • 11
  • 1
    It may help to add jira.create_issue_link('Cloners', new_issue, issue), to create the link as clone after issue creation – srand9 Jul 05 '20 at 16:09
0

Add Subtasks as well

The above approach is suggested, we're doing the same in our project using JS & Java. However one point missed is to add Subtasks.

Additional Steps in your loop; After your new Issue is created, take it's key and create Subtasks. Change any property you wish to:

new_issue = jira.create_issue(jira_dict_convert)


for(subtask in range(len(subtasks)):
    subtasks[subtask]["fields"]["parent"] = {
            "key":  new_issue.get("key")
        }
    subtasks[subtask]["fields"]["summary"]="new subtask summary"
    jira.create_issue(subtasks[subtask])
Saurabh Talreja
  • 335
  • 2
  • 6