Skip to content

Commit d87ecf1

Browse files
unhappychoiceclaude
andcommitted
refactor(github): route items.rs through forge_items shared pipeline
`IssueItem` gains two fields the wider shape coverage needs: - `comments: u64` → drives the `Bars` per-row activity weight. - `user: Option<GithubUserRef>` → carries `avatar_url` for `ImageLinkedList`. `render_items` now converts items into `forge_items::ForgeRow` via the new `to_forge_row` and delegates rendering to `forge_items::render_forge_rows`. The function signature stays unchanged so the existing per-fetcher call sites keep compiling; the per-fetcher files migrate to the richer `dispatch_rows_async` API in follow-up commits. `sample_rows` is a small fixture helper mirroring the gitlab/items.rs counterpart so docs / catalog samples format consistently across both forges. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent ad1ae8d commit d87ecf1

1 file changed

Lines changed: 147 additions & 68 deletions

File tree

‎src/fetcher/github/items.rs‎

Lines changed: 147 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
//! Shared response types for the issue / PR family. The REST search endpoints and the
22
//! per-repo PR/issue endpoints return almost the same item shape; they share this struct so
33
//! each fetcher can focus on its own URL composition and rendering.
4+
//!
5+
//! Rendering goes through [`crate::fetcher::forge_items::render_forge_rows`] so the github and
6+
//! gitlab families produce structurally identical output for every shape — adding a new shape
7+
//! variant lights up on both families simultaneously.
48
59
use serde::Deserialize;
610

7-
use crate::payload::{
8-
Body, EntriesData, Entry, LinkedLine, LinkedTextBlockData, TextBlockData, TimelineData,
9-
TimelineEvent,
10-
};
11+
use crate::fetcher::forge_items::{self, ForgeRow};
12+
use crate::payload::Body;
1113
use crate::render::Shape;
1214

1315
use super::common::{RepoSlug, parse_timestamp};
1416

