#!/usr/bin/env python3
"""Create a synthetic GitLab fixture; glab must already be authenticated.
Usage: python3 create-fixture.py /path/to/fixture.json
Pass that output as PR_WATCH_FIXTURE_FILE when running the scenario.
Delete the created project after the proof: glab api --method DELETE projects/<id>.
"""
import json
import subprocess
import sys
import time

def api(*args):
    return json.loads(subprocess.check_output(["glab", "api", *args]))

name = "pr-watch-baseline-proof-" + str(int(time.time()))
project = api("--method", "POST", "projects", "-f", "name=" + name,
              "-f", "path=" + name, "-f", "visibility=private",
              "-f", "initialize_with_readme=true")
project_id = project["id"]
api("--method", "POST", f"projects/{project_id}/repository/branches",
    "-f", "branch=feature", "-f", "ref=main")
api("--method", "POST", f"projects/{project_id}/repository/files/proof.txt",
    "-f", "branch=feature", "-f", "content=CI baseline race proof",
    "-f", "commit_message=Add proof fixture")
mr = api("--method", "POST", f"projects/{project_id}/merge_requests",
         "-f", "source_branch=feature", "-f", "target_branch=main",
         "-f", "title=CI fails during watch baseline")
with open(sys.argv[1], "w") as output:
    json.dump({"id": project_id, "repository": project["path_with_namespace"],
               "url": mr["web_url"], "sha": mr["sha"], "iid": mr["iid"]}, output)
