-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend_dad_joke.py
72 lines (63 loc) · 2.8 KB
/
append_dad_joke.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
from openai import AsyncOpenAI
import re
import sys
import subprocess
# Read the last non-dad-joke commit message
def get_last_non_dad_joke_commit():
log_output = os.popen('git log --pretty=%B').read().strip().split('\n')
for message in log_output:
if not re.search(r'chore: add dad joke to README', message, re.IGNORECASE):
return message.strip()
return None
# Generate a dad joke using OpenAI's API based on the commit message
async def generate_dad_joke(commit_message):
prompt = f"Create a dad joke inspired by the following commit message: \"{commit_message}\""
try:
client = AsyncOpenAI(api_key=os.environ['OPENAI_API_KEY'])
completion = await client.chat.completions.create(model="gpt-4-turbo", messages=[{"role": "user", "content": prompt}])
joke = completion.choices[0].message.content.strip()
return joke
except Exception as e:
print(f"Error generating joke: {e}")
sys.exit(1)
async def main():
commit_message = get_last_non_dad_joke_commit()
if not commit_message:
print("No suitable commit message found. Skipping.")
sys.exit(0)
joke = await generate_dad_joke(commit_message)
# Append the joke to the README file
readme_path = 'README.md'
try:
with open(readme_path, 'a') as readme_file:
readme_file.write(f"\n\n## Dad Joke of the Day\n{joke}\n")
print("Dad joke appended to README.")
except FileNotFoundError:
print(f"Error: {readme_path} not found.")
sys.exit(1)
except Exception as e:
print(f"Error appending to README: {e}")
sys.exit(1)
# Commit and push changes
def commit_and_push_changes():
github_token = os.environ.get('GITHUB_TOKEN')
repo_url = os.environ.get('REPO_URL')
repo_user = os.environ.get('REPO_USER')
if not github_token:
print("Error: GITHUB_TOKEN environment variable is not set.")
sys.exit(1)
try:
subprocess.run(["git", "config", "--local", "user.name", "github-actions[bot]"], check=True)
subprocess.run(["git", "config", "--local", "user.email", "github-actions[bot]@users.noreply.github.com"], check=True)
subprocess.run(["git", "add", readme_path], check=True)
subprocess.run(["git", "commit", "-m", "chore: add dad joke to README"], check=True)
subprocess.run(["git", "push", f"https://x-access-token:{github_token}@github.com/{repo_user}/{repo_url}.git"], check=True)
print("Changes committed and pushed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error committing or pushing changes: {e}")
sys.exit(1)
commit_and_push_changes()
if __name__ == "__main__":
import asyncio
asyncio.run(main())