Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub: Consider only the events we are interested in #2066

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packit_service/service/api/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,27 @@ def post(self):
def interested():
"""
Check whether we want to process this event.

Returns:
`False` if we are not interested in this kind of event
False if we are not interested in this kind of event
"""
event_type = request.headers.get("X-GitHub-Event")
event = request.headers.get("X-GitHub-Event")
uuid = request.headers.get("X-GitHub-Delivery")
action = request.json.get("action")
deleted = request.json.get("deleted")

interests = {
"check_run": action == "rerequested",
"pull_request": action in {"opened", "reopened", "synchronize"},
"pull_request_review_comment": action in {"created", "edited"},
"issue_comment": action == "created",
"release": action == "published",
"push": not deleted,
"installation": action == "created",
}
_interested = interests.get(event, False)

# we don't care about created or completed check runs
_interested = event_type != "check_run" or action == "rerequested"
logger.debug(f"{event_type} {uuid}{'' if _interested else ' (not interested)'}")
logger.debug(f"{event} {uuid}{'' if _interested else ' (not interested)'}")
return _interested

@staticmethod
Expand Down
130 changes: 130 additions & 0 deletions tests/unit/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,133 @@ def test_validate_token(mock_config, headers, is_good):
webhooks.GitlabWebhook.validate_token(temp)
else:
webhooks.GitlabWebhook.validate_token(temp)


@pytest.mark.parametrize(
"headers, payload, interested",
[
(
{"X-GitHub-Event": "check_run", "X-GitHub-Delivery": "uuid"},
{"action": "rerequested"},
True,
),
(
{"X-GitHub-Event": "pull_request", "X-GitHub-Delivery": "uuid"},
{"action": "opened"},
True,
),
(
{"X-GitHub-Event": "pull_request", "X-GitHub-Delivery": "uuid"},
{"action": "reopened"},
True,
),
(
{"X-GitHub-Event": "pull_request", "X-GitHub-Delivery": "uuid"},
{"action": "closed"},
False,
),
(
{"X-GitHub-Event": "pull_request", "X-GitHub-Delivery": "uuid"},
{"action": "edited"},
False,
),
(
{"X-GitHub-Event": "pull_request", "X-GitHub-Delivery": "uuid"},
{"action": "synchronize"},
True,
),
(
{
"X-GitHub-Event": "pull_request_review_comment",
"X-GitHub-Delivery": "uuid",
},
{"action": "created"},
True,
),
(
{
"X-GitHub-Event": "pull_request_review_comment",
"X-GitHub-Delivery": "uuid",
},
{"action": "edited"},
True,
),
(
{
"X-GitHub-Event": "pull_request_review_comment",
"X-GitHub-Delivery": "uuid",
},
{"action": "deleted"},
False,
),
(
{"X-GitHub-Event": "issue_comment", "X-GitHub-Delivery": "uuid"},
{"action": "created"},
True,
),
(
{"X-GitHub-Event": "issue_comment", "X-GitHub-Delivery": "uuid"},
{"action": "created"},
True,
),
(
{"X-GitHub-Event": "issue_comment", "X-GitHub-Delivery": "uuid"},
{"action": "edited"},
False,
),
(
{"X-GitHub-Event": "release", "X-GitHub-Delivery": "uuid"},
{"action": "created"},
False,
),
(
{"X-GitHub-Event": "release", "X-GitHub-Delivery": "uuid"},
{"action": "published"},
True,
),
(
{"X-GitHub-Event": "release", "X-GitHub-Delivery": "uuid"},
{"action": "released"},
False,
),
(
{"X-GitHub-Event": "push", "X-GitHub-Delivery": "uuid"},
{"deleted": False},
True,
),
(
{"X-GitHub-Event": "push", "X-GitHub-Delivery": "uuid"},
{"deleted": True},
False,
),
(
{"X-GitHub-Event": "installation", "X-GitHub-Delivery": "uuid"},
{"action": "created"},
True,
),
(
{"X-GitHub-Event": "installation", "X-GitHub-Delivery": "uuid"},
{"action": "deleted"},
False,
),
(
{"X-GitHub-Event": "label"},
{"action": "created"},
False,
),
],
)
def test_interested(mock_config, headers, payload, interested):
# flexmock config before import as it fails on looking for config
flexmock(ServiceConfig).should_receive("get_service_config").and_return(
flexmock(ServiceConfig)
)

from packit_service.service.api import webhooks

webhooks.config = mock_config

with Flask(__name__).test_request_context(
json=payload, content_type="application/json", headers=headers
):
assert webhooks.GithubWebhook.interested() == interested