webhooks/github: Suppress "comment edited" events when body is same.

GitHub generates spurious edited events on hiding/unhiding a comment,
and on clicking "edit"/"update" without any changes, when the
comment "body" contains certain specific characters.

Instead of sending a notification message, we suppress those edge cases
by comparing the "body" content of the comment and changes in the event
payload.

Fixes #31042, #26136.
This commit is contained in:
Niloth P
2025-01-03 03:50:33 +05:30
committed by Tim Abbott
parent 1ce1412166
commit 09f47d5a0a
3 changed files with 263 additions and 0 deletions

View File

@@ -0,0 +1,244 @@
{
"action": "edited",
"changes": {
"body": {
"from": "Cool. @Niloth-p please go ahead and send a PR. You'll want to address that feedback from @AnotherUser, but it's fine for that to happen after the initial revision of the PR — there'll probably be feedback I have on the code that's independent of that point about the sizing behavior of the bottom sheet."
}
},
"issue": {
"url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1",
"repository_url": "https://api.github.com/repos/Niloth-p/webhook-tester",
"labels_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1/labels{/name}",
"comments_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1/comments",
"events_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1/events",
"html_url": "https://github.com/Niloth-p/webhook-tester/pull/1",
"id": 2608853794,
"node_id": "PR_kwDONEwUIs9_oLMc",
"number": 1,
"title": "Generic issue title",
"user": {
"login": "Niloth-p",
"id": 20517308,
"node_id": "MDQ6VXNlcjIwMzE1MzA4",
"avatar_url": "https://avatars.githubusercontent.com/u/20319309?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Niloth-p",
"html_url": "https://github.com/Niloth-p",
"followers_url": "https://api.github.com/users/Niloth-p/followers",
"following_url": "https://api.github.com/users/Niloth-p/following{/other_user}",
"gists_url": "https://api.github.com/users/Niloth-p/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Niloth-p/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Niloth-p/subscriptions",
"organizations_url": "https://api.github.com/users/Niloth-p/orgs",
"repos_url": "https://api.github.com/users/Niloth-p/repos",
"events_url": "https://api.github.com/users/Niloth-p/events{/privacy}",
"received_events_url": "https://api.github.com/users/Niloth-p/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"labels": [],
"state": "open",
"locked": false,
"assignee": null,
"assignees": [],
"milestone": null,
"comments": 6,
"created_at": "2024-10-23T14:31:40Z",
"updated_at": "2024-12-20T00:01:51Z",
"closed_at": null,
"author_association": "OWNER",
"active_lock_reason": null,
"draft": false,
"pull_request": {
"url": "https://api.github.com/repos/Niloth-p/webhook-tester/pulls/1",
"html_url": "https://github.com/Niloth-p/webhook-tester/pull/1",
"diff_url": "https://github.com/Niloth-p/webhook-tester/pull/1.diff",
"patch_url": "https://github.com/Niloth-p/webhook-tester/pull/1.patch",
"merged_at": null
},
"body": null,
"reactions": {
"url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"timeline_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1/timeline",
"performed_via_github_app": null,
"state_reason": null
},
"comment": {
"url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/comments/2555981590",
"html_url": "https://github.com/Niloth-p/webhook-tester/pull/1#issuecomment-2555981590",
"issue_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/1",
"id": 2555981590,
"node_id": "IC_kwDONEtUIs6YWS8W",
"user": {
"login": "Niloth-p",
"id": 20315308,
"node_id": "MDQ6VXNlcjIwMzE1MzA4",
"avatar_url": "https://avatars.githubusercontent.com/u/20315308?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Niloth-p",
"html_url": "https://github.com/Niloth-p",
"followers_url": "https://api.github.com/users/Niloth-p/followers",
"following_url": "https://api.github.com/users/Niloth-p/following{/other_user}",
"gists_url": "https://api.github.com/users/Niloth-p/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Niloth-p/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Niloth-p/subscriptions",
"organizations_url": "https://api.github.com/users/Niloth-p/orgs",
"repos_url": "https://api.github.com/users/Niloth-p/repos",
"events_url": "https://api.github.com/users/Niloth-p/events{/privacy}",
"received_events_url": "https://api.github.com/users/Niloth-p/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"created_at": "2024-12-20T00:01:30Z",
"updated_at": "2024-12-20T00:01:51Z",
"author_association": "OWNER",
"body": "Cool. @Niloth-p please go ahead and send a PR. You'll want to address that feedback from @AnotherUser, but it's fine for that to happen after the initial revision of the PR — there'll probably be feedback I have on the code that's independent of that point about the sizing behavior of the bottom sheet.",
"reactions": {
"url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/comments/2555981590/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"performed_via_github_app": null
},
"repository": {
"id": 877351970,
"node_id": "R_kgDONEtUIg",
"name": "webhook-tester",
"full_name": "Niloth-p/webhook-tester",
"private": true,
"owner": {
"login": "Niloth-p",
"id": 20315308,
"node_id": "MDQ6VXNlcjIwMzE1MzA4",
"avatar_url": "https://avatars.githubusercontent.com/u/20315308?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Niloth-p",
"html_url": "https://github.com/Niloth-p",
"followers_url": "https://api.github.com/users/Niloth-p/followers",
"following_url": "https://api.github.com/users/Niloth-p/following{/other_user}",
"gists_url": "https://api.github.com/users/Niloth-p/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Niloth-p/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Niloth-p/subscriptions",
"organizations_url": "https://api.github.com/users/Niloth-p/orgs",
"repos_url": "https://api.github.com/users/Niloth-p/repos",
"events_url": "https://api.github.com/users/Niloth-p/events{/privacy}",
"received_events_url": "https://api.github.com/users/Niloth-p/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"html_url": "https://github.com/Niloth-p/webhook-tester",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/Niloth-p/webhook-tester",
"forks_url": "https://api.github.com/repos/Niloth-p/webhook-tester/forks",
"keys_url": "https://api.github.com/repos/Niloth-p/webhook-tester/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Niloth-p/webhook-tester/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Niloth-p/webhook-tester/teams",
"hooks_url": "https://api.github.com/repos/Niloth-p/webhook-tester/hooks",
"issue_events_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/events{/number}",
"events_url": "https://api.github.com/repos/Niloth-p/webhook-tester/events",
"assignees_url": "https://api.github.com/repos/Niloth-p/webhook-tester/assignees{/user}",
"branches_url": "https://api.github.com/repos/Niloth-p/webhook-tester/branches{/branch}",
"tags_url": "https://api.github.com/repos/Niloth-p/webhook-tester/tags",
"blobs_url": "https://api.github.com/repos/Niloth-p/webhook-tester/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Niloth-p/webhook-tester/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Niloth-p/webhook-tester/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Niloth-p/webhook-tester/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Niloth-p/webhook-tester/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Niloth-p/webhook-tester/languages",
"stargazers_url": "https://api.github.com/repos/Niloth-p/webhook-tester/stargazers",
"contributors_url": "https://api.github.com/repos/Niloth-p/webhook-tester/contributors",
"subscribers_url": "https://api.github.com/repos/Niloth-p/webhook-tester/subscribers",
"subscription_url": "https://api.github.com/repos/Niloth-p/webhook-tester/subscription",
"commits_url": "https://api.github.com/repos/Niloth-p/webhook-tester/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Niloth-p/webhook-tester/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Niloth-p/webhook-tester/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Niloth-p/webhook-tester/contents/{+path}",
"compare_url": "https://api.github.com/repos/Niloth-p/webhook-tester/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Niloth-p/webhook-tester/merges",
"archive_url": "https://api.github.com/repos/Niloth-p/webhook-tester/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Niloth-p/webhook-tester/downloads",
"issues_url": "https://api.github.com/repos/Niloth-p/webhook-tester/issues{/number}",
"pulls_url": "https://api.github.com/repos/Niloth-p/webhook-tester/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Niloth-p/webhook-tester/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Niloth-p/webhook-tester/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Niloth-p/webhook-tester/labels{/name}",
"releases_url": "https://api.github.com/repos/Niloth-p/webhook-tester/releases{/id}",
"deployments_url": "https://api.github.com/repos/Niloth-p/webhook-tester/deployments",
"created_at": "2024-10-23T14:00:56Z",
"updated_at": "2024-10-23T15:20:20Z",
"pushed_at": "2024-10-23T14:30:45Z",
"git_url": "git://github.com/Niloth-p/webhook-tester.git",
"ssh_url": "git@github.com:Niloth-p/webhook-tester.git",
"clone_url": "https://github.com/Niloth-p/webhook-tester.git",
"svn_url": "https://github.com/Niloth-p/webhook-tester",
"homepage": null,
"size": 1,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": false,
"has_pages": false,
"has_discussions": true,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 2,
"license": null,
"allow_forking": true,
"is_template": false,
"web_commit_signoff_required": false,
"topics": [],
"visibility": "private",
"forks": 0,
"open_issues": 25,
"watchers": 0,
"default_branch": "main"
},
"sender": {
"login": "AnotherUser",
"id": 139634087,
"node_id": "U_kgDOCFKlpw",
"avatar_url": "https://avatars.githubusercontent.com/u/139634087?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/AnotherUser",
"html_url": "https://github.com/AnotherUser",
"followers_url": "https://api.github.com/users/AnotherUser/followers",
"following_url": "https://api.github.com/users/AnotherUser/following{/other_user}",
"gists_url": "https://api.github.com/users/AnotherUser/gists{/gist_id}",
"starred_url": "https://api.github.com/users/AnotherUser/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/AnotherUser/subscriptions",
"organizations_url": "https://api.github.com/users/AnotherUser/orgs",
"repos_url": "https://api.github.com/users/AnotherUser/repos",
"events_url": "https://api.github.com/users/AnotherUser/events{/privacy}",
"received_events_url": "https://api.github.com/users/AnotherUser/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
}
}

View File

@@ -629,6 +629,14 @@ A temporary team so that I can get some webhook fixtures!
expected_message = "sbansal1999 edited a [comment](https://github.com/sbansal1999/testing-gh/discussions/20#discussioncomment-6332416) on [discussion #20](https://github.com/sbansal1999/testing-gh/discussions/20):\n\n~~~ quote\nsome random comment edited\n~~~"
self.check_webhook("discussion_comment__edited", TOPIC_DISCUSSION, expected_message)
def test_comment_edited_unchanged_skipped(self) -> None:
self.check_webhook(
fixture_name="issue_comment__edited__unchanged",
expected_topic_name=None,
expected_message=None,
expect_noop=True,
)
class GitHubSponsorsHookTests(WebhookTestCase):
CHANNEL_NAME = "github"

View File

@@ -919,6 +919,17 @@ def api_github_webhook(
# Ignore private repository events
return json_success(request)
# Ignore 'comment edited' events (except discussion comments)
# if the comment body remains unchanged.
if (
"comment" in header_event
and "discussion" not in header_event
and payload.get("action", "").tame(check_string) == "edited"
and payload["changes"]["body"]["from"].tame(check_string)
== payload["comment"]["body"].tame(check_string)
):
return json_success(request)
event = get_zulip_event_name(header_event, payload, branches)
if event is None:
# This is nothing to worry about--get_event() returns None