Skip to content

Commit f9feac9

Browse files
committed
refactor: clearly identify data added by dinghy
The JSON data rendered by templates is mostly the GraphQL data returned by GitHub. But there is a bit of computed data added by dinghy, and some pieces have been re-parented to indicate hierarchy. All of the keys added by dinghy now have a `dinghy_` prefix for clarity. This is in support of issues #31 and #32.
1 parent 739176d commit f9feac9

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/dinghy/digest.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ def _dec(func):
3939
return _dec
4040

4141

42+
def dd(name: str) -> str:
43+
"""Create an key for Dinghy-specfic data. (dd == Dinghy Data)
44+
45+
Dinghy adds extra data to GitHub responses. All of the keys for this data
46+
are prefixed, created with this function.
47+
"""
48+
return f"dinghy_{name}"
49+
50+
51+
DD_children = dd("children")
52+
53+
4254
class Digester:
4355
"""
4456
Use GitHub GraphQL to get data about recent changes.
@@ -78,8 +90,8 @@ async def get_org_project_entries(self, org, number, home_repo="", title=None):
7890
entries = await self._process_entries(entries)
7991
for entry in entries:
8092
entry["other_repo"] = entry["repository"]["nameWithOwner"] != home_repo
81-
if "children" not in entry:
82-
entry["children"] = entry["comments"]["nodes"]
93+
if DD_children not in entry:
94+
entry[DD_children] = entry["comments"]["nodes"]
8395
project = glom(project, "data.organization.project")
8496
container = {
8597
"url": project["url"],
@@ -332,7 +344,7 @@ async def _process_issue(self, issue):
332344
issue (dict): the issue to populate.
333345
"""
334346
await self.get_more(issue["comments"], "issue_comments.graphql", issue["id"])
335-
issue["children"] = self._trim_unwanted(issue["comments"]["nodes"])
347+
issue[DD_children] = self._trim_unwanted(issue["comments"]["nodes"])
336348

337349
async def _process_pull_request(self, pull):
338350
"""
@@ -377,7 +389,7 @@ async def _process_pull_request(self, pull):
377389

378390
# Make a map of the reviews.
379391
for rev in pull["reviews"]["nodes"]:
380-
rev["review_state"] = rev["state"]
392+
rev[dd("review_state")] = rev["state"]
381393
reviews[rev["id"]] = rev
382394

383395
# For each thread, attach the thread as a child of the review. Each
@@ -387,24 +399,24 @@ async def _process_pull_request(self, pull):
387399
# comment 1.
388400
for thread in pull["reviewThreads"]["nodes"]:
389401
com0 = thread["comments"]["nodes"][0]
390-
com0["children"] = thread["comments"]["nodes"][1:]
402+
com0[DD_children] = thread["comments"]["nodes"][1:]
391403
com0["isResolved"] = thread["isResolved"]
392404
rev_id = com0["pullRequestReview"]["id"]
393-
review_comments = reviews[rev_id].setdefault("children", [])
405+
review_comments = reviews[rev_id].setdefault(DD_children, [])
394406
review_comments.append(com0)
395407

396408
# For each review, show it if it has a body, or if it has children, or
397409
# if it's not just "COMMENTED".
398410
for rev in reviews.values():
399-
if rev["bodyText"] or rev.get("children") or rev["state"] != "COMMENTED":
411+
if rev["bodyText"] or rev.get(DD_children) or rev["state"] != "COMMENTED":
400412
com = children.setdefault(rev["id"], dict(rev))
401-
com["review_state"] = rev["state"]
413+
com[dd("review_state")] = rev["state"]
402414

403-
if not rev["bodyText"] and len(rev.get("children", ())) == 1:
415+
if not rev["bodyText"] and len(rev.get(DD_children, ())) == 1:
404416
# A review with just one comment and no body: the comment should
405417
# go where the review would have been.
406-
com = rev["children"][0]
407-
com["review_state"] = rev["review_state"]
418+
com = rev[DD_children][0]
419+
com[dd("review_state")] = rev[dd("review_state")]
408420
children[rev["id"]] = com
409421

410422
# Comments are simple: they all get shown.
@@ -414,7 +426,7 @@ async def _process_pull_request(self, pull):
414426
# Examine all the resulting threads (children). Keep a thread if it has
415427
# any comments newer than our since date. Mark older comments as old.
416428
kids, _ = self._trim_unwanted_tree(children.values())
417-
pull["children"] = kids
429+
pull[DD_children] = kids
418430

419431
def _trim_unwanted_tree(self, nodes):
420432
"""
@@ -429,12 +441,12 @@ def _trim_unwanted_tree(self, nodes):
429441
any_interesting = True
430442
else:
431443
any_interesting = False
432-
node["boring"] = True
444+
node[dd("boring")] = True
433445
kids, any_interesting_kids = self._trim_unwanted_tree(
434-
node.get("children", ())
446+
node.get(DD_children, ())
435447
)
436448
if any_interesting or any_interesting_kids:
437-
node["children"] = kids
449+
node[DD_children] = kids
438450
keep.append(node)
439451
any_interesting_total = True
440452
keep = sorted(keep, key=operator.itemgetter("updatedAt"))
@@ -451,7 +463,7 @@ def _add_reasons(self, entry):
451463
# write "reasonCreated" based on "createdAt", etc.
452464
for slug in ["Created", "Closed", "Merged"]:
453465
at = slug.lower() + "At"
454-
entry[f"reason{slug}"] = bool(entry.get(at) and entry[at] > self.since)
466+
entry[dd(f"reason{slug}")] = bool(entry.get(at) and entry[at] > self.since)
455467

