bitflicker64 commented on code in PR #779: URL: https://github.com/apache/hugegraph-toolchain/pull/779#discussion_r4183173828
########## .github/scripts/check-rerun.py: ########## @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Check the failed run again immediately before an automatic retry.""" + +import argparse +import json +import os +import subprocess +from urllib.parse import quote, urlencode + + +def api(path): + result = subprocess.run( + ["gh", "api", "--method", "GET", path], check=True, + capture_output=True, text=True, + ) + return json.loads(result.stdout) + + +def decide(repository, run_id, expected_attempt, max_reruns, fetch=api): + run = fetch(f"repos/{repository}/actions/runs/{run_id}") + if (run.get("status") != "completed" or run.get("conclusion") != "failure" + or run.get("run_attempt") != expected_attempt): + return "skip", "source run changed or is no longer a completed failure" + if expected_attempt > max_reruns: + return "skip", "retry limit reached" + sha = run.get("head_sha") + if not sha: + return "skip", "missing head SHA" + if run.get("event") == "pull_request": + def local(candidate): + base_repository = candidate.get("base", {}).get("repo", {}) + return (base_repository.get("full_name") == repository + or base_repository.get("url") == f"https://api.github.com/repos/{repository}") + + candidates = [pr for pr in run.get("pull_requests", []) if local(pr)] + if not candidates: + candidates = [pr for pr in fetch( Review Comment: Important: Both PR lookups come back empty for a fork PR, so every failed fork PR run is skipped. GitHub leaves `pull_requests` empty on workflow runs whose head is in a fork, and `commits/{sha}/pulls` returns `[]` for a commit that exists only in the fork. I checked this read-only on the live repo: `gh api repos/apache/hugegraph-toolchain/commits/514c5b46e4ef544991fd46f87db5b8b126308bf7/pulls` returns `[]` for open fork PR #771 (its run 37298674115 has `pull_requests: []`), and the same happens for this PR's head `5b375de8`. Calling `decide()` on run 37298674115, with the status forced to a completed failure, returns `skip: no open PR in this repository with the failed head`. Same-repo PR #780 returns `rerun`. The old shell check read `head_repository` plus `head_branch`, so it did retry fork PRs. Most PRs here come from forks (`hugegraph/*` and contributor forks), so this turns automatic retries off for them with no notice. Please add a third lookup that uses the run's head: `re pos/{repository}/pulls?state=open&head={run['head_repository']['owner']['login']}:{run['head_branch']}`. For #771 that returns PR 771 at `514c5b46`, and the existing `head.sha == sha` and base-repo checks then still apply. Please also add a test where `pull_requests` and `commits/{sha}/pulls` are both empty but that lookup finds the PR. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
