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 <robin@jarry.cc>
This commit is contained in:
Robin Jarry 2022-01-14 21:09:41 +01:00 committed by Drew DeVault
parent 800aa59bda
commit 1b33a612d1
2 changed files with 42 additions and 0 deletions

View File

@ -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"],

View File

@ -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}