forked from tinygrad/tinygrad
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ec33e2ef6 | ||
|
|
fd62ad3034 | ||
|
|
60996454a3 | ||
|
|
a54bb3b795 |
@@ -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']}")
|
||||
@@ -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
|
||||
|
||||
@@ -620,3 +620,5 @@ class count:
|
||||
cur = self.n
|
||||
self.n += self.step
|
||||
return cur
|
||||
|
||||
# test change for the szdiff bot
|
||||
|
||||
Reference in New Issue
Block a user