Skip to content

Commit

Permalink
scripts/merge-pr.py: Manually map Github username to email address
Browse files Browse the repository at this point in the history
You can get pretty much any old email address out of Github API, so
let's just map the emails manually.
  • Loading branch information
penberg committed Sep 22, 2024
1 parent e910c4b commit dc3586c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"penberg": {
"name": "Pekka Enberg",
"email": "[email protected]"
}
}
27 changes: 16 additions & 11 deletions scripts/merge-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,43 @@
import subprocess
import tempfile
import textwrap

import json

def run_command(command):
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
return output.decode('utf-8').strip(), error.decode('utf-8').strip(), process.returncode

def load_user_mapping(file_path='.github.json'):
if os.path.exists(file_path):
with open(file_path, 'r') as f:
return json.load(f)
return {}

user_mapping = load_user_mapping()

def get_user_email(g, username):
if username in user_mapping:
return f"{user_mapping[username]['name']} <{user_mapping[username]['email']}>"

try:
user = g.get_user(username)
name = user.name if user.name else username
if user.email:
return user.email
return f"{name} <{user.email}>"
# If public email is not available, try to get from events
events = user.get_events()
for event in events:
if event.type == "PushEvent" and event.payload.get("commits"):
for commit in event.payload["commits"]:
if commit.get("author") and commit["author"].get("email"):
return commit["author"]["email"]
return f"{name} <{commit['author']['email']}>"
except Exception as e:
print(f"Error fetching email for user {username}: {str(e)}")

# If we couldn't find an email, return a noreply address
return f"{username}@users.noreply.github.com"

return f"{username} <{username}@users.noreply.github.com>"

def get_pr_info(g, repo, pr_number):
pr = repo.get_pull(int(pr_number))
Expand All @@ -55,9 +65,7 @@ def get_pr_info(g, repo, pr_number):
for review in reviews:
if review.state == 'APPROVED':
reviewer = review.user
reviewer_name = reviewer.name if reviewer.name else reviewer.login
reviewer_email = get_user_email(g, reviewer.login)
reviewed_by.append(f"{reviewer_name} <{reviewer_email}>")
reviewed_by.append(get_user_email(g, reviewer.login))

return {
'number': pr.number,
Expand All @@ -68,7 +76,6 @@ def get_pr_info(g, repo, pr_number):
'reviewed_by': reviewed_by
}


def wrap_text(text, width=72):
lines = text.split('\n')
wrapped_lines = []
Expand All @@ -83,7 +90,6 @@ def wrap_text(text, width=72):
wrapped_lines.extend(textwrap.wrap(line, width=width))
return '\n'.join(wrapped_lines)


def merge_pr(pr_number):
# GitHub authentication
token = os.getenv('GITHUB_TOKEN')
Expand Down Expand Up @@ -147,7 +153,6 @@ def merge_pr(pr_number):
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>")
Expand Down

0 comments on commit dc3586c

Please sign in to comment.