"""Create the real GitLab merge request used by pr-pre-literal-video.e2e.ts.
Requires glab authentication and Git HTTPS credentials. Run in a temporary folder.
"""
import json
import subprocess
from pathlib import Path

def run(*args):
    return subprocess.check_output(args, text=True).strip()

project = json.loads(run("glab", "api", "-X", "POST", "projects",
    "-f", "name=pre-block-proof-48766", "-f", "visibility=private",
    "-f", "initialize_with_readme=true"))
run("git", "clone", project["http_url_to_repo"], "repo")
for args in [
    ("switch", "-c", "feature"),
    ("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid",
     "commit", "--allow-empty", "-m", "Feature"),
    ("push", "origin", "feature"),
]:
    run("git", "-C", "repo", *args)
body = "<pre>\nhttps://example.test/example.mp4\n</pre>\n\nhttps://example.test/recording.mp4"
mr = json.loads(run("glab", "api", "-X", "POST",
    f"projects/{project['id']}/merge_requests", "-f", "source_branch=feature",
    "-f", "target_branch=main", "-f", "title=Pre block repro", "-f", "description=" + body))
stored = json.loads(run("glab", "api", f"projects/{project['id']}/merge_requests/{mr['iid']}"))
assert stored["description"] == body
print(stored["web_url"])
# Cleanup after running both sides: glab api -X DELETE projects/<project id>
