Skip to content

Commit 50ea0cf

Browse files
committed
Merge branch 'release/2.4.0.1'
2 parents d0c5423 + ef1883b commit 50ea0cf

File tree

10 files changed

+101
-123
lines changed

10 files changed

+101
-123
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes for Webhooks for Craft CMS
22

3+
## 2.4.0.1 - 2021-08-30
4+
5+
### Fixed
6+
- Fixed a bug where pending webhook requests weren’t being updated when new webhooks were trigered with matching debounce keys.
7+
38
## 2.4.0 - 2021-08-30
49

510
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "craftcms/webhooks",
33
"description": "Post webhooks when events are triggered in Craft CMS.",
4-
"version": "2.4.0",
4+
"version": "2.4.0.1",
55
"type": "craft-plugin",
66
"keywords": [
77
"html",

src/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Group extends Model
2525
/**
2626
* @inheritdoc
2727
*/
28-
public function rules()
28+
protected function defineRules(): array
2929
{
3030
return [
3131
[['name'], 'trim'],

src/Plugin.php

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
use craft\services\Gc;
1616
use craft\web\UrlManager;
1717
use craft\webhooks\filters\DraftFilter;
18-
use craft\webhooks\filters\ProvisionalDraftFilter;
1918
use craft\webhooks\filters\DuplicatingFilter;
2019
use craft\webhooks\filters\ElementEnabledFilter;
2120
use craft\webhooks\filters\FilterInterface;
2221
use craft\webhooks\filters\FirstSaveFilter;
2322
use craft\webhooks\filters\NewElementFilter;
2423
use craft\webhooks\filters\PropagatingFilter;
24+
use craft\webhooks\filters\ProvisionalDraftFilter;
2525
use craft\webhooks\filters\ResavingFilter;
2626
use craft\webhooks\filters\RevisionFilter;
2727
use DateTime;
@@ -207,8 +207,8 @@ function(Event $e) use ($webhook) {
207207
if ($webhook->debounceKeyFormat) {
208208
$debounceKey = Craft::parseEnv($webhook->debounceKeyFormat);
209209
$debounceKey = $webhook->id . ':' . $view->renderString($debounceKey, [
210-
'event' => $e,
211-
]);
210+
'event' => $e,
211+
]);
212212
}
213213

214214
$this->request($webhook->method, $url, $headers, $body, $webhook->id, $debounceKey ?? null);
@@ -285,8 +285,6 @@ public function getCpNavItem()
285285
*/
286286
public function request(string $method, string $url, array $headers = null, string $body = null, int $webhookId = null, string $debounceKey = null)
287287
{
288-
$db = Craft::$app->getDb();
289-
290288
if ($debounceKey !== null) {
291289
// See if there is an existing pending request with the same key
292290
$requestId = (new Query())
@@ -300,37 +298,34 @@ public function request(string $method, string $url, array $headers = null, stri
300298

301299
// If so and we get a lock on it, update that request instead of creating a new one
302300
if ($requestId && $this->_lockRequest($requestId)) {
303-
$db->createCommand()
304-
->update('{{%webhookrequests}}', [
305-
'method' => $method,
306-
'url' => $url,
307-
'requestHeaders' => $headers ? Json::encode($headers) : null,
308-
'requestBody' => $body,
309-
'dateCreated' => Db::prepareDateForDb(new DateTime()),
310-
], [
311-
'id' => $requestId,
312-
], [], false);
301+
Db::update('{{%webhookrequests}}', [
302+
'method' => $method,
303+
'url' => $url,
304+
'requestHeaders' => $headers ? Json::encode($headers) : null,
305+
'requestBody' => $body,
306+
'dateCreated' => Db::prepareDateForDb(new DateTime()),
307+
], [
308+
'id' => $requestId,
309+
], [], false);
313310
$this->_unlockRequest($requestId);
314311
return;
315312
}
316313
}
317314

318-
$db->createCommand()
319-
->insert('{{%webhookrequests}}', [
320-
'webhookId' => $webhookId,
321-
'debounceKey' => $debounceKey,
322-
'status' => self::STATUS_PENDING,
323-
'method' => $method,
324-
'url' => $url,
325-
'requestHeaders' => $headers ? Json::encode($headers) : null,
326-
'requestBody' => $body,
327-
'dateCreated' => Db::prepareDateForDb(new DateTime()),
328-
'uid' => StringHelper::UUID(),
329-
], false)
330-
->execute();
315+
Db::insert('{{%webhookrequests}}', [
316+
'webhookId' => $webhookId,
317+
'debounceKey' => $debounceKey,
318+
'status' => self::STATUS_PENDING,
319+
'method' => $method,
320+
'url' => $url,
321+
'requestHeaders' => $headers ? Json::encode($headers) : null,
322+
'requestBody' => $body,
323+
'dateCreated' => Db::prepareDateForDb(new DateTime()),
324+
'uid' => StringHelper::UUID(),
325+
], false);
331326

332327
$this->_pendingJobs[] = new SendRequestJob([
333-
'requestId' => $db->getLastInsertID('{{%webhookrequests}}'),
328+
'requestId' => Craft::$app->getDb()->getLastInsertID('{{%webhookrequests}}'),
334329
'webhookId' => $webhookId,
335330
]);
336331

@@ -383,9 +378,9 @@ public function getRequestData(int $requestId): array
383378
* Sends a request by its ID.
384379
*
385380
* @param int $requestId
386-
* @throws Exception
387-
* @throws GuzzleException
388381
* @return bool Whether the request came back with a 2xx response
382+
* @throws GuzzleException
383+
* @throws Exception
389384
*/
390385
public function sendRequest(int $requestId): bool
391386
{
@@ -405,13 +400,10 @@ public function sendRequest(int $requestId): bool
405400
}
406401

407402
// Update the request
408-
$db = Craft::$app->getDb();
409-
$db->createCommand()
410-
->update('{{%webhookrequests}}', [
411-
'status' => self::STATUS_REQUESTED,
412-
'dateRequested' => Db::prepareDateForDb(new DateTime()),
413-
], ['id' => $requestId], [], false)
414-
->execute();
403+
Db::update('{{%webhookrequests}}', [
404+
'status' => self::STATUS_REQUESTED,
405+
'dateRequested' => Db::prepareDateForDb(new DateTime()),
406+
], ['id' => $requestId], [], false);
415407

416408
$startTime = microtime(true);
417409
$response = null;
@@ -432,17 +424,14 @@ public function sendRequest(int $requestId): bool
432424
$time = round(1000 * (microtime(true) - $startTime));
433425
$attempt = ($data['attempts'] ?? 0) + 1;
434426

435-
$db = Craft::$app->getDb();
436-
$db->createCommand()
437-
->update('{{%webhookrequests}}', [
438-
'status' => self::STATUS_DONE,
439-
'attempts' => $attempt,
440-
'responseStatus' => $response ? $response->getStatusCode() : null,
441-
'responseHeaders' => $response ? Json::encode($response->getHeaders()) : null,
442-
'responseBody' => $response ? (string)$response->getBody() : null,
443-
'responseTime' => $time,
444-
], ['id' => $requestId], [], false)
445-
->execute();
427+
Db::update('{{%webhookrequests}}', [
428+
'status' => self::STATUS_DONE,
429+
'attempts' => $attempt,
430+
'responseStatus' => $response ? $response->getStatusCode() : null,
431+
'responseHeaders' => $response ? Json::encode($response->getHeaders()) : null,
432+
'responseBody' => $response ? (string)$response->getBody() : null,
433+
'responseTime' => $time,
434+
], ['id' => $requestId], [], false);
446435

447436
// Release the lock
448437
$this->_unlockRequest($requestId);
@@ -530,7 +519,7 @@ public function getAllFilters(): array
530519
];
531520

532521
$event = new RegisterComponentTypesEvent([
533-
'types' => $filterTypes
522+
'types' => $filterTypes,
534523
]);
535524
$this->trigger(self::EVENT_REGISTER_FILTER_TYPES, $event);
536525

src/Webhook.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function attributeLabels()
109109
/**
110110
* @inheritdoc
111111
*/
112-
public function rules()
112+
protected function defineRules(): array
113113
{
114114
return [
115115
[['name', 'event', 'url'], 'trim'],
@@ -118,7 +118,7 @@ public function rules()
118118
'filter',
119119
'filter' => function(string $value) {
120120
return trim($value, ' \\');
121-
}, 'skipOnArray' => true
121+
}, 'skipOnArray' => true,
122122
],
123123
[['name', 'class', 'event', 'method', 'url'], 'required'],
124124
[['name'], UniqueValidator::class, 'targetClass' => WebhookRecord::class],
@@ -131,7 +131,7 @@ function(string $attribute, array $params = null, Validator $validator) {
131131
if (!class_exists($this->class)) {
132132
$validator->addError($this, $attribute, Craft::t('webhooks', 'Class {value} doesn’t exist.'));
133133
}
134-
}
134+
},
135135
],
136136
[
137137
['event'],
@@ -155,7 +155,7 @@ function(string $attribute, array $params = null, Validator $validator) {
155155
$validator->addError($this, $attribute, Craft::t('webhooks', 'Class {class} doesn’t appear to have a {value} event.', ['class' => $this->class]));
156156
}
157157
}
158-
}
158+
},
159159
],
160160
[['debounceKeyFormat'], 'string'],
161161
[
@@ -171,13 +171,13 @@ function() {
171171
unset($this->filters[$class]);
172172
}
173173
}
174-
}
174+
},
175175
],
176176
[
177177
['headers'],
178178
function() {
179179
$this->headers = $this->headers ? array_values($this->headers) : [];
180-
}
180+
},
181181
],
182182
[['userAttributes', 'senderAttributes'], 'validateAttributeList'],
183183
[['eventAttributes'], 'validateAttributeList', 'params' => ['regex' => '/^[a-z]\w*\.[a-z]\w*(?:\.[a-z]\w*)*$/i']],

