|
| 1 | +//! `gitlab_repo_stars` — stargazer count + forks / open-issues rollup for one GitLab project. |
| 2 | +//! GitLab has no separate "watchers" concept, so the `watchers` row that ships with |
| 3 | +//! `github_repo_stars` is omitted here. |
| 4 | +
|
| 5 | +use async_trait::async_trait; |
| 6 | +use serde::Deserialize; |
| 7 | + |
| 8 | +use crate::options::OptionSchema; |
| 9 | +use crate::payload::{ |
| 10 | + BadgeData, Bar, BarsData, Body, EntriesData, Entry, MarkdownTextBlockData, Payload, Status, |
| 11 | + TextBlockData, TextData, |
| 12 | +}; |
| 13 | +use crate::render::Shape; |
| 14 | + |
| 15 | +use super::super::{FetchContext, FetchError, Fetcher, Safety}; |
| 16 | +use super::client::{default_host, rest_get}; |
| 17 | +use super::common::{ProjectPath, cache_key, parse_options, payload, resolve_project}; |
| 18 | +use super::my_mrs::resolve_host; |
| 19 | + |
| 20 | +const SHAPES: &[Shape] = &[ |
| 21 | + Shape::Text, |
| 22 | + Shape::TextBlock, |
| 23 | + Shape::MarkdownTextBlock, |
| 24 | + Shape::Entries, |
| 25 | + Shape::Bars, |
| 26 | + Shape::Badge, |
| 27 | +]; |
| 28 | + |
| 29 | +const OPTION_SCHEMAS: &[OptionSchema] = &[ |
| 30 | + OptionSchema { |
| 31 | + name: "host", |
| 32 | + type_hint: "hostname", |
| 33 | + required: false, |
| 34 | + default: Some("gitlab.com"), |
| 35 | + description: "GitLab instance host. Use this for self-hosted GitLab.", |
| 36 | + }, |
| 37 | + OptionSchema { |
| 38 | + name: "project", |
| 39 | + type_hint: "\"group/name\" or \"group/sub/name\"", |
| 40 | + required: false, |
| 41 | + default: Some("git remote of cwd"), |
| 42 | + description: "Project to query. Falls back to the current directory's GitLab remote.", |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +pub struct GitlabRepoStars; |
| 47 | + |
| 48 | +#[derive(Debug, Default, Deserialize)] |
| 49 | +#[serde(deny_unknown_fields)] |
| 50 | +pub struct Options { |
| 51 | + #[serde(default)] |
| 52 | + pub host: Option<String>, |
| 53 | + #[serde(default)] |
| 54 | + pub project: Option<String>, |
| 55 | +} |
| 56 | + |
| 57 | +#[async_trait] |
| 58 | +impl Fetcher for GitlabRepoStars { |
| 59 | + fn name(&self) -> &str { |
| 60 | + "gitlab_repo_stars" |
| 61 | + } |
| 62 | + fn safety(&self) -> Safety { |
| 63 | + Safety::Safe |
| 64 | + } |
| 65 | + fn description(&self) -> &'static str { |
| 66 | + "Social counters for a GitLab project: stargazers, forks, and open-issue count. GitLab has no separate watchers count, so that row is omitted relative to the GitHub sibling." |
| 67 | + } |
| 68 | + fn refresh_interval(&self) -> u64 { |
| 69 | + 60 * 60 |
| 70 | + } |
| 71 | + fn shapes(&self) -> &[Shape] { |
| 72 | + SHAPES |
| 73 | + } |
| 74 | + fn option_schemas(&self) -> &[OptionSchema] { |
| 75 | + OPTION_SCHEMAS |
| 76 | + } |
| 77 | + fn cache_key(&self, ctx: &FetchContext) -> String { |
| 78 | + cache_key(self.name(), ctx, &cache_extra(ctx)) |
| 79 | + } |
| 80 | + fn sample_body(&self, shape: Shape) -> Option<Body> { |
| 81 | + if !SHAPES.contains(&shape) { |
| 82 | + return None; |
| 83 | + } |
| 84 | + let info = ProjectInfo { |
| 85 | + star_count: 142, |
| 86 | + forks_count: 9, |
| 87 | + open_issues_count: 7, |
| 88 | + }; |
| 89 | + Some(render_body(&info, shape)) |
| 90 | + } |
| 91 | + async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> { |
| 92 | + let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?; |
| 93 | + let host = resolve_host(opts.host.as_deref())?; |
| 94 | + let project = resolve_project(opts.project.as_deref(), &host)?; |
| 95 | + let path = format!("/projects/{}", project.encoded()); |
| 96 | + let info: ProjectInfo = rest_get(&host, &path).await?; |
| 97 | + let shape = ctx.shape.unwrap_or(Shape::Text); |
| 98 | + Ok(payload(render_body(&info, shape))) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Debug, Deserialize)] |
| 103 | +struct ProjectInfo { |
| 104 | + #[serde(default)] |
| 105 | + star_count: u64, |
| 106 | + #[serde(default)] |
| 107 | + forks_count: u64, |
| 108 | + #[serde(default)] |
| 109 | + open_issues_count: u64, |
| 110 | +} |
| 111 | + |
| 112 | +fn render_body(info: &ProjectInfo, shape: Shape) -> Body { |
| 113 | + match shape { |
| 114 | + Shape::TextBlock => Body::TextBlock(TextBlockData { |
| 115 | + lines: vec![ |
| 116 | + format!("★ {}", info.star_count), |
| 117 | + format!("🍴 {}", info.forks_count), |
| 118 | + format!("🔓 {}", info.open_issues_count), |
| 119 | + ], |
| 120 | + }), |
| 121 | + Shape::MarkdownTextBlock => Body::MarkdownTextBlock(MarkdownTextBlockData { |
| 122 | + value: format!( |
| 123 | + "- **★ stars** {}\n- **🍴 forks** {}\n- **🔓 open issues** {}", |
| 124 | + info.star_count, info.forks_count, info.open_issues_count |
| 125 | + ), |
| 126 | + }), |
| 127 | + Shape::Entries => Body::Entries(EntriesData { |
| 128 | + items: vec![ |
| 129 | + entry("stars", info.star_count), |
| 130 | + entry("forks", info.forks_count), |
| 131 | + entry("open_issues", info.open_issues_count), |
| 132 | + ], |
| 133 | + }), |
| 134 | + Shape::Bars => Body::Bars(BarsData { |
| 135 | + bars: vec![ |
| 136 | + Bar { |
| 137 | + label: "stars".into(), |
| 138 | + value: info.star_count, |
| 139 | + }, |
| 140 | + Bar { |
| 141 | + label: "forks".into(), |
| 142 | + value: info.forks_count, |
| 143 | + }, |
| 144 | + Bar { |
| 145 | + label: "open_issues".into(), |
| 146 | + value: info.open_issues_count, |
| 147 | + }, |
| 148 | + ], |
| 149 | + }), |
| 150 | + Shape::Badge => Body::Badge(BadgeData { |
| 151 | + status: Status::Ok, |
| 152 | + label: format!("★ {}", info.star_count), |
| 153 | + }), |
| 154 | + _ => Body::Text(TextData { |
| 155 | + value: format!("★ {}", info.star_count), |
| 156 | + }), |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +fn entry(key: &str, value: u64) -> Entry { |
| 161 | + Entry { |
| 162 | + key: key.into(), |
| 163 | + value: Some(value.to_string()), |
| 164 | + status: None, |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +fn cache_extra(ctx: &FetchContext) -> String { |
| 169 | + let host = ctx |
| 170 | + .options |
| 171 | + .as_ref() |
| 172 | + .and_then(|v| v.get("host")) |
| 173 | + .and_then(|v| v.as_str()) |
| 174 | + .unwrap_or(default_host()) |
| 175 | + .to_string(); |
| 176 | + let project = ctx |
| 177 | + .options |
| 178 | + .as_ref() |
| 179 | + .and_then(|v| v.get("project")) |
| 180 | + .and_then(|v| v.as_str()) |
| 181 | + .map(String::from) |
| 182 | + .or_else(|| { |
| 183 | + resolve_project(None, &host) |
| 184 | + .ok() |
| 185 | + .map(|p: ProjectPath| p.as_str().to_string()) |
| 186 | + }) |
| 187 | + .unwrap_or_default(); |
| 188 | + format!("{host}|{project}") |
| 189 | +} |
| 190 | + |
| 191 | +#[cfg(test)] |
| 192 | +mod tests { |
| 193 | + use super::*; |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn options_deserialize_both_fields() { |
| 197 | + let opts: Options = |
| 198 | + toml::from_str("host = \"git.example.org\"\nproject = \"g/p\"").unwrap(); |
| 199 | + assert_eq!(opts.host.as_deref(), Some("git.example.org")); |
| 200 | + assert_eq!(opts.project.as_deref(), Some("g/p")); |
| 201 | + assert!(toml::from_str::<Options>("bogus = 1").is_err()); |
| 202 | + } |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn shapes_table_lists_the_six_supported_variants() { |
| 206 | + let fetcher = GitlabRepoStars; |
| 207 | + assert_eq!( |
| 208 | + fetcher.shapes(), |
| 209 | + &[ |
| 210 | + Shape::Text, |
| 211 | + Shape::TextBlock, |
| 212 | + Shape::MarkdownTextBlock, |
| 213 | + Shape::Entries, |
| 214 | + Shape::Bars, |
| 215 | + Shape::Badge, |
| 216 | + ] |
| 217 | + ); |
| 218 | + assert_eq!(fetcher.default_shape(), Shape::Text); |
| 219 | + } |
| 220 | + |
| 221 | + #[test] |
| 222 | + fn sample_text_carries_the_star_count_headline() { |
| 223 | + let Some(Body::Text(t)) = GitlabRepoStars.sample_body(Shape::Text) else { |
| 224 | + panic!("expected text"); |
| 225 | + }; |
| 226 | + assert_eq!(t.value, "★ 142"); |
| 227 | + } |
| 228 | + |
| 229 | + #[test] |
| 230 | + fn sample_entries_drops_the_watchers_row() { |
| 231 | + let Some(Body::Entries(e)) = GitlabRepoStars.sample_body(Shape::Entries) else { |
| 232 | + panic!("expected entries"); |
| 233 | + }; |
| 234 | + assert_eq!(e.items.len(), 3); |
| 235 | + let keys: Vec<&str> = e.items.iter().map(|i| i.key.as_str()).collect(); |
| 236 | + assert_eq!(keys, vec!["stars", "forks", "open_issues"]); |
| 237 | + assert!(!keys.contains(&"watchers")); |
| 238 | + } |
| 239 | + |
| 240 | + #[test] |
| 241 | + fn sample_bars_value_matches_counts() { |
| 242 | + let Some(Body::Bars(b)) = GitlabRepoStars.sample_body(Shape::Bars) else { |
| 243 | + panic!("expected bars"); |
| 244 | + }; |
| 245 | + assert_eq!(b.bars[0].value, 142); |
| 246 | + assert_eq!(b.bars[1].value, 9); |
| 247 | + assert_eq!(b.bars[2].value, 7); |
| 248 | + } |
| 249 | + |
| 250 | + #[test] |
| 251 | + fn sample_badge_is_calm_ok_status_for_pure_info() { |
| 252 | + let Some(Body::Badge(b)) = GitlabRepoStars.sample_body(Shape::Badge) else { |
| 253 | + panic!("expected badge"); |
| 254 | + }; |
| 255 | + assert_eq!(b.status, Status::Ok); |
| 256 | + assert_eq!(b.label, "★ 142"); |
| 257 | + } |
| 258 | + |
| 259 | + #[test] |
| 260 | + fn sample_markdown_uses_bullet_list_with_bold_labels() { |
| 261 | + let Some(Body::MarkdownTextBlock(m)) = |
| 262 | + GitlabRepoStars.sample_body(Shape::MarkdownTextBlock) |
| 263 | + else { |
| 264 | + panic!("expected markdown"); |
| 265 | + }; |
| 266 | + assert!(m.value.contains("- **★ stars** 142")); |
| 267 | + assert!(m.value.contains("- **🍴 forks** 9")); |
| 268 | + } |
| 269 | + |
| 270 | + #[test] |
| 271 | + fn render_body_falls_back_to_text_for_unknown_shapes() { |
| 272 | + let info = ProjectInfo { |
| 273 | + star_count: 5, |
| 274 | + forks_count: 0, |
| 275 | + open_issues_count: 0, |
| 276 | + }; |
| 277 | + let Body::Text(t) = render_body(&info, Shape::Ratio) else { |
| 278 | + panic!("expected text fallback"); |
| 279 | + }; |
| 280 | + assert_eq!(t.value, "★ 5"); |
| 281 | + } |
| 282 | +} |
0 commit comments