83 lines
2.8 KiB
Python
Executable File
83 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""PR CI: a pull request must add its row to TOKENS.md.
|
|
|
|
TOKENS.md holds one row per piece of work: the tokens of the AI model that
|
|
went into it and the hours that were worked on it. This check compares the
|
|
rows of the pull request with the rows on the branch it goes to, and fails
|
|
when the pull request brings no new row. The new rows are printed, so every
|
|
pull request shows in its log what it cost.
|
|
|
|
Exempt, because nobody worked on them: the release pull request of the
|
|
pipeline (release/*), the dependency updates (renovate/*), and a pull
|
|
request that only carries one long-lived branch into another (production
|
|
into staging, staging into production, develop).
|
|
|
|
Environment (set by the workflow):
|
|
BASE_REF the branch the pull request goes to
|
|
HEAD_REF the branch the pull request comes from
|
|
PR_AUTHOR who opened it
|
|
|
|
Standard library only.
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
LEDGER = "TOKENS.md"
|
|
EXEMPT_PREFIXES = ("release/", "renovate/")
|
|
EXEMPT_BRANCHES = ("production", "staging", "develop")
|
|
EXEMPT_AUTHORS = ("deploy-bot", "renovate-bot")
|
|
|
|
|
|
def rows(text):
|
|
"""The rows of the table: the lines that start with a date."""
|
|
return [line.strip() for line in text.splitlines() if line.startswith("| 20")]
|
|
|
|
|
|
def git(*args):
|
|
return subprocess.run(["git", *args], capture_output=True, text=True)
|
|
|
|
|
|
def main():
|
|
base = os.environ.get("BASE_REF", "").strip()
|
|
head = os.environ.get("HEAD_REF", "").strip()
|
|
author = os.environ.get("PR_AUTHOR", "").strip()
|
|
|
|
if head.startswith(EXEMPT_PREFIXES) or head in EXEMPT_BRANCHES or author in EXEMPT_AUTHORS:
|
|
print(f"No row needed: {head or 'this pull request'} by {author or 'unknown'} is not a piece of work.")
|
|
return 0
|
|
|
|
if not base:
|
|
print("BASE_REF is not set: cannot tell which branch this pull request goes to.")
|
|
return 1
|
|
|
|
if not os.path.exists(LEDGER):
|
|
print(f"{LEDGER} is missing. See AGENTS.md, 'Token usage and hours'.")
|
|
return 1
|
|
|
|
fetch = git("fetch", "--quiet", "--no-tags", "--depth=1", "origin", base)
|
|
if fetch.returncode != 0:
|
|
print(f"Could not fetch {base}: {fetch.stderr.strip()}")
|
|
return 1
|
|
|
|
shown = git("show", f"FETCH_HEAD:{LEDGER}")
|
|
before = set(rows(shown.stdout)) if shown.returncode == 0 else set()
|
|
with open(LEDGER, encoding="utf-8") as handle:
|
|
new = [row for row in rows(handle.read()) if row not in before]
|
|
|
|
if not new:
|
|
print(f"This pull request adds no row to {LEDGER}.")
|
|
print("Every pull request records what went into it: the tokens of the AI model and the hours.")
|
|
print("Add the row with dev/token-usage.py (see AGENTS.md, 'Token usage and hours') and push again.")
|
|
return 1
|
|
|
|
print(f"{len(new)} new row(s) in {LEDGER}:")
|
|
for row in new:
|
|
print(" " + row)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|