From 87253a06bf56534993444979d839cd221cfd0a13 Mon Sep 17 00:00:00 2001 From: driazati Date: Tue, 1 Feb 2022 16:12:16 -0800 Subject: [PATCH] use a branch instead of a tag --- ....yml => update_last_successful_branch.yml} | 16 ++++---- tests/python/unittest/test_ci.py | 4 +- tests/scripts/git_utils.py | 6 ++- .../{update_tag.py => update_branch.py} | 39 +++++++++---------- 4 files changed, 32 insertions(+), 33 deletions(-) rename .github/workflows/{update_tag.yml => update_last_successful_branch.yml} (80%) rename tests/scripts/{update_tag.py => update_branch.py} (83%) diff --git a/.github/workflows/update_tag.yml b/.github/workflows/update_last_successful_branch.yml similarity index 80% rename from .github/workflows/update_tag.yml rename to .github/workflows/update_last_successful_branch.yml index 6348b9e3cd05..1e8def4040ae 100644 --- a/.github/workflows/update_tag.yml +++ b/.github/workflows/update_last_successful_branch.yml @@ -19,27 +19,25 @@ # We use it to cover windows and mac builds # Jenkins is still the primary CI -name: Update last-successful tag +name: Update last-successful branch on: - workflow_dispatch: schedule: - cron: 15/* * * * * + - cron: "0/15 * * * *" + workflow_dispatch: concurrency: - group: update-last-successful-tag + group: update-last-successful-branch cancel-in-progress: true jobs: - update-last-successful-tag: + update-last-successful-branch: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - with: - submodules: "recursive" - - name: Update last-successful tag + - name: Update last-successful branch env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -eux - python tests/scripts/update_tag.py || echo step failed + python tests/scripts/update_branch.py || echo step failed diff --git a/tests/python/unittest/test_ci.py b/tests/python/unittest/test_ci.py index b98ca45ea2f8..d3e7c79b88ed 100644 --- a/tests/python/unittest/test_ci.py +++ b/tests/python/unittest/test_ci.py @@ -68,8 +68,8 @@ def run(pr_body, expected_reviewers): ) -def test_update_tag(tmpdir_factory): - update_script = REPO_ROOT / "tests" / "scripts" / "update_tag.py" +def test_update_branch(tmpdir_factory): + update_script = REPO_ROOT / "tests" / "scripts" / "update_branch.py" def run(statuses, expected_rc, expected_output): git = TempGit(tmpdir_factory.mktemp("tmp_git_dir")) diff --git a/tests/scripts/git_utils.py b/tests/scripts/git_utils.py index 52f44b83ba3b..088590713001 100644 --- a/tests/scripts/git_utils.py +++ b/tests/scripts/git_utils.py @@ -91,5 +91,7 @@ def parse_remote(remote: str) -> Tuple[str, str]: def git(command, **kwargs): command = ["git"] + command print("Running", command) - proc = subprocess.run(command, stdout=subprocess.PIPE, check=True, **kwargs) - return proc.stdout.decode().strip() + proc = subprocess.run(command, stdout=subprocess.PIPE, encoding="utf-8", **kwargs) + if proc.returncode != 0: + raise RuntimeError(f"Command failed {command}:\nstdout:\n{proc.stdout}") + return proc.stdout.strip() diff --git a/tests/scripts/update_tag.py b/tests/scripts/update_branch.py similarity index 83% rename from tests/scripts/update_tag.py rename to tests/scripts/update_branch.py index 2ce0469da798..8f2558742217 100755 --- a/tests/scripts/update_tag.py +++ b/tests/scripts/update_branch.py @@ -117,29 +117,27 @@ def commit_passed_ci(commit: Dict[str, Any]) -> bool: return passed_ci -def update_tag(user: str, repo: str, sha: str, tag_name: str, message: str) -> None: - with tempfile.TemporaryDirectory() as f: - # Clone only a specific commit: https://stackoverflow.com/a/3489576 - git(["init"], cwd=f) - git(["remote", "add", "origin", f"git@github.com:{user}/{repo}.git"], cwd=f) - git(["fetch", "origin", sha], cwd=f) - git(["reset", "--hard", "FETCH_HEAD"], cwd=f) +def update_branch(user: str, repo: str, sha: str, branch_name: str) -> None: + git(["fetch", "origin", sha]) + git(["reset", "--hard", "FETCH_HEAD"]) + try: + git(["branch", "-D", branch_name]) + except RuntimeError: + # Ignore failures (i.e. the branch did not exist in the first place) + pass + git(["checkout", "-b", branch_name]) - # Create a push the tag - git(["tag", "--annotate", tag_name, f"--message={message}"], cwd=f) - git(["push", "origin", "--force", tag_name], cwd=f) - print(f"Pushed tag {tag_name} with commit {sha}") + # Create and push the branch + git(["push", "origin", "--force", branch_name]) + print(f"Pushed branch {branch_name} with commit {sha}") if __name__ == "__main__": - help = "Push the a tag to the last commit that passed all CI runs" + help = "Push the a branch to the last commit that passed all CI runs" parser = argparse.ArgumentParser(description=help) parser.add_argument("--remote", default="origin", help="ssh remote to parse") parser.add_argument("--dry-run", action="store_true", help="don't submit to GitHub") - parser.add_argument("--tag", default="last-successful", help="tag name") - parser.add_argument( - "--message", default="last 'main' commit that passed CI", help="label to add" - ) + parser.add_argument("--branch", default="last-successful", help="branch name") parser.add_argument( "--testonly-json", help="(testing) data to use instead of fetching from GitHub" ) @@ -147,6 +145,8 @@ def update_tag(user: str, repo: str, sha: str, tag_name: str, message: str) -> N remote = git(["config", "--get", f"remote.{args.remote}.url"]) user, repo = parse_remote(remote) + # TODO: Remove this before landing + user, repo = ("apache", "tvm") if args.testonly_json: r = json.loads(args.testonly_json) @@ -167,14 +167,13 @@ def update_tag(user: str, repo: str, sha: str, tag_name: str, message: str) -> N if commit_passed_ci(commit): print(f"Found last good commit: {commit['oid']}: {commit['messageHeadline']}") if not args.dry_run: - update_tag( + update_branch( user=user, repo=repo, sha=commit["oid"], - tag_name=args.tag, - message=args.message, + branch_name=args.branch, ) - # Nothing to do after updating the tag, exit early + # Nothing to do after updating the branch, exit early exit(0) # No good commit found, proceed to next page of results