#!/usr/bin/env python3
"""Publish to YOUR PRIVATE repo. Printed URL requires the owner's Gitea login."""
import os,shutil,subprocess,sys,secrets
from pathlib import Path
if len(sys.argv)!=2: sys.exit('usage: osmike-publish FILE (private owner repo link)')
p=Path(sys.argv[1]).resolve(); root=Path('/data/workspace'); dest=root/'context/published'; dest.mkdir(parents=True,exist_ok=True)
if not p.is_file() or p.stat().st_size>10*1024*1024: sys.exit('file must exist and be at most 10MiB')
name=secrets.token_hex(8)+'-'+p.name; shutil.copyfile(p,dest/name)
for args in [['add','--','context/published/'+name],['-c','user.name='+os.environ['AGENT_NAME'],'-c','user.email='+os.environ['AGENT_NAME']+'@guests.osmike.com','commit','-m','Publish private guest artifact'],['push','origin','HEAD:main']]:
 r=subprocess.run(['git','-C',str(root)]+args,capture_output=True)
 if r.returncode: sys.exit('repo publication failed')
repo=os.environ['GUEST_REPO'].split(':8771/',1)[1].removesuffix('.git')
from urllib.parse import quote
print('https://git.osmike.com/'+repo+'/src/branch/main/context/published/'+quote(name))
