Skip to content

Commit

Permalink
simplify bot script
Browse files Browse the repository at this point in the history
  • Loading branch information
msaroufim committed Nov 5, 2024
1 parent ae5856e commit c343162
Showing 1 changed file with 6 additions and 55 deletions.
61 changes: 6 additions & 55 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,23 @@
from github import Github
import os
import time
from github import GithubException

# Load environment variables
load_dotenv()

def trigger_github_action(file_path='train.py'):
def trigger_github_action():
"""
Triggers a GitHub action after updating train.py
Just triggers the GitHub action without modifying any files
Returns the run ID for monitoring
"""
# Initialize GitHub client
gh = Github(os.getenv('GITHUB_TOKEN'))
repo = gh.get_repo(os.getenv('GITHUB_REPO'))

try:
# Read the local train.py file
with open(file_path, 'r') as file:
content = file.read()

# Update or create train.py in the repository
try:
file = repo.get_contents("train.py")
repo.update_file(
"train.py",
"Update train.py via script",
content,
file.sha
)
except GithubException as e:
if e.status == 404: # File doesn't exist yet
repo.create_file(
"train.py",
"Create train.py via script",
content
)
else:
raise

try:
# Trigger the workflow
workflow = repo.get_workflow("train_workflow.yml")
run = workflow.create_dispatch("main")
return run.id
except GithubException as e:
if e.status == 404:
print("Error: Workflow file 'train_workflow.yml' not found in repository")
print("Please ensure you have created .github/workflows/train_workflow.yml")
print("Check the README for the correct workflow file content")
else:
print(f"GitHub API Error: {e.data.get('message', str(e))}")
return None

# Trigger the workflow
workflow = repo.get_workflow("train_workflow.yml")
run = workflow.create_dispatch("main")
return run.id
except Exception as e:
print(f"Error: {str(e)}")
return None
Expand All @@ -76,20 +41,6 @@ def check_workflow_status(run_id):
time.sleep(30)

if __name__ == "__main__":
# Validate environment variables
if not os.getenv('GITHUB_TOKEN'):
print("Error: GITHUB_TOKEN not found in .env file")
exit(1)
if not os.getenv('GITHUB_REPO'):
print("Error: GITHUB_REPO not found in .env file")
exit(1)

# Check if train.py exists locally
if not os.path.exists('train.py'):
print("Error: train.py not found in current directory")
exit(1)

# Trigger the action
run_id = trigger_github_action()

if run_id:
Expand Down

0 comments on commit c343162

Please sign in to comment.