"""Seed a real Forgejo 11 instance. Supply FORGEJO_URL and FORGEJO_TOKEN.
The authenticated user must be tester; menu must not already exist.
"""
import os, urllib.request, json, base64
root = os.environ['FORGEJO_URL'].rstrip('/')
def api(path, data):
    request = urllib.request.Request(root + '/api/v1' + path,
        data=json.dumps(data).encode(), headers={
            'Authorization': 'token ' + os.environ['FORGEJO_TOKEN'],
            'Content-Type': 'application/json'})
    return json.load(urllib.request.urlopen(request))
api('/user/repos', {'name': 'menu', 'auto_init': True, 'default_branch': 'main'})
api('/repos/tester/menu/branches', {'new_branch_name': 'accent', 'old_branch_name': 'main'})
api('/repos/tester/menu/contents/menu.txt', {'branch': 'accent',
    'content': base64.b64encode(b'Coffee\n').decode(), 'message': 'Add menu'})
pr = api('/repos/tester/menu/pulls', {'base': 'main', 'head': 'accent', 'title': 'cafe\u0301 menu'})
print('Title code points:', [f'U+{ord(c):04X}' for c in pr['title']])
