bump_action.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. import argparse
  3. import glob
  4. import os.path
  5. import re
  6. import subprocess
  7. import sys
  8. from typing import Sequence
  9. def main(argv: Sequence[str] | None = None) -> int:
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("action")
  12. parser.add_argument("version")
  13. parser.add_argument("--base-dir", default=".")
  14. args = parser.parse_args(argv)
  15. reg = re.compile(f"(?<=uses: {re.escape(args.action)}@).*$", re.M)
  16. filenames = [
  17. filename
  18. for yml_glob in (".github/workflows/*.yml", ".github/actions/*/*.yml")
  19. for filename in glob.glob(os.path.join(args.base_dir, yml_glob))
  20. ]
  21. changed = []
  22. for filename in filenames:
  23. with open(filename) as f:
  24. original_contents = f.read()
  25. contents = reg.sub(args.version, original_contents)
  26. if contents != original_contents:
  27. print(f"{filename} upgrading {args.action}...")
  28. changed.append(filename)
  29. with open(filename, "w") as f:
  30. f.write(contents)
  31. if changed:
  32. print("freezing...")
  33. return subprocess.call((sys.executable, "-m", "tools.pin_github_action", *changed))
  34. else:
  35. return 0
  36. if __name__ == "__main__":
  37. raise SystemExit(main())