Grapic dots

Add Webhooks to Platform.sh Projects in PHP

Mike Milano

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!

  1. <?php
  2. /**
  3.   * Assures webhooks are added to each project.
  4.   *
  5.   * @return array An array of projects which webhook integrations were added to.
  6.   */
  7. public function addWebhookIntegrations()
  8. {
  9. // Your endpoint which is to receive the Platform.sh POST request.
  10. $url = 'https://example.com/webhooks/platformsh?token=' . WEBHOOK_TOKEN;
  11.  
  12. $integrationsAdded = [];
  13.  
  14. // Platform.sh API Client.
  15. //$client = $this->client;
  16. $client = new \Platformsh\Client\PlatformClient();
  17.  
  18. // Retrieve the projects.
  19. $projects = $client->getProjects();
  20.  
  21. // Run through projects.
  22. foreach ($projects as $p) {
  23.  
  24. $hasWebhook = false;
  25.  
  26. // Retrieve integrations for this project.
  27. $integrations = $p->getIntegrations();
  28.  
  29. // Run through the integrations.
  30. foreach ($integrations as $i) {
  31. // Check if this is a webhook integration.
  32. if ($i->type === 'webhook') {
  33. // We found what we came for.
  34. $hasWebhook = true;
  35. break;
  36. }
  37. }
  38.  
  39. // Add the integration if it doesn't exist.
  40. if (!$hasWebhook) {
  41. $p->addIntegration('webhook', ['url' => $url]);
  42. $integrationsAdded[] = $p->title;
  43. }
  44. }
  45.  
  46. return $integrationsAdded;
  47. }
Tags:  Platform.shPHP

8 Reasons Why Standardizing on Drupal is the Right Choice for Enterprise.
Read about it in this free white paper.

Download for Free Now!