From 1c22b5179527b741f04628df8c80d4d32ef04e95 Mon Sep 17 00:00:00 2001 From: Webb Scales Date: Thu, 26 Sep 2024 14:03:14 -0400 Subject: [PATCH] Refactor: use common code for reformatting GitHub Issues and PRs --- sync2jira/upstream_issue.py | 59 ++++++++++++++++++++++--------------- sync2jira/upstream_pr.py | 46 ++--------------------------- 2 files changed, 38 insertions(+), 67 deletions(-) diff --git a/sync2jira/upstream_issue.py b/sync2jira/upstream_issue.py index e910f99b..056f520b 100644 --- a/sync2jira/upstream_issue.py +++ b/sync2jira/upstream_issue.py @@ -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): diff --git a/sync2jira/upstream_pr.py b/sync2jira/upstream_pr.py index f1b5120f..faf60d5d 100644 --- a/sync2jira/upstream_pr.py +++ b/sync2jira/upstream_pr.py @@ -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)