import subprocess,json
P='projects/89699760'  # Synthetic private proof project; replace with your own project id.
def api(path,data):
 r=subprocess.run(['glab','api','-X','POST',P+path,'--input','-'],input=json.dumps(data),text=True,capture_output=True,check=True);return json.loads(r.stdout)
api('/repository/commits',dict(branch='main',commit_message='Add two distinct filenames',actions=[dict(action='create',file_path=p,content=c) for p,c in [(' test.ts ','line one\nline two\nline three\n'),('test.ts','alpha\nbeta\ngamma\n')]]))
api('/repository/branches',dict(branch='feature',ref='main'))
api('/repository/commits',dict(branch='feature',commit_message='Change line 2',actions=[dict(action='update',file_path=p,content=c) for p,c in [(' test.ts ','line one\nline two CHANGED\nline three\n'),('test.ts','alpha\nbeta CHANGED\ngamma\n')]]))
mr=api('/merge_requests',dict(source_branch='feature',target_branch='main',title='Change line 2'))
print('Created MR',mr['iid'])

r=json.loads(subprocess.check_output(['glab','api',P+'/merge_requests/1']))
refs=r['diff_refs']
assert refs
n=api('/merge_requests/1/discussions',dict(body='Comment on line 2 of the space-padded file',position=dict(position_type='text',base_sha=refs['base_sha'],start_sha=refs['start_sha'],head_sha=refs['head_sha'],new_path=' test.ts ',old_path=' test.ts ',new_line=2)))
print(repr(n['notes'][0]['position']['new_path']))
