|
1 | | -#!/usr/bin/env python3 |
2 | 1 | import os |
3 | | -import sys |
| 2 | +from github import Github |
4 | 3 | import argparse |
5 | | -from github import Github, GithubException |
6 | 4 |
|
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") |
17 | 8 | if not token: |
18 | | - sys.exit("Error: Please set the GITHUB_TOKEN environment variable.") |
| 9 | + raise EnvironmentError("GITHUB_TOKEN environment variable is not set.") |
19 | 10 |
|
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 = [] |
27 | 14 |
|
28 | | - # Iterate over all repositories in the organization |
29 | 15 | 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) |
33 | 18 |
|
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 |
40 | 20 |
|
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 |
43 | 29 |
|
44 | | - print(f"\nProcessing repository: {repo.full_name}") |
| 30 | + print(f"No 'run-struct.yaml' workflow found in repository: {repo.full_name}") |
45 | 31 |
|
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.") |
55 | 36 |
|
56 | | - print(" Found .struct.yaml file.") |
| 37 | + args = parser.parse_args() |
57 | 38 |
|
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 |
67 | 41 |
|
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}'.") |
69 | 45 |
|
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) |
76 | 48 |
|
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}") |
83 | 51 |
|
84 | 52 | if __name__ == "__main__": |
85 | 53 | main() |
0 commit comments