456468

457469
def coro_from_item(digester, item):

src/dinghy/templates/digest.html.j2

+12-12
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,23 @@
214214

215215
<p><a href="{{ entry.url }}">{# -#}
216216
<span class="octicons" title="
217-
{%- if entry.reasonCreated %}newly created; {% endif -%}
218-
{%- if entry.reasonMerged -%}newly merged; {% elif entry.reasonClosed %}newly closed; {% endif -%}
217+
{%- if entry.dinghy_reasonCreated %}newly created; {% endif -%}
218+
{%- if entry.dinghy_reasonMerged -%}newly merged; {% elif entry.dinghy_reasonClosed %}newly closed; {% endif -%}
219219
{%- if entry.isDraft %}draft {% endif -%}
220220
{%- if entry.merged %}merged {% elif entry.closed %}closed {% endif -%}
221221
{%- if entry.__typename == "PullRequest" %}pull request{% endif -%}
222222
{%- if entry.__typename == "Issue" %}issue{% endif -%}
223223
{%- if entry.__typename == "Release" %}release{% endif -%}
224224
">{# -#}
225225

226-
{%- if entry.reasonCreated -%}
226+
{%- if entry.dinghy_reasonCreated -%}
227227
<span class="octicon star"></span>
228228
{%- else -%}
229229
<span class="octicon dash"></span>
230230
{%- endif -%}
231-
{% if entry.reasonMerged -%}
231+
{% if entry.dinghy_reasonMerged -%}
232232
<span class="octicon check"></span>
233-
{%- elif entry.reasonClosed -%}
233+
{%- elif entry.dinghy_reasonClosed -%}
234234
<span class="octicon {% if entry.__typename == "PullRequest" %}x{% else %}check{% endif %}"></span>
235235
{%- else -%}
236236
<span class="octicon dash"></span>
@@ -270,15 +270,15 @@
270270
{%- if entry.bodyText %}<p>{{ entry.bodyText|trim|truncate(120) }}</p>{% endif -%}
271271
{%- if entry.description %}<p>{{ entry.description|trim|truncate(120) }}</p>{% endif -%}
272272

273-
{% if entry.children -%}
273+
{% if entry.dinghy_children -%}
274274
<ul class="comments">
275-
{% for child in entry.children recursive -%}
275+
{% for child in entry.dinghy_children recursive -%}
276276
<li{% if child.boring %} class="boring"{% endif %}>
277277
<p><a href="{{ child.url }}" title="{{ child.updatedAt|datetime() }}">
278-
{% if child.review_state -%}
279-
{% if child.review_state == "APPROVED" -%}
278+
{% if child.dinghy_review_state -%}
279+
{% if child.dinghy_review_state == "APPROVED" -%}
280280
<span class="octicon checkcircle" title="reviewed, approved"></span>
281-
{% elif child.review_state == "CHANGES_REQUESTED" -%}
281+
{% elif child.dinghy_review_state == "CHANGES_REQUESTED" -%}
282282
<span class="octicon filediff" title="reviewed, changes requested"></span>
283283
{% else %}
284284
<span class="octicon eye" title="reviewed"></span>
@@ -291,8 +291,8 @@
291291
<b>{{ child.author.login }}</b>{# -#}
292292
{%- if child.bodyText %}: {{ child.bodyText|trim|truncate(100) }}{% endif -%}
293293
</a></p>
294-
{% if child.children %}
295-
<ul class="thread_comment">{{ loop(child.children) }}</ul>
294+
{% if child.dinghy_children %}
295+
<ul class="thread_comment">{{ loop(child.dinghy_children) }}</ul>
296296
{% endif %}
297297
</li>
298298
{% endfor -%}

0 commit comments

Comments
 (0)