Compare commits

...
Author SHA1 Message Date
geohot 1ec33e2ef6 wip: test change, do not merge 2026-08-01 10:50:45 -07:00
geohot fd62ad3034 Merge pull request 'szdiff: make workflow work on both github and gitea (supersedes #8)' (#9) from szdiff_portable into master 2026-08-01 10:49:44 -07:00
geohot 60996454a3 szdiff: make comment steps work on both github and gitea
- checkbranch: fetch master from the base repo clone_url instead of the
  PR head remote (origin), which on GitHub is the fork for fork PRs
- replace the curl comment snippets with sticky_comment.py: stdlib-only,
  works with the REST API on both platforms, and keeps the
  skip_unchanged/ignore_empty behavior of the old action
2026-08-01 10:44:41 -07:00
geohot a54bb3b795 fix szdiff workflow for Gitea: use local master and REST API comments
Two bugs prevented the 'Core Library Line Count' bot from working:

1. checkbranch fetched master from github.com/tinygrad/tinygrad instead of
   the local Gitea instance. Since the Gitea mirror lags behind GitHub,
   every PR appeared 'behind' and the szdiff job was always skipped.

2. marocchino/sticky-pull-request-comment@v3 uses GraphQL to find existing
   comments, but Gitea has no GraphQL API (returns 404). Both the szdiff
   and rebase comment steps failed with '404 page not found'.

Fix 1: fetch origin/master (the local Gitea repo) instead of adding a
remote to github.com.

Fix 2: replace the third-party action with curl calls to the Gitea REST
API (GET/POST/PATCH /repos/.../issues/.../comments), which fully supports
the sticky-comment pattern (find existing, update or create).
2026-08-01 10:34:37 -07:00
3 changed files with 55 additions and 17 deletions
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
# Sticky PR comment via the REST API: find an existing comment containing MARKER and PATCH it, or POST a new one.
# Works on GitHub and Gitea (stdlib only, replaces marocchino/sticky-pull-request-comment which needs GraphQL).
# Env vars: GITHUB_TOKEN, GITHUB_API_URL, GITHUB_REPOSITORY (set by the runner), PR_NUMBER, MARKER, and BODY_FILE or MESSAGE.
import json, os, sys, urllib.request
api, repo = os.environ["GITHUB_API_URL"], os.environ["GITHUB_REPOSITORY"]
pr, marker = os.environ["PR_NUMBER"], os.environ["MARKER"]
body = open(os.environ["BODY_FILE"]).read() if os.environ.get("BODY_FILE") else os.environ["MESSAGE"]
if not body.strip():
print("comment body is empty, not posting")
sys.exit(0)
def req(url, method="GET", payload=None):
r = urllib.request.Request(url, data=None if payload is None else json.dumps(payload).encode(), method=method,
headers={"Authorization": f"token {os.environ['GITHUB_TOKEN']}", "Accept": "application/json", "Content-Type": "application/json"})
return json.load(urllib.request.urlopen(r))
# find the latest sticky comment (paginate, 100 comments per page)
existing, page = None, 1
while True:
comments = req(f"{api}/repos/{repo}/issues/{pr}/comments?per_page=100&page={page}")
stickies = [c for c in comments if marker in (c.get("body") or "")]
if stickies: existing = stickies[-1]
if not comments or len(comments) < 100: break
page += 1
if existing is not None and existing["body"] == body:
print("comment is already up to date")
sys.exit(0)
url = f"{api}/repos/{repo}/issues/comments/{existing['id']}" if existing is not None else f"{api}/repos/{repo}/issues/{pr}/comments"
resp = req(url, 'PATCH' if existing is not None else 'POST', {'body': body})
print(f"{'updated' if existing is not None else 'created'} comment {resp['id']}")
+19 -17
View File
@@ -26,14 +26,14 @@ jobs:
- name: Check whether branch is up-to-date
id: brstat
run: |
git remote add tinygrad https://github.com/tinygrad/tinygrad
git fetch tinygrad master
# fetch master from the base repo (tinygrad/tinygrad on GitHub, the mirror on Gitea), not the PR head remote
git fetch "${{ github.event.pull_request.base.repo.clone_url }}" master
echo "${{ github.event.pull_request.head.sha }}"
git rev-list --left-right --count tinygrad/master...${{ github.event.pull_request.head.sha }} | awk '{print "Behind "$1" - Ahead "$2""}'
count=$(git rev-list --left-right --count tinygrad/master...${{ github.event.pull_request.head.sha }} | awk '{print $1}')
git rev-list --left-right --count FETCH_HEAD...${{ github.event.pull_request.head.sha }} | awk '{print "Behind "$1" - Ahead "$2""}'
count=$(git rev-list --left-right --count FETCH_HEAD...${{ github.event.pull_request.head.sha }} | awk '{print $1}')
if [ $count -gt 0 ]
then
echo "Current branch is behind tinygrad master branch!"
echo "Current branch is behind ${{ github.event.pull_request.base.repo.full_name }} master branch!"
echo "stat=true" >> "$GITHUB_OUTPUT"
else
echo "stat=false" >> "$GITHUB_OUTPUT"
@@ -75,13 +75,13 @@ jobs:
python sz.py "$BASE" "$PR" > loc_content.txt
- name: Comment Code Line Diff
continue-on-error: false
uses: marocchino/sticky-pull-request-comment@v3
with:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ignore_empty: true
skip_unchanged: true
recreate: true
path: loc_content.txt
PR_NUMBER: ${{ github.event.pull_request.number }}
MARKER: "### Changes"
BODY_FILE: loc_content.txt
# note: run the script from the base checkout, never from the PR checkout
run: python3 "$GITHUB_WORKSPACE/base/.github/workflows/sticky_comment.py"
rebase:
name: Core Library Line Difference
@@ -91,12 +91,14 @@ jobs:
needs: checkbranch
if: needs.checkbranch.outputs.branchstat == 'true'
steps:
# pull_request_target: a plain checkout gets the base repo, so no PR code is executed
- uses: actions/checkout@v6
- name: Comment Rebase
continue-on-error: false
uses: marocchino/sticky-pull-request-comment@v3
with:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
skip_unchanged: true
recreate: true
message: |
This branch currently is behind tinygrad/master. The line count difference bot is disabled.
PR_NUMBER: ${{ github.event.pull_request.number }}
MARKER: "line count difference bot is disabled"
MESSAGE: |
This branch currently is behind ${{ github.event.pull_request.base.repo.full_name }} master. The line count difference bot is disabled.
run: python3 .github/workflows/sticky_comment.py
+2
View File
@@ -620,3 +620,5 @@ class count:
cur = self.n
self.n += self.step
return cur
# test change for the szdiff bot