-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathissue_comment_hook.php
121 lines (89 loc) · 3.51 KB
/
issue_comment_hook.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
function checkPullRequestData($data)
{
if((!array_key_exists("head", $data)) || (!array_key_exists("user", $data["head"])) || (!array_key_exists("login", $data["head"]["user"])))
return false;
if((!array_key_exists("head", $data)) || (!array_key_exists("sha", $data["head"])))
return false;
return true;
}
function checkBuildRequest($data)
{
if((!array_key_exists("issue", $data)) || (!array_key_exists("pull_request", $data["issue"])) || (!array_key_exists("url", $data["issue"]["pull_request"])))
return false;
if((!array_key_exists("action", $data)) || ($data["action"] != "created"))
return false;
if((!array_key_exists("issue", $data)) || (!array_key_exists("number", $data["issue"])))
return false;
if((!array_key_exists("comment", $data)) || (!array_key_exists("user", $data["comment"])) || (!array_key_exists("login", $data["comment"]["user"])))
return false;
if((!array_key_exists("comment", $data)) || (!array_key_exists("author_association", $data["comment"])) || ($data["comment"]["author_association"] != "MEMBER"))
return false;
if((!array_key_exists("comment", $data)) || (!array_key_exists("body", $data["comment"])))
return false;
if(strpos($data["comment"]["body"], "SFMLBuildMaster: Build this please.") === false)
return false;
return true;
}
function main()
{
$json = json_decode(file_get_contents("php://input"), true);
if(!checkBuildRequest($json))
{
print("<html><head><title>Issue Comment Hook</title></head><body>Build not started, build request check failed.</body></html>");
return;
}
$options = array(
"http" => array(
"user_agent" => "SFML BuildBot"
)
);
$prjson = json_decode(file_get_contents($json["issue"]["pull_request"]["url"], false, stream_context_create($options)), true);
if(!checkPullRequestData($prjson))
{
print("<html><head><title>Issue Comment Hook</title></head><body>Build not starte, pull request data check failed.</body></html>");
return;
}
$id = $json["issue"]["number"];
$user = $json["comment"]["user"]["login"];
$author = $prjson["head"]["user"]["login"];
$branch = "refs/pull/$id/head";
$revision = $prjson["head"]["sha"];
$repository = "SFML/SFML";
$comment = "Starting build requested by $user for pull request #$id on $repository.";
$category = "PR";
$data = array(
"repository" => "https://github.com/$repository.git",
"project" => $repository,
"branch" => $branch,
"revision" => $revision,
"comments" => $comment,
"author" => $author,
"category" => $category
);
$socket = stream_socket_client('unix:///home/buildbot/buildbot.sock', $errno, $errst, 4);
$payload = http_build_query($data);
$payloadlength = strlen($payload);
$file = fopen("../changehook.passwd", "r");
if($file === false)
{
print("<html><head><title>Issue Comment Hook</title></head><body>Build not started, cannot get changehook authentication data.</body></html>");
return;
}
preg_match('/([^:\s]*):([^:\s]*)/', fgets($file), $matches);
fclose($file);
$auth = base64_encode($matches[1] . ":" . $matches[2]);
$socketdata = "";
$socketdata .= "POST /change_hook/base HTTP/1.1\r\n";
$socketdata .= "Host: ci.sfml-dev.org\r\n";
$socketdata .= "Authorization: Basic $auth\r\n";
$socketdata .= "Content-type: application/x-www-form-urlencoded\r\n";
$socketdata .= "Content-Length: $payloadlength\r\n";
$socketdata .= "Connection: close\r\n\r\n";
fwrite($socket, $socketdata);
fwrite($socket, $payload);
fclose($socket);
print("<html><head><title>Issue Comment Hook</title></head><body>Build started.</body></html>");
}
main();
?>