Skip to content

Commit

Permalink
scripts/merge-pr.py: Use temporal file for commit message
Browse files Browse the repository at this point in the history
...fixes problems like eating perfectly valid Markdown and stuff from
pull request descriptions.
  • Loading branch information
penberg committed Aug 1, 2024
1 parent ca00953 commit 296d6cd
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions scripts/merge-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
# ```
# pip install PyGithub
# ```

import sys
import re
from github import Github
import os
import subprocess
import tempfile

def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Expand All @@ -34,55 +34,64 @@ def get_pr_info(repo, pr_number):
def merge_pr(pr_number):
# GitHub authentication
token = os.getenv('GITHUB_TOKEN')

g = Github(token)

# Get the repository
repo_name = os.getenv('GITHUB_REPOSITORY')
if not repo_name:
print("Error: GITHUB_REPOSITORY environment variable not set")
sys.exit(1)

repo = g.get_repo(repo_name)

# Get PR information
pr_info = get_pr_info(repo, pr_number)

# Format commit message
commit_message = f"Merge '{pr_info['title']}' from {pr_info['author']}\n\n{pr_info['body']}\n\nCloses #{pr_info['number']}"


# Create a temporary file for the commit message
with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file:
temp_file.write(commit_message)
temp_file_path = temp_file.name

# Fetch the PR branch
cmd = f"git fetch origin pull/{pr_number}/head:{pr_info['head']}"
output, error, returncode = run_command(cmd)
if returncode != 0:
print(f"Error fetching PR branch: {error}")
os.unlink(temp_file_path)
sys.exit(1)

# Checkout main branch
cmd = "git checkout main"
output, error, returncode = run_command(cmd)
if returncode != 0:
print(f"Error checking out main branch: {error}")
os.unlink(temp_file_path)
sys.exit(1)

# Merge the PR
cmd = f"git merge --no-ff {pr_info['head']} -m \"{commit_message}\""
cmd = f"git merge --no-ff {pr_info['head']} -F {temp_file_path}"
output, error, returncode = run_command(cmd)
if returncode != 0:
print(f"Error merging PR: {error}")
os.unlink(temp_file_path)
sys.exit(1)


# Clean up the temporary file
os.unlink(temp_file_path)

print("Pull request merged successfully!")
print(f"Merge commit message:\n{commit_message}")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python merge_pr.py <pr_number>")
sys.exit(1)

pr_number = sys.argv[1]
if not re.match(r'^\d+$', pr_number):
print("Error: PR number must be a positive integer")
sys.exit(1)
merge_pr(pr_number)

merge_pr(pr_number)

0 comments on commit 296d6cd

Please sign in to comment.