import os,json,urllib.request,base64,sys
org=os.environ['AZURE_DEVOPS_TEST_ORG'].rstrip('/')
project=os.environ['AZURE_DEVOPS_TEST_PROJECT']
auth=base64.b64encode(('pat:'+os.environ['AZURE_DEVOPS_EXT_PAT']).encode()).decode()
base=org+'/'+project+'/_apis/git/repositories/t3-factory-test'
def api(path,data=None):
 r=urllib.request.Request(base+path, data=None if data is None else json.dumps(data).encode(),headers={'Authorization':'Basic '+auth,'Content-Type':'application/json'})
 return json.load(urllib.request.urlopen(r))
repo=api('?api-version=7.1')
refs=api('/refs?filter=heads/main&api-version=7.1')['value']
sha=next(x['objectId'] for x in refs if x['name']=='refs/heads/main')
branch='fx45722-proof-a1796013392\u00a0'
r=api('/refs?api-version=7.1',[{'name':'refs/heads/'+branch,'oldObjectId':'0'*40,'newObjectId':sha}])
print(json.dumps(r))
pr=api('/pullrequests?api-version=7.1',{'sourceRefName':'refs/heads/'+branch,'targetRefName':'refs/heads/main','title':'NBSP checkout proof 45722','description':'Temporary exact branch name checkout verification.'})
with open(sys.argv[1],'w') as f: json.dump({'url':repo['remoteUrl'],'pr':pr['pullRequestId'],'branch':branch},f)
print('Created PR',pr['pullRequestId'])
