Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 452 lines (389 sloc) 11.173 kB
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
1 #!/usr/bin/php
2 <?php
3 /**
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
4 * An example command line application built on the Joomla Platform.
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
5 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
6 * To run this example, adjust the executable path above to suite your operating system,
7 * make this file executable and run the file.
8 *
9 * Alternatively, run the file using:
10 *
11 * php -f run.php
12 *
eda07a6 @LouisLandry Cleaning up bootstrap/import files and adding gc_disable() to PHPUnit
LouisLandry authored
13 * @package Joomla.Examples
93d90a7 @mbabker Update copyright notices for 2013
mbabker authored
14 * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
eda07a6 @LouisLandry Cleaning up bootstrap/import files and adding gc_disable() to PHPUnit
LouisLandry authored
15 * @license GNU General Public License version 2 or later; see LICENSE
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
16 */
17
18 // Setup the path related constants.
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
19 define('JPATH_BASE', dirname(__FILE__));
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
20
21 // Bootstrap the application.
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
22 require realpath('../libraries/import.php');
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
23
24 /**
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
25 * Joomla Platform Changelog builder.
26 *
27 * This application builds the HTML version of the Joomla Platform change log from the Github API
28 * that is used in news annoucements.
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
29 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
30 * @package Joomla.Examples
31 * @subpackage Changlog
32 * @since 12.1
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
33 */
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
34 class Changelog extends JApplicationCli
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
35 {
36 /**
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
37 * The github API object.
38 *
39 * @var JGithub
40 * @since 12.1
41 */
42 protected $api;
43
44 /**
45 * Debug mode switch.
46 *
47 * @var boolean
48 * @since 12.1
49 */
50 protected $debug = false;
51
52 /**
53 * An array of output buffers.
54 *
55 * @var array
56 * @since 12.1
57 */
58 protected $buffers = array();
59
60 /**
61 * Overrides JGithub constructor to initialise the api property.
62 *
63 * @param mixed $input An optional argument to provide dependency injection for the application's
64 * input object. If the argument is a JInputCli object that object will become
65 * the application's input object, otherwise a default input object is created.
66 * @param mixed $config An optional argument to provide dependency injection for the application's
67 * config object. If the argument is a JRegistry object that object will become
68 * the application's config object, otherwise a default config object is created.
69 * @param mixed $dispatcher An optional argument to provide dependency injection for the application's
70 * event dispatcher. If the argument is a JDispatcher object that object will become
71 * the application's event dispatcher, if it is null then the default event dispatcher
72 * will be created based on the application's loadDispatcher() method.
73 *
74 * @see loadDispatcher()
75 * @since 11.1
76 */
77 public function __construct(JInputCli $input = null, JRegistry $config = null, JDispatcher $dispatcher = null)
78 {
79 parent::__construct($input, $config, $dispatcher);
80
5a60f0a @eddieajau Changed log script to get html body instead of translating markdown.
eddieajau authored
81 $options = new JRegistry;
82 $options->set('headers.Accept', 'application/vnd.github.html+json');
83 $this->api = new JGithub($options);
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
84 }
85
86 /**
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
87 * Execute the application.
88 *
89 * @return void
90 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
91 * @since 12.1
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
92 */
93 public function execute()
94 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
95 // Check if we just want the help page.
96 if ($this->input->get('h'))
97 {
98 $this->help();
99
100 return;
101 }
102
103 // Check for debug mode.
104 $this->debug = $this->input->getBool('d');
105
106 // Set the maximum number of pages (and runaway failsafe).
107 $cutoff = 100;
108 $page = 1;
109
110 // Check if we only want to get the latest version information.
111 $latestOnly = $this->input->get('l');
112
113 // Initialise the version cutoffs.
114 $versions = array(
115 0 => '12.1',
116 653 => '11.4',
117 310 => '11.3',
118 140 => '11.2',
119 72 => '11.1',
120 );
121
122 // Initialise arrays and metrics.
123 $log = array();
124 $userCount = array();
125 $pullCount = 0;
126 $mergedBy = array();
127 $labelled = array();
128
129 // Set the current version.
130 $version = $versions[0];
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
131
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
132 while ($cutoff--)
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
133 {
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
134 // Get a page of the closed issues.
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
135 $issues = $this->getIssues($page++);
136
137 // Check if we've gone past the last page.
138 if (empty($issues))
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
139 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
140 break;
141 }
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
142
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
143 // Loop through each pull.
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
144 foreach ($issues as $i => $issue)
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
145 {
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
146 $this->out(sprintf(' %03d ', $i + 1), false);
147
148 // Check if the issue is a pull request.
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
149 if (empty($issue->pull_request->html_url))
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
150 {
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
151 $this->out('Skipped; no pull request url.');
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
152 continue;
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
153 }
154
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
155 // Change the version
156 if (isset($versions[$issue->number]) || ($this->debug && $pullCount > 0))
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
157 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
158 // Populate buffers.
159 $this->setBuffer("$version.userCount", $userCount);
160 $this->setBuffer("$version.pullCount", $pullCount, false);
161 $this->setBuffer("$version.mergedBy", $mergedBy);
162 $this->setBuffer("$version.labelled", $labelled);
163
164 // Reset counters.
165 $pullCount = 0;
166 $userCount = array();
167 $mergedBy = array();
168 $labelled = array();
169
170 // Break if we only want the latest version.
171 if ($latestOnly || $this->debug)
5e33a19 @eddieajau Performance improvement.
eddieajau authored
172 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
173 break 2;
5e33a19 @eddieajau Performance improvement.
eddieajau authored
174 }
175
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
176 // Increment version.
177 $version = $versions[$issue->number];
178 }
179
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
180 // Get specific information about the pull request.
181 $data = $this->getPull($issue->number);
182
183 // Check if merged.
184 if (!$data->merged || $data->commits == 0)
185 {
186 $this->out(' - not merged.');
187 continue;
188 }
189
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
190 // Check if the issue is labelled.
191 foreach ($issue->labels as $label)
192 {
193 if (!isset($labelled[$label->name]))
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
194 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
195 $labelled[$label->name] = array();
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
196 }
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
197 $labelled[$label->name][] = '<a href="' . $issue->html_url . '">' . $issue->title . '</a>';
198 }
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
199
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
200 // Prepare the link to the pull.
201 $html = '[<a href="' . $issue->html_url . '" title="Closed ' . $issue->closed_at . '">';
202 $html .= '#' . $issue->number;
203 $html .= '</a>] <strong>' . $issue->title . '</strong>';
204 $html .= ' (<a href="https://github.com/' . $issue->user->login . '">' . $issue->user->login . '</a>)';
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
205
5a60f0a @eddieajau Changed log script to get html body instead of translating markdown.
eddieajau authored
206 if (trim($data->body_html))
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
207 {
5a60f0a @eddieajau Changed log script to get html body instead of translating markdown.
eddieajau authored
208 $html .= $data->body_html;
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
209 }
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
210
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
211 $this->setBuffer("$version.log", $html);
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
212
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
213 if (!isset($userCount[$issue->user->login]))
214 {
215 $userCount[$issue->user->login] = 0;
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
216 }
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
217 $userCount[$issue->user->login]++;
218 $pullCount++;
6aa3854 @eddieajau Updated changelog
eddieajau authored
219
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
220 if (!isset($mergedBy[$data->merged_by->login]))
221 {
222 $mergedBy[$data->merged_by->login] = 0;
223 }
224 $mergedBy[$data->merged_by->login]++;
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
225
226 $this->out(' - ok');
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
227 }
228 }
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
229
230 // Check if the output folder exists.
231 if (!is_dir('./docs'))
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
232 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
233 mkdir('./docs');
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
234 }
235
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
236 // Write the file.
237 file_put_contents('./docs/changelog.html', $this->render($latestOnly ? array($versions[0]) : $versions));
238
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
239 // Close normally.
240 $this->close();
241 }
242
243 /**
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
244 * Display the help text for the app.
245 *
246 * @return void
247 *
248 * @since 12.1
249 */
250 public function help()
251 {
252 $this->out();
253 $this->out('Joomla Platform Changelog');
254 $this->out('-------------------------');
255 $this->out();
256 $this->out('Usage: ./changelog [-l]');
257 $this->out();
258 $this->out(' -l Build the changelog for the latest version only.');
259 $this->out(' -d Debug mode. Test one issue.');
260 $this->out();
261 $this->out('Output is sent to docs/changelog.html');
262 $this->out();
263 }
264
265 /**
266 * Gets a named buffer.
267 *
268 * @param string $name the name of the buffer.
269 *
270 * @return string
271 *
272 * @since 12.1
273 */
274 protected function getBuffer($name)
275 {
276 if (isset($this->buffers[$name]))
277 {
278 return $this->buffers[$name];
279 }
280 else
281 {
282 return '';
283 }
284 }
285
286 /**
5e33a19 @eddieajau Performance improvement.
eddieajau authored
287 * Get a page of issue data.
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
288 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
289 * @param integer $page The page number.
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
290 *
291 * @return array
292 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
293 * @since 12.1
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
294 */
5e33a19 @eddieajau Performance improvement.
eddieajau authored
295 protected function getIssues($page)
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
296 {
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
297 $this->out(sprintf('Getting issues page #%02d.', $page));
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
298 $this->out(str_pad('', 40, '-'));
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
299
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
300 $issues = $this->api->issues
301 ->getListByRepository('joomla', 'joomla-platform', null, 'closed', null, null, null, 'updated', 'desc', null, $page, 100);
302
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
303 $this->out(sprintf('Got %s issues.', count($issues)));
304
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
305 return $issues;
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
306 }
5e33a19 @eddieajau Performance improvement.
eddieajau authored
307
308 /**
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
309 * Get information about a specific pull request.
5e33a19 @eddieajau Performance improvement.
eddieajau authored
310 *
eda07a6 @LouisLandry Cleaning up bootstrap/import files and adding gc_disable() to PHPUnit
LouisLandry authored
311 * @param integer $id The GitHub pull request number.
312 *
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
313 * @return object
314 *
315 * @since 12.1
316 */
317 protected function getPull($id)
318 {
8044dbc @eddieajau Fix up an edge case bug and pretty up log output.
eddieajau authored
319 $this->out(sprintf('Getting info for pull %6d', $id), false);
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
320
321 return $this->api->pulls->get('joomla', 'joomla-platform', $id);
322 }
323
324 /**
325 * Renders the output.
326 *
327 * @param array $versions An array of the versions to render.
328 *
329 * @return string
330 *
331 * @since 12.1
332 */
333 protected function render(array $versions)
334 {
335 $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
336 <html>
337 <head>
338 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
339 <title>Joomla Platform pull request log</title>
340 </head>
341 <body>';
342
343 foreach ($versions as $version)
344 {
345 // Print the version number.
346 $html .= PHP_EOL . ' <h1>' . $version . '</h1>';
347
348 // Print out the labelled version of the changelog first.
349 $labelled = $this->getBuffer("$version.labelled");
a3fef21 @ianmacl Code style fixes.
ianmacl authored
350
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
351 if ($labelled)
352 {
353 foreach ($labelled as $label => $links)
354 {
355 $html .= PHP_EOL . "<h2>$label</h2>";
356 $html .= PHP_EOL . '<ul>';
357
358 foreach ($links as $link)
359 {
360 $html .= PHP_EOL . "<li>$link</li>";
361 }
362
363 $html .= PHP_EOL . '</ul>';
364 }
365 }
366
367 // Print out the detailed version of the changelog.
368 $log = $this->getBuffer("$version.log");
369 $html .= PHP_EOL . '<h2>The following pull requests made by community contributors were merged:</h2>';
370 $html .= PHP_EOL . '<ol>';
a3fef21 @ianmacl Code style fixes.
ianmacl authored
371
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
372 foreach ($log as $issue)
373 {
374 $html .= PHP_EOL . "<li>$issue</li>";
375 }
376 $html .= PHP_EOL . '</ol>';
377
378 // Print out the user-pull statistics.
379 $userCount = $this->getBuffer("$version.userCount");
380 arsort($userCount);
381 $pullCount = $this->getBuffer("$version.pullCount");
382
383 $html .= PHP_EOL . sprintf('<h4>%d pull requests.</h4>', $pullCount);
384 $html .= PHP_EOL . '<ol>';
a3fef21 @ianmacl Code style fixes.
ianmacl authored
385
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
386 foreach ($userCount as $user => $count)
387 {
388 $html .= PHP_EOL . sprintf('<li><a href="https://github.com/%1$s">%1$s</a>: %2$d</li>', $user, $count);
389 }
390 $html .= PHP_EOL . ' </ol>';
391
392 // Print out the admin-merge statistics.
393 $mergedBy = $this->getBuffer("$version.mergedBy");
394 arsort($mergedBy);
395
396 $html .= PHP_EOL . '<h4>Merged by:</h4>';
397 $html .= PHP_EOL . '<ol>';
a3fef21 @ianmacl Code style fixes.
ianmacl authored
398
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
399 foreach ($mergedBy as $user => $count)
400 {
401 $html .= PHP_EOL . sprintf('<li><a href="https://github.com/%1$s">%1$s</a>: %2$d</li>', $user, $count);
402 }
403 $html .= PHP_EOL . '</ol>';
404 }
405
406 $html .= PHP_EOL . '</body>';
407 $html .= PHP_EOL . '</html>';
408
409 return $html;
410 }
411
412 /**
413 * Sets a named buffer.
414 *
415 * @param string $name The name of the buffer.
416 * @param mixed $text The text to put into/append to the buffer.
417 * @param boolean $append Append to the array buffer.
418 *
419 * @return void
420 *
421 * @since 12.1
422 */
423 protected function setBuffer($name, $text, $append = true)
424 {
425 if (!isset($this->buffers[$name]))
426 {
427 $this->buffers[$name] = array();
428 }
429
430 if (is_array($text) || !$append)
431 {
432 $this->buffers[$name] = $text;
433 }
434 else
435 {
436 $this->buffers[$name][] = $text;
437 }
438 }
1a47168 @eddieajau Added command line application to automatically generate the changelo…
eddieajau authored
439 }
440
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
441 // Catch any exceptions thrown.
442 try
443 {
a3fef21 @ianmacl Code style fixes.
ianmacl authored
444 JApplicationCli::getInstance('Changelog')->execute();
d9a518a @eddieajau Upgraded automatic changelog builder.
eddieajau authored
445 }
446 catch (Exception $e)
447 {
448 // An exception has been caught, just echo the message.
449 fwrite(STDOUT, $e->getMessage() . "\n");
450 exit($e->getCode());
451 }
Something went wrong with that request. Please try again.