Project

General

Profile

Script to import issues via REST API in private project

Added by Victor Sergienko over 3 years ago

I'm trying to create an issue via POST request to hostedredmine.com API in Python 3 like:

url = "https://www.hostedredmine.com/issues.json" 
key = "***" 

def createIssue(body):
    body = {'issue': body}
    data = urllib.parse.urlencode({'issue': body})
    data = data.encode('utf-8')
    request = urllib.request.Request(url)

    # adding charset parameter to the Content-Type header.
    request.add_header("Content-Type", "application/json")
    request.add_header("X-Redmine-API-Key", key)
    #request.add_header("Content-Length", len(data))

    f = urllib.request.urlopen(request, data)
    print(f.read().decode('utf-8'))

createIssue({
    'project_id': 'plando',
    'subject': 'test1',
    'notes': 'tetest',
     #'parent_issue_id': '215453',
    }
)

and getting a HTTP Error 500: Internal Server Error.

I've tried various URLs, with and without .json suffix or my project path.

Does anybody have any idea, what could it be?


Replies (8)

RE: Problem creating an issue via REST API in private project - Added by Victor Sergienko over 3 years ago

The double {'issue':body} thing is a latest addition and I get the same after fixing it ;)

RE: Problem creating an issue via REST API in private project - Added by Victor Sergienko over 3 years ago

...got it. It was a single quote instead of double quotes.

RE: Problem creating an issue via REST API in private project - Added by Victor Sergienko over 3 years ago

For those who are interested, here's a ready import script.

import urllib.parse
import urllib.request
import json

url = "https://www.hostedredmine.com/issues.json" 
key = "***" 

# http://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl
# curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -H "X-Redmine-API-Key: xxxx" http://redmine/issues/388.json

def createIssue(body):
    body = {'issue': body}

    data = json.dumps(body).encode('utf-8')
    #print(data)

    request = urllib.request.Request(url, data)

    request.add_header("Content-Type", "application/json")
    request.add_header("X-Redmine-API-Key", key)
    request.add_header("Content-Length", len(data))

    f = urllib.request.urlopen(request, data)
    response = json.loads(f.read().decode('utf-8'))
    print(response)
    return response

#createIssue({
#    'project_id': 'plando',
#    'subject': 'test1',
#    'notes': 'tetest',
#     #'parent_issue_id': '215453',
#     #'tracker_id': 2,
#    })
#
#exit(0)

with open('/home/me/tasks.csv') as f:
    story_id = "" 
    for line in f.readlines():

        splat = list(s.strip() for s in line.split('|'))
        if len(splat) < 3 or not splat[0]:
            story_id = "" 
            continue
        (title, hrs, desc) = splat[0:3]

        issue = {
            'project_id': 'myproject',
            'subject': title,
            'notes': desc,
            'parent_issue_id': story_id,
            'tracker_id': 6, # User Story
            'estimated_hours': hrs,
            }
        if story_id:
            print("Task: '{}' for {} hrs".format(title, hrs))
            issue['parent_issue_id'] = story_id
            issue['tracker_id'] = 4 # Task
            if hrs.isdigit():
                issue['estimated_hours'] = hrs
            createIssue(issue)
        else:
            print("Story: '{}'".format(title))
            response = createIssue(issue)
            story_id = response['issue']['id']
            print(" = ", story_id)

RE: Script to import issues via REST API in private project - Added by Victor Sergienko over 3 years ago

Please don't worry. There's no rest. It's that short.

RE: Script to import issues via REST API in private project - Added by Brad Rushworth over 3 years ago

Mukesh Ghatiya wrote:

Where is the rest of the code ?? !!

If it isn't obvious, you need to install Python 3 on your computer before being able to run this. Most Linux computers will already have this installed, but on Windows you will need to download it and install before running the script above.

RE: Script to import issues via REST API in private project - Added by Aleksey Oriekhov about 3 years ago

Thank you for the script!
Just a couple notes:
1. Seems like instead of 'notes' field 'description' should be used. I have no luck, when used 'notes'
2. Does anybody know, how I can export/import not only issues name & desc, but also journal notes? I have a lot of cases where important information is placed in notes during discussion, not in the description of the issue. I've tried to find the way how to export the list of issues with journal, but haven't found any script..

RE: Script to import issues via REST API in private project - Added by Aleksey Oriekhov about 3 years ago

Sorry guys, my question is not actual anymore.
I've created small .NET app with journal notes copying support. Details here: http://www.hostedredmine.com/boards/1/topics/8844

    (1-8/8)