Platform.sh

Add Webhooks to Platform.sh Projects in PHP

Add Webhooks to Platform.sh Projects in PHP

If you're reading this post, you're likely familiar with the Platform.sh CLI, but did you know there is a PHP Client available as well?

This library is invaluable to us at Thinkbean as it allows us to integrate into our internal Symfony based devops application which among other things, we use to monitor application status and health.

Platform.sh provides Generic Webhook integrations which allows you to provide a URL for Platform.sh specific activity to be sent to. For example, when a deployment is made, Platform.sh will sent a POST request to the endpoint you provided.

Our devops application receives and logs these events for display and trigger handlers based on certain criteria.

I recently noticed we missed the webhook on one of our projects and decided automate the creation of it. This not only resolves the problem of a developer missing the integration step on setup, but removes that step of the setup entirely.

For the sake of simplicity, the solution can best be described as a daily cron job which adds the webhook integration to any projects that are missing it.

Our application uses a event queue to trigger and process jobs, however a task as simple as this could be achieved by using the function below in a Symfony Console Command or even a vanilla PHP script.

This is a pretty niche use case, but I encourage you to explore Platform's PHP library and start integrating Platform.sh into your own applications!

<?php
    /**
     * Assures webhooks are added to each project.
     *
     * @return array An array of projects which webhook integrations were added to.
     */
    public function addWebhookIntegrations()
    {
        // Your endpoint which is to receive the Platform.sh POST request.
        $url =  'https://example.com/webhooks/platformsh?token=' . WEBHOOK_TOKEN;

        $integrationsAdded = [];

        // Platform.sh API Client.
        //$client = $this->client;
        $client = new \Platformsh\Client\PlatformClient();

        // Retrieve the projects.
        $projects = $client->getProjects();

        // Run through projects.
        foreach ($projects as $p) {

            $hasWebhook = false;

            // Retrieve integrations for this project.
            $integrations = $p->getIntegrations();

            // Run through the integrations.
            foreach ($integrations as $i) {
                // Check if this is a webhook integration.
                if ($i->type === 'webhook') {
                    // We found what we came for.
                    $hasWebhook = true;
                    break;
                }
            }

            // Add the integration if it doesn't exist.
            if (!$hasWebhook) {
                $p->addIntegration('webhook', ['url' => $url]);
                $integrationsAdded[] = $p->title;
            }
        }

        return $integrationsAdded;
    }

Up Next

Ready To Get Started?

Schedule a complimentary 30-minute strategy consultation with one of our Drupal experts as early as today. We promise...they don't bite!