From 235d8012461bb1eeb3d9658aa95ed7b81eac403d Mon Sep 17 00:00:00 2001 From: jmastiholimath Date: Thu, 2 Jun 2016 21:43:56 +0530 Subject: [PATCH 1/3] update issues and comments of source repo to destination repo. --- gh-issues-import.py | 86 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/gh-issues-import.py b/gh-issues-import.py index ee354e0..8ed238b 100755 --- a/gh-issues-import.py +++ b/gh-issues-import.py @@ -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') @@ -278,10 +280,10 @@ 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'] @@ -289,16 +291,28 @@ def import_comments(comments, issue_number): 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 @@ -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'] @@ -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: + result_issue = matching_issue + + url="issues/%s" % 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 @@ -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("") From aed6e8ef40062b5c7ab31c96e9c24a85b8d5f132 Mon Sep 17 00:00:00 2001 From: jmastiholimath Date: Fri, 3 Jun 2016 13:34:57 +0530 Subject: [PATCH 2/3] Editing issues based on source id. --- gh-issues-import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gh-issues-import.py b/gh-issues-import.py index 8ed238b..e6eecf7 100755 --- a/gh-issues-import.py +++ b/gh-issues-import.py @@ -430,7 +430,7 @@ def get_label_by_name(name): if matching_issue is not None: result_issue = matching_issue - url="issues/%s" % issue['number'] + 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'])) From 49d22721cd53f14b6eaad2adb5e1682d2257580d Mon Sep 17 00:00:00 2001 From: jmastiholimath Date: Mon, 8 Aug 2016 16:08:54 +0530 Subject: [PATCH 3/3] If issue is closed at target repo. Instead of edit, create a new one. --- gh-issues-import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gh-issues-import.py b/gh-issues-import.py index e6eecf7..57ca8a6 100755 --- a/gh-issues-import.py +++ b/gh-issues-import.py @@ -427,7 +427,7 @@ def get_label_by_name(name): result_issue = None - if matching_issue is not None: + if matching_issue is not None and matching_issue['state']=='open': result_issue = matching_issue url="issues/%s" % matching_issue['number']