From a1d93ce5264cbe0f090810387e9841bf22c90a60 Mon Sep 17 00:00:00 2001 From: Kent Sutherland Date: Sat, 10 Nov 2018 12:17:05 -0600 Subject: [PATCH 1/2] Added include_attachments option to import attachments from Redmine --- src/github_issues/github_issue_maker.py | 22 ++++++++++++++++--- src/github_issues/migration_manager.py | 3 +++ src/github_issues/templates/comment.md | 7 ++++++ src/github_issues/templates/description.md | 8 +++++-- .../redmine_issue_downloader.py | 2 +- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/github_issues/github_issue_maker.py b/src/github_issues/github_issue_maker.py index b6485af..35322d9 100644 --- a/src/github_issues/github_issue_maker.py +++ b/src/github_issues/github_issue_maker.py @@ -307,6 +307,7 @@ def make_github_issue(self, redmine_json_fname, **kwargs): include_comments = kwargs.get('include_comments', True) include_assignee = kwargs.get('include_assignee', True) + include_attachments = kwargs.get('include_attachments', True) json_str = open(redmine_json_fname, 'rU').read() rd = json.loads(json_str) # The redmine issue as a python dict @@ -321,6 +322,10 @@ def make_github_issue(self, redmine_json_fname, **kwargs): author_name = rd.get('author', {}).get('name', None) author_github_username = self.format_name_for_github(author_name) + attachments = [] + + if include_attachments: + attachments = rd.get('attachments') desc_dict = {'description' : translate_for_github(rd.get('description', 'no description'))\ , 'redmine_link' : self.format_redmine_issue_link(rd.get('id'))\ @@ -328,7 +333,8 @@ def make_github_issue(self, redmine_json_fname, **kwargs): , 'start_date' : rd.get('start_date', None)\ , 'author_name' : author_name\ , 'author_github_username' : author_github_username\ - , 'redmine_assignee' : self.get_redmine_assignee_name(rd) + , 'redmine_assignee' : self.get_redmine_assignee_name(rd)\ + , 'attachments' : attachments } description_info = template.render(desc_dict) @@ -377,7 +383,7 @@ def make_github_issue(self, redmine_json_fname, **kwargs): if include_comments: journals = rd.get('journals', None) if journals: - self.add_comments_for_issue(issue_obj.number, journals) + self.add_comments_for_issue(issue_obj.number, journals, attachments) # @@ -410,7 +416,7 @@ def is_redmine_issue_closed(self, redmine_issue_dict): - def add_comments_for_issue(self, issue_num, journals): + def add_comments_for_issue(self, issue_num, journals, attachments): """ Add comments """ @@ -427,11 +433,21 @@ def add_comments_for_issue(self, issue_num, journals): author_name = j.get('user', {}).get('name', None) author_github_username = self.format_name_for_github(author_name) + comment_attachments = [] + + # Find attachments that were added with this comment + for d in j.get('details'): + if d.get('property') == 'attachment': + attachment = next((x for x in attachments if str(x.get('id')) == d.get('name')), None) + + if attachment is not None: + comment_attachments.append(attachment) note_dict = { 'description' : translate_for_github(notes)\ , 'note_date' : j.get('created_on', None)\ , 'author_name' : author_name\ , 'author_github_username' : author_github_username\ + , 'attachments' : comment_attachments\ } comment_info = comment_template.render(note_dict) diff --git a/src/github_issues/migration_manager.py b/src/github_issues/migration_manager.py index 04931c1..a5741cd 100644 --- a/src/github_issues/migration_manager.py +++ b/src/github_issues/migration_manager.py @@ -28,6 +28,7 @@ def __init__(self, redmine_json_directory, redmine2github_map_file, **kwargs): self.include_comments = kwargs.get('include_comments', True) self.include_assignee = kwargs.get('include_assignee', True) + self.include_attachments = kwargs.get('include_attachments', True) self.user_mapping_filename = kwargs.get('user_mapping_filename', None) @@ -196,6 +197,7 @@ def migrate_issues(self): json_fname_fullpath = os.path.join(self.redmine_json_directory, json_fname) gm_kwargs = { 'include_assignee' : self.include_assignee \ , 'include_comments' : self.include_comments \ + , 'include_attachments' : self.include_attachments \ } github_issue_number = gm.make_github_issue(json_fname_fullpath, **gm_kwargs) @@ -215,6 +217,7 @@ def migrate_issues(self): , redmine_issue_end_number=5000\ #, user_mapping_filename=USER_MAP_FILE # optional , include_assignee=False # Optional. Assignee must be in the github repo and USER_MAP_FILE above + , include_attachments=True # optional , label_mapping_filename=LABEL_MAP_FILE # optional #, milestone_mapping_filename=MILESTONE_MAP_FILE # optional ) diff --git a/src/github_issues/templates/comment.md b/src/github_issues/templates/comment.md index 39a9499..7b54812 100644 --- a/src/github_issues/templates/comment.md +++ b/src/github_issues/templates/comment.md @@ -7,3 +7,10 @@ Original Redmine Comment --- {{ description }} + +{% if attachments|length > 0 -%} +--- +{% for attachment in attachments %} +- [{{ attachment.filename }}]({{ attachment.content_url }}) ({{ attachment.author.name }}) +{%- endfor %} +{%- endif %} diff --git a/src/github_issues/templates/description.md b/src/github_issues/templates/description.md index 0b6b458..426836b 100644 --- a/src/github_issues/templates/description.md +++ b/src/github_issues/templates/description.md @@ -10,5 +10,9 @@ {{ description }} - - +{% if attachments|length > 0 -%} +--- +{% for attachment in attachments %} +- [{{ attachment.filename }}]({{ attachment.content_url }}) ({{ attachment.author.name }}) +{%- endfor %} +{%- endif %} diff --git a/src/redmine_ticket/redmine_issue_downloader.py b/src/redmine_ticket/redmine_issue_downloader.py index 4446d94..125de2d 100644 --- a/src/redmine_ticket/redmine_issue_downloader.py +++ b/src/redmine_ticket/redmine_issue_downloader.py @@ -280,7 +280,7 @@ def get_single_issue(self, issue_id): :returns: json string with issue information """ # test using .issue.get - issue = self.redmine_conn.issue.get(issue_id, include='children,journals,watchers,relations') + issue = self.redmine_conn.issue.get(issue_id, include='children,attachments,journals,watchers,relations') json_str = json.dumps(issue._attributes, indent=4) msg('Issue retrieved: %s' % issue_id) return json_str From 63a839a69e189f25d873627c9f0a3a81cf553c3f Mon Sep 17 00:00:00 2001 From: Kent Sutherland Date: Sat, 10 Nov 2018 12:17:52 -0600 Subject: [PATCH 2/2] Added project to get_label_names to make it easier to assign labels based on project This is useful when importing multiple separate Redmine projects into a single repository --- src/github_issues/label_helper.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/github_issues/label_helper.py b/src/github_issues/label_helper.py index 9ef5206..9bbba75 100644 --- a/src/github_issues/label_helper.py +++ b/src/github_issues/label_helper.py @@ -217,6 +217,11 @@ def get_label_names(self, redmine_issue_dict, non_formatted=False): if category_label_name: label_names.append(category_label_name) + # Add project + project_label_name = self.get_label_from_id_name(redmine_issue_dict, 'project', 'Project:', non_formatted) + if project_label_name: + label_names.append(project_label_name) + # Add component # "custom_fields": [ # {