Skip to content

Commit 5f77b45

Browse files
authored
[autorevert] Remove some vibes from the code (#7013)
1 parent ce90e32 commit 5f77b45

File tree

8 files changed

+31
-57
lines changed

8 files changed

+31
-57
lines changed

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/__main__.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from .clickhouse_client_helper import CHCliFactory
1111
from .github_client_helper import GHClientFactory
1212
from .testers.autorevert import autorevert_checker
13-
from .testers.do_restart import do_restart_workflow
1413
from .testers.restart_checker import workflow_restart_checker
1514

1615

@@ -65,7 +64,7 @@ def get_opts() -> argparse.Namespace:
6564
parser.add_argument(
6665
"--dry-run",
6766
action="store_true",
68-
help="Show what would be restarted without actually doing it (use with --do-restart)",
67+
help="Show what would be restarted without actually doing it",
6968
)
7069

7170
# no subcommand runs the lambda flow
@@ -125,19 +124,6 @@ def get_opts() -> argparse.Namespace:
125124
help="If no `--commit` specified, look back days for bulk query (default: 7)",
126125
)
127126

128-
# do-restart subcommand
129-
do_restart_parser = subparsers.add_parser(
130-
"do-restart", help="Restart a workflow for a specific commit"
131-
)
132-
do_restart_parser.add_argument(
133-
"workflow",
134-
help="Workflow file name to restart (e.g., trunk.yml)",
135-
)
136-
do_restart_parser.add_argument(
137-
"commit",
138-
help="Commit SHA to restart the workflow for",
139-
)
140-
141127
return parser.parse_args()
142128

143129

@@ -191,8 +177,6 @@ def main(*args, **kwargs) -> None:
191177
)
192178
elif opts.subcommand == "workflow-restart-checker":
193179
workflow_restart_checker(opts.workflow, commit=opts.commit, days=opts.days)
194-
elif opts.subcommand == "do-restart":
195-
do_restart_workflow(opts.workflow, commit=opts.commit)
196180

197181

198182
if __name__ == "__main__":

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/autorevert_checker.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ def has_pending_jobs(self) -> bool:
4545
"""Check if any jobs are not yet completed (queued/in_progress)."""
4646
return any(j.status != "completed" for j in self.jobs)
4747

48-
@property
48+
@cached_property
4949
def job_base_names(self) -> Set[str]:
50-
if not hasattr(self, "_job_base_names"):
51-
self._job_base_names = self.get_job_base_names()
52-
return self._job_base_names
50+
return self.get_job_base_names()
5351

