It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Creating Jira issues through Python script: trouble with component, duedate and fixversion field Edited

I'm currently writing a simple Python script which reads data from an Excel sheet and creates Jira tasks based on that data. I've got the Excel data-reading part working and the Jira task creation mostly as well, but am struggling with a couple of fields. 

The following fields are mandatory for our Jira tasks:

  1. project
  2. issue type
  3. summary
  4. parent watcher (custom field)
  5. priority 
  6. external bid (custom field)
  7. fix version/s
  8. component 
  9. due date

I got the first six working with the following line of code in Python:

    issue = jira.create_issue(project=pro, summary=sum, issuetype=type, customfield_13700 = { "name": parent }, priority = {'name': priority}, customfield_12501 = external_bid )

However, I am not sure how to fill in the other parameters of the jira.create_issue function for the fix versions/s, component and due date bid fields. 

I've tried many different things:

  • For the fix version field:
    issue = jira.create_issue(fixversion=fixversion )
    issue = jira.create_issue(fixversion = {'name': fixversion})
    issue = jira.create_issue(fixversion = {'value': fixversion})
    which all results in the following error:
    "errors":{"fixversion":"Field 'fixversion' cannot be set. It is not on the appropriate screen, or unknown."}}
  • For the component field:
    issue = jira.create_issue(component = component )
    issue = jira.create_issue(component = {'name': component} )
    issue = jira.create_issue(fixversion = {'value': component})

    Which again all results in the following error:
    errors":{"component":"Field 'component' cannot be set. It is not on the appropriate screen, or unknown."}}

  • For the due date:
    issue = jira.create_issue(duedate = duedate)
    issue = jira.create_issue(duedate = {'name': duedate} )
    issue = jira.create_issue(duedate = {'value': duedate} )

    Which throws up the following error:
    TypeError: Object of type 'datetime' is not JSON serializable

At this point, I've tried a host of different things and looked a bunch of stuff up, most of which point to this page: 

JIRA projects may contain many different issue types. Some issue screens have different requirements for fields in a new issue. This information is available through the ‘createmeta’ method. Further examples are available here. 

Which isn't really helpful for me. Anybody have any idea how to best proceed from here?

2 answers

1 accepted

0 votes
Answer accepted
Andy Heinzer Atlassian Team Feb 22, 2019

For FixVersion and Components, the problem here is likely not to be your python script at all.  Instead the error message is more indicative of a configuration problem in Jira.  

Jira projects have a set of screens that could appear for all issue types.  You can learn more about this in Defining a screen.  Essentially what I think is happening here is that the project you are creating issues in, does not have the fixversion or component fields appearing on the create screen for that issue type.  As a result, you can't actually give values to those fields when creating issues of that type.   If you are an Admin of that Jira site, you could edit the screens in use in that project in order to add those specific fields to the create screen.   Project settings -> Screens from that page you can edit the screens of that issue type, find the create screen, and add those fields to that screen.

 

As for the date field, it looks like the way python is trying to format that datetime value is inconsistent with the way Jira handles that datatype.  I found a related thread with this error over on a github project:

the default falcon.media.JSONHandler only supports the objects and types listed in the table documented under json.JSONEncoder https://docs.python.org/3.6/library/json.html#json.JSONEncoder To handle additional types, you can either serialize them beforehand, or create a custom JSON media handler that sets the _default_ param for json.dumps(). When deserializing an incoming request body, you may also wish to implement _object_hook_ for json.loads(). Note, however, that setting the _default_ and/or _object_hook_ params will negatively impact the performance of JSON media (de)serialization.

There is also another user that posted another script there to serialize that data payload beforehand.

Hi Andrew, 

Thank you for your reply. 

I've considered your proposed solution for FixVersion and Component, however both fields are present in the create screen of the project. Would you know an other approach to fix perhaps something that is going wrong in the Jira project settings/back end?

Thank you for the information regarding the JSON, with some extra fiddling in the Python script, it was solved with these two lines: 


duedate = str(getCellValue(ws, i, eColumn.DUE_DATE.value).strftime('%Y-%m-%d'))

issue = jira.create_issue(duedate = duedate + "T00:00:00.000-0500")

Andy Heinzer Atlassian Team Feb 26, 2019

These two fields are also different from the others because they can have an array of values at the same time.  The other fields only can have a single value.  So I'm thinking the problem here might be in regards to the way the value for those fields is being provided in the python syntax.  

I'm not sure the exact python syntax here, but if using curl, you could set the json payload to look like this


"fixVersions": [
{
"name": "Version 3.0"
}
],
"components": [
{
"name" :"awesome"
}
]

I'm thinking that you just need an additional set of [ ] brackets that encapsulate those {"name":"value"} entries to make this work.    I found a related thread about this same problem when using python to create issues that seems to confirm my thought process here, check out https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-add-component-while-creating-an-issue-via-JIRA-REST-API/qaq-p/493660

Like # people like this

Hey Andrew, thanks for your reply. That actually did the trick. Also, important note, the parameter in the Jira create function is called "fixVersions" not "fixVersion". This can be quite confusing because when using JQL the parameter is called "fixVersion". 

Like # people like this

Hi @christianvuye  I am trying to import bulk issues from excel sheet but unable to do achieve this,  could you please help me with the python script ? 

Thanks, 

Vishal

Hi @Vishal which part are you struggling with? 

Hi @christianvuye  thanks for the reply

I am struggling with excel sheet to import the issues,

I have created one issue with the below script 


Data= neon.dumps(({
{


"fields": {
"project" :

{
   "key": "VT"

}
" Summary": "python script",

"issuetype":{
         "name":"Task"

}}))

 

Now I want to create 1000 issues using rest api and python script at one go, I don't want to go with application csv import option, I want try with script,  I really appreciate if you could help here:) 


Thanks and have a great day!!

Hi, @Vishal have a look here: jira.readthedocs.io/en/master/

For creating issues, you should use the 

jira.create_issue

function, with the different fields as the function's arguments. 

Like Vishal likes this

Wonderful,  you are so helpful, Thank you so much:)  

Like christianvuye likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you