forked from ltutech/buildiator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetBuilds.php
More file actions
executable file
·203 lines (179 loc) · 5.85 KB
/
Copy pathgetBuilds.php
File metadata and controls
executable file
·203 lines (179 loc) · 5.85 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
require_once ('lib/MockCI.php');
require_once ('lib/JenkinsCI.php');
require_once ('lib/Exceptions.php');
function isort($a,$b) {
return ($a['timeElapse']) > ($b['timeElapse']);
}
function displayJobsProblem($jobs)
{
$html = '';
if (isset($jobs))
foreach ($jobs as $job) {
$lsIcon = '';
$lsUrl = $job['url'];
$lsStatus = $job['status'][0];
if ($lsStatus == 'cancelled') {
$lsIcon = '<img src="images/pause.png" height=100% style="float:left">';
}
if ($lsStatus == 'disabled') {
$lsIcon = '<img src="images/stop.png" height=100% style="float:left">';
}
if ($lsStatus == 'failed') {
$lsUrl .= "/lastBuild/console";
}
if ($lsStatus == 'unstable') {
$lsUrl .= "/lastBuild/testReport";
}
if (count($job['status']) >= 2 and ($job['status'][1] == 'building')) {
$lsUrl = $job['url']."/lastBuild/console";
}
$claim = '';
if (!empty($job['claim'])) {
$culprit = $job['claim'];
$claim = '<img src="https://account.corp.ltutech.com/photos.php?user=' . $culprit .'" height=60 style="float:right"> ';
}
$winImage = '';
if (preg_match('/Win/', $job['name'], $matches, PREG_OFFSET_CAPTURE, 3)) {
$winImage = '<img src="images/win-logo.png" height=100% style="float:right">';
}
$progress = 0;
if (!empty($job['progress'])) {
$progress = $job['progress'];
}
$html .= "<li class = 'box jobBroken " . implode(" ",$job['status'] ) . "' onclick=\"window.open('$lsUrl')\">
<div class=\"buildName\">
{$job['name']}
</div>
<span style=\"width:{$progress}%\"></span>
</li>";
$html .= "<div id=\"building_jobs\">".displayJobsSuccess($jobsStableBuilding)."</div>
<li class = \"lastSuccedBuild\">
{$job['lastSuccessfulBuildTime']}{$claim}
</li>";
}
return $html;
}
function imageVictory()
{
$html = '';
$html .= '<div class="imageVictoryRight imageVictory"></div>';
$html .= '<div class="imageVictoryLeft imageVictory flip-horizontal"></div>';
return $html;
}
function displayRandomTumblrLesJoixDuCode()
{
$html = '';
$url = 'http://lesjoiesducode.tumblr.com/random';
$str = @file_get_contents($url);
if (preg_match('/<div class="post">(.+?)<h3>(.+?)">(.+?)<\/a>(.+?)<img(.+?)src="(.+?)\.gif"/', $str, $matches)) {
$lsMessage = $matches[3];
$lsURLGif = $matches[6] . ".gif";
}
$html .= "<img class=tumblrImage src=\"$lsURLGif\">" . "<div class=tumblrMessage><a>{$lsMessage}</a>";
return $html;
}
function displayJobsBorder($countJobsStable, $countJobsUnstable, $countJobsFailed)
{
$html = '<ul>
<li class = "box counter successful">'.
"$countJobsStable
</li>
<li class = \"box counter unstable\">".
"$countJobsUnstable
</li>
<li class = \"box counter failed\">".
"$countJobsFailed
</li>
</ul>";
return $html;
}
function displayJobsSuccess($jobsStable)
{
$html = '';
if (isset($jobsStable))
foreach ($jobsStable as $job) {
$lsUrl = $job['url'];
if ($job['status'][1] and ($job['status'][1] == 'building')) {
$lsUrl .= "/lastBuild/console";
}
$winImage = '';
if (preg_match('/Win/', $job['name'], $matches, PREG_OFFSET_CAPTURE, 3)) {
$winImage = '<img src="images/win-logo.png" height=100% style="float:right">';
}
$html .="<li class='box jobSuccess " . implode(" ",$job['status'] ) .
" onclick=\"window.open('$lsUrl')\">
<div class=\"buildName\">{$job['name']}{$winImage}</div>
<span style=\"width:{$job['progress']}%\"></span></li>";
}
return $html;
}
function generateHtml($jobs)
{
$html = '';
$jobsFailed = array();
$jobsUnstable = array();
$jobsCancel = array();
$jobsStable = array();
$jobsStableBuilding = array();
if (isset($jobs)) {
foreach ($jobs as $job) {
$lsStatus = $job['status'][0];
if ($lsStatus == 'failed') {
$jobsFailed[] = $job;
}
if ($lsStatus == 'unstable') {
$jobsUnstable[] = $job;
}
if (($lsStatus == 'cancelled') or ($lsStatus == 'disabled')) {
$jobsCancel[] = $job;
}
if ($lsStatus == 'successful') {
if (count($job['status']) >= 2 and ($job['status'][1] == 'building')) {
$jobsStableBuilding[] = $job;
}
else {
$jobsStable[] = $job;
}
}
}
}
@usort($jobsFailed, "isort");
@usort($jobsUnstable, "isort");
@usort($jobsCancel, "isort");
@usort($jobsClaim, "isort");
$html .= '<ul id="job_alerts">';
$html .= displayJobsProblem($jobsFailed);
$html .= displayJobsProblem($jobsUnstable);
$html .= displayJobsProblem($jobsClaim);
$html .= displayJobsProblem($jobsCancel);
$html .= '</ul>';
$html .= '<ul id="running_jobs">'.displayJobsSuccess($jobsStableBuilding).'</ul>';
$html .= '<div id="overlayCounter">'.displayJobsBorder(count($jobsStableBuilding) + count($jobsStable), count($jobsUnstable), count($jobsFailed)).'</div>';
if (count($jobsUnstable) + count($jobsFailed) + count($jobsCancel) == 0) {
$html .= imageVictory();
$minutes = intval(date('i'));
$secondes = intval(date('s'));
if (($minutes == 0) and ($second < 10)) {
$html .= displayRandomTumblrLesJoixDuCode();
}
}
return $html;
}
$result = '';
if (isset($_GET['view'])) {
$ci = new JenkinsCI('https://continuousintegration.corp.ltutech.com', $_GET['view']);
} else {
$ci = new JenkinsCI('https://continuousintegration.corp.ltutech.com');
}
try {
$jobs = $ci->getAllJobs();
} catch (BuildiatorCIServerCommunicationException $e) {
$result = array('status' => 'error', 'content' => $e->getMessage());
}
if (!is_array($result)) {
$html = generateHtml($jobs);
$result = array('status' => 'ok', 'content' => $html);
}
echo json_encode($result);
?>