-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.php
More file actions
119 lines (112 loc) · 4.54 KB
/
github.php
File metadata and controls
119 lines (112 loc) · 4.54 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
$text_from_github = file_get_contents('php://input');
$json_from_github = json_decode($text_from_github, true);
$event_type = $_SERVER['HTTP_X_GITHUB_EVENT'];
function get_text($event_type, $json_from_github) {
if ($event_type == 'ping') {
return 'ping from github.com';
} else if ($event_type == 'push') {
$commits = $json_from_github['commits'];
$text = '#### ' . $event_type . " from github.com\n";
$ref = $json_from_github['ref'];
// $ref_type = explode('/', $ref)[1];
$ref_url = $json_from_github['repository']['html_url'] . '/tree/' . $ref;
$text .= '[' . $ref . '](' . $ref_url . ")\n";
foreach ($commits as $commit) {
$text .= '[' . $commit['id'] . '](' . $commit['url'] . ') ' . $commit['message'] . "\n";
$added = $commit['added'];
if (count($added) > 0) {
foreach ($added as $file) {
$text .= 'A ' . $file . "\n";
}
}
$modified = $commit['modified'];
if (count($modified) > 0) {
foreach ($modified as $file) {
$text .= 'M ' . $file . "\n";
}
}
$removed = $commit['removed'];
if (count($removed) > 0) {
foreach ($removed as $file) {
$text .= 'D ' . $file . "\n";
}
}
}
return $text;
} else if ($event_type == 'pull_request') {
$action = $json_from_github['action'];
$pull_request = $json_from_github['pull_request'];
$text = '#### ' . $action . " pull request: [" . $pull_request['title'] . "](" . $pull_request['html_url'] . ")\n";
$text .= $pull_request['body'] . "\n";
return $text;
} else if ($event_type == 'pull_request_review_comment') {
$action = $json_from_github['action'];
$pull_request = $json_from_github['pull_request'];
$comment = $json_from_github['comment'];
$text = '#### ' . $action . " comment on pull request: [" . $pull_request['title'] . "](" . $comment['html_url'] . ")\n";
$text .= "```diff\n" . $comment['diff_hunk'] . "\n```\n";
if ($comment['in_reply_to_id'] != null) {
$text .= "in reply to [" . $comment['in_reply_to_id'] . "](" . str_replace($comment['id'], $comment['in_reply_to_id'], $comment['html_url']) . ")\n";
}
$text .= $comment['body'] . "\n";
return $text;
} else if ($event_type == 'issues') {
$action = $json_from_github['action'];
$issue = $json_from_github['issue'];
$text = '#### ' . $action . " issue: [" . $issue['title'] . "](" . $issue['html_url'] . ")\n";
$text .= $issue['body'] . "\n";
return $text;
} else if ($event_type == 'issue_comment') {
$action = $json_from_github['action'];
$issue = $json_from_github['issue'];
$comment = $json_from_github['comment'];
$text = '#### ' . $action . " comment on issue: [" . $issue['title'] . "](" . $issue['html_url'] . ")\n";
$text .= $comment['body'] . "\n";
return $text;
} else if ($event_type == 'release') {
$action = $json_from_github['action'];
$release = $json_from_github['release'];
$text = '#### ' . $action . " release: [" . $release['name'] . "](" . $release['html_url'] . ")\n";
$text .= $release['body'] . "\n";
return $text;
} else if ($event_type == 'create') {
$ref_type = $json_from_github['ref_type'];
$ref = $json_from_github['ref'];
$text = '#### ' . $event_type . " " . $ref_type . ": " . $ref . "\n";
return $text;
} else {
return "#### " . $event_type . " from github.com\n" .
"```json\n" . $text_from_github . "\n```";
}
}
// Mattermost webhook URL
$url = $_GET['mattermost'];
$postData = [
'text' => get_text($event_type, $json_from_github),
'username' => $json_from_github['sender']['login'],
'icon_url' => $json_from_github['sender']['avatar_url'],
];
// Setup cURL
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
echo "response: $response\n";
// Check for errors
if($response === FALSE){
echo "curl_getinfo: \n";
echo print_r(curl_getinfo($ch), true);
echo "\n";
die(curl_error($ch));
}
// Close the cURL handler
curl_close($ch);
?>