Skip to content

Commit

Permalink
fix(3564) Support PATCH Method In Outgoing webhook (#3580)
Browse files Browse the repository at this point in the history
# What this PR does
Adds PATCH method Support for outgoing webhook

## Which issue(s) this PR fixes
Fixes #3564 

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)

---------

Co-authored-by: Joey Orlando <[email protected]>
  • Loading branch information
ravishankar15 and joeyorlando authored Dec 20, 2023
1 parent 647d462 commit bcbca9d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support e2e tests in Tilt and Makefile ([#3516](https://github.com/grafana/oncall/pull/3516))
- Support PATCH method for outgoing webhooks by @ravishankar15 ([#3580](https://github.com/grafana/oncall/pull/3580))

### Changed

Expand Down
10 changes: 8 additions & 2 deletions engine/apps/api/tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,10 @@ def test_create_invalid_missing_fields(webhook_internal_api_setup, make_user_aut
}
response = client.post(url, data, format="json", **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["http_method"][0] == "This field must be one of ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']."
assert (
response.json()["http_method"][0]
== "This field must be one of ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH']."
)

data = {
"name": "test webhook 3",
Expand All @@ -711,7 +714,10 @@ def test_create_invalid_missing_fields(webhook_internal_api_setup, make_user_aut
}
response = client.post(url, data, format="json", **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["http_method"][0] == "This field must be one of ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']."
assert (
response.json()["http_method"][0]
== "This field must be one of ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH']."
)

data = {"name": "test webhook 3", "url": TEST_URL, "trigger_type": 2000000, "http_method": "POST"}
response = client.post(url, data, format="json", **make_user_auth_headers(user, token))
Expand Down
6 changes: 4 additions & 2 deletions engine/apps/webhooks/models/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from apps.alerts.models import EscalationPolicy

WEBHOOK_FIELD_PLACEHOLDER = "****************"
PUBLIC_WEBHOOK_HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
PUBLIC_WEBHOOK_HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"]

logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -186,7 +186,7 @@ def build_request_kwargs(self, event_data, raise_data_errors=False):
if self.authorization_header:
request_kwargs["headers"]["Authorization"] = self.authorization_header

if self.http_method in ["POST", "PUT"]:
if self.http_method in ["POST", "PUT", "PATCH"]:
if self.forward_all:
request_kwargs["json"] = event_data
if self.is_legacy:
Expand Down Expand Up @@ -255,6 +255,8 @@ def make_request(self, url, request_kwargs):
r = requests.delete(url, timeout=OUTGOING_WEBHOOK_TIMEOUT, **request_kwargs)
elif self.http_method == "OPTIONS":
r = requests.options(url, timeout=OUTGOING_WEBHOOK_TIMEOUT, **request_kwargs)
elif self.http_method == "PATCH":
r = requests.patch(url, timeout=OUTGOING_WEBHOOK_TIMEOUT, **request_kwargs)
else:
raise ValueError(f"Unsupported http method: {self.http_method}")
return r
Expand Down
2 changes: 1 addition & 1 deletion engine/apps/webhooks/tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_make_request(make_organization, make_custom_webhook):
organization = make_organization()

with patch("apps.webhooks.models.webhook.requests") as mock_requests:
for method in ("GET", "POST", "PUT", "DELETE", "OPTIONS"):
for method in ("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"):
webhook = make_custom_webhook(organization=organization, http_method=method)
webhook.make_request("url", {"foo": "bar"})
expected_call = getattr(mock_requests, method.lower())
Expand Down

0 comments on commit bcbca9d

Please sign in to comment.