15-
#[derive(Debug, Deserialize)]
17+
#[derive(Debug, Deserialize, Default)]
1618
pub struct IssueItem {
1719
pub title: String,
1820
pub number: u64,
@@ -25,6 +27,18 @@ pub struct IssueItem {
2527
pub html_url: String,
2628
#[serde(default)]
2729
pub state: String,
30+
/// Comments count, fed into the `Bars` shape as a per-row activity weight.
31+
#[serde(default)]
32+
pub comments: u64,
33+
/// Author block — only `avatar_url` is used today (for the `ImageLinkedList` shape).
34+
#[serde(default)]
35+
pub user: Option<GithubUserRef>,
36+
}
37+
38+
#[derive(Debug, Deserialize, Default)]
39+
pub struct GithubUserRef {
40+
#[serde(default)]
41+
pub avatar_url: Option<String>,
2842
}
2943

3044
#[derive(Debug, Deserialize)]
@@ -40,78 +54,60 @@ pub fn repo_from_url(url: &str) -> Option<RepoSlug> {
4054
RepoSlug::parse(rest)
4155
}
4256

43-
/// Renders issue / PR items to one of `TextBlock` / `LinkedTextBlock` / `Entries` / `Timeline`.
44-
/// When `include_repo` is true, each line/event is prefixed with `owner/name` — used by
45-
/// user-scope fetchers that return items across many repos. Per-repo fetchers pass `false` so
46-
/// the repo name isn't repeated on every row.
57+
/// Renders issue / PR items via the shared forge-row pipeline. When `include_repo` is true,
58+
/// each label is prefixed with `owner/name#42` — used by user-scope fetchers whose results span
59+
/// many repos. Per-repo fetchers pass `false` so the row stays at `#42`.
4760
pub fn render_items(items: &[IssueItem], shape: Shape, include_repo: bool) -> Body {
48-
match shape {
49-
Shape::Entries => Body::Entries(EntriesData {
50-
items: items
51-
.iter()
52-
.map(|i| Entry {
53-
key: entries_key(i, include_repo),
54-
value: Some(i.title.clone()),
55-
status: None,
56-
})
57-
.collect(),
58-
}),
59-
Shape::LinkedTextBlock => Body::LinkedTextBlock(LinkedTextBlockData {
60-
items: items
61-
.iter()
62-
.map(|i| LinkedLine {
63-
text: format!("{} {}", short_label(i, include_repo), i.title),
64-
url: link_for(i),
65-
})
66-
.collect(),
67-
}),
68-
Shape::Timeline => Body::Timeline(TimelineData {
69-
events: items
70-
.iter()
71-
.map(|i| TimelineEvent {
72-
timestamp: parse_timestamp(&i.updated_at),
73-
title: short_label(i, include_repo),
74-
detail: Some(i.title.clone()),
75-
status: None,
76-
})
77-
.collect(),
78-
}),
79-
_ => Body::TextBlock(TextBlockData {
80-
lines: items
81-
.iter()
82-
.map(|i| format!("{} {}", short_label(i, include_repo), i.title))
83-
.collect(),
84-
}),
85-
}
61+
let rows = to_forge_rows(items, include_repo);
62+
forge_items::render_forge_rows(&rows, shape)
8663
}
8764

88-
fn link_for(i: &IssueItem) -> Option<String> {
89-
if i.html_url.is_empty() {
90-
None
91-
} else {
92-
Some(i.html_url.clone())
93-
}
65+
/// Lower-level converter so fetchers can pre-process rows (e.g. resolving thumbnails for
66+
/// `ImageLinkedList`) before handing them to the shared renderer.
67+
pub fn to_forge_rows(items: &[IssueItem], include_repo: bool) -> Vec<ForgeRow> {
68+
items
69+
.iter()
70+
.map(|i| to_forge_row(i, include_repo))
71+
.collect()
72+
}
73+
74+
/// Build a tiny [`ForgeRow`] set with stable values for `sample_body`. Mirrors the gitlab
75+
/// helper so both families share the same row-formatting conventions in docs.
76+
pub fn sample_rows(rows: &[(&str, &str, Option<&str>, u64, i64)]) -> Vec<ForgeRow> {
77+
rows.iter()
78+
.map(|(label, title, url, activity, ts)| ForgeRow {
79+
label: (*label).into(),
80+
title: (*title).into(),
81+
url: url.map(String::from),
82+
avatar_url: None,
83+
avatar_path: None,
84+
updated_at_unix: *ts,
85+
activity_count: *activity,
86+
})
87+
.collect()
9488
}
9589

96-
fn short_label(i: &IssueItem, include_repo: bool) -> String {
97-
if include_repo {
90+
fn to_forge_row(i: &IssueItem, include_repo: bool) -> ForgeRow {
91+
let label = if include_repo {
9892
let repo = repo_from_url(&i.repository_url)
9993
.map(|s| s.as_path())
10094
.unwrap_or_else(|| "?".into());
10195
format!("{repo}#{}", i.number)
10296
} else {
10397
format!("#{}", i.number)
104-
}
105-
}
106-
107-
fn entries_key(i: &IssueItem, include_repo: bool) -> String {
108-
if include_repo {
109-
let name = repo_from_url(&i.repository_url)
110-
.map(|s| s.name)
111-
.unwrap_or_else(|| "?".into());
112-
format!("{name} #{}", i.number)
113-
} else {
114-
format!("#{}", i.number)
98+
};
99+
ForgeRow {
100+
label,
101+
title: i.title.clone(),
102+
url: if i.html_url.is_empty() {
103+
None
104+
} else {
105+
Some(i.html_url.clone())
106+
},
107+
avatar_url: i.user.as_ref().and_then(|u| u.avatar_url.clone()),
108+
avatar_path: None,
109+
updated_at_unix: parse_timestamp(&i.updated_at),
110+
activity_count: i.comments,
115111
}
116112
}
117113

@@ -128,6 +124,10 @@ mod tests {
128124
updated_at: "2026-04-22T10:15:30Z".into(),
129125
html_url: "https://github.com/unhappychoice/splashboard/issues/42".into(),
130126
state: "open".into(),
127+
comments: 3,
128+
user: Some(GithubUserRef {
129+
avatar_url: Some("https://avatars.example/u.png".into()),
130+
}),
131131
}
132132
}
133133

@@ -152,11 +152,48 @@ mod tests {
152152
assert!(repo_from_url("https://api.github.com/repos/foo").is_none());
153153
}
154154

155+
#[test]
156+
fn to_forge_row_carries_comments_and_avatar_through() {
157+
let row = to_forge_row(&issue_item(), true);
158+
assert_eq!(row.label, "unhappychoice/splashboard#42");
159+
assert_eq!(row.activity_count, 3);
160+
assert_eq!(
161+
row.avatar_url.as_deref(),
162+
Some("https://avatars.example/u.png")
163+
);
164+
assert!(row.avatar_path.is_none());
165+
}
166+
167+
#[test]
168+
fn to_forge_row_without_repo_uses_number_only_label() {
169+
let row = to_forge_row(&issue_item(), false);
170+
assert_eq!(row.label, "#42");
171+
}
172+
173+
#[test]
174+
fn to_forge_row_falls_back_to_question_mark_for_bad_repo_url() {
175+
let mut item = issue_item();
176+
item.repository_url = "https://api.github.com/not-a-repo".into();
177+
let row = to_forge_row(&item, true);
178+
assert_eq!(row.label, "?#42");
179+
}
180+
181+
#[test]
182+
fn to_forge_row_empty_html_url_collapses_to_none() {
183+
let mut item = issue_item();
184+
item.html_url.clear();
185+
let row = to_forge_row(&item, false);
186+
assert!(row.url.is_none());
187+
}
188+
155189
#[test]
156190
fn render_entries_include_repo_name_and_title() {
157191
let value = body_json(render_items(&[issue_item()], Shape::Entries, true));
158192
assert_eq!(value["shape"], "entries");
159-
assert_eq!(value["data"]["items"][0]["key"], "splashboard #42");
193+
assert_eq!(
194+
value["data"]["items"][0]["key"],
195+
"unhappychoice/splashboard#42"
196+
);
160197
assert_eq!(
161198
value["data"]["items"][0]["value"],
162199
"Fix cached splash mismatch"
@@ -235,12 +272,54 @@ mod tests {
235272
item.repository_url = "https://api.github.com/not-a-repo".into();
236273
let value = body_json(render_items(&[item], Shape::Entries, true));
237274
assert_eq!(value["shape"], "entries");
238-
assert_eq!(value["data"]["items"][0]["key"], "? #42");
275+
assert_eq!(value["data"]["items"][0]["key"], "?#42");
276+
}
277+
278+
#[test]
279+
fn render_bars_uses_comments_count_as_value() {
280+
let Body::Bars(data) = render_items(&[issue_item()], Shape::Bars, false) else {
281+
panic!("expected bars");
282+
};
283+
assert_eq!(data.bars[0].value, 3);
284+
}
285+
286+
#[test]
287+
fn render_markdown_links_when_url_present() {
288+
let Body::MarkdownTextBlock(data) =
289+
render_items(&[issue_item()], Shape::MarkdownTextBlock, true)
290+
else {
291+
panic!("expected markdown");
292+
};
293+
assert!(data.value.contains("[Fix cached splash mismatch]"));
294+
}
295+
296+
#[test]
297+
fn render_image_linked_carries_empty_thumbnail_until_resolved() {
298+
// The fetcher is responsible for resolving `avatar_url` -> `avatar_path` via the
299+
// shared thumbnails downloader before reaching the renderer. Calling `render_items`
300+
// directly leaves the column blank — verifies the unresolved path still produces a
301+
// structurally valid body rather than crashing.
302+
let Body::ImageLinkedList(data) =
303+
render_items(&[issue_item()], Shape::ImageLinkedList, true)
304+
else {
305+
panic!("expected image linked list");
306+
};
307+
assert_eq!(data.items.len(), 1);
308+
assert!(data.items[0].thumbnail_path.is_none());
239309
}
240310

241311
#[test]
242312
fn search_result_defaults_missing_items_to_empty() {
243313
let result: SearchResult = serde_json::from_str("{}").unwrap();
244314
assert!(result.items.is_empty());
245315
}
316+
317+
#[test]
318+
fn issue_item_tolerates_partial_payloads_for_new_fields() {
319+
// Older fixtures and tests construct IssueItem without `comments` / `user`. Both
320+
// default cleanly so we don't break callers that pre-date the shape expansion.
321+
let item: IssueItem = serde_json::from_str(r#"{"title":"x","number":1}"#).unwrap();
322+
assert_eq!(item.comments, 0);
323+
assert!(item.user.is_none());
324+
}
246325
}

0 commit comments

Comments
 (0)