5452
def normalize_job_name(self, name: str) -> str:
5553
"""Normalize job name to a stable base for matching across commits.
@@ -100,7 +98,6 @@ def workflow_commits(self) -> List[CommitJobs]:
10098

10199
@cached_property
102100
def commit_history(self) -> List[Dict]:
103-
"""Get commit history, fetching if needed."""
104101
return self._fetch_commit_history()
105102

106103
@cached_property

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/event_logger.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def log_autorevert_event(
2222
Uses the misc.autorevert_events table. Best-effort: logs and continues on failure.
2323
"""
2424
try:
25-
client = CHCliFactory().client
2625
columns = [
2726
"workflow",
2827
"action",
@@ -48,7 +47,7 @@ def log_autorevert_event(
4847
]
4948
]
5049
# Specify database explicitly since this table lives in 'misc'
51-
client.insert(
50+
CHCliFactory().client.insert(
5251
table="autorevert_events",
5352
data=data,
5453
column_names=columns,

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/testers/autorevert.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,8 @@ def autorevert_checker(
116116

117117
# For clarity in naming
118118
workflow_name = pattern["workflow_name"]
119-
first_failing = pattern["newer_commits"][
120-
1
121-
] # the older of the two failing commits
122-
previous_commit = pattern[
123-
"older_commit"
124-
] # previously successful commit for the matched job
119+
first_failing = pattern["newer_commits"][1]
120+
previous_commit = pattern["older_commit"]
125121
# Prepare base DTO for logging events for this pattern
126122
evt = AutoRevertEvent(
127123
workflow=workflow_name,

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/testers/do_restart.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/testers/restart_checker.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33

44
def workflow_restart_checker(workflow: str, commit: str = None, days: int = 7) -> None:
5-
checker = WorkflowRestartChecker()
65
if commit:
76
# Check specific commit
8-
result = checker.has_restarted_workflow(workflow, commit)
7+
result = WorkflowRestartChecker().has_restarted_workflow(workflow, commit)
98
print(f"Commit {commit}: {'✓ RESTARTED' if result else '✗ Not restarted'}")
109
else:
1110
# Get all restarted commits in date range
12-
commits = checker.get_restarted_commits(workflow, days)
11+
commits = WorkflowRestartChecker().get_restarted_commits(workflow, days)
1312
print(f"Restarted commits for {workflow} (last {days} days):")
1413
if commits:
1514
for commit in sorted(commits):

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/workflow_checker.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import logging
77
from datetime import datetime, timedelta
8-
from functools import cached_property
98
from typing import Dict, Set
109

1110
from .clickhouse_client_helper import CHCliFactory
@@ -173,6 +172,5 @@ def restart_workflow(self, workflow_name: str, commit_sha: str) -> bool:
173172
logging.error(f"Error dispatching workflow {workflow_name}: {e}")
174173
return False
175174

176-
@cached_property
177175
def resolver(self) -> WorkflowResolver:
178176
return WorkflowResolver.get(f"{self.repo_owner}/{self.repo_name}")

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/workflow_resolver.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from __future__ import annotations
99

1010
import os
11+
import re
1112
from dataclasses import dataclass
12-
from functools import lru_cache
1313
from typing import Optional
1414

1515
import github
@@ -34,6 +34,8 @@ class WorkflowResolver:
3434
wf = resolver.resolve("pull.yml") # file basename
3535
"""
3636

37+
_resolver_cache: dict[str, WorkflowResolver] = {}
38+
3739
def __init__(
3840
self, repo_full_name: str, repository: "github.Repository.Repository"
3941
) -> None:
@@ -43,22 +45,33 @@ def __init__(
4345
self._by_file: dict[str, WorkflowRef] = {}
4446
self._build_indices()
4547

48+
def __new__(
49+
cls, repo_full_name: str, repository: "github.Repository.Repository"
50+
) -> WorkflowResolver:
51+
"""Create or return a cached resolver for the given repo."""
52+
53+
if re.match(r"^[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+$", repo_full_name) is None:
54+
raise ValueError(
55+
f"Invalid repo format: {repo_full_name}. Expected 'owner/repo'."
56+
)
57+
58+
if repository is None or not isinstance(
59+
repository, github.Repository.Repository
60+
):
61+
raise ValueError(f"Invalid repository object for {repo_full_name}.")
62+
63+
if repo_full_name not in cls._resolver_cache:
64+
cls._resolver_cache[repo_full_name] = super().__new__(cls)
65+
return cls._resolver_cache[repo_full_name]
66+
4667
@staticmethod
47-
@lru_cache(maxsize=None)
4868
def get(repo: str) -> "WorkflowResolver":
4969
"""Get a cached resolver for a repo in owner/repo format.
5070
5171
Internally creates a GitHub Repository client using GHClientFactory when
5272
available; otherwise falls back to an anonymous client for public repos.
5373
"""
54-
# Build a client: prefer configured factory; fall back to anonymous
55-
try:
56-
client = GHClientFactory().client
57-
except Exception:
58-
# Anonymous client for public data; may be rate limited
59-
client = github.Github()
60-
61-
repository = client.get_repo(repo)
74+
repository = GHClientFactory().client.get_repo(repo)
6275
return WorkflowResolver(repo_full_name=repo, repository=repository)
6376

6477
def resolve(self, input_name: str) -> Optional[WorkflowRef]:
@@ -76,7 +89,6 @@ def require(self, input_name: str) -> WorkflowRef:
7689
"""Resolve or raise ValueError with a helpful message."""
7790
ref = self.resolve(input_name)
7891
if ref is None:
79-
# Build an informative message with available names
8092
display = ", ".join(sorted(self._by_display))
8193
files = ", ".join(sorted(self._by_file))
8294
raise ValueError(
@@ -85,8 +97,6 @@ def require(self, input_name: str) -> WorkflowRef:
8597
)
8698
return ref
8799

88-
# Internal helpers
89-
90100
def _build_indices(self) -> None:
91101
for w in self._repository.get_workflows():
92102
name = getattr(w, "name", "") or ""

0 commit comments

Comments
 (0)