Skip to content

Commit 17c687f

Browse files
unhappychoiceclaude
andcommitted
refactor(github): extend repo_stars to 6 shapes
Adds `TextBlock`, `MarkdownTextBlock`, `Bars`, and `Badge` on top of the existing `Text` and `Entries`. `Bars` ranks stars / forks / watchers / open_issues so the same fetched payload can fill a chart slot without a sibling fetcher. `Badge` is a calm "★ N" Info-tone pill suitable for a narrow status row. Renderer dispatch goes through a single `render_body(info, shape)` helper shared between `sample_body` and `fetch` so the two paths can't drift on new shapes. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 7eed488 commit 17c687f

1 file changed

Lines changed: 116 additions & 28 deletions

File tree

‎src/fetcher/github/repo_stars.rs‎

Lines changed: 116 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ use async_trait::async_trait;
55
use serde::Deserialize;
66

77
use crate::options::OptionSchema;
8-
use crate::payload::{Body, EntriesData, Entry, Payload, TextData};
8+
use crate::payload::{
9+
BadgeData, Bar, BarsData, Body, EntriesData, Entry, MarkdownTextBlockData, Payload, Status,
10+
TextBlockData, TextData,
11+
};
912
use crate::render::Shape;
10-
use crate::samples;
1113

1214
use super::super::{FetchContext, FetchError, Fetcher, Safety};
1315
use super::client::rest_get;
1416
use super::common::{RepoSlug, cache_key, parse_options, payload, resolve_repo};
1517

16-
const SHAPES: &[Shape] = &[Shape::Text, Shape::Entries];
18+
const SHAPES: &[Shape] = &[
19+
Shape::Text,
20+
Shape::TextBlock,
21+
Shape::MarkdownTextBlock,
22+
Shape::Entries,
23+
Shape::Bars,
24+
Shape::Badge,
25+
];
1726

