Created
December 23, 2022 15:46
-
-
Save codeinthehole/3fc29fc6f1d9e0d9224e97762ff3537a to your computer and use it in GitHub Desktop.
A Python script that generates a pull request body
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
#!/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