"""Create a synthetic GitLab MR for the long-line diff reproduction; requires glab login."""
import json
import subprocess
import tempfile

def api(endpoint, payload=None):
    args = ['glab', 'api', endpoint]
    if payload is None:
        return json.loads(subprocess.check_output(args))
    with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as f:
        json.dump(payload, f)
        f.flush()
        return json.loads(subprocess.check_output(args + ['--method', 'POST', '--input', f.name]))

project = api('projects', {'name': 'long-lines-proof', 'visibility': 'private', 'initialize_with_readme': True})
content = 'header\n' + 'A' * 1000 + '\r\n' + 'b' * 999 + '😀z\r\n' + 'c' * 999 + '😀z\n'
content += ''.join(f'filler {i} ' + 'x' * 100 + '\n' for i in range(3000))
api(f'projects/{project["id"]}/repository/commits', {
    'branch': 'long-lines', 'start_branch': 'main', 'commit_message': 'Synthetic long diff lines',
    'actions': [{'action': 'create', 'file_path': 'big.txt', 'content': content}],
})
mr = api(f'projects/{project["id"]}/merge_requests', {
    'source_branch': 'long-lines', 'target_branch': 'main', 'title': 'repro long lines',
})
print(mr['web_url'])
