Files
transformers/utils/get_github_job_time.py
d0e91c7ff1 CI: use a single function for GH calls (#47474)
* use a single function for GH calls

* check token before loop

* Add pre-flight token/quota check and per-request rate-limit logging to github_utils

- Add _log_token_status(): calls /rate_limit once before the retry loop (no quota
  consumed). Fails immediately if: CI env with no token, token rejected (401), or
  quota already exhausted (remaining=0). 401 with vs. without token gives distinct
  actionable error messages.
- Add _log_rate_limit_headers(): prints limit/used/remaining/reset on every request
  attempt, labeled [initial] / [retry N/M]. Skipped on 401 (GitHub omits those
  headers on auth failures).
- Remove _is_expired_or_bad_token(): pre-flight check catches bad tokens earlier;
  401s in the retry loop fall through to the generic fail-hard with the raw body.
- Update tests: remove IsExpiredOrBadTokenTest (function deleted), add
  LogTokenStatusTest covering all _log_token_status failure modes, patch
  _log_token_status in GithubRequestTest.setUp so mock responses are not consumed
  by the pre-flight call.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix stale comments about get_github_json error handling

get_github_json now raises RuntimeError on error responses (rate limiting,
auth failures, etc.) rather than returning an error dict. Update comments
in notification_service.py and check_bad_commit.py that incorrectly described
the old behaviour, while keeping the defensive .get() calls which guard
against unexpected response shapes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* format

* fix

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-28 16:03:35 +02:00

70 lines
2.0 KiB
Python

import argparse
import math
import time
import traceback
import dateutil.parser as date_parser
from github_utils import get_github_json
def extract_time_from_single_job(job):
"""Extract time info from a single job in a GitHub Actions workflow run"""
job_info = {}
start = job["started_at"]
end = job["completed_at"]
start_datetime = date_parser.parse(start)
end_datetime = date_parser.parse(end)
duration_in_min = round((end_datetime - start_datetime).total_seconds() / 60.0)
job_info["started_at"] = start
job_info["completed_at"] = end
job_info["duration"] = duration_in_min
return job_info
def get_job_time(workflow_run_id, token=None):
"""Extract time info for all jobs in a GitHub Actions workflow run"""
url = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=50"
result = get_github_json(url, token=token)
job_time = {}
try:
job_time.update({job["name"]: extract_time_from_single_job(job) for job in result["jobs"]})
pages_to_iterate_over = math.ceil((result["total_count"] - 50) / 50)
for i in range(pages_to_iterate_over):
time.sleep(1)
result = get_github_json(url + f"&page={i + 2}", token=token)
job_time.update({job["name"]: extract_time_from_single_job(job) for job in result["jobs"]})
return job_time
except Exception:
print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}")
return {}
if __name__ == "__main__":
r"""
Example:
python get_github_job_time.py --workflow_run_id 2945609517
"""
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.")
args = parser.parse_args()
job_time = get_job_time(args.workflow_run_id)
job_time = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True))
for k, v in job_time.items():
print(f"{k}: {v['duration']}")