-
Notifications
You must be signed in to change notification settings - Fork 21
/
RoboFile.php
450 lines (385 loc) · 12.2 KB
/
RoboFile.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* This is project's console commands configuration for Robo task runner.
*
* @see https://robo.li/
*/
class RoboFile extends \Robo\Tasks
{
public function buildLog($a, $b = 'HEAD', $with_head = true) {
$log = ConventionalChangelog::buildLog($a, $b, $with_head);
echo implode(PHP_EOL, $log);
}
}
class ConventionalChangelog
{
/**
* @param array $commmits commits to filter
*/
public static function filterCommits($commits) {
$types = [
'build', 'chore', 'ci', 'docs', 'fix', 'feat', 'perf', 'refactor', 'style', 'test'
];
$types = implode('|', $types);
$scope = "(\((?P<scope>[^\)]*)\))?";
$subject = ".*";
$filter = "/^(?P<type>$types)$scope:(?P<subject>$subject)$/";
$filtered = [];
$matches = null;
foreach ($commits as $commit) {
if (preg_match($filter, $commit->subject, $matches) === 1) {
$commit->type = $matches['type'];
$commit->scope = '';
if (isset($matches['scope']) && strlen($matches['scope']) > 0) {
$commit->scope = $matches['scope'];
}
$commit->subject = trim($matches['subject']);
$filtered[] = $commit;
}
}
return $filtered;
}
public static function compareCommits($a, $b) {
// comapre scopes
if ($a->scope < $b->scope) {
return -1;
} else if ($a->scope > $b->scope) {
return 1;
}
// then compare subject
if ($a->subject < $b->subject) {
return -1;
} else if ($a->subject > $b->subject) {
return 1;
}
return 0;
}
public static function buildLog($a, $b = 'HEAD', $with_head = true) {
// if (!Git::tagExists($b)) {
// // $b is not a tag, try to find a matching one
// if (Git::getTagOfCommit($b) === false) {
// throw new Exception("current HEAD does not match a tag");
// }
// }
// get All tags between $a and $b
$tags = Git::getAllTags();
// Remove non semver compliant versions
$tags = array_filter($tags, function ($tag) {
return Semver::isSemVer($tag);
});
$startVersion = $a;
$endVersion = $b;
$prefix = 'v';
if (substr($startVersion, 0, strlen($prefix)) == $prefix) {
$startVersion = substr($startVersion, strlen($prefix));
}
if (substr($endVersion, 0, strlen($prefix)) == $prefix) {
$endVersion = substr($endVersion, strlen($prefix));
}
$tags = array_filter($tags, function ($version) use ($startVersion, $endVersion) {
$prefix = 'v';
if (substr($version, 0, strlen($prefix)) == $prefix) {
$version = substr($version, strlen($prefix));
}
if (version_compare($version, $startVersion) < 0) {
return false;
}
if ($endVersion !== 'HEAD' && version_compare($version, $endVersion) > 0) {
return false;
}
return true;
});
// sort tags older DESCENDING
usort($tags, function ($a, $b) {
return version_compare($b, $a);
});
$log = [];
if ($b === 'HEAD') {
array_unshift($tags, $b);
}
$startRef = array_shift($tags);
if ($startRef === null) {
throw new RuntimeException("$a not found");
}
while ($endRef = array_shift($tags)) {
$log = array_merge($log, self::buildLogOneBump($startRef, $endRef, $with_head));
$startRef = $endRef;
}
if ($with_head) {
$log = array_merge($log, self::buildLogOneBump($startRef, null , $with_head));
}
return $log;
}
public static function buildLogOneBump($a, $b, $with_head = true) {
$tag = $a;
/*if (!Git::tagExists($b)) {
// $b is not a tag, try to find a matching one
if ($tag = Git::getTagOfCommit($b) === false) {
$tag = 'Unreleased';
}
}*/
// get remote
$remotes = Git::getRemotes();
$remote = $remotes['origin'];
// Get all commits from A to B
$commits = Git::createCommitList(Git::getLog($b, $a));
// Remove non conventional commits
$commits = self::filterCommits($commits);
// Keep only useful commits for changelog
$fixes = array_filter($commits, function ($commit) {
if (in_array($commit->type, ['fix'])) {
return true;
}
return false;
});
$feats = array_filter($commits, function ($commit) {
if (in_array($commit->type, ['feat'])) {
return true;
}
return false;
});
// Sort commits
usort($fixes, [self::class, 'compareCommits']);
usort($feats, [self::class, 'compareCommits']);
// generate markdown log
$log = [];
$tagDate = (new DateTime())->format('Y-m-d');
$compare = "$remote/compare/$b..";
if ($tag !== 'Unreleased') {
$tagDate = Git::getTagDate($tag)->format('Y-m-d');
$compare .= $tag;
} else {
$compare .= Git::getCurrentBranch();
}
if ($with_head && $tag !== 'HEAD') {
$log[] = '<a name="' . $tag . '"></a>';
$log[] = '## [' . $tag . '](' . $compare . ') (' . $tagDate . ')';
}
if (count($fixes) > 0) {
if ($with_head) {
$log[] = '';
$log[] = '';
}
$log[] = '### Bug Fixes';
$log[] = '';
foreach ($fixes as $commit) {
$log[] = self::buildLogLine($commit, $remote);
}
}
if (count($feats) > 0) {
$log[] = '';
$log[] = '';
$log[] = '### Features';
$log[] = '';
foreach ($feats as $commit) {
$log[] = self::buildLogLine($commit, $remote);
}
}
$log[] = '';
$log[] = '';
$log[] = '';
return $log;
}
public static function buildLogLine($commit, $remote) {
$line = '* ';
$scope = $commit->scope;
if ($scope !== '') {
$scope = "**$scope:**";
$line .= $scope;
}
$hash = $commit->hash;
$line .= " $commit->subject"
. " ([$hash]($remote/commit/$hash))";
// Search for closed issues
$body = explode(PHP_EOL, $commit->body ?? '');
$pattern = '/^((close|closes|fix|fixed) #(?P<id>\\d+)(,\s+)?)/i';
$commit->close = [];
foreach ($body as $bodyLine) {
$matches = null;
if (preg_match($pattern, $bodyLine, $matches)) {
if (!is_array($matches['id'])) {
$matches['id'] = [$matches['id']];
}
$commit->close = $matches['id'];
}
}
if (count($commit->close) > 0) {
foreach ($commit->close as &$issue) {
$issue = "[#$issue]($remote/issues/$issue)";
}
$line .= ', closes ' . implode(', ', $commit->close);
}
return $line;
}
}
class Git
{
public static function getCurrentCommitHash() {
exec('git rev-parse HEAD', $output, $retCode);
if ($retCode != '0') {
throw new Exception("failed to get curent commit hash");
}
return $output[0];
}
public static function isTagMatchesCurrentCommit($tag) {
$commitHash = self::getCurrentCommitHash();
exec("git tag -l --contains $commitHash", $output, $retCode);
if (isset($output[0]) && $output[0] == $tag) {
return true;
}
return false;
}
public static function getTagOfCommit($hash) {
exec("git tag -l --contains $hash", $output, $retCode);
if (isset($output[0])) {
return $output[0];
}
return false;
}
public static function getTagDate($tag) {
exec("git log -1 --format=%ai $tag", $output, $retCode);
if ($retCode != '0') {
throw new Exception("failed to get date of a tag");
}
if (isset($output[0])) {
return new DateTime($output[0]);
}
return false;
}
/**
* get highest sember tag from list
* @param array $tags list of tags
*/
public static function getLastTag($tags, $prefix = '') {
// Remove prefix from all tags
if ($prefix !== '') {
$newTags = [];
foreach ($tags as $tag) {
if (substr($tag, 0, strlen($prefix)) == $prefix) {
$tag = substr($tag, strlen($prefix));
}
$newTags[] = $tag;
}
$tags = $newTags;
}
// get tags and keey only sember compatible ones
$tags = array_filter($tags, [SemVer::class, 'isSemVer']);
// Sort tags
usort($tags, function ($a, $b) {
return version_compare($a, $b);
});
return end($tags);
}
public static function createCommitList($commits) {
// Biuld list of commits, latest is oldest
$commitObjects = [];
foreach ($commits as $commit) {
$line = explode(' ', $commit, 2);
$commitObject = new StdClass();
$commitObject->hash = $line[0];
$commitObject->subject = $line[1];
$commitObjects[] = $commitObject;
$commitObject->body = self::getCommitBody($commitObject->hash);
}
return $commitObjects;
}
public static function getLog($a, $b = 'HEAD') {
if ($a === null) {
exec("git log --oneline $b", $output, $retCode);
if ($retCode != '0') {
// An error occured
throw new Exception("Unable to get log from the repository");
}
} else {
exec("git log --oneline $a..$b", $output, $retCode);
if ($retCode != '0') {
// An error occured
throw new Exception("Unable to get log from the repository");
}
}
return $output;
}
public static function getRemotes() {
exec("git remote -v", $output, $retCode);
if ($retCode != '0') {
// An error occured
throw new Exception("Unable to get remotes of the repository");
}
$remotes = [];
foreach ($output as $line) {
$line = explode("\t", $line);
$line[1] = explode(' ', $line[1]);
$line[1] = $line[1][0];
// 0 = name
// 1 = URL
// 2 = (fetch) or (push)
if (strpos($line[1], 'git@') === 0) {
// remote type is SSH
$split = explode('@', $line[1]);
//$user = $split[0];
$split = explode(':', $split[1]);
$url = 'https://' . $split[0] . '/' . $split[1];
}
if (strpos($line[1], 'https://') === 0) {
$url = $line[1];
}
$remotes[$line[0]] = $url;
}
return $remotes;
}
public static function getAllTags() {
exec("git tag -l", $output, $retCode);
if ($retCode != '0') {
// An error occured
throw new Exception("Unable to get tags from the repository");
}
return $output;
}
public static function tagExists($version) {
$tags = self::getAllTags();
return in_array($version, $tags);
}
/**
* Get a file from git tree
* @param string $path
* @param string $rev a commit hash, a tag or a branch
* @throws Exception
* @return string content of the file
*/
public static function getFileFromGit($path, $rev) {
$output = shell_exec("git show $rev:$path");
if ($output === null) {
throw new Exception ("coult not get file from git: $rev:$path");
}
return $output;
}
public static function getCommitBody($hash) {
$output = shell_exec("git log $hash --max-count=1 --pretty=format:\"%b\"");
return $output;
}
public static function getCurrentBranch() {
$output = shell_exec("git rev-parse --abbrev-ref HEAD");
if ($output === null) {
throw new Exception ("could not get current branch");
}
return $output;
}
}
class SemVer
{
/**
* Check the version is made of numbers separated by dots
*
* Returns true if the version is well formed, false otherwise
*
* @param string $version
* @return boolean
*/
public static function isSemVer($version) {
$semverPattern = '#\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b#i';
if (preg_match($semverPattern, $version) !== 1) {
return false;
}
return true;
}
}