Skip to content

Commit 8df9939

Browse files
authored
Merge pull request #1105 from syucream/feature/webhook-verification-error-details
Enable to check verification error details on webhooks by users
2 parents 27696fe + 52a2d90 commit 8df9939

File tree

11 files changed

+62
-19
lines changed

11 files changed

+62
-19
lines changed

apiclient/typescript-fetch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dmm-com/airone-apiclient-typescript-fetch",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "AirOne APIv2 client in TypeScript",
55
"main": "src/autogenerated/index.ts",
66
"scripts": {

entity/api_v2/serializers.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ class WebhookSerializer(serializers.ModelSerializer):
3434

3535
class Meta:
3636
model = Webhook
37-
fields = ["id", "label", "url", "is_enabled", "is_verified", "headers", "is_deleted"]
38-
read_only_fields = ["is_verified"]
37+
fields = [
38+
"id",
39+
"label",
40+
"url",
41+
"is_enabled",
42+
"is_verified",
43+
"verification_error_details",
44+
"headers",
45+
"is_deleted",
46+
]
47+
read_only_fields = ["is_verified", "verification_error_details"]
3948

4049

4150
class WebhookCreateUpdateSerializer(serializers.ModelSerializer):
@@ -291,6 +300,7 @@ def _update_or_create(
291300
id=webhook_data.get("id", None), defaults={**webhook_data}
292301
)
293302
entity.webhooks.add(webhook)
303+
294304
try:
295305
resp = requests.post(
296306
webhook.url,
@@ -300,11 +310,17 @@ def _update_or_create(
300310
)
301311
# The is_verified parameter will be set True,
302312
# when requests received HTTP 200 from specifying endpoint.
303-
webhook.is_verified = resp.ok
304-
except ConnectionError:
313+
if resp.ok:
314+
webhook.is_verified = True
315+
webhook.verification_error_details = None
316+
else:
317+
webhook.is_verified = False
318+
webhook.verification_error_details = resp.reason
319+
except ConnectionError as e:
305320
webhook.is_verified = False
321+
webhook.verification_error_details = str(e)
306322

307-
webhook.save(update_fields=["is_verified"])
323+
webhook.save(update_fields=["is_verified", "verification_error_details"])
308324

309325
# unset Editing MODE
310326
if is_created_entity:

entity/tests/test_api_v2.py

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def test_retrieve_entity_with_webhook(self):
267267
"url": "http://airone.com/",
268268
"is_enabled": True,
269269
"is_verified": True,
270+
"verification_error_details": None,
270271
"label": "hoge",
271272
"headers": [],
272273
}
@@ -291,6 +292,7 @@ def test_retrieve_entity_with_webhook(self):
291292
"url": "http://airone.com/",
292293
"is_enabled": True,
293294
"is_verified": True,
295+
"verification_error_details": None,
294296
"label": "hoge",
295297
"headers": [
296298
{"header_key": "key1", "header_value": "value1"},

frontend/src/components/entity/entityForm/EntityFormSchema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const schema = z.object({
1616
label: z.string().default(""),
1717
isEnabled: z.boolean().default(false),
1818
isVerified: z.boolean().default(false).optional(),
19+
verificationErrorDetails: z.string().nullable().optional(),
1920
headers: z
2021
.array(
2122
z.object({

frontend/src/components/entity/entityForm/WebhookFields.tsx

+12-3
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,23 @@ export const WebhookFields: FC<Props> = ({ control }) => {
9595
<Controller
9696
name={`webhooks.${index}.isVerified`}
9797
control={control}
98-
render={({ field }) => {
99-
switch (field.value) {
98+
render={({ field: _field }) => {
99+
switch (_field.value) {
100100
case undefined:
101101
return <></>;
102102
case true:
103103
return <CheckCircleOutlineIcon color="success" />;
104104
case false:
105-
return <ErrorOutlineIcon color="error" />;
105+
return (
106+
<ErrorOutlineIcon
107+
color="error"
108+
titleAccess={
109+
field.verificationErrorDetails
110+
? `エラーのため webhook が有効になっていません。詳細: ${field.verificationErrorDetails}`
111+
: undefined
112+
}
113+
/>
114+
);
106115
}
107116
}}
108117
/>

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"zod": "^3.22.4"
7979
},
8080
"dependencies": {
81-
"@dmm-com/airone-apiclient-typescript-fetch": "^0.0.10",
81+
"@dmm-com/airone-apiclient-typescript-fetch": "^0.0.11",
8282
"@emotion/react": "^11.10.5",
8383
"@emotion/styled": "^11.10.5",
8484
"@jest/globals": "^27.0.6",

templates/list_webhooks.html

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ <h5 class='d-flex align-items-center'>Webhooks</h5>
9090
</div>
9191
</div>
9292
<small>{{ webhook.label }}</small>
93+
{% if not webhook.is_verified %}
94+
<p class='text-danger small'>
95+
エラーのため webhook が有効になっていません。詳細: {{ webhook.verification_error_details }}
96+
</p>
97+
{% endif %}
9398
</li>
9499
{% endfor %}
95100

webhook/api_v1/views.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ def set_webhook(request, entity_id, recv_data):
7878

7979
# The is_verified parameter will be set True,
8080
# when requests received HTTP 200 from specifying endpoint.
81-
webhook.is_verified = resp.ok
82-
except ConnectionError:
81+
if resp.ok:
82+
webhook.is_verified = True
83+
webhook.verification_error_details = None
84+
else:
85+
webhook.is_verified = False
86+
webhook.verification_error_details = resp.reason
87+
except ConnectionError as e:
8388
webhook.is_verified = False
89+
webhook.verification_error_details = str(e)
8490

8591
webhook.save()
8692

webhook/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class Webhook(models.Model):
2020
# This is set by system when specified webhook_url is available
2121
is_verified = models.BooleanField(default=False)
2222

23+
# This is set by system when specified webhook_url is unavailable
24+
verification_error_details = models.TextField(null=True, blank=True)
25+
2326
# This contains HTTP headers when sending request to the specified URL
2427
# (e.g. authentication header if it's needed)
2528
# [{

webhook/tests/test_api_v1.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_create_invalid_endpoint_webhook(self, mock_requests):
1717
# Declare requests mock that responds HTTP 400(ERROR)
1818
mock_resp = mock.Mock()
1919
mock_resp.ok = False
20+
mock_resp.reason = "test-failure"
2021
mock_resp.text = "test-failure"
2122
mock_requests.post.return_value = mock_resp
2223

0 commit comments

Comments
 (0)