-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added commands to manage issue labels
- Loading branch information
1 parent
777fa6c
commit 2433f5e
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |