Skip to content

Commit e8a2840

Browse files
authored
Improve file diff display in GitHub PR comments (#12918)
1 parent 4b70d1c commit e8a2840

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

readthedocs/builds/tests/test_tasks.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,12 @@ def test_post_build_overview(self, get_diff, post_comment):
352352
[<kbd> &nbsp; 🔍 Preview build &nbsp; </kbd>](http://my-project--1.readthedocs.build/en/1/)
353353
354354
355-
<details>
356-
<summary>Show files changed (3 files in total): 📝 1 modified | ➕ 1 added | ➖ 1 deleted</summary>
357-
358-
| File | Status |
359-
| --- | --- |
360-
| [changes.html](http://my-project--1.readthedocs.build/en/1/changes.html) | ➕ added |
361-
| [deleteme.html](http://my-project--1.readthedocs.build/en/1/deleteme.html) | ➖ deleted |
362-
| [index.html](http://my-project--1.readthedocs.build/en/1/index.html) | 📝 modified |
363-
364-
355+
<details open>
356+
<summary>3 files changed</summary>
357+
<br>
358+
<code>+</code> <a href="http://my-project--1.readthedocs.build/en/1/changes.html"><code>changes.html</code></a><br>
359+
<code>±</code> <a href="http://my-project--1.readthedocs.build/en/1/index.html"><code>index.html</code></a><br>
360+
<code>-</code> <a href="http://my-project--1.readthedocs.build/en/1/deleteme.html"><code>deleteme.html</code></a><br>
365361
</details>
366362
367363
"""
@@ -401,17 +397,20 @@ def test_post_build_overview_more_than_5_files(self, get_diff, post_comment):
401397
402398
403399
<details>
404-
<summary>Show files changed (6 files in total): 📝 4 modified | ➕ 1 added | ➖ 1 deleted</summary>
400+
<summary>6 files changed · <code>+</code> 1 added · <code>±</code> 4 modified · <code>-</code> 1 deleted</summary>
401+
<br>
402+
403+
`+` **Added**
404+
- [`changes.html`](http://my-project--1.readthedocs.build/en/1/changes.html)
405405
406-
| File | Status |
407-
| --- | --- |
408-
| [changes.html](http://my-project--1.readthedocs.build/en/1/changes.html) | ➕ added |
409-
| [deleteme.html](http://my-project--1.readthedocs.build/en/1/deleteme.html) | ➖ deleted |
410-
| [index.html](http://my-project--1.readthedocs.build/en/1/index.html) | 📝 modified |
411-
| [one.html](http://my-project--1.readthedocs.build/en/1/one.html) | 📝 modified |
412-
| [three.html](http://my-project--1.readthedocs.build/en/1/three.html) | 📝 modified |
413-
| [two.html](http://my-project--1.readthedocs.build/en/1/two.html) | 📝 modified |
406+
`±` **Modified**
407+
- [`index.html`](http://my-project--1.readthedocs.build/en/1/index.html)
408+
- [`one.html`](http://my-project--1.readthedocs.build/en/1/one.html)
409+
- [`three.html`](http://my-project--1.readthedocs.build/en/1/three.html)
410+
- [`two.html`](http://my-project--1.readthedocs.build/en/1/two.html)
414411
412+
`-` **Deleted**
413+
- [`deleteme.html`](http://my-project--1.readthedocs.build/en/1/deleteme.html)
415414
416415
</details>
417416

readthedocs/filetreediff/dataclasses.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,8 @@ def deleted(self):
175175
def modified(self):
176176
"""List of modified files."""
177177
return [file for file in self.files if file.status == FileTreeDiffFileStatus.modified]
178+
179+
@cached_property
180+
def should_auto_expand(self):
181+
"""Auto-expand the details view when there are few files."""
182+
return len(self.files) < 5

readthedocs/templates/core/build-overview.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@ Template used for generating the build overview page that is posted as a comment
33

44
Whitespaces and newlines are important in some places like lists and tables,
55
make sure to adjust the tags accordingly, as they introduce newlines.
6+
7+
Markdown inside <details> requires a blank line after </summary>.
68
{% endcomment %}
79
### Documentation build overview
810

911
> 📚 [{{ project.name }}](https://{{ PRODUCTION_DOMAIN }}{% url "projects_detail" project.slug %}) | 🛠️ Build [#{{ current_version_build.pk }}](https://{{ PRODUCTION_DOMAIN }}{% url "builds_detail" project.slug current_version_build.pk %}) | 📁 Comparing {{ current_version_build.commit }} against [{{ base_version.verbose_name }}]({{ base_version.get_absolute_url }}) ({{ base_version_build.commit }})
1012
1113
[<kbd> &nbsp; 🔍 Preview build &nbsp; </kbd>]({{ current_version.get_absolute_url }})
1214

13-
{% if diff.files %}
15+
{% if diff.files %}{% if diff.should_auto_expand %}
16+
<details open>
17+
<summary>{{ diff.files|length }} files changed</summary>
18+
<br>
19+
{% for file in diff.added %}<code>+</code> <a href="{{ file.url }}"><code>{{ file.path }}</code></a><br>
20+
{% endfor %}{% for file in diff.modified %}<code>±</code> <a href="{{ file.url }}"><code>{{ file.path }}</code></a><br>
21+
{% endfor %}{% for file in diff.deleted %}<code>-</code> <a href="{{ file.url }}"><code>{{ file.path }}</code></a><br>
22+
{% endfor %}</details>
23+
{% else %}
1424
<details>
15-
<summary>Show files changed ({{ diff.files|length }} files in total): 📝 {{ diff.modified|length }} modified | ➕ {{ diff.added|length }} added | ➖ {{ diff.deleted|length }} deleted</summary>
16-
17-
| File | Status |
18-
| --- | --- |
19-
{% for file in diff.files %}| [{{ file.path }}]({{ file.url }}) | {{ file.status.emoji }} {{ file.status }} |
20-
{% endfor %}
21-
25+
<summary>{{ diff.files|length }} files changed{% if diff.added %} · <code>+</code> {{ diff.added|length }} added{% endif %}{% if diff.modified %} · <code>±</code> {{ diff.modified|length }} modified{% endif %}{% if diff.deleted %} · <code>-</code> {{ diff.deleted|length }} deleted{% endif %}</summary>
26+
<br>
27+
{% if diff.added %}
28+
`+` **Added**
29+
{% for file in diff.added|slice:":10" %}- [`{{ file.path }}`]({{ file.url }})
30+
{% endfor %}{% if diff.added|length > 10 %}- *and {{ diff.added|length|add:"-10" }} more...*
31+
{% endif %}{% endif %}{% if diff.modified %}
32+
`±` **Modified**
33+
{% for file in diff.modified|slice:":10" %}- [`{{ file.path }}`]({{ file.url }})
34+
{% endfor %}{% if diff.modified|length > 10 %}- *and {{ diff.modified|length|add:"-10" }} more...*
35+
{% endif %}{% endif %}{% if diff.deleted %}
36+
`-` **Deleted**
37+
{% for file in diff.deleted|slice:":10" %}- [`{{ file.path }}`]({{ file.url }})
38+
{% endfor %}{% if diff.deleted|length > 10 %}- *and {{ diff.deleted|length|add:"-10" }} more...*
39+
{% endif %}{% endif %}
2240
</details>
23-
{% else %}
41+
{% endif %}{% else %}
2442
No files changed.
2543
{% endif %}

0 commit comments

Comments
 (0)