Skip to content

Commit 5b78569

Browse files
authored
Merge pull request #35 from jsor-labs/component-changelog
Implement custom changelog from github releases for components
2 parents ec99e4e + b74ddef commit 5b78569

File tree

7 files changed

+101
-78
lines changed

7 files changed

+101
-78
lines changed

src/berti.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Website\Berti;
44

55
use Berti\Document;
6+
use function React\Website\Data\releases_by_year;
67
use Symfony\Component\Process\Process;
78
use function Berti\uri_rewriter;
89

@@ -83,10 +84,18 @@ function ($component) use ($repo) {
8384

8485
$context['component'] = $component;
8586

86-
$name = 'component.html.twig';
87-
88-
if ('LICENSE' === $documentInput->getFilename()) {
89-
$name = 'component-license.html.twig';
87+
$context['component_releases_by_year'] = releases_by_year($component['releases']);
88+
89+
switch ($documentInput->getFilename()) {
90+
case 'LICENSE':
91+
$name = 'component-license.html.twig';
92+
break;
93+
case 'CHANGELOG.md':
94+
$name = 'component-changelog.html.twig';
95+
break;
96+
default:
97+
$name = 'component.html.twig';
98+
break;
9099
}
91100

92101
return $bertiRenderer($name, $context);

src/data.php

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,47 @@ function components(Client $client, CacheItemPoolInterface $markdownCache): arra
1414
$component['contributors'] = $client->repo()->contributors($username, $repository);
1515
$component['participation'] = $client->repo()->participation($username, $repository);
1616

17-
$releases = (new ResultPager($client))
17+
$apiReleases = (new ResultPager($client))
1818
->fetchAll(
1919
$client->repo()->releases(),
2020
'all',
2121
[$username, $repository, ['per_page' => 100]]
2222
);
2323

24-
$releases = array_map(
25-
function (array $release) use ($client, $markdownCache, $component) {
26-
$cacheKey = 'gfm' . md5($component['repository'] . $release['body']);
27-
28-
$cacheItem = $markdownCache->getItem($cacheKey);
29-
30-
if ($cacheItem->isHit()) {
31-
$html = $cacheItem->get();
32-
} else {
33-
$html = $client->markdown()->render(
34-
$release['body'],
35-
'gfm',
36-
$component['repository']
37-
);
38-
39-
$cacheItem->set($html);
40-
$markdownCache->save($cacheItem);
41-
}
42-
43-
return [
44-
'version' => ltrim($release['tag_name'], 'v'),
45-
'tag' => $release['tag_name'],
46-
'date' => new \DateTimeImmutable($release['created_at']),
47-
'html' => $html,
48-
'url' => $release['html_url'],
49-
];
50-
},
51-
$releases
52-
);
24+
$releases = [];
25+
26+
foreach ($apiReleases as $release) {
27+
$cacheKey = 'gfm' . md5($component['repository'] . $release['body']);
28+
29+
$cacheItem = $markdownCache->getItem($cacheKey);
30+
31+
if ($cacheItem->isHit()) {
32+
$html = $cacheItem->get();
33+
} else {
34+
$html = $client->markdown()->render(
35+
$release['body'],
36+
'gfm',
37+
$component['repository']
38+
);
39+
40+
$cacheItem->set($html);
41+
$markdownCache->save($cacheItem);
42+
}
43+
44+
$date = new \DateTimeImmutable($release['created_at']);
5345

54-
usort($releases, function ($a, $b) {
55-
return \version_compare($b['version'], $a['version']);
56-
});
46+
$releases[(int) $date->format('U')] = [
47+
'version' => ltrim($release['tag_name'], 'v'),
48+
'tag' => $release['tag_name'],
49+
'date' => $date,
50+
'html' => $html,
51+
'url' => $release['html_url'],
52+
'component' => $component['title'],
53+
'repository' => $component['repository'],
54+
];
55+
}
56+
57+
krsort($releases, SORT_NATURAL);
5758

5859
$component['releases'] = array_values($releases);
5960

@@ -87,14 +88,11 @@ function releases(array $components): array
8788
foreach ($component['releases'] as $release) {
8889
$time = (int) $release['date']->format('U');
8990

90-
$releases[(PHP_INT_MAX - $time) . '-' . $component['repository']] = [
91-
'component' => $component['title'],
92-
'repository' => $component['repository'],
93-
] + $release;
91+
$releases[$time . '-' . $component['repository']] = $release;
9492
}
9593
}
9694

97-
ksort($releases, SORT_NATURAL);
95+
krsort($releases, SORT_NATURAL);
9896

9997
return array_values($releases);
10098
}

