From 7c7df691ec05775606dea09af85e53c951ca18a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sven=20M=C3=A4der?= <maeder@phys.ethz.ch>
Date: Mon, 10 Feb 2020 19:38:37 +0100
Subject: [PATCH] Add subcommand comment

---
 rtlinks.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/rtlinks.py b/rtlinks.py
index 7b4425b..cff6c30 100755
--- a/rtlinks.py
+++ b/rtlinks.py
@@ -61,9 +61,16 @@ class RTLinksPlugin(Plugin):
 
     async def edit_ticket(self, number: str, status: str) -> None:
         api_edit = '{}ticket/{}/edit'.format(self.api, number)
-        data = {**self.post_data, **{'content': 'Status: {}'.format(status)}}
+        content = {'content': 'Status: {}'.format(status)}
+        data = {**self.post_data, **content}
         await self.http.post(api_edit, data=data, headers=self.headers)
 
+    async def comment_ticket(self, number: str, comment: str) -> None:
+        api_comment = '{}ticket/{}/comment'.format(self.api, number)
+        content = {'content': 'id: {}\nAction: comment\nText: {}'.format(number, comment)}
+        data = {**self.post_data, **content}
+        await self.http.post(api_comment, data=data, headers=self.headers)
+
     @command.passive("((^| )([rR][tT]#?))([0-9]{6})", multiple=True)
     async def handler(self, evt: MessageEvent, subs: List[Tuple[str, str]]) -> None:
         await evt.mark_read()
@@ -152,3 +159,14 @@ class RTLinksPlugin(Plugin):
             return
         await evt.mark_read()
         await evt.reply('😂 lol, this is your job!')
+
+    @rt.subcommand("comment", help="Add a comment.")
+    @command.argument("number", "ticket number", parser=str)
+    @command.argument("comment", "comment text", pass_raw=True)
+    async def comment(self, evt: MessageEvent, number: str, comment: str) -> None:
+        if not await self.can_manage(evt) or not self.is_valid_number(number):
+            return
+        await evt.mark_read()
+        await self.comment_ticket(number, comment)
+        markdown_link = await self.get_markdown_link(number)
+        await evt.respond('{} comment added'.format(markdown_link))
-- 
GitLab