src/WebhookManager.php

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Craft;
66
use craft\db\Query;
7+
use craft\helpers\Db;
78
use craft\helpers\Json;
89
use craft\helpers\StringHelper;
910
use yii\base\InvalidArgumentException;
@@ -69,19 +70,15 @@ public function saveGroup(Group $group, bool $runValidation = true): bool
6970
}
7071

7172
if ($group->id) {
72-
$db->createCommand()
73-
->update('{{%webhookgroups}}', [
74-
'name' => $name,
75-
], [
76-
'id' => $group->id,
77-
])
78-
->execute();
73+
Db::update('{{%webhookgroups}}', [
74+
'name' => $name,
75+
], [
76+
'id' => $group->id,
77+
]);
7978
} else {
80-
$db->createCommand()
81-
->insert('{{%webhookgroups}}', [
82-
'name' => $name,
83-
])
84-
->execute();
79+
Db::insert('{{%webhookgroups}}', [
80+
'name' => $name,
81+
]);
8582

8683
$group->id = (int)$db->getLastInsertID('{{%webhookgroups}}');
8784
}
@@ -96,11 +93,9 @@ public function saveGroup(Group $group, bool $runValidation = true): bool
9693
*/
9794
public function deleteGroupById(int $id)
9895
{
99-
Craft::$app->getDb()->createCommand()
100-
->delete('{{%webhookgroups}}', [
101-
'id' => $id,
102-
])
103-
->execute();
96+
Db::delete('{{%webhookgroups}}', [
97+
'id' => $id,
98+
]);
10499
}
105100

