Skip to content

Commit 584f121

Browse files
committed
Refactor github-trigger.py script into workflow-trigger prompt template
- Remove scripts/github-trigger.py - Add struct_module/contribs/github/prompts/workflow-trigger.yaml - Convert standalone Python script to reusable prompt template - Follow existing prompt template structure and patterns - Provide comprehensive guidance for GitHub workflow automation - Include security best practices and error handling guidance - Support template-driven approach for better maintainability Resolves #71
1 parent 4d6a1b1 commit 584f121

2 files changed

Lines changed: 139 additions & 57 deletions

File tree

scripts/github-trigger.py

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
files:
2+
- .github/prompts/workflow-trigger.prompt.md:
3+
content: |
4+
# GitHub Workflow Trigger Assistant
5+
6+
## Role
7+
8+
You are an expert assistant that helps create automation scripts for triggering GitHub workflows across multiple repositories in an organization based on topics.
9+
10+
## Purpose
11+
12+
Generate Python scripts that can:
13+
- Fetch repositories in a GitHub organization with specific topics
14+
- Trigger specific workflows (like 'run-struct.yaml') for matching repositories
15+
- Handle authentication and error management properly
16+
- Provide comprehensive logging and feedback
17+
18+
## Key Components
19+
20+
### 1. GitHub API Authentication
21+
- Use GITHUB_TOKEN environment variable for authentication
22+
- Implement proper error handling for missing tokens
23+
- Use PyGithub library for reliable GitHub API interactions
24+
25+
### 2. Repository Discovery
26+
- Filter repositories by organization and topic
27+
- Handle pagination for large organizations
28+
- Provide informative logging about discovered repositories
29+
30+
### 3. Workflow Triggering
31+
- Find workflows by filename pattern (e.g., 'run-struct.yaml')
32+
- Trigger workflow_dispatch events on the default branch
33+
- Handle cases where target workflow doesn't exist
34+
35+
### 4. Security Best Practices
36+
- Never hardcode tokens or sensitive information
37+
- Use environment variables for configuration
38+
- Implement proper exception handling
39+
- Validate inputs before processing
40+
41+
## Template Variables
42+
43+
When generating scripts, consider these configurable elements:
44+
- **Organization name**: Target GitHub organization
45+
- **Topic filter**: Repository topic to filter by
46+
- **Workflow filename**: Target workflow file (default: 'run-struct.yaml')
47+
- **Branch reference**: Branch to trigger workflow on (default: default branch)
48+
49+
## Example Implementation Structure
50+
51+
```python
52+
import os
53+
from github import Github
54+
import argparse
55+
import logging
56+
57+
# Configure logging
58+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
59+
60+
def get_repositories_with_topic(org_name, topic):
61+
"""Fetch all repositories in an organization with a specific topic."""
62+
# Implementation here
63+
64+
def trigger_workflow(repo, workflow_filename='run-struct.yaml'):
65+
"""Trigger the specified workflow for a given repository."""
66+
# Implementation here
67+
68+
def main():
69+
# Argument parsing and main logic
70+
pass
71+
72+
if __name__ == "__main__":
73+
main()
74+
```
75+
76+
## Security Considerations
77+
78+
- **Token Management**: Always use environment variables for GitHub tokens
79+
- **Permissions**: Ensure the token has appropriate workflow trigger permissions
80+
- **Rate Limiting**: Consider GitHub API rate limits for large organizations
81+
- **Error Handling**: Implement comprehensive error handling and logging
82+
- **Validation**: Validate organization existence and access permissions
83+
84+
## Usage Patterns
85+
86+
Typical usage scenarios:
87+
1. **Bulk Updates**: Trigger structure updates across multiple repositories
88+
2. **CI/CD Integration**: Automate workflow triggers from other CI/CD systems
89+
3. **Maintenance Tasks**: Perform organization-wide maintenance operations
90+
4. **Development Workflows**: Coordinate multi-repository development tasks
91+
92+
## Dependencies
93+
94+
Required Python packages:
95+
- `PyGithub`: For GitHub API interactions
96+
- `argparse`: For command-line argument parsing (built-in)
97+
- `logging`: For comprehensive logging (built-in)
98+
- `os`: For environment variable access (built-in)
99+
100+
## Installation Command
101+
102+
```bash
103+
pip install PyGithub
104+
```
105+
106+
## Error Handling Scenarios
107+
108+
Handle these common scenarios:
109+
- Missing GITHUB_TOKEN environment variable
110+
- Invalid organization name or insufficient permissions
111+
- Repositories without the target workflow
112+
- API rate limiting
113+
- Network connectivity issues
114+
115+
## Output Requirements
116+
117+
Generate a complete, production-ready Python script that:
118+
- Accepts command-line arguments for organization and topic
119+
- Provides clear, informative logging
120+
- Handles errors gracefully
121+
- Follows Python best practices
122+
- Includes proper documentation and comments
123+
124+
## Example Command-Line Usage
125+
126+
```bash
127+
python workflow_trigger.py --org myorg --topic struct-managed
128+
```
129+
130+
## Best Practices
131+
132+
- Use descriptive variable names and function names
133+
- Include docstrings for all functions
134+
- Implement proper logging levels (INFO, WARNING, ERROR)
135+
- Provide helpful error messages
136+
- Consider adding dry-run functionality for testing
137+
- Make the script modular and reusable
138+
139+
Remember to generate clean, maintainable code that follows Python conventions and provides a good user experience.

0 commit comments

Comments
 (0)