/
functions.php
81 lines (58 loc) · 2.25 KB
/
functions.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
<?php
function log_github_postdata(){
$gh_log_file = 'logs/github.log';
if(isset($_POST))
{
$gh_data = parse_gh_data();
$log = "\r". date("Y-m-d H:i:s - ") . $gh_data['repo'] . ' - ' . $gh_data['user'] . ' - ' . $gh_data['rev'];
file_put_contents($gh_log_file, $log , FILE_APPEND | LOCK_EX);
}
}
function parse_gh_data(){
$raw_data = json_decode($_POST['payload']);
$new_data['repo'] = $raw_data->repository->url;
$new_data['user'] = $raw_data->pusher->name;
$new_data['rev'] = $raw_data->after;
return $new_data;
}
function post_to_newrelic($app_id, $apikey){
$gh_data = parse_gh_data();
$dep_change = "This is a change log entry";
$dep_user = "This is the user entry";
$dep_rev = "This is a version number";
#compose the data string for curl
$dep_dat = "deployment[app_id]=".$app_id;
$dep_dat = $dep_dat."&deployment[description]=".$gh_data['repo'];
$dep_dat = $dep_dat."&deployment[changelog]=". $gh_data['user'] . ' deployed changes. revision: ' . $gh_data['rev'];
$dep_dat = $dep_dat."&deployment[user]=".$gh_data['user'];
$dep_dat = $dep_dat."&deployment[revision]=".$gh_data['rev'];
#There should be no changes necessary beyond this point
#deployment url at New Relic
$url = "https://api.newrelic.com/deployments.xml";
#Create header info
$header = array("x-api-key:".$apikey);
$nr_log_file = 'logs/newrelic.log';
$log = "\r". date("Y-m-d H:i:s - ") . $app_id . ' - ' . $gh_data['user'] . ' - ' . $gh_data['rev'];
file_put_contents($nr_log_file, $log , FILE_APPEND | LOCK_EX);
#initialize curl
$ch = curl_init();
curl_setopt ($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $dep_dat );
# Make the curl call for deployment
$http_result = curl_exec ($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
#close curl
curl_close ($ch);
#output status
vprintf ("Code %s\n", $http_code);
vprintf ("Results %s\n", $http_result);
file_put_contents($nr_log_file, $http_code . ' - '. $http_result , FILE_APPEND | LOCK_EX);
if ($error) {
vprintf ("Error %s\n",$error);
file_put_contents($nr_log_file, $error , FILE_APPEND | LOCK_EX);
}
}