"""Seed a fresh Forgejo 11 at localhost:13300 after creating maria and reviewer.
Both disposable users use Proof47947Password. Creates a real fork and deletes it.
"""
import base64,json,urllib.request
ROOT='http://localhost:13300/api/v1/'
def api(user,path,data=None,method=None):
    req=urllib.request.Request(ROOT+path,data=None if data is None else json.dumps(data).encode(),method=method,headers={'Authorization':'Basic '+base64.b64encode((user+':Proof47947Password').encode()).decode(),'Content-Type':'application/json'})
    with urllib.request.urlopen(req) as r:
        body=r.read()
        return json.loads(body) if body else None
api('maria','user/repos',{'name':'project','auto_init':True,'default_branch':'main'})
api('reviewer','repos/maria/project/forks',{})
api('reviewer','repos/reviewer/project/branches',{'new_branch_name':'greeting','old_branch_name':'main'})
api('reviewer','repos/reviewer/project/contents/greeting.txt',{'branch':'greeting','content':base64.b64encode(b'Hello from the fork\n').decode(),'message':'Add greeting'})
api('reviewer','repos/maria/project/pulls',{'head':'reviewer:greeting','base':'main','title':'Add greeting from fork'})
api('reviewer','repos/reviewer/project',method='DELETE')
p=api('maria','repos/maria/project/pulls/1')
print(json.dumps({'state':p['state'],'head':p['head'],'baseRepository':p['base']['repo']['full_name']},indent=2))
