|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +from github import Github, GithubException |
| 6 | + |
| 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") |
| 17 | + if not token: |
| 18 | + sys.exit("Error: Please set the GITHUB_TOKEN environment variable.") |
| 19 | + |
| 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}") |
| 27 | + |
| 28 | + # Iterate over all repositories in the organization |
| 29 | + for repo in org.get_repos(): |
| 30 | + # Filter for private repositories only |
| 31 | + if not repo.private: |
| 32 | + continue |
| 33 | + |
| 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 |
| 40 | + |
| 41 | + if args.topic not in topics: |
| 42 | + continue |
| 43 | + |
| 44 | + print(f"\nProcessing repository: {repo.full_name}") |
| 45 | + |
| 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 |
| 55 | + |
| 56 | + print(" Found .struct.yaml file.") |
| 57 | + |
| 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 |
| 67 | + |
| 68 | + print(" Found workflow file .github/workflows/run-struct.yaml.") |
| 69 | + |
| 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 |
| 76 | + |
| 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}") |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
0 commit comments