"""Create a disposable real GitLab project, then invoke run-proof.py.
Requires authenticated glab and git HTTPS credentials. All checkouts need vp i.
Usage: python3 setup-proof.py --root <scratch> --base <base-checkout> --head <head-checkout>
The driver prints the project id. Delete that disposable project after uploading evidence.
"""
import argparse,datetime,json,pathlib,subprocess,sys
p=argparse.ArgumentParser();p.add_argument('--root',required=True);p.add_argument('--base',required=True);p.add_argument('--head',required=True);a=p.parse_args()
r=pathlib.Path(a.root).resolve();r.mkdir(parents=True,exist_ok=True)
def cmd(xs,cwd=None):return subprocess.run(xs,cwd=cwd,capture_output=True,text=True,check=True).stdout
name='watch-ci-proof-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d%H%M%S')
project=json.loads(cmd(['glab','api','-X','POST','projects','-f',f'name={name}','-f','visibility=private','-f','initialize_with_readme=true']))
print('Disposable project id:',project['id'],flush=True)
repo=r/'repo';cmd(['git','clone',project['http_url_to_repo'],str(repo)])
cmd(['git','checkout','-b','ci-completion'],repo)
(repo/'.gitlab-ci.yml').write_text('stages: [test]\nmacos:\n  stage: test\n  script:\n    - sleep 75\n    - exit 1\nwindows:\n  stage: test\n  script:\n    - sleep 85\n')
cmd(['git','add','.gitlab-ci.yml'],repo)
cmd(['git','-c','user.name=CI Proof','-c','user.email=proof@example.invalid','commit','-m','CI completion reproduction'],repo)
cmd(['git','push','-u','origin','ci-completion'],repo)
mr=json.loads(cmd(['glab','api','-X','POST',f'projects/{project["id"]}/merge_requests','-f','source_branch=ci-completion','-f','target_branch=main','-f','title=CI completion proof']))
# The evidence run used an existing MR and an empty commit so both fresh servers start
# immediately after the push; this makes both jobs finish before their first scheduled sweep.
subprocess.run([sys.executable,str(pathlib.Path(__file__).with_name('run-proof.py')),'--root',str(r),'--base',a.base,'--head',a.head,'--repo',str(repo),'--project-id',str(project['id']),'--mr-url',mr['web_url']],check=True)
