This repository has been archived on 2023-12-16. You can view files and clone it, but cannot push or open issues or pull requests.
taiga-to-gitea-issues/import_issues.py

67 lines
2.2 KiB
Python
Executable File

#!/usr/bin/python3
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):
payload = {
'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(),
created_date=issue['created_date'],
author=issue['history'][0]['user'][1],
),
'closed': bool(issue['status'] in ('Done', 'Won\'t fix')),
'title': issue['subject'],
}
# Create the issue.
create_response = requests.post(
'https://{}/api/v1/repos/{}/issues'.format(hostname, project_path),
auth=(username, password),
headers=HEADERS,
data=json.dumps(payload),
).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
if __name__ == '__main__':
main(*sys.argv[1:])