1
1
"""
2
- WorkflowRestartChecker for querying restarted workflows via ClickHouse.
2
+ WorkflowRestartChecker for querying restarted workflows via ClickHouse and
3
+ dispatching workflows via GitHub with consistent workflow name resolution.
3
4
"""
4
5
5
6
import logging
6
7
from datetime import datetime , timedelta
8
+ from functools import cached_property
7
9
from typing import Dict , Set
8
10
9
11
from .clickhouse_client_helper import CHCliFactory
12
+ from .workflow_resolver import WorkflowResolver
10
13
11
14
12
15
class WorkflowRestartChecker :
@@ -28,9 +31,10 @@ def has_restarted_workflow(self, workflow_name: str, commit_sha: str) -> bool:
28
31
Returns:
29
32
bool: True if workflow was restarted (workflow_dispatch with trunk/* branch)
30
33
"""
31
- # Normalize workflow name - remove .yml extension for consistency
32
- normalized_workflow_name = workflow_name .replace (".yml" , "" )
33
- cache_key = f"{ normalized_workflow_name } :{ commit_sha } "
34
+ # Resolve to display name via GitHub (exact display or file name)
35
+ display_name = self .resolver .require (workflow_name ).display_name
36
+
37
+ cache_key = f"{ display_name } :{ commit_sha } "
34
38
if cache_key in self ._cache :
35
39
return self ._cache [cache_key ]
36
40
@@ -54,7 +58,7 @@ def has_restarted_workflow(self, workflow_name: str, commit_sha: str) -> bool:
54
58
"commit_sha" : commit_sha ,
55
59
"workflow_event" : "workflow_dispatch" ,
56
60
"head_branch" : f"trunk/{ commit_sha } " ,
57
- "workflow_name" : normalized_workflow_name ,
61
+ "workflow_name" : display_name ,
58
62
},
59
63
)
60
64
@@ -73,8 +77,7 @@ def get_restarted_commits(self, workflow_name: str, days_back: int = 7) -> Set[s
73
77
Returns:
74
78
Set of commit SHAs that have restarted workflows
75
79
"""
76
- # Normalize workflow name - remove .yml extension for consistency
77
- normalized_workflow_name = workflow_name .replace (".yml" , "" )
80
+ display_name = self .resolver .require (workflow_name ).display_name
78
81
since_date = datetime .now () - timedelta (days = days_back )
79
82
80
83
query = """
@@ -87,14 +90,14 @@ def get_restarted_commits(self, workflow_name: str, days_back: int = 7) -> Set[s
87
90
"""
88
91
89
92
result = CHCliFactory ().client .query (
90
- query , {"workflow_name" : normalized_workflow_name , "since_date" : since_date }
93
+ query , {"workflow_name" : display_name , "since_date" : since_date }
91
94
)
92
95
93
96
commits = {row [0 ] for row in result .result_rows }
94
97
95
98
# Update cache
96
99
for commit_sha in commits :
97
- cache_key = f"{ normalized_workflow_name } :{ commit_sha } "
100
+ cache_key = f"{ display_name } :{ commit_sha } "
98
101
self ._cache [cache_key ] = True
99
102
100
103
return commits
@@ -114,13 +117,10 @@ def restart_workflow(self, workflow_name: str, commit_sha: str) -> bool:
114
117
Returns:
115
118
bool: True if workflow was successfully dispatched, False otherwise
116
119
"""
117
- # Normalize workflow name
118
- normalized_workflow_name = workflow_name .replace (".yml" , "" )
119
-
120
120
# Check if already restarted
121
- if self .has_restarted_workflow (normalized_workflow_name , commit_sha ):
121
+ if self .has_restarted_workflow (workflow_name , commit_sha ):
122
122
logging .warning (
123
- f"Workflow { normalized_workflow_name } already restarted for commit { commit_sha } "
123
+ f"Workflow { workflow_name } already restarted for commit { commit_sha } "
124
124
)
125
125
return False
126
126
@@ -144,33 +144,35 @@ def restart_workflow(self, workflow_name: str, commit_sha: str) -> bool:
144
144
# Use trunk/{sha} tag format
145
145
tag_ref = f"trunk/{ commit_sha } "
146
146
147
- # Add .yml extension for workflow name
148
- workflow_file_name = f" { normalized_workflow_name } .yml"
147
+ # Resolve workflow
148
+ wf_ref = self . resolver . require ( workflow_name )
149
149
150
- # Get repo and workflow objects
151
- repo = client .get_repo (f"{ self .repo_owner } /{ self .repo_name } " )
152
- workflow = repo .get_workflow (workflow_file_name )
153
-
154
- # Dispatch the workflow
155
- workflow .create_dispatch (ref = tag_ref , inputs = {})
150
+ # Dispatch via file name
151
+ client .get_repo (f"{ self .repo_owner } /{ self .repo_name } " ).get_workflow (
152
+ wf_ref .file_name
153
+ ).create_dispatch (ref = tag_ref , inputs = {})
156
154
157
155
# Construct the workflow runs URL
158
156
workflow_url = (
159
157
f"https://github.com/{ self .repo_owner } /{ self .repo_name } "
160
- f"/actions/workflows/{ workflow_file_name } "
158
+ f"/actions/workflows/{ wf_ref . file_name } "
161
159
f"?query=branch%3Atrunk%2F{ commit_sha } "
162
160
)
163
161
logging .info (
164
- f"Successfully dispatched workflow { normalized_workflow_name } for commit { commit_sha } \n "
162
+ f"Successfully dispatched workflow { wf_ref . display_name } for commit { commit_sha } \n "
165
163
f" View at: { workflow_url } "
166
164
)
167
165
168
166
# Invalidate cache for this workflow/commit
169
- cache_key = f"{ normalized_workflow_name } :{ commit_sha } "
167
+ cache_key = f"{ wf_ref . display_name } :{ commit_sha } "
170
168
if cache_key in self ._cache :
171
169
del self ._cache [cache_key ]
172
170
return True
173
171
174
172
except Exception as e :
175
- logging .error (f"Error dispatching workflow { normalized_workflow_name } : { e } " )
173
+ logging .error (f"Error dispatching workflow { workflow_name } : { e } " )
176
174
return False
175
+
176
+ @cached_property
177
+ def resolver (self ) -> WorkflowResolver :
178
+ return WorkflowResolver .get (f"{ self .repo_owner } /{ self .repo_name } " )
0 commit comments