diff --git a/README.md b/README.md index 755ad81..f53435b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Simple Python script that takes a JSON file for an exported Taiga projects and imports its issue into it a Gitea project via API. -It doesn't have much in the way of error handling. Caveat usor. +It doesn't have much in the way of error handling, and is most certainly not +idempotent. Caveat usor. The script relies on the requests library. diff --git a/import_issues.py b/import_issues.py index 8ee7611..fe02990 100755 --- a/import_issues.py +++ b/import_issues.py @@ -4,22 +4,33 @@ import json import requests import sys +HEADERS = { + 'Accept': 'application/json', + 'Content-Type': 'application/json', +} # TODO: Append comments! def main(input_path, hostname, project_path, username, password): issues = json.load(open(input_path))['issues'] + type_to_label_id = {} + + # Create labels. + for label_payload in ({'color': '#e11d21', 'name': 'bug'}, {'color': '#009800', 'name': 'question / support'}): + label_id = requests.post( + 'https://{}/api/v1/repos/{}/labels'.format(hostname, project_path), + auth=(username, password), + headers=HEADERS, + data=json.dumps(label_payload), + ).json()['id'] + + type_to_label_id[label_payload['name']] = label_id # Reverse issues so they're actually ordered by ID. for issue in reversed(issues): - headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - } payload = { - 'body': issue['description'] + '\n\n---\nImported from Taiga issue {taiga_id}: {status} {type}. Created on {created_date} by {author}.'.format( + 'body': issue['description'] + '\n\n---\nImported from Taiga issue {taiga_id} ({status}). Created on {created_date} by {author}.'.format( taiga_id=issue['ref'], status=issue['status'].lower(), - type=issue['type'].lower(), created_date=issue['created_date'], author=issue['history'][0]['user'][1], ), @@ -27,13 +38,27 @@ def main(input_path, hostname, project_path, username, password): 'title': issue['subject'], } - response = requests.post( + # Create the issue. + create_response = requests.post( 'https://{}/api/v1/repos/{}/issues'.format(hostname, project_path), auth=(username, password), - headers=headers, + headers=HEADERS, data=json.dumps(payload), - ) - return # TODO + ).json() + + # Work-around for https://github.com/go-gitea/gitea/issues/3302 + issue_url = create_response['url'].replace(str(create_response['id']), str(create_response['number'])) + + # Add a label for the issue type (if applicable). + label_id = type_to_label_id.get(issue['type'].lower()) + if label_id: + requests.post( + issue_url + '/labels', + auth=(username, password), + headers=HEADERS, + data=json.dumps({'labels': [label_id]}), + ) + return # TODO: remove me