106101
// Webhooks
@@ -205,17 +200,13 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
205200
'userAttributes' => $webhook->userAttributes,
206201
'senderAttributes' => $webhook->senderAttributes,
207202
'eventAttributes' => $webhook->eventAttributes,
208-
'payloadTemplate' => $webhook->payloadTemplate
203+
'payloadTemplate' => $webhook->payloadTemplate,
209204
];
210205

211206
if ($webhook->id) {
212-
$db->createCommand()
213-
->update('{{%webhooks}}', $data, ['id' => $webhook->id])
214-
->execute();
207+
Db::update('{{%webhooks}}', $data, ['id' => $webhook->id]);
215208
} else {
216-
$db->createCommand()
217-
->insert('{{%webhooks}}', $data)
218-
->execute();
209+
Db::insert('{{%webhooks}}', $data);
219210
$webhook->id = (int)$db->getLastInsertID('{{%webhooks}}');
220211
}
221212

@@ -229,9 +220,7 @@ public function saveWebhook(Webhook $webhook, bool $runValidation = true): bool
229220
*/
230221
public function deleteWebhookById(int $id)
231222
{
232-
Craft::$app->getDb()->createCommand()
233-
->delete('{{%webhooks}}', ['id' => $id])
234-
->execute();
223+
Db::delete('{{%webhooks}}', ['id' => $id]);
235224
}
236225

237226
/**

src/controllers/ActivityController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Craft;
66
use craft\db\Paginator;
77
use craft\db\Query;
8+
use craft\helpers\Db;
89
use craft\helpers\Json;
910
use craft\web\twig\variables\Paginate;
1011
use craft\webhooks\assets\activity\ActivityAsset;
@@ -36,7 +37,7 @@ public function actionIndex(): Response
3637
->orderBy(['id' => SORT_DESC]);
3738

3839
$paginator = new Paginator($query, [
39-
'currentPage' => Craft::$app->getRequest()->getPageNum()
40+
'currentPage' => $this->request->getPageNum(),
4041
]);
4142

4243
$requests = $paginator->getPageResults();
@@ -58,10 +59,7 @@ public function actionIndex(): Response
5859
*/
5960
public function actionClear(): Response
6061
{
61-
Craft::$app->getDb()->createCommand()
62-
->delete('{{%webhookrequests}}', ['status' => Plugin::STATUS_DONE])
63-
->execute();
64-
62+
Db::delete('{{%webhookrequests}}', ['status' => Plugin::STATUS_DONE]);
6563
return $this->redirect('webhooks/activity');
6664
}
6765

@@ -73,7 +71,7 @@ public function actionClear(): Response
7371
*/
7472
public function actionDetails(): Response
7573
{
76-
$requestId = Craft::$app->getRequest()->getRequiredBodyParam('requestId');
74+
$requestId = $this->request->getRequiredBodyParam('requestId');
7775
$request = Plugin::getInstance()->getRequestData($requestId);
7876

7977
if ($request['requestHeaders'] && $request['requestBody'] && $this->_isJson($request['requestHeaders'])) {
@@ -85,7 +83,7 @@ public function actionDetails(): Response
8583

8684
return $this->asJson([
8785
'html' => Craft::$app->getView()->renderTemplate('webhooks/_activity/details', [
88-
'request' => $request
86+
'request' => $request,
8987
]),
9088
]);
9189
}
@@ -98,7 +96,7 @@ public function actionDetails(): Response
9896
*/
9997
public function actionRedeliver(): Response
10098
{
101-
$requestId = Craft::$app->getRequest()->getRequiredBodyParam('requestId');
99+
$requestId = $this->request->getRequiredBodyParam('requestId');
102100
Plugin::getInstance()->sendRequest($requestId);
103101

104102
return $this->runAction('details');

0 commit comments

Comments
 (0)