import json, subprocess

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

project = api("projects", "--method", "POST", "-f", "name=pr44658-media-proof", "-f", "visibility=public", "-f", "initialize_with_readme=true")
id = str(project["id"])
api(f"projects/{id}/repository/branches", "--method", "POST", "-f", "branch=demo", "-f", "ref=main")
payload = {"branch": "demo", "commit_message": "Add demo", "actions": [{"action":"create", "file_path":"demo.txt", "content":"Demo change"}]}
subprocess.run(["glab", "api", f"projects/{id}/repository/commits", "--method", "POST", "--input", "-"], input=json.dumps(payload).encode(), check=True, stdout=subprocess.DEVNULL)
body = "Summary of the change.\n\n<!--\nhttps://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4\n-->\n\nVisible demo:\n\nhttps://interactive-examples.mdn.mozilla.net/media/cc0-videos/friday.mp4"
mr = api(f"projects/{id}/merge_requests", "--method", "POST", "-f", "source_branch=demo", "-f", "target_branch=main", "-f", "title=Repro hidden media", "-f", "description=" + body)
print(project["http_url_to_repo"])
print(mr["web_url"])
print("Cleanup: glab api projects/" + id + " --method DELETE")
