Skip to content

Commit dd4803f

Browse files
committed
replace direct requests with github SDK
1 parent 4486c84 commit dd4803f

1 file changed

Lines changed: 35 additions & 48 deletions

File tree

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

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from datetime import datetime, timedelta
88
from typing import Dict, Set
99

10-
import requests
11-
1210
from .clickhouse_client_helper import CHCliFactory
1311

1412

@@ -125,20 +123,20 @@ def restart_workflow(self, workflow_name: str, commit_sha: str) -> bool:
125123
)
126124
return False
127125

128-
# Use GitHub token from environment or client
129-
github_token = os.getenv("GITHUB_TOKEN")
130-
if not github_token:
131-
# Try to get from GitHub client if available
132-
try:
133-
from .github_client_helper import GHClientFactory
126+
# Get GitHub client
127+
try:
128+
from .github_client_helper import GHClientFactory
134129

135-
if GHClientFactory().token_auth_provided:
136-
github_token = GHClientFactory()._token
137-
except Exception:
138-
pass
130+
if not (
131+
GHClientFactory().token_auth_provided
132+
or GHClientFactory().key_auth_provided
133+
):
134+
logging.error("GitHub authentication not configured")
135+
return False
139136

140-
if not github_token:
141-
logging.error("GITHUB_TOKEN not available for workflow dispatch")
137+
client = GHClientFactory().client
138+
except Exception as e:
139+
logging.error(f"Failed to get GitHub client: {e}")
142140
return False
143141

144142
repo_owner = os.getenv("GITHUB_REPO_OWNER", "pytorch")
@@ -148,43 +146,32 @@ def restart_workflow(self, workflow_name: str, commit_sha: str) -> bool:
148146
# Use trunk/{sha} tag format
149147
tag_ref = f"trunk/{commit_sha}"
150148

151-
# Add .yml extension for API call
149+
# Add .yml extension for workflow name
152150
workflow_file_name = f"{normalized_workflow_name}.yml"
153151

154-
url = (
155-
f"https://api.github.com/repos/{repo_owner}/{repo_name}"
156-
f"/actions/workflows/{workflow_file_name}/dispatches"
152+
# Get repo and workflow objects
153+
repo = client.get_repo(f"{repo_owner}/{repo_name}")
154+
workflow = repo.get_workflow(workflow_file_name)
155+
156+
# Dispatch the workflow
157+
workflow.create_dispatch(ref=tag_ref, inputs={})
158+
159+
# Construct the workflow runs URL
160+
workflow_url = (
161+
f"https://github.com/{repo_owner}/{repo_name}"
162+
f"/actions/workflows/{workflow_file_name}"
163+
f"?query=branch%3Atrunk%2F{commit_sha}"
157164
)
158-
headers = {
159-
"Authorization": f"token {github_token}",
160-
"Accept": "application/vnd.github.v3+json",
161-
}
162-
data = {"ref": tag_ref, "inputs": {}}
163-
164-
response = requests.post(url, headers=headers, json=data)
165-
166-
if response.status_code == 204:
167-
# Construct the workflow runs URL
168-
workflow_url = (
169-
f"https://github.com/{repo_owner}/{repo_name}"
170-
f"/actions/workflows/{workflow_file_name}"
171-
f"?query=branch%3Atrunk%2F{commit_sha}"
172-
)
173-
logging.info(
174-
f"Successfully dispatched workflow {normalized_workflow_name} for commit {commit_sha}\n"
175-
f" View at: {workflow_url}"
176-
)
177-
178-
# Invalidate cache for this workflow/commit
179-
cache_key = f"{normalized_workflow_name}:{commit_sha}"
180-
if cache_key in self._cache:
181-
del self._cache[cache_key]
182-
return True
183-
else:
184-
logging.error(
185-
f"Failed to dispatch workflow: {response.status_code} - {response.text}"
186-
)
187-
return False
165+
logging.info(
166+
f"Successfully dispatched workflow {normalized_workflow_name} for commit {commit_sha}\n"
167+
f" View at: {workflow_url}"
168+
)
169+
170+
# Invalidate cache for this workflow/commit
171+
cache_key = f"{normalized_workflow_name}:{commit_sha}"
172+
if cache_key in self._cache:
173+
del self._cache[cache_key]
174+
return True
188175

189176
except Exception as e:
190177
logging.error(f"Error dispatching workflow {normalized_workflow_name}: {e}")

0 commit comments

Comments
 (0)