Last active
September 16, 2016 14:48
-
-
Save k7y6t5/b6e01d5d181fee3e1f8a2f09111d9aa9 to your computer and use it in GitHub Desktop.
Beanstalk Modular Webhooks -> Glip Webhooks Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example Beanstalk json formatting (Git Deployment) | |
* http://support.beanstalkapp.com/article/958-modular-webhooks-json-examples | |
* | |
* { | |
* "trigger": "deploy", | |
* "payload": { | |
* "type": "Deployment", | |
* "id": 42, | |
* "state": "success", | |
* "deployed_at": "2014/08/18 23:54:43 +0000", | |
* "comment": "Deployment description.", | |
* "deploy_from_scratch": false, | |
* "retries": 0, | |
* "revision": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
* "user": { | |
* "type": "Owner", | |
* "id": 42, | |
* "login": "username", | |
* "email": "[email protected]", | |
* "name": "Name Surname" | |
* }, | |
* "environment": { | |
* "type": "DeploymentEnvironment", | |
* "id": 42, | |
* "name": "prod", | |
* "branch": "master", | |
* "automatic": false, | |
* "servers": [ | |
* { | |
* "type": "Server", | |
* "id": 42, | |
* "name": "server-name", | |
* "protocol": "shell", | |
* "remote_path": "~/", | |
* "repository_path": "/" | |
* } | |
* ] | |
* }, | |
* "repository": { | |
* "id": 42, | |
* "name": "repository-name", | |
* "title": "Repository Title", | |
* "type": "GitRepository" | |
* } | |
* } | |
* } | |
* | |
*/ | |
/** | |
* Example Glip json formatting | |
* | |
* { | |
* "icon": "http://tinyurl.com/pn46fgp", | |
* "activity": "Beer consumed", | |
* "title": "Jeff is having a Maple Bacon Coffee Porter", | |
* "body": "* Location: [The Funky Buddha Lounge](http://www.thefunkybuddha.com)\n* Beer Advocate Rating: [99](http://tinyurl.com/psf4uzq)" | |
* } | |
* | |
*/ | |
// Receive data from Beanstalk | |
$postData = file_get_contents("php://input"); | |
$postData = json_decode($postData); | |
// Glip's only required field is "body" | |
// You can use basic Markdown in your message body | |
$body .= "User: " . $postData->payload->user->name; | |
$body .= "\nRepo: " . $postData->payload->repository->title; | |
$body .= "\nBranch: " . $postData->payload->environment->branch; | |
$body .= "\nEnvironment: " . $postData->payload->environment->name . " (" . $postData->payload->environment->servers[0]->name . ")"; | |
$body .= "\nComment: " . $postData->payload->comment; | |
// Build your output data according to Glip's formatting | |
$output = array( | |
"activity" => $postData->payload->type . " - " . ucfirst($postData->payload->state) . "!", | |
"body" => $body, | |
); | |
$output = json_encode($output); | |
// Post to Glip | |
$url = "{YOUR GLIP WEBHOOK URL}"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $output); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json" | |
) | |
); | |
$result = curl_exec($ch); | |
// Close your connection! | |
curl_close($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment