96 lines
4.3 KiB
Python
96 lines
4.3 KiB
Python
"""Synchronize maintained docs into both adjacent Gitea repository wikis."""
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
from urllib.error import HTTPError
|
|
from urllib.parse import urlsplit
|
|
from urllib.request import Request, urlopen
|
|
|
|
|
|
def git(*args, cwd=None, env=None, capture=False):
|
|
return subprocess.run(['git', *args], cwd=cwd, env=env, check=True, text=True,
|
|
stdout=subprocess.PIPE if capture else None).stdout
|
|
|
|
|
|
def credentials(host):
|
|
token = os.environ.get('GITEA_TOKEN')
|
|
if token:
|
|
return os.environ.get('GITEA_USER', 'Dymas'), token
|
|
result = subprocess.run(['git', 'credential', 'fill'], input=f'protocol=https\nhost={host}\n\n',
|
|
text=True, capture_output=True, check=True)
|
|
values = dict(line.split('=', 1) for line in result.stdout.splitlines() if '=' in line)
|
|
return values['username'], values['password']
|
|
|
|
|
|
def sync(repository):
|
|
repository = Path(repository).resolve()
|
|
origin = git('remote', 'get-url', 'origin', cwd=repository, capture=True).strip()
|
|
remote = urlsplit(origin)
|
|
if remote.scheme != 'https' or remote.username or remote.password or not remote.hostname:
|
|
raise ValueError('Wiki sync requires an HTTPS origin without embedded credentials.')
|
|
owner, name = remote.path.removesuffix('.git').strip('/').split('/')
|
|
username, token = credentials(remote.netloc)
|
|
authorization = 'Basic ' + base64.b64encode(f'{username}:{token}'.encode()).decode()
|
|
base = f'{remote.scheme}://{remote.netloc}'
|
|
|
|
def api(path, method='GET', body=None):
|
|
data = None if body is None else json.dumps(body).encode()
|
|
request = Request(base + '/api/v1' + path, data=data, method=method,
|
|
headers={'Authorization': authorization, 'Content-Type': 'application/json'})
|
|
with urlopen(request, timeout=30) as response:
|
|
raw = response.read()
|
|
return json.loads(raw) if raw else None
|
|
|
|
path = f'/repos/{owner}/{name}'
|
|
if not api(path)['has_wiki']:
|
|
api(path, 'PATCH', {'has_wiki': True})
|
|
try:
|
|
api(path + '/wiki/page/Home')
|
|
except HTTPError as error:
|
|
if error.code != 404:
|
|
raise
|
|
home = (repository / 'docs/Home.md').read_bytes()
|
|
api(path + '/wiki/new', 'POST', {'title': 'Home', 'content_base64': base64.b64encode(home).decode()})
|
|
env = os.environ.copy()
|
|
# Keep credentials out of process arguments, files, and repository remotes.
|
|
count = int(env.get('GIT_CONFIG_COUNT', '0'))
|
|
env.update({f'GIT_CONFIG_KEY_{count}': f'http.{base}/.extraHeader',
|
|
f'GIT_CONFIG_VALUE_{count}': 'Authorization: ' + authorization,
|
|
'GIT_CONFIG_COUNT': str(count + 1)})
|
|
with tempfile.TemporaryDirectory(prefix=f'{name}-wiki-') as temporary:
|
|
git('clone', '--quiet', base + f'/{owner}/{name}.wiki.git', temporary, env=env)
|
|
pages = sorted((repository / 'docs').glob('*.md'))
|
|
if not pages:
|
|
raise ValueError(f'No documentation pages in {repository}')
|
|
for page in pages:
|
|
# Wiki routes use page slugs; local documentation links keep .md.
|
|
content = re.sub(r'(\]\()([A-Za-z0-9_-]+)\.md(?=[)#])', r'\1\2', page.read_text())
|
|
(Path(temporary) / page.name).write_text(content)
|
|
git('add', '--', *[page.name for page in pages], cwd=temporary)
|
|
changed = subprocess.run(['git', 'diff', '--cached', '--quiet'], cwd=temporary)
|
|
if changed.returncode == 1:
|
|
git('commit', '-m', f'Update {name} function and operations documentation', cwd=temporary)
|
|
git('push', 'origin', 'HEAD', cwd=temporary, env=env)
|
|
elif changed.returncode != 0:
|
|
raise RuntimeError('Unable to inspect wiki changes')
|
|
print(f'Synchronized {name} wiki ({len(pages)} maintained pages).')
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('repositories', nargs='*', type=Path)
|
|
args = parser.parse_args()
|
|
workspace = Path(__file__).resolve().parents[2]
|
|
repositories = args.repositories or [workspace / 'web-framework', workspace / 'web-framework-resources']
|
|
for repository in repositories:
|
|
sync(repository)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|