Skip to content

Commit

Permalink
Added support for custom fields in issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin committed Mar 20, 2017
1 parent 28c4b91 commit 777fa6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
16 changes: 13 additions & 3 deletions jiractl/commands/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class JiraIssueMixin(object):
columns = ("project", "id", "key", "summary", "type", "assignee", "status")

@staticmethod
def format_issue(issue):
def format_issue(issue, custom_fields=None):
"""Converts issue to tuple."""
return (

result = (
issue.fields.project.name, issue.id, issue.key, issue.fields.summary,
issue.fields.issuetype.name, issue.fields.assignee.name, issue.fields.status.name,
)
if custom_fields:
result += tuple(getattr(issue.fields, x) for x in custom_fields)
return result


class CreateIssue(JiraIssueMixin, base.JiraShow):
Expand Down Expand Up @@ -76,6 +80,7 @@ def get_parser(self, prog_name):
parser.add_argument("--description", type=base.utf8, help="New issue description")
parser.add_argument("--assignee", type=base.utf8, help="New issue assignee")
parser.add_argument("--status", type=base.utf8, help="Transition issue to status")
parser.add_argument("--fields", nargs='+', metavar="NAME:VALUE", type=base.utf8, help="Custom fields to update")

return parser

Expand All @@ -86,6 +91,8 @@ def take_action(self, parsed_args):
fields["summary"] = parsed_args.summary
if parsed_args.description:
fields["description"] = parsed_args.description
if parsed_args.fields:
fields.update((x.split(':', 2)) for x in parsed_args.fields)

if fields:
self.app.jira.issue(parsed_args.id).update(fields=fields)
Expand All @@ -109,7 +116,10 @@ def get_parser(self, prog_name):

def take_action(self, parsed_args):
"""Get issue by id."""
return self.columns, self.format_issue(self.app.jira.issue(parsed_args.id))
known_fields = set(self.columns)
custom_fields = tuple(x for x in parsed_args.columns if x not in known_fields)
columns = self.columns + custom_fields
return columns, self.format_issue(self.app.jira.issue(parsed_args.id), custom_fields)


class ListIssues(JiraIssueMixin, base.JiraList):
Expand Down
4 changes: 2 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def setUp(self):
self.addCleanup(self.stdout.stop)

@staticmethod
def check_output(command, argv, expected_data):
def check_output(command, argv, expected_data, columns=None):
with mock.patch.object(command, 'produce_output') as output_mock:
app.debug("command", command, argv)

# parsed_args, columns, data
output_mock.assert_called_once_with(mock.ANY, command.columns, expected_data)
output_mock.assert_called_once_with(mock.ANY, columns or command.columns, expected_data)

def check_output_one(self, command, argv):
expected_data = (mock.ANY,) * len(command.columns)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ def test_specify_required_arguments_only(self):
self.assertEqual(0, self.jira.assign_issue.call_count)
self.assertEqual(0, self.jira.transition_issue.call_count)

def test_update_custom_fields(self):
argv = ['--id=1', "--fields", "custom_1:value1", "custom_2:value2"]
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.return_value.update.assert_called_once_with(
fields={"custom_1": "value1", "custom_2": "value2"}
)
self.assertEqual(0, self.jira.assign_issue.call_count)
self.assertEqual(0, self.jira.transition_issue.call_count)

def test_required_arguments(self):
argv = ['--id=1']
self.check_required_arguments(self.command, argv)
Expand All @@ -87,6 +96,13 @@ def test_success(self):
self.check_output_one(self.command, argv)
self.jira.issue.assert_called_once_with("1")

def test_show_with_custom_column(self):
argv = ['--id=1', '-c', 'custom_1']
columns = self.command.columns + ("custom_1",)
expected_data = (mock.ANY,) * len(columns)
self.check_output(self.command, argv, expected_data, columns=columns)
self.jira.issue.assert_called_once_with("1")

def test_required_arguments(self):
argv = ['--id=1']
self.check_required_arguments(self.command, argv)
Expand Down

0 comments on commit 777fa6c

Please sign in to comment.