Skip to content

Commit

Permalink
Merge pull request #252 from release-engineering/refactor-reformattin…
Browse files Browse the repository at this point in the history
…g-code

Refactor:  use common code for reformatting GitHub Issues and PRs
  • Loading branch information
webbnh authored Dec 5, 2024
2 parents fb57cfb + 1c22b51 commit 2d068cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 67 deletions.
59 changes: 35 additions & 24 deletions sync2jira/upstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,49 +248,60 @@ def reformat_github_issue(issue, upstream, github_client):
else:
# We have multiple comments and need to make api call to get them
repo = github_client.get_repo(upstream)
comments = []
github_issue = repo.get_issue(number=issue['number'])
for comment in github_issue.get_comments():
# First make API call to get the users name
comments.append({
'author': comment.user.name or comment.user.login,
'name': comment.user.login,
'body': comment.body,
'id': comment.id,
'date_created': comment.created_at,
'changed': None
})
# Assign the message with the newly formatted comments :)
issue['comments'] = comments
issue['comments'] = reformat_github_comments(github_issue.get_comments())

# Update the rest of the parts
reformat_github_common(issue, github_client)


def reformat_github_comments(comments):
"""Helper function which encapsulates reformatting comments"""
return [
{
'author': comment.user.name or comment.user.login,
'name': comment.user.login,
'body': comment.body,
'id': comment.id,
'date_created': comment.created_at,
'changed': None
} for comment in comments
]


def reformat_github_common(item, github_client):
"""Helper function which tweaks the data format of the parts of Issues and
PRs which are common so that they better match Pagure
"""
# Update reporter:
# Search for the user
reporter = github_client.get_user(issue['user']['login'])
reporter = github_client.get_user(item['user']['login'])
# Update the reporter field in the message (to match Pagure format)
if reporter.name:
issue['user']['fullname'] = reporter.name
item['user']['fullname'] = reporter.name
else:
issue['user']['fullname'] = issue['user']['login']
item['user']['fullname'] = item['user']['login']

# Update assignee(s):
assignees = []
for person in issue['assignees']:
for person in item.get('assignees', []):
assignee = github_client.get_user(person['login'])
assignees.append({'fullname': assignee.name})
# Update the assignee field in the message (to match Pagure format)
issue['assignees'] = assignees
item['assignees'] = assignees

# Update the label field in the message (to match Pagure format)
if issue['labels']:
# loop through all the labels on GitHub and add them
if item['labels']:
# Loop through all the labels on GitHub and add them
# to the new label list and then reassign the message
new_label = []
for label in issue['labels']:
for label in item['labels']:
new_label.append(label['name'])
issue['labels'] = new_label
item['labels'] = new_label

# Update the milestone field in the message (to match Pagure format)
if issue.get('milestone'):
issue['milestone'] = issue['milestone']['title']
if item.get('milestone'):
item['milestone'] = item['milestone']['title']


def generate_github_items(api_method, upstream, config):
Expand Down
46 changes: 3 additions & 43 deletions sync2jira/upstream_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,47 +87,7 @@ def reformat_github_pr(pr, upstream, github_client):
else:
# We have multiple comments and need to make api call to get them
repo = github_client.get_repo(upstream)
comments = []
github_pr = repo.get_pull(number=pr['number'])
for comment in github_pr.get_issue_comments():
# First make API call to get the users name
comments.append({
'author': comment.user.name or comment.user.login,
'name': comment.user.login,
'body': comment.body,
'id': comment.id,
'date_created': comment.created_at,
'changed': None
})
# Assign the message with the newly formatted comments :)
pr['comments'] = comments

# Update reporter:
# Search for the user
reporter = github_client.get_user(pr['user']['login'])
# Update the reporter field in the message (to match Pagure format)
if reporter.name:
pr['user']['fullname'] = reporter.name
else:
pr['user']['fullname'] = pr['user']['login']

# Update assignee(s):
assignees = []
for person in pr.get('assignees', []):
assignee = github_client.get_user(person['login'])
assignees.append({'fullname': assignee.name})
# Update the assignee field in the message (to match Pagure format)
pr['assignees'] = assignees

# Update the label field in the message (to match Pagure format)
if pr['labels']:
# Loop through all the labels on GitHub and add them
# to the new label list and then reassign the message
new_label = []
for label in pr['labels']:
new_label.append(label['name'])
pr['labels'] = new_label

# Update milestone:
if pr.get('milestone'):
pr['milestone'] = pr['milestone']['title']
pr['comments'] = u_issue.reformat_github_comments(github_pr.get_issue_comments())

u_issue.reformat_github_common(pr, github_client)

0 comments on commit 2d068cd

Please sign in to comment.