From 1b33a612d1831ed6291e6272e3011c4920e41c87 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Fri, 14 Jan 2022 21:09:41 +0100 Subject: [PATCH] webhooks: avoid duplicate todo comments When pushing a tag, the webhooks are invoked twice: once for the the commit and once for the tag. Avoid inserting the same comment multiple times. This also fixes the multiple comments issue when a git repository is associated with more than one project. This requires the new graphql method to get specific tickets by their scoped ID on a given tracker. Signed-off-by: Robin Jarry --- hubsrht/blueprints/webhooks.py | 9 +++++++++ hubsrht/services.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/hubsrht/blueprints/webhooks.py b/hubsrht/blueprints/webhooks.py index ea1a72b..4cfcfc9 100644 --- a/hubsrht/blueprints/webhooks.py +++ b/hubsrht/blueprints/webhooks.py @@ -152,6 +152,15 @@ def _handle_commit_trailer(trailer, value, pusher, repo, commit): [{commit_sha}]: {commit_url} "{commit_message}"\ """ try: + existing_comments = todo.get_ticket_comments( + user=pusher, + owner=match["owner"], + tracker=match["tracker"], + ticket=int(match["ticket"]), + ) + if comment in existing_comments: + # avoid duplicate comments + return todo.update_ticket( user=pusher, owner=match["owner"], diff --git a/hubsrht/services.py b/hubsrht/services.py index 79e78fd..b59c65c 100644 --- a/hubsrht/services.py +++ b/hubsrht/services.py @@ -496,6 +496,39 @@ class TodoService(SrhtService): except: pass # nbd, upstream was presumably deleted + def get_ticket_comments(self, user, owner, tracker, ticket): + query = """ + query TicketComments($owner: String!, $tracker: String!, $ticket: Int!) { + trackerByOwner(owner: $owner, tracker: $tracker) { + ticket(id: $ticket) { + events { + results { + changes { + ... on Comment { + text + } + } + } + } + } + } + } + """ + r = self.post(user, None, f"{_todosrht}/query", { + "query": query, + "variables": { + "owner": owner, + "tracker": tracker, + "ticket": ticket, + } + }) + comments = [] + for e in r["data"]["trackerByOwner"]["ticket"]["events"]["results"]: + for c in e["changes"]: + if "text" in c: + comments.append(c["text"]) + return comments + def update_ticket(self, user, owner, tracker, ticket, comment, resolution=None): url = f"{_todosrht}/api/user/{owner}/trackers/{tracker}/tickets/{ticket}" payload = {"comment": comment}