signed_push.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Commit version Markdown with a configured signing key, verify, then push."""
  2. import argparse
  3. import subprocess
  4. from pathlib import Path
  5. from pipeline import ROOT, LIB
  6. def output(args):
  7. return subprocess.check_output(args, cwd=ROOT, text=True).strip()
  8. def publish(branch, message, paths):
  9. if output(['gh', 'api', 'user', '--jq', '.login']).casefold() != 'hilda-bot':
  10. raise RuntimeError('gh must authenticate as Hilda-Bot')
  11. if output(['git', 'branch', '--show-current']) != branch:
  12. raise RuntimeError('Check out the intended publishing branch first')
  13. # Explicit configuration prevents accidental use of the personal default key.
  14. if output(['git', 'config', '--local', '--get', 'user.email']) != '[email protected]':
  15. raise RuntimeError('Configure repository-local [email protected] identity')
  16. key = output(['git', 'config', '--local', '--get', 'user.signingkey'])
  17. if not key:
  18. raise RuntimeError('Configure a repository-local Hilda-Bot signing key')
  19. if output(['git', 'diff', '--cached', '--name-only']):
  20. raise RuntimeError('Index must be empty before publishing')
  21. files = []
  22. for item in paths:
  23. path = (ROOT / item).resolve()
  24. if not path.is_relative_to(LIB.resolve()) or path.suffix != '.md':
  25. raise ValueError('Only version Markdown files may be published')
  26. content = path.read_text()
  27. if '\nllm-transalte: true\n' not in content or '\nwip:' in content:
  28. raise ValueError('Apply the translation packet first')
  29. files.append(str(path.relative_to(ROOT)))
  30. if not files:
  31. raise ValueError('No translations')
  32. subprocess.run(['git', 'add', '--', *files], cwd=ROOT, check=True)
  33. subprocess.run(['git', '-c', 'user.name=Hilda Bot', '-c', '[email protected]',
  34. 'commit', '-S', '-m', message], cwd=ROOT, check=True)
  35. subprocess.run(['git', 'verify-commit', 'HEAD'], cwd=ROOT, check=True)
  36. subprocess.run(['git', '-c', 'credential.helper=', '-c', 'credential.helper=!gh auth git-credential',
  37. 'push', 'https://github.com/Hilda-Bot/PCL2-NewsHomepage.git',
  38. f'HEAD:refs/heads/{branch}'], cwd=ROOT, check=True)
  39. if __name__ == '__main__':
  40. parser = argparse.ArgumentParser(description=__doc__)
  41. parser.add_argument('--branch', required=True)
  42. parser.add_argument('--message', required=True)
  43. parser.add_argument('paths', nargs='+')
  44. args = parser.parse_args()
  45. publish(args.branch, args.message, args.paths)