#!/usr/bin/python3 import json import requests import sys HEADERS = { 'Accept': 'application/json', 'Content-Type': 'application/json', } 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]}), ) # 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] ), }), ) if __name__ == '__main__': main(*sys.argv[1:])