-
Notifications
You must be signed in to change notification settings - Fork 6
/
gitlabhook.php
104 lines (76 loc) · 3.08 KB
/
gitlabhook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
// Core Configuration
$jabberServer = "yourjabber.server.com";
$jabberPort = 5222;
$jabberUser = "gitlab";
$jabberPassword = "password";
$jabberDomain = "server.com";
// The jabber notification types. groupchat for messages in a Jabber MUC,
// broadcast for Openfires broadcast plugin: http://www.igniterealtime.org/projects/openfire/plugins/broadcast/readme.html
$notificationType = "groupchat"; // groupchat / broadcast
// The following lines depend on which notification type you have chosen. For
// a MUC, ensure $groupChatRoom is correct, for the broadcast plugin, chek $broadcastService
$groupChatRoom = "[email protected]/GitLab";
$broadcastService = "[email protected]";
// End configuration
/**
* Checks whether a string is valid json.
*
* @param string $string
* @return boolean
*/
function valid_json($string)
{
try {
// try to decode string
json_decode($string);
}
catch (ErrorException $e) {
// exception has been caught which means argument wasn't a string and thus is definitely no json.
return FALSE;
}
// check if error occured
return (json_last_error() == JSON_ERROR_NONE);
}
// Gitlab Hook Handler
// Get the $_REQUEST and check if it is valid JSON.
$request = file_get_contents('php://input');
$json = ( valid_json($request) ? json_decode($request) : die() );
// Parse the JSON into a message payload that will be sent to the Jabber Server
$message = "\n";
$branch = $json->repository->name . ':' . $json->ref . " - " . $json->repository->url;
$message .= $json->user_name . " pushed " . count($json->commits) . " commits to " . $branch . "\n";
foreach ($json->commits as $commit) {
// prepare commits message
$message .= $commit->author->name . ": " . $commit->message . " - " . $commit->url . "\n";
}
$message .= "\n";
// Jabber Message Handling
include 'XMPPHP/XMPP.php';
// Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports. Set $printlog=true and run from
// the cli to test.
// If this doesn't work, are you running 64-bit PHP with < 5.2.6?
$conn = new XMPPHP_XMPP($jabberServer, $jabberPort, $jabberUser, $jabberPassword, 'gitlabhook', $jabberDomain, $printlog=false, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
try {
$conn->connect();
$conn->processUntil('session_start');
switch ($notificationType) {
case 'groupchat':
$conn->presence(NULL, "gitlab", $groupChatRoom, "available");
$conn->message($groupChatRoom, $message, $type='groupchat');
$conn->presence(NULL, "gitlab", $groupChatRoom, "unavailable");
break;
case 'broadcast':
$conn->presence(NULL, "gitlab", null, "available");
sleep(5); // Openfire fix. Message wont be sent if we connect, send and disconnect too fast.
$conn->message($broadcastService, $message);
$conn->presence(NULL, "gitlab", null, "unavailable");
break;
}
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
// Close the Jabber Connection
$conn = null;
?>