-
-
Save jonnyspicer/6ca946d1c81fd76d242517572416a017 to your computer and use it in GitHub Desktop.
Quick script that encourages you to make predictions about a pull request, hopefully murphijitsuing them a little.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from datetime import datetime, timedelta | |
import os | |
import tempfile | |
import subprocess | |
from github import Github | |
from dotenv import load_dotenv | |
QUESTIONS = [ | |
"Will this CR be approved by the reviewers with no changes needed?", | |
"Will this commit be reverted in the next 24 hours?", | |
"Will additional work later be required in order to complete this task?", | |
"Will I uncover unexpected behaviour in production in the next week?" | |
] | |
def get_user_forecast(question): | |
while True: | |
try: | |
forecast = float(input("Enter your forecast (0-100) for \"" + question + "\":")) | |
if 0 <= forecast <= 100: | |
return forecast / 100 # Convert to decimal between 0 and 1 | |
else: | |
print("Please enter a number between 0 and 100.") | |
except ValueError: | |
print("Invalid input. Please enter a number.") | |
def create_fatebook_question(question): | |
# Get Fatebook API key from environment variable | |
api_key = os.getenv('FATEBOOK_API_KEY') | |
# Set up the question details | |
title = question | |
resolve_by = (datetime.now() + timedelta(days=7)).strftime('%Y-%m-%d') | |
tag = "murphy-pr-autogenerated" | |
forecast = get_user_forecast(question) | |
# Construct the API URL | |
base_url = "https://fatebook.io/api/v0/createQuestion" | |
# Set up the parameters | |
params = { | |
'apiKey': api_key, | |
'title': title, | |
'resolveBy': resolve_by, | |
'tags': tag, | |
'forecast': forecast, | |
} | |
# Make the API call | |
try: | |
response = requests.get(base_url, params=params) | |
print(f"Status Code: {response.status_code}") | |
if response.status_code == 200: | |
print("Question created successfully.") | |
return True | |
else: | |
print(f"Failed to create question. Status code: {response.status_code}") | |
return False | |
except requests.exceptions.RequestException as e: | |
print(f"Error making request: {e}") | |
return False | |
def get_editor(): | |
return os.environ.get('EDITOR', 'nano') # Default to nano if EDITOR is not set | |
def edit_in_editor(initial_message=""): | |
editor = get_editor() | |
with tempfile.NamedTemporaryFile(mode='w+', suffix=".md", delete=False) as tf: | |
tf.write(initial_message) | |
tf.flush() | |
tf_name = tf.name | |
try: | |
subprocess.call([editor, tf_name]) | |
with open(tf_name, 'r') as tf: | |
edited_message = tf.read() | |
return edited_message | |
finally: | |
os.unlink(tf_name) | |
def create_pull_request(pr_title): | |
# Get GitHub token and repository information from environment variables | |
github_token = os.getenv('GITHUB_TOKEN') | |
repo_name = os.getenv('GITHUB_REPO') | |
assignee = os.getenv('PR_ASSIGNEE', '@me') # Default to @me if not specified | |
base_branch = os.getenv('BASE_BRANCH', 'main') # Default to 'main' if not specified | |
reviewer = os.getenv('PR_REVIEWER') | |
template = os.getenv('PR_TEMPLATE', '.github/pull_request_template.md') # Default to 'pull_request_template.md' if not specified | |
# Initialize GitHub client | |
g = Github(github_token) | |
repo = g.get_repo(repo_name) | |
# Get the current branch name | |
current_branch = os.popen('git rev-parse --abbrev-ref HEAD').read().strip() | |
# Handle PR body | |
if template and os.path.exists(template): | |
with open(template, 'r') as f: | |
template_content = f.read() | |
print(f"Using template from {template}") | |
else: | |
template_content = "" | |
user_choice = input("Enter PR body [(e) to launch editor, Enter to skip]: ") | |
if user_choice.lower() == 'e': | |
pr_body = edit_in_editor(template_content) | |
elif template_content: | |
pr_body = template_content | |
else: | |
pr_body = "" | |
# Create the pull request | |
pr = repo.create_pull(title=pr_title, body=pr_body, head=current_branch, base=base_branch, draft=True) | |
# Assign the pull request if an assignee is specified | |
if assignee: | |
pr.add_to_assignees(assignee) | |
# Add reviewer if specified | |
if reviewer: | |
pr.create_review_request([reviewer]) | |
print(f"Pull request created: {pr.html_url}") | |
if __name__ == "__main__": | |
load_dotenv() | |
print("Did you remember to push your changes to the remote branch?") | |
pr_title = input("Enter the pull request title: ") | |
for question in QUESTIONS: | |
create_fatebook_question(pr_title + ": " + question) | |
create_pull_request(pr_title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment