Skip to content

Commit 8fbc191

Browse files
committed
Refactor GitHub trigger script to improve repository filtering and workflow triggering logic
1 parent 6448b0a commit 8fbc191

1 file changed

Lines changed: 34 additions & 66 deletions

File tree

scripts/github-trigger.py

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,53 @@
1-
#!/usr/bin/env python3
21
import os
3-
import sys
2+
from github import Github
43
import argparse
5-
from github import Github, GithubException
64

7-
def main():
8-
parser = argparse.ArgumentParser(
9-
description="Trigger the 'run-struct' workflow for all private repos in an organization that have a .struct.yaml file and the specified topic."
10-
)
11-
parser.add_argument("org", help="Name of the GitHub organization")
12-
parser.add_argument("topic", help="Repository topic to filter by (e.g., 'struct-enabled')")
13-
args = parser.parse_args()
14-
15-
# Ensure that the GitHub token is set in the environment
16-
token = os.environ.get("GITHUB_TOKEN")
5+
def get_repositories_with_topic(org_name, topic):
6+
"""Fetch all repositories in an organization with a specific topic."""
7+
token = os.getenv("GITHUB_TOKEN")
178
if not token:
18-
sys.exit("Error: Please set the GITHUB_TOKEN environment variable.")
9+
raise EnvironmentError("GITHUB_TOKEN environment variable is not set.")
1910

20-
# Connect to GitHub
21-
g = Github(token)
22-
23-
try:
24-
org = g.get_organization(args.org)
25-
except GithubException as e:
26-
sys.exit(f"Error getting organization '{args.org}': {e}")
11+
github = Github(token)
12+
org = github.get_organization(org_name)
13+
repos_with_topic = []
2714

28-
# Iterate over all repositories in the organization
2915
for repo in org.get_repos():
30-
# Filter for private repositories only
31-
if not repo.private:
32-
continue
16+
if topic in repo.get_topics():
17+
repos_with_topic.append(repo)
3318

34-
# Check if the repository has the specified topic
35-
try:
36-
topics = repo.get_topics()
37-
except GithubException as e:
38-
print(f"Could not retrieve topics for repo {repo.full_name}: {e}")
39-
continue
19+
return repos_with_topic
4020

41-
if args.topic not in topics:
42-
continue
21+
def trigger_workflow(repo):
22+
"""Trigger the 'run-struct.yaml' workflow for a given repository."""
23+
workflows = repo.get_workflows()
24+
for workflow in workflows:
25+
if workflow.path.endswith("run-struct.yaml"):
26+
workflow.create_dispatch(ref=repo.default_branch)
27+
print(f"Triggered workflow for repository: {repo.full_name}")
28+
return
4329

44-
print(f"\nProcessing repository: {repo.full_name}")
30+
print(f"No 'run-struct.yaml' workflow found in repository: {repo.full_name}")
4531

46-
# Check for the existence of .struct.yaml file (in the repo's default branch)
47-
try:
48-
_ = repo.get_contents(".struct.yaml", ref=repo.default_branch)
49-
except GithubException as e:
50-
if e.status == 404:
51-
print(" .struct.yaml file not found. Skipping.")
52-
else:
53-
print(f" Error retrieving .struct.yaml: {e}")
54-
continue
32+
def main():
33+
parser = argparse.ArgumentParser(description="Trigger 'run-struct.yaml' workflow for repositories with a specific topic.")
34+
parser.add_argument("--org", required=True, help="The GitHub organization name.")
35+
parser.add_argument("--topic", required=True, help="The topic to filter repositories by.")
5536

56-
print(" Found .struct.yaml file.")
37+
args = parser.parse_args()
5738

58-
# Check if the workflow file exists at .github/workflows/run-struct.yaml
59-
try:
60-
_ = repo.get_contents(".github/workflows/run-struct.yaml", ref=repo.default_branch)
61-
except GithubException as e:
62-
if e.status == 404:
63-
print(" Workflow file .github/workflows/run-struct.yaml not found. Skipping workflow trigger.")
64-
else:
65-
print(f" Error retrieving workflow file: {e}")
66-
continue
39+
org_name = args.org
40+
topic = args.topic
6741

68-
print(" Found workflow file .github/workflows/run-struct.yaml.")
42+
try:
43+
repos = get_repositories_with_topic(org_name, topic)
44+
print(f"Found {len(repos)} repositories with topic '{topic}'.")
6945

70-
# Retrieve the workflow object (using the file name as identifier)
71-
try:
72-
workflow = repo.get_workflow("run-struct.yaml")
73-
except GithubException as e:
74-
print(f" Error retrieving workflow object: {e}")
75-
continue
46+
for repo in repos:
47+
trigger_workflow(repo)
7648

77-
# Trigger a workflow dispatch event on the default branch
78-
try:
79-
workflow.create_dispatch(ref=repo.default_branch)
80-
print(" Triggered run-struct workflow successfully.")
81-
except GithubException as e:
82-
print(f" Error triggering workflow: {e}")
49+
except Exception as e:
50+
print(f"Error: {e}")
8351

8452
if __name__ == "__main__":
8553
main()

0 commit comments

Comments
 (0)