|
| 1 | +""" |
| 2 | +This file is part of jiractl |
| 3 | +
|
| 4 | +The above copyright notice and this permission notice shall be included in all |
| 5 | +copies or substantial portions of the Software. |
| 6 | +
|
| 7 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 13 | +SOFTWARE. |
| 14 | +""" |
| 15 | + |
| 16 | +from jiractl.commands import base |
| 17 | + |
| 18 | + |
| 19 | +class JiraCommentMixin(object): |
| 20 | + columns = ("id", "updated", "author", "body") |
| 21 | + |
| 22 | + def get_parser(self, prog_name): |
| 23 | + parser = super(JiraCommentMixin, self).get_parser(prog_name) |
| 24 | + parser.add_argument("--issue", type=base.utf8, help="Issue ID", required=True) |
| 25 | + return parser |
| 26 | + |
| 27 | + @staticmethod |
| 28 | + def format_comment(comment): |
| 29 | + """Converts issue comment to tuple.""" |
| 30 | + return comment.id, comment.updated, comment.author.name, comment.body |
| 31 | + |
| 32 | + |
| 33 | +class ListComments(JiraCommentMixin, base.JiraList): |
| 34 | + """Gets all comments for issue.""" |
| 35 | + |
| 36 | + def take_action(self, parsed_args): |
| 37 | + """Get issue by id.""" |
| 38 | + return self.columns, [self.format_comment(x) for x in self.app.jira.comments(parsed_args.issue)] |
| 39 | + |
| 40 | + |
| 41 | +class AddComment(JiraCommentMixin, base.JiraShow): |
| 42 | + """Adds new comment.""" |
| 43 | + |
| 44 | + def get_parser(self, prog_name): |
| 45 | + parser = super(AddComment, self).get_parser(prog_name) |
| 46 | + parser.add_argument("--body", type=base.utf8, help="Comment", required=True) |
| 47 | + parser.add_argument( |
| 48 | + "--visibility", type=base.utf8, metavar="TYPE:VALUE", help="Visibility of issue. format %(metavar)" |
| 49 | + ) |
| 50 | + return parser |
| 51 | + |
| 52 | + def take_action(self, parsed_args): |
| 53 | + """Adds new comment.""" |
| 54 | + visibility = None |
| 55 | + if parsed_args.visibility: |
| 56 | + visibility = dict(zip(("type", "value"), parsed_args.visibility.split(":", 2))) |
| 57 | + |
| 58 | + comment = self.app.jira.add_comment(parsed_args.issue, parsed_args.body, visibility=visibility) |
| 59 | + return self.columns, self.format_comment(comment) |
| 60 | + |
| 61 | + |
| 62 | +class EditComment(JiraCommentMixin, base.JiraCommand): |
| 63 | + """Edit comment.""" |
| 64 | + |
| 65 | + def get_parser(self, prog_name): |
| 66 | + parser = super(EditComment, self).get_parser(prog_name) |
| 67 | + parser.add_argument("--id", type=base.utf8, help="Comment ID", required=True) |
| 68 | + parser.add_argument("--body", type=base.utf8, help="Comment", required=True) |
| 69 | + parser.add_argument("--visibility", type=base.utf8, help="Visibility in format type:value") |
| 70 | + return parser |
| 71 | + |
| 72 | + def take_action(self, parsed_args): |
| 73 | + """Get issue by id.""" |
| 74 | + visibility = None |
| 75 | + if parsed_args.visibility: |
| 76 | + visibility = dict(zip(("type", "value"), parsed_args.visibility.split(":", 2))) |
| 77 | + |
| 78 | + comment = self.app.jira.comment(parsed_args.issue, parsed_args.id) |
| 79 | + comment.update(body=parsed_args.body, visibility=visibility) |
| 80 | + self.app.stdout.write("Done.\n") |
| 81 | + |
| 82 | + |
| 83 | +class ShowComment(JiraCommentMixin, base.JiraShow): |
| 84 | + """Shows one comment.""" |
| 85 | + |
| 86 | + def get_parser(self, prog_name): |
| 87 | + parser = super(ShowComment, self).get_parser(prog_name) |
| 88 | + parser.add_argument("--id", type=base.utf8, help="Comment ID", required=True) |
| 89 | + return parser |
| 90 | + |
| 91 | + def take_action(self, parsed_args): |
| 92 | + """Get issue by id.""" |
| 93 | + return self.columns, self.format_comment(self.app.jira.comment(parsed_args.issue, parsed_args.id)) |
0 commit comments