theme/changelog.html.twig

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,7 @@
66
<div class="wrapper wrapper--small">
77
{{ berti.content|raw }}
88

9-
<ul class="toc">
10-
{% for year, releases in releases_by_year %}
11-
<li><a href="#{{ year }}" title="Jump to year {{ year }} ({{ releases|length }} releases)">{{ year }}</a></li>
12-
{% endfor %}
13-
</ul>
14-
15-
{% for year, releases in releases_by_year %}
16-
<h2>
17-
<a id="{{ year }}" class="anchor" href="#{{ year }}" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>
18-
{{ year }}
19-
</h2>
20-
21-
{% for release in releases %}
22-
23-
{% set _id = release.component|lower ~ '-' ~ release.version|replace({'.': ''}) ~ '-' ~ release.date|date('Y-m-d') %}
24-
<h3>
25-
<a id="{{ _id }}" class="anchor" href="#{{ _id }}" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>
26-
27-
{{ release.component }} {{ release.version }}
28-
29-
<small>
30-
({{ release.date|date('Y-m-d') }})
31-
<a href="{{ release.url }}" title="Release on GitHub">
32-
<i class="icon-github" aria-hidden="true"></i> <span class="visually-hidden">Release on GitHub</span>
33-
</a>
34-
</small>
35-
</h3>
36-
37-
{{ release.html|raw }}
38-
39-
<hr>
40-
{% endfor %}
41-
{% endfor %}
9+
{% include 'partials/changelog.html.twig' with {releases_by_year: releases_by_year, is_combined_changelog: true} %}
4210
</div>
4311
</div>
4412

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'component.html.twig' %}
2+
3+
{% block title %}{{ component.title ~ ': Changelog' }}{% endblock %}
4+
{% block component_content %}
5+
<h1>{{ component.title }} Changelog</h1>
6+
7+
{% include 'partials/changelog.html.twig' with {releases_by_year: component_releases_by_year, is_combined_changelog: false} %}
8+
{% endblock %}

theme/component-license.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% block title %}{{ component.title ~ ': License' }}{% endblock %}
44
{% block component_content %}
5-
{{ '# License'|markdown }}
5+
<h1>{{ component.title }} License</h1>
66

7-
{{ parent() }}
7+
{{ berti.content|strip_title|emoji|raw }}
88
{% endblock %}

theme/component.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<div class="wrapper">
1313
<div class="grid grid--wrap grid--gutter">
1414
<main class="main">
15-
{% block component_content %}{{ berti.content|emoji|raw }}{% endblock %}
15+
{% block component_content %}
16+
<h1>{{ component.title }}</h1>
17+
18+
{{ berti.content|strip_title|emoji|raw }}
19+
{% endblock %}
1620
</main>
1721
<aside class="sidebar" role="complementary">
1822
<section class="component-info">

theme/partials/changelog.html.twig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<ul class="toc">
2+
{% for year, releases in releases_by_year %}
3+
<li><a href="#{{ year }}" title="Jump to year {{ year }} ({{ releases|length }} release{% if releases|length != 1 %}s{% endif %})">{{ year }}</a></li>
4+
{% endfor %}
5+
</ul>
6+
7+
{% for year, releases in releases_by_year %}
8+
<h2>
9+
<a id="{{ year }}" class="anchor" href="#{{ year }}" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>
10+
{{ year }}
11+
</h2>
12+
13+
{% for release in releases %}
14+
15+
{% set _id = release.version|replace({'.': ''}) ~ '-' ~ release.date|date('Y-m-d') %}
16+
{% if is_combined_changelog == true %}
17+
{% set _id = release.component|lower ~ '-' ~ _id %}
18+
{% endif %}
19+
<h3>
20+
<a id="{{ _id }}" class="anchor" href="#{{ _id }}" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>
21+
22+
{% if is_combined_changelog == true %}{{ release.component }} {% endif %}{{ release.version }}
23+
24+
<small>
25+
({{ release.date|date('Y-m-d') }})
26+
<a href="{{ release.url }}" title="Release on GitHub">
27+
<i class="icon-github" aria-hidden="true"></i> <span class="visually-hidden">Release on GitHub</span>
28+
</a>
29+
</small>
30+
</h3>
31+
32+
{{ release.html|raw }}
33+
34+
<hr>
35+
{% endfor %}
36+
{% endfor %}

0 commit comments

Comments
 (0)