1827
const OPTION_SCHEMAS: &[OptionSchema] = &[OptionSchema {
1928
name: "repo",
@@ -57,36 +66,83 @@ impl Fetcher for GithubRepoStars {
5766
cache_key(self.name(), ctx, &extra)
5867
}
5968
fn sample_body(&self, shape: Shape) -> Option<Body> {
60-
Some(match shape {
61-
Shape::Text => samples::text("★ 142"),
62-
Shape::Entries => samples::entries(&[
63-
("stars", "142"),
64-
("forks", "9"),
65-
("watchers", "12"),
66-
("open_issues", "7"),
67-
]),
68-
_ => return None,
69-
})
69+
if !SHAPES.contains(&shape) {
70+
return None;
71+
}
72+
let info = RepoInfo {
73+
stargazers_count: 142,
74+
forks_count: 9,
75+
subscribers_count: 12,
76+
open_issues_count: 7,
77+
};
78+
Some(render_body(&info, shape))
7079
}
7180
async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> {
7281
let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?;
7382
let slug = resolve_repo(opts.repo.as_deref())?;
7483
let path = format!("/repos/{}/{}", slug.owner, slug.name);
7584
let repo: RepoInfo = rest_get(&path).await?;
76-
let body = match ctx.shape.unwrap_or(Shape::Text) {
77-
Shape::Entries => Body::Entries(EntriesData {
78-
items: vec![
79-
entry("stars", &repo.stargazers_count.to_string()),
80-
entry("forks", &repo.forks_count.to_string()),
81-
entry("watchers", &repo.subscribers_count.to_string()),
82-
entry("open_issues", &repo.open_issues_count.to_string()),
83-
],
84-
}),
85-
_ => Body::Text(TextData {
86-
value: format!("★ {}", repo.stargazers_count),
87-
}),
88-
};
89-
Ok(payload(body))
85+
Ok(payload(render_body(
86+
&repo,
87+
ctx.shape.unwrap_or(Shape::Text),
88+
)))
89+
}
90+
}
91+
92+
fn render_body(info: &RepoInfo, shape: Shape) -> Body {
93+
match shape {
94+
Shape::TextBlock => Body::TextBlock(TextBlockData {
95+
lines: vec![
96+
format!("★ {}", info.stargazers_count),
97+
format!("🍴 {}", info.forks_count),
98+
format!("👁 {}", info.subscribers_count),
99+
format!("🔓 {}", info.open_issues_count),
100+
],
101+
}),
102+
Shape::MarkdownTextBlock => Body::MarkdownTextBlock(MarkdownTextBlockData {
103+
value: format!(
104+
"- **★ stars** {}\n- **🍴 forks** {}\n- **👁 watchers** {}\n- **🔓 open issues** {}",
105+
info.stargazers_count,
106+
info.forks_count,
107+
info.subscribers_count,
108+
info.open_issues_count
109+
),
110+
}),
111+
Shape::Entries => Body::Entries(EntriesData {
112+
items: vec![
113+
entry("stars", &info.stargazers_count.to_string()),
114+
entry("forks", &info.forks_count.to_string()),
115+
entry("watchers", &info.subscribers_count.to_string()),
116+
entry("open_issues", &info.open_issues_count.to_string()),
117+
],
118+
}),
119+
Shape::Bars => Body::Bars(BarsData {
120+
bars: vec![
121+
Bar {
122+
label: "stars".into(),
123+
value: info.stargazers_count,
124+
},
125+
Bar {
126+
label: "forks".into(),
127+
value: info.forks_count,
128+
},
129+
Bar {
130+
label: "watchers".into(),
131+
value: info.subscribers_count,
132+
},
133+
Bar {
134+
label: "open_issues".into(),
135+
value: info.open_issues_count,
136+
},
137+
],
138+
}),
139+
Shape::Badge => Body::Badge(BadgeData {
140+
status: Status::Ok,
141+
label: format!("★ {}", info.stargazers_count),
142+
}),
143+
_ => Body::Text(TextData {
144+
value: format!("★ {}", info.stargazers_count),
145+
}),
90146
}
91147
}
92148

@@ -187,7 +243,17 @@ mod tests {
187243
assert_eq!(fetcher.name(), "github_repo_stars");
188244
assert_eq!(fetcher.safety(), Safety::Safe);
189245
assert!(fetcher.description().contains("watchers"));
190-
assert_eq!(fetcher.shapes(), &[Shape::Text, Shape::Entries]);
246+
assert_eq!(
247+
fetcher.shapes(),
248+
&[
249+
Shape::Text,
250+
Shape::TextBlock,
251+
Shape::MarkdownTextBlock,
252+
Shape::Entries,
253+
Shape::Bars,
254+
Shape::Badge,
255+
]
256+
);
191257
assert_eq!(fetcher.default_shape(), Shape::Text);
192258
assert_eq!(fetcher.option_schemas().len(), 1);
193259
assert_eq!(fetcher.option_schemas()[0].name, "repo");
@@ -211,6 +277,28 @@ mod tests {
211277
assert_eq!(entries.items.len(), 4);
212278
assert_eq!(entries.items[0].key, "stars");
213279
assert_eq!(entries.items[3].key, "open_issues");
280+
281+
let Some(Body::TextBlock(t)) = fetcher.sample_body(Shape::TextBlock) else {
282+
panic!("expected text block sample");
283+
};
284+
assert_eq!(t.lines[0], "★ 142");
285+
286+
let Some(Body::MarkdownTextBlock(m)) = fetcher.sample_body(Shape::MarkdownTextBlock) else {
287+
panic!("expected markdown sample");
288+
};
289+
assert!(m.value.contains("- **★ stars** 142"));
290+
291+
let Some(Body::Bars(b)) = fetcher.sample_body(Shape::Bars) else {
292+
panic!("expected bars sample");
293+
};
294+
assert_eq!(b.bars[0].value, 142);
295+
296+
let Some(Body::Badge(bd)) = fetcher.sample_body(Shape::Badge) else {
297+
panic!("expected badge sample");
298+
};
299+
assert_eq!(bd.status, crate::payload::Status::Ok);
300+
assert_eq!(bd.label, "★ 142");
301+
214302
assert!(fetcher.sample_body(Shape::Timeline).is_none());
215303
}
216304

0 commit comments

Comments
 (0)