import subprocess,json,time
from pathlib import Path
def api(path,method='GET',data=None):
 args=['glab','api',path,'-X',method]
 if data is not None: args+=['--input','-']
 p=subprocess.run(args,input=json.dumps(data) if data else None,text=True,capture_output=True)
 if p.returncode: raise RuntimeError(p.stderr)
 return json.loads(p.stdout) if p.stdout else None
p=api('projects','POST',{'name':'approval-history-proof-'+str(int(time.time())),'visibility':'private','initialize_with_readme':True})
id=p['id']
api(f'projects/{id}/repository/commits','POST',{'branch':'feature','start_branch':'main','commit_message':'add a','actions':[{'action':'create','file_path':'a.txt','content':'Synthetic review history example\n'}]})
mr=api(f'projects/{id}/merge_requests','POST',{'source_branch':'feature','target_branch':'main','title':'Repro MR','description':'Synthetic approval and withdrawal history.'})
api(f'projects/{id}/merge_requests/1/approve','POST')
api(f'projects/{id}/merge_requests/1/unapprove','POST')
api(f'projects/{id}/merge_requests/1/notes','POST',{'body':'A normal comment'})
notes=api(f'projects/{id}/merge_requests/1/notes')
Path('fixture.json').write_text(json.dumps({'id':id,'url':p['http_url_to_repo'],'mr':mr['web_url'],'notes':[{k:n[k] for k in ['body','system']} for n in notes]},indent=2))
print(id,p['http_url_to_repo']);print([(n['system'],n['body']) for n in notes])
