Skip to content

Commit 62ecf7f

Browse files
committed
Improve the way workflow runs are fetched
* Fetch all workflows * Then fetch workflow runs of each workflow * This was we process smaller sets, but faster and returns all the workflows * Performance is improved by 85%
1 parent b6d2fbc commit 62ecf7f

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

app.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,32 @@
1414

1515

1616
def get_workflow_runs(owner, repo, token):
17-
endpoint = f"https://api.github.com/repos/{owner}/{repo}/actions/runs"
17+
endpoint = f"https://api.github.com/repos/{owner}/{repo}/actions/workflows"
1818
headers = {
1919
"Authorization": f"Bearer {token}",
2020
"Accept": "application/vnd.github.v3+json"
2121
}
2222
results = []
2323

24-
with ThreadPoolExecutor(max_workers=10) as executor:
25-
futures = []
26-
page = 1
27-
while True:
28-
future = executor.submit(requests.get, f"{endpoint}?per_page=100&page={page}", headers=headers, timeout=10)
29-
futures.append(future)
30-
if 'next' in future.result().links and page <= 3:
31-
logger.info("Found next page %s, fetching more results...", page)
32-
page += 1
33-
else:
34-
break
35-
for future in futures:
36-
response = future.result()
37-
if response is None:
38-
logger.error("Failed to get response from GitHub API")
39-
elif response.status_code != 200:
40-
logger.error("GitHub API returned status code %d", response.status_code)
41-
else:
42-
data = response.json()
43-
results += data["workflow_runs"]
24+
response = requests.get(endpoint, headers=headers, timeout=10)
25+
if response.status_code != 200:
26+
logger.error("GitHub API returned status code %d", response.status_code)
27+
else:
28+
workflows = response.json()["workflows"]
29+
with ThreadPoolExecutor(max_workers=10) as executor:
30+
futures = []
31+
for workflow in workflows:
32+
future = executor.submit(requests.get, f"{workflow['url']}/runs", headers=headers, timeout=10)
33+
futures.append(future)
34+
for future in futures:
35+
response = future.result()
36+
if response is None:
37+
logger.error("Failed to get response from GitHub API")
38+
elif response.status_code != 200:
39+
logger.error("GitHub API returned status code %d", response.status_code)
40+
else:
41+
data = response.json()
42+
results += data["workflow_runs"]
4443

4544
return results
4645

0 commit comments

Comments
 (0)