Skip to content

Commit

Permalink
added commands to manage issue labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin committed May 4, 2017
1 parent 777fa6c commit 2433f5e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
71 changes: 71 additions & 0 deletions jiractl/commands/labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
This file is part of jiractl
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import urlparse

from jiractl.commands import base


class JiraLabelMixin(object):
columns = "label",

def get_parser(self, prog_name):
parser = super(JiraLabelMixin, self).get_parser(prog_name)
parser.add_argument("--issue", type=base.utf8, help="Issue ID", required=True)
return parser


class ListLabels(JiraLabelMixin, base.JiraList):
"""Gets all labels for issue."""

def take_action(self, parsed_args):
"""Get issue by id."""

return self.columns, [(x,) for x in self.app.jira.issue(parsed_args.issue).fields.labels]


class AddLabel(JiraLabelMixin, base.JiraCommand):
"""Adds new link."""

def get_parser(self, prog_name):
parser = super(AddLabel, self).get_parser(prog_name)
parser.add_argument("--labels", type=base.utf8, nargs='+', required=True, help="Label text")
return parser

def take_action(self, parsed_args):
"""Adds new link."""
issue = self.app.jira.issue(parsed_args.issue)
labels = issue.fields.labels
missing = set(parsed_args.labels).difference(labels)
if missing:
issue.update(fields={'labels': labels + sorted(missing)})

self.app.stdout.write("Done.\n")


class DropLabel(JiraLabelMixin, base.JiraCommand):
def get_parser(self, prog_name):
parser = super(DropLabel, self).get_parser(prog_name)
parser.add_argument("--labels", type=base.utf8, nargs='+', required=True, help="Label text")
return parser

def take_action(self, parsed_args):
"""Get issue by id."""
issue = self.app.jira.issue(parsed_args.issue)
labels = set(issue.fields.labels).difference(parsed_args.labels)
if len(labels) != len(issue.fields.labels):
issue.update(fields={'labels': sorted(labels)})

self.app.stdout.write("Done.\n")
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jiractl =
issue_comment_edit=jiractl.commands.comments:EditComment
issue_comments=jiractl.commands.comments:ListComments
issue_comments_add=jiractl.commands.comments:AddComment
issue_labels=jiractl.commands.labels:ListLabels
issue_labels_add=jiractl.commands.labels:AddLabel
issue_labels_drop=jiractl.commands.labels:DropLabel
issue_link=jiractl.commands.links:ShowLink
issue_links=jiractl.commands.links:ListLinks
issue_links_add=jiractl.commands.links:AddLink
Expand Down
84 changes: 84 additions & 0 deletions tests/test_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
This file is part of jiractl
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import mock

from jiractl.commands import labels

from tests import base


class TestListLabels(base.BaseUnitTest):
command = labels.ListLabels

def test_success(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=["label1"]))
self.check_output_many(self.command, ['--issue=ISSUE'], 1)

self.jira.issue.assert_called_once_with("ISSUE")

def test_required_arguments(self):
self.check_required_arguments(self.command, ['--issue=ISSUE'])


class TestAddLabels(base.BaseUnitTest):
command = labels.AddLabel

def test_add_existed_label(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=["label1"]))
argv = ['--issue=ISSUE', '--labels=label1']
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.assert_called_once_with("ISSUE")
self.assertEqual(0, self.jira.issue.return_value.update.call_count)

def test_add_new_label(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=[]))
argv = ['--issue=ISSUE', '--labels=label1']
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.assert_called_once_with("ISSUE")
self.jira.issue.return_value.update.assert_called_once_with(fields={"labels": ["label1"]})

def test_add_labels(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=["label1", "label2"]))
argv = ['--issue=ISSUE', '--labels', 'label2', 'label3']
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.assert_called_once_with("ISSUE")
self.jira.issue.return_value.update.assert_called_once_with(fields={"labels": ["label1", "label2", "label3"]})

def test_required_arguments(self):
argv = ['--issue=ISSUE', '--labels=label']
self.check_required_arguments(self.command, argv)


class TestDropLabels(base.BaseUnitTest):
command = labels.DropLabel

def test_delete_non_existed_labels(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=["label2"]))
argv = ['--issue=ISSUE', '--labels', 'label1']
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.assert_called_once_with("ISSUE")
self.assertEqual(0, self.jira.issue.return_value.update.call_count)

def test_delete_labels(self):
self.jira.issue.return_value = mock.Mock(fields=mock.Mock(labels=["label1", "label2"]))
argv = ['--issue=ISSUE', '--labels', 'label2', 'label3']
self.check_stdout(self.command, argv, "Done.\n")
self.jira.issue.assert_called_once_with("ISSUE")
self.jira.issue.return_value.update.assert_called_once_with(fields={"labels": ["label1"]})

def test_required_arguments(self):
argv = ['--issue=ISSUE', '--labels=label']
self.check_required_arguments(self.command, argv)

0 comments on commit 2433f5e

Please sign in to comment.