• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NIT-Administrative-Systems / SysDev-laravel-soa / 6382763040

02 Oct 2023 04:20PM UTC coverage: 46.235% (-0.08%) from 46.316%
6382763040

push

github

nie7321
Fix for creating new webhooks

EventHub began disallowing the topic name in POSTs for new webhooks in 98787d7c6

1 of 1 new or added line in 1 file covered. (100.0%)

264 of 571 relevant lines covered (46.23%)

18.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Console/Commands/EventHub/WebhookConfiguration.php
1
<?php
2

3
namespace Northwestern\SysDev\SOA\Console\Commands\EventHub;
4

5
use Illuminate\Console\Command;
6
use Northwestern\SysDev\SOA\EventHub;
7
use Illuminate\Console\ConfirmableTrait;
8
use Northwestern\SysDev\SOA\Routing\EventHubWebhookRegistration;
9

10
class WebhookConfiguration extends Command
11
{
12
    use ConfirmableTrait;
13

14
    protected $signature = 'eventhub:webhook:configure {--force}';
15
    protected $description = 'Configure webhook routes in EventHub';
16

17
    protected $webhook_api;
18
    protected $hook_registry;
19

20
    public function __construct(EventHub\Webhook $webhook_api, EventHubWebhookRegistration $hook_registry)
21
    {
22
        parent::__construct();
×
23

24
        $this->webhook_api = $webhook_api;
×
25
        $this->hook_registry = $hook_registry;
×
26
    } // end __construct
27

28
    public function handle()
29
    {
30
        $force_delete = $this->option('force');
×
31

32
        $registered_hooks = collect($this->webhook_api->listAll()['webhooks']);
×
33
        $registered_hooks = $registered_hooks->map(function ($hook) {
×
34
            return $hook['topicName'];
×
35
        })->all();
×
36

37
        // We can remove stuff from this one as we touch them -- that we we know which still exist.
38
        $existing_hooks_updated = $registered_hooks;
×
39

40
        $desired_hooks = $this->hook_registry->getHooks();
×
41
        foreach ($desired_hooks as $hook) {
×
42
            $hook = $hook->toArray();
×
43

44
            // Remove from unmanaged list
45
            $existing_hooks_updated = array_diff($existing_hooks_updated, [$hook['topicName']]);
×
46

47
            $this->createOrUpdate($hook, $registered_hooks);
×
48
        }
49

50
        if (sizeof($existing_hooks_updated) > 0) {
×
51
            $this->line('');
×
52
            $this->error('The following webhooks are configured in EventHub but do not have a corresponding entry in the routes file:');
×
53
            $this->line('');
×
54
            $this->error(implode(', ', $existing_hooks_updated));
×
55
            $this->line('');
×
56

57
            $delete_unmanaged = $force_delete ?: $this->confirm('Would you like to delete these unmanaged webhooks?');
×
58
            if ($delete_unmanaged === true) {
×
59
                foreach ($existing_hooks_updated as $hook_to_delete) {
×
60
                    $this->delete($hook_to_delete);
×
61
                }
62
            }
63
        }
64

65
        // Display status after the changes are made.
66
        $this->call('eventhub:webhook:status');
×
67

68
        return 0;
×
69
    } // end handle
70

71
    protected function createOrUpdate($hook, $registered_hooks)
72
    {
73
        try {
74
            // If the hook exists on EventHub already, we don't need to touch the 'active' status.
75
            // But, new ones will require it.
76
            if (in_array($hook['topicName'], $registered_hooks) === false) {
×
77
                // Not permitted in creates since Jan 2022 (but fine for updates)
78
                unset($hook['topicName']);
×
79

80
                $hook['active'] = true;
×
81

82
                $this->webhook_api->create($hook['topicName'], $hook);
×
83
            } else {
84
                $this->webhook_api->updateConfig($hook['topicName'], $hook);
×
85
            }
86
        } catch (EventHub\Exception\EventHubError $e) {
×
87
            $this->line('');
×
88
            $this->error(vsprintf('Failed to update %s: %s', [$hook['topicName'], $e->getMessage()]));
×
89
            $this->line('');
×
90
        }
91
    } // end createOrUpdate
92

93
    protected function delete($queue_name)
94
    {
95
        try {
96
            $this->webhook_api->delete($queue_name);
×
97
        } catch (EventHub\Exception\EventHubError $e) {
×
98
            $this->line('');
×
99
            $this->error(vsprintf('Failed to delete %s: %s', [$queue_name, $e->getMessage()]));
×
100
            $this->line('');
×
101
        }
102
    } // end delete
103
} // end WebhookConfiguration
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc