From 0f532d3eb7f2fb63d3780837e5e56738e7fc6331 Mon Sep 17 00:00:00 2001 From: Marcelo Freitas Date: Fri, 17 Dec 2021 20:52:00 -0300 Subject: [PATCH 1/4] Indent issue body when working with multi-issue file --- repobee_feedback/feedback.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/repobee_feedback/feedback.py b/repobee_feedback/feedback.py index 6014698..1425416 100644 --- a/repobee_feedback/feedback.py +++ b/repobee_feedback/feedback.py @@ -11,6 +11,7 @@ import re import sys import argparse +from textwrap import indent from typing import Iterable, Tuple, List, Mapping import repobee_plug as plug @@ -18,7 +19,7 @@ PLUGIN_NAME = "feedback" BEGIN_ISSUE_PATTERN = r"#ISSUE#(.*?)#(.*)" - +INDENTATION_STR = " >" def callback(args: argparse.Namespace, api: plug.PlatformAPI) -> None: repo_name_to_team: Mapping[str, plug.StudentTeam] = { @@ -110,11 +111,10 @@ def command(self, api: plug.PlatformAPI): def _ask_for_open(issue: plug.Issue, repo_name: str, trunc_len: int) -> bool: plug.echo( - 'Processing issue "{}" for {}: {}{}'.format( + '\nProcessing issue "{}" for {}:\n{}'.format( issue.title, repo_name, - issue.body[:trunc_len], - "[...]" if len(issue.body) > trunc_len else "", + indent(issue.body[:trunc_len], INDENTATION_STR) ) ) return ( From 95424ac335ae8ef6938e9e51c2432e32020a27d8 Mon Sep 17 00:00:00 2001 From: Marcelo Freitas Date: Fri, 17 Dec 2021 22:03:21 -0300 Subject: [PATCH 2/4] black formatting --- repobee_feedback/feedback.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repobee_feedback/feedback.py b/repobee_feedback/feedback.py index 1425416..0629b5a 100644 --- a/repobee_feedback/feedback.py +++ b/repobee_feedback/feedback.py @@ -21,6 +21,7 @@ BEGIN_ISSUE_PATTERN = r"#ISSUE#(.*?)#(.*)" INDENTATION_STR = " >" + def callback(args: argparse.Namespace, api: plug.PlatformAPI) -> None: repo_name_to_team: Mapping[str, plug.StudentTeam] = { plug.generate_repo_name( @@ -114,7 +115,7 @@ def _ask_for_open(issue: plug.Issue, repo_name: str, trunc_len: int) -> bool: '\nProcessing issue "{}" for {}:\n{}'.format( issue.title, repo_name, - indent(issue.body[:trunc_len], INDENTATION_STR) + indent(issue.body[:trunc_len], INDENTATION_STR), ) ) return ( From 819e470f51749ac160bf460eca688512a2f78292 Mon Sep 17 00:00:00 2001 From: Marcelo Freitas Date: Sun, 19 Dec 2021 12:08:15 -0300 Subject: [PATCH 3/4] Add unit tests --- repobee_feedback/feedback.py | 24 +++++++++++++----------- tests/test_feedback.py | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/repobee_feedback/feedback.py b/repobee_feedback/feedback.py index 0629b5a..f4ceb59 100644 --- a/repobee_feedback/feedback.py +++ b/repobee_feedback/feedback.py @@ -19,7 +19,8 @@ PLUGIN_NAME = "feedback" BEGIN_ISSUE_PATTERN = r"#ISSUE#(.*?)#(.*)" -INDENTATION_STR = " >" +INDENTATION_STR = " " +TRUNC_SIGN = "[...]" def callback(args: argparse.Namespace, api: plug.PlatformAPI) -> None: @@ -110,19 +111,20 @@ def command(self, api: plug.PlatformAPI): callback(self.args, api) +def _indent_issue_body(body: str, trunc_len: int): + indented_body = indent(body[:trunc_len], INDENTATION_STR) + body_end = TRUNC_SIGN if len(body) > trunc_len else "" + return indented_body + body_end + + def _ask_for_open(issue: plug.Issue, repo_name: str, trunc_len: int) -> bool: - plug.echo( - '\nProcessing issue "{}" for {}:\n{}'.format( - issue.title, - repo_name, - indent(issue.body[:trunc_len], INDENTATION_STR), - ) + indented_body = _indent_issue_body(issue.body, trunc_len) + issue_description = ( + f'\nProcessing issue "{issue.title}" for {repo_name}:\n{indented_body}' ) + plug.echo(issue_description) return ( - input( - 'Open issue "{}" in repo {}? (y/n) '.format(issue.title, repo_name) - ) - == "y" + input(f'Open issue "{issue.title}" in repo {repo_name}? (y/n) ') == "y" ) diff --git a/tests/test_feedback.py b/tests/test_feedback.py index 790e602..f892ac9 100644 --- a/tests/test_feedback.py +++ b/tests/test_feedback.py @@ -223,3 +223,23 @@ def test_skips_unexpected_issues_in_multi_issues_file( feedback.callback(args=args, api=api_mock) api_mock.create_issue.assert_has_calls(expected_calls, any_order=True) + + +class TestIndentIssueBody: + """Tests for the method that addds indentation to the issue body""" + + def test_issue_indented_and_truncated( + self, + ): + """Test that a long issue body get truncated to a certain length""" + body = "Hello\nworld\n" + indented_body = feedback._indent_issue_body(body, 10) + assert feedback.TRUNC_SIGN in indented_body + + def test_issue_indented_and_not_truncated( + self, + ): + """Test that a short issue body does not get truncated""" + body = "This is an issue\n" + indented_body = feedback._indent_issue_body(body, 20) + assert not feedback.TRUNC_SIGN in indented_body From 8e7d1c3688c4db4aa0ac1d1d92be7ddfb092eb27 Mon Sep 17 00:00:00 2001 From: Marcelo Freitas Date: Mon, 20 Dec 2021 15:00:46 -0300 Subject: [PATCH 4/4] strengthen unit test assertions --- tests/test_feedback.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_feedback.py b/tests/test_feedback.py index f892ac9..67667d4 100644 --- a/tests/test_feedback.py +++ b/tests/test_feedback.py @@ -232,14 +232,17 @@ def test_issue_indented_and_truncated( self, ): """Test that a long issue body get truncated to a certain length""" - body = "Hello\nworld\n" - indented_body = feedback._indent_issue_body(body, 10) - assert feedback.TRUNC_SIGN in indented_body + body = "Hello world\nfrom python\n" + indented_body = feedback._indent_issue_body( + body, trunc_len=len(body) // 2 + ) + assert indented_body.startswith(f"{feedback.INDENTATION_STR}Hello") + assert indented_body.endswith(feedback.TRUNC_SIGN) def test_issue_indented_and_not_truncated( self, ): """Test that a short issue body does not get truncated""" body = "This is an issue\n" - indented_body = feedback._indent_issue_body(body, 20) - assert not feedback.TRUNC_SIGN in indented_body + indented_body = feedback._indent_issue_body(body, 2 * len(body)) + assert indented_body == f"{feedback.INDENTATION_STR}{body}"