Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update issues and comments of source repo to destination repo. #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 68 additions & 18 deletions gh-issues-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ def send_request(which, url, post_data=None):
if post_data is not None:
post_data = json.dumps(post_data).encode("utf-8")


full_url = "%s/%s" % (config.get(which, 'url'), url)

req = urllib.request.Request(full_url, post_data)

username = config.get(which, 'username')
Expand Down Expand Up @@ -278,27 +280,39 @@ def import_label(source):
print("Successfully created label '%s'" % result_label['name'])
return result_label

def import_comments(comments, issue_number):
def import_comments(comments, issue_number, target_comments):
result_comments = []
for comment in comments:

template_data = {}
template_data['user_name'] = comment['user']['login']
template_data['user_url'] = comment['user']['html_url']
template_data['user_avatar'] = comment['user']['avatar_url']
template_data['date'] = format_date(comment['created_at'])
template_data['url'] = comment['html_url']
template_data['body'] = comment['body']

comment['body'] = format_comment(template_data)

result_comment = send_request('target', "issues/%s/comments" % issue_number, comment)
result_comments.append(result_comment)

result_comment = None
comment_exist = False
for i in target_comments:
if template_data['date'] in i['body']:
url= "issues/comments/%s" % i['id']
result_comment = send_request('target', url, comment)
print("Comments has beed Edited")
comment_exist = True
break

if comment_exist is False:

result_comment = send_request('target', "issues/%s/comments" % issue_number, comment)
result_comments.append(result_comment)

return result_comments

# Will only import milestones and issues that are in use by the imported issues, and do not exist in the target repository
def import_issues(issues):
def import_issues(issues, target_issues):

state.current = state.GENERATING

Expand All @@ -320,10 +334,11 @@ def get_label_by_name(name):
new_labels = []

for issue in issues:

new_issue = {}
new_issue['title'] = issue['title']

new_issue['number'] = issue['number']

# Temporary fix for marking closed issues
if issue['closed_at']:
new_issue['title'] = "[CLOSED] " + new_issue['title']
Expand Down Expand Up @@ -402,14 +417,35 @@ def get_label_by_name(name):
issue_labels.append(label['name'])
issue['labels'] = issue_labels
del issue['label_objects']

matching_issue = None
for i in target_issues:
if 'source_number' in i:
if i['source_number'] == issue['number']:
matching_issue = i
break

result_issue = None

result_issue = send_request('target', "issues", issue)
print("Successfully created issue '%s'" % result_issue['title'])

if matching_issue is not None and matching_issue['state']=='open':
result_issue = matching_issue

url="issues/%s" % matching_issue['number']
result_issue = send_request('target', url, issue)
print("Issue updated'%s' because it already exists. Source issue # -> %s and Target issue # -> %s" % (issue['title'], issue['number'], matching_issue['number']))

else:
result_issue = send_request('target', "issues", issue)
print("Successfully created issue '%s'" % result_issue['title'])

result_issue['comments'] = get_comments_on_issue('target', result_issue)
if 'comments' in issue:
result_comments = import_comments(issue['comments'], result_issue['number'])
result_comments = import_comments(issue['comments'], result_issue['number'], result_issue['comments'])
print(" > Successfully added", len(result_comments), "comments.")


else:
print(" > No comments for this issue")

result_issues.append(result_issue)

state.current = state.IMPORT_COMPLETE
Expand All @@ -423,27 +459,41 @@ def get_label_by_name(name):

issue_ids = init_config()
issues = []
target_issues = []

state.current = state.FETCHING_ISSUES

# Argparser will prevent us from getting both issue ids and specifying issue state, so no duplicates will be added
if (len(issue_ids) > 0):
issues += get_issues_by_id('source', issue_ids)
target_issues += get_issues_by_state('target', 'open')
target_issues += get_issues_by_state('target', 'closed')

if config.getboolean('settings', 'import-open-issues'):
issues += get_issues_by_state('source', 'open')

target_issues += get_issues_by_state('target', 'open')

if config.getboolean('settings', 'import-closed-issues'):
issues += get_issues_by_state('source', 'closed')

target_issues += get_issues_by_state('target', 'closed')

# Sort issues based on their original `id` field
# Confusing, but taken from http://stackoverflow.com/a/2878123/617937
issues.sort(key=lambda x:x['number'])


target_issues.sort(key=lambda x:x['number'])

for issue in target_issues:
try:
issue['source_number'] = int(issue['body'].split(str("%s/issues/" % config.get('source', 'repository')))[1].split('_\n')[0])
except:
pass

# Further states defined within the function
# Finally, add these issues to the target repository
import_issues(issues)
import_issues(issues, target_issues)

state.current = state.COMPLETE

print("")