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

81 lines
2.7 KiB
Python
Raw Permalink Normal View History

2018-01-04 06:23:05 +00:00
#!/usr/bin/python3
import json
import requests
import sys
2018-01-05 05:02:06 +00:00
HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
2018-01-04 06:23:05 +00:00
def main(input_path, hostname, project_path, username, password):
issues = json.load(open(input_path))['issues']
2018-01-05 05:02:06 +00:00
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
2018-01-04 06:23:05 +00:00
# Reverse issues so they're actually ordered by ID.
for issue in reversed(issues):
payload = {
2018-01-05 05:02:06 +00:00
'body': issue['description'] + '\n\n---\nImported from Taiga issue {taiga_id} ({status}). Created on {created_date} by {author}.'.format(
2018-01-04 06:23:05 +00:00
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'],
}
2018-01-05 05:02:06 +00:00
# Create the issue.
create_response = requests.post(
2018-01-04 06:23:05 +00:00
'https://{}/api/v1/repos/{}/issues'.format(hostname, project_path),
auth=(username, password),
2018-01-05 05:02:06 +00:00
headers=HEADERS,
2018-01-04 06:23:05 +00:00
data=json.dumps(payload),
2018-01-05 05:02:06 +00:00
).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]}),
)
2018-01-05 05:17:26 +00:00
# Add comments for the issue.
for entry in issue['history']:
if not entry['comment']:
continue
requests.post(
issue_url + '/comments',
auth=(username, password),
headers=HEADERS,
data=json.dumps({
'body': entry['comment'] + '\n\n---\nComment on {created_at} by {author}.'.format(
created_at=entry['created_at'],
author=entry['user'][1]
),
}),
)
2018-01-04 06:23:05 +00:00
if __name__ == '__main__':
main(*sys.argv[1:])