Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Python script that generates a pull request body
#!/usr/bin/env python
#
# Print out a summary of the pull request.
#
# This combines the commit messages and removes the hard wrapping so the text renders better in
# Github's UI. The output won't be suitable as is, but provides a good start for moulding into a
# good description.
import subprocess
def main(target_branch: str) -> str:
"""
Return a summary of the pull request.
"""
# Grab full commit bodies.
process = subprocess.Popen(
["git", "log", f"{target_branch}..", "--format=%B"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
stdout, stderr = process.communicate()
# Group paragraphs
combined_lines = []
combined_line_parts = []
for line in stdout.split("\n"):
if line:
combined_line_parts.append(line)
else:
combined_lines.append(" ".join(combined_line_parts))
combined_line_parts = []
# If only one commit, we skip first line as that will be the PR subject.
num_commits = _num_commits(target_branch)
if num_commits == 1:
combined_lines = combined_lines[1:]
return "\n\n".join(combined_lines)
def _num_commits(target_branch: str) -> int:
"""
Return the number of commits in this pull request.
"""
return int(subprocess.getoutput(f"git log {target_branch}.. --oneline | wc -l"))
if __name__ == "__main__":
print(main(target_branch="origin/master"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment