Skip to content

Instantly share code, notes, and snippets.

@d3y4n
Created April 12, 2017 20:53
Show Gist options
  • Save d3y4n/1aaed12459607474c5422938b4af4cd9 to your computer and use it in GitHub Desktop.
Save d3y4n/1aaed12459607474c5422938b4af4cd9 to your computer and use it in GitHub Desktop.
post-checkout
#!/usr/bin/env php
<?php
/**
* @author Dejan Marjanovic <dm@php.net>
*/
$repository = basename(trim(`git rev-parse --show-toplevel`));
$branch = trim(`git rev-parse --abbrev-ref HEAD 2>/dev/null`);
$ip = gethostbyname(gethostname());
$message = "`$repository` switched to branch `$branch` on `$ip`";
$payload = array(
'channel' => '#channel',
'username' => 'Git',
'icon_emoji' => ':git:',
'text' => $message,
'attachments' =>
array(
array(
'color' => '#000000',
'text' => trim(`git log -n 1 --pretty=medium`),
'mrkdwn_in' => array('text'),
),
),
);
$payload = json_encode($payload);
$payload = array('payload' => $payload);
$payload = http_build_query($payload);
$webhook = 'https://hooks.slack.com/services/...';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhook);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
curl_close($ch);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment