mirror of https://git.sr.ht/~sircmpwn/hub.sr.ht
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:
parent
800aa59bda
commit
1b33a612d1
|
@ -152,6 +152,15 @@ def _handle_commit_trailer(trailer, value, pusher, repo, commit):
|
||||||
[{commit_sha}]: {commit_url} "{commit_message}"\
|
[{commit_sha}]: {commit_url} "{commit_message}"\
|
||||||
"""
|
"""
|
||||||
try:
|
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(
|
todo.update_ticket(
|
||||||
user=pusher,
|
user=pusher,
|
||||||
owner=match["owner"],
|
owner=match["owner"],
|
||||||
|
|
|
@ -496,6 +496,39 @@ class TodoService(SrhtService):
|
||||||
except:
|
except:
|
||||||
pass # nbd, upstream was presumably deleted
|
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):
|
def update_ticket(self, user, owner, tracker, ticket, comment, resolution=None):
|
||||||
url = f"{_todosrht}/api/user/{owner}/trackers/{tracker}/tickets/{ticket}"
|
url = f"{_todosrht}/api/user/{owner}/trackers/{tracker}/tickets/{ticket}"
|
||||||
payload = {"comment": comment}
|
payload = {"comment": comment}
|
||||||
|
|
Loading…
Reference in New Issue