|
| 1 | +//! `gitlab_repo_issues` — open issues against one specific GitLab project. |
| 2 | +
|
| 3 | +use async_trait::async_trait; |
| 4 | +use serde::Deserialize; |
| 5 | + |
| 6 | +use crate::fetcher::forge_items::{self, LIST_SHAPES}; |
| 7 | +use crate::options::OptionSchema; |
| 8 | +use crate::payload::{Body, Payload}; |
| 9 | +use crate::render::Shape; |
| 10 | + |
| 11 | +use super::super::{FetchContext, FetchError, Fetcher, Safety}; |
| 12 | +use super::client::{default_host, rest_get}; |
| 13 | +use super::common::{ProjectPath, cache_key, parse_options, payload, resolve_project}; |
| 14 | +use super::items::{GitlabIssueLike, sample_rows, to_forge_rows}; |
| 15 | +use super::my_mrs::resolve_host; |
| 16 | + |
| 17 | +const DEFAULT_LIMIT: u32 = 10; |
| 18 | + |
| 19 | +const OPTION_SCHEMAS: &[OptionSchema] = &[ |
| 20 | + OptionSchema { |
| 21 | + name: "host", |
| 22 | + type_hint: "hostname", |
| 23 | + required: false, |
| 24 | + default: Some("gitlab.com"), |
| 25 | + description: "GitLab instance host. Use this for self-hosted GitLab.", |
| 26 | + }, |
| 27 | + OptionSchema { |
| 28 | + name: "project", |
| 29 | + type_hint: "\"group/name\" or \"group/sub/name\"", |
| 30 | + required: false, |
| 31 | + default: Some("git remote of cwd"), |
| 32 | + description: "Project to query. Falls back to the current directory's GitLab remote.", |
| 33 | + }, |
| 34 | + OptionSchema { |
| 35 | + name: "limit", |
| 36 | + type_hint: "integer (1..=30)", |
| 37 | + required: false, |
| 38 | + default: Some("10"), |
| 39 | + description: "Maximum number of issues to show.", |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +pub struct GitlabRepoIssues; |
| 44 | + |
| 45 | +#[derive(Debug, Default, Deserialize)] |
| 46 | +#[serde(deny_unknown_fields)] |
| 47 | +pub struct Options { |
| 48 | + #[serde(default)] |
| 49 | + pub host: Option<String>, |
| 50 | + #[serde(default)] |
| 51 | + pub project: Option<String>, |
| 52 | + #[serde(default)] |
| 53 | + pub limit: Option<u32>, |
| 54 | +} |
| 55 | + |
| 56 | +#[async_trait] |
| 57 | +impl Fetcher for GitlabRepoIssues { |
| 58 | + fn name(&self) -> &str { |
| 59 | + "gitlab_repo_issues" |
| 60 | + } |
| 61 | + fn safety(&self) -> Safety { |
| 62 | + Safety::Safe |
| 63 | + } |
| 64 | + fn description(&self) -> &'static str { |
| 65 | + "Open issues against one specific GitLab project. Mirrors `github_repo_issues` for GitLab." |
| 66 | + } |
| 67 | + fn refresh_interval(&self) -> u64 { |
| 68 | + 60 * 10 |
| 69 | + } |
| 70 | + fn shapes(&self) -> &[Shape] { |
| 71 | + LIST_SHAPES |
| 72 | + } |
| 73 | + fn option_schemas(&self) -> &[OptionSchema] { |
| 74 | + OPTION_SCHEMAS |
| 75 | + } |
| 76 | + fn cache_key(&self, ctx: &FetchContext) -> String { |
| 77 | + cache_key(self.name(), ctx, &cache_extra(ctx)) |
| 78 | + } |
| 79 | + fn sample_body(&self, shape: Shape) -> Option<Body> { |
| 80 | + let rows = sample_rows(&[ |
| 81 | + ( |
| 82 | + "#75", |
| 83 | + "Heatmap renderer eats narrow columns", |
| 84 | + Some("https://gitlab.com/g/splashboard/-/issues/75"), |
| 85 | + 3, |
| 86 | + 1_774_000_000, |
| 87 | + ), |
| 88 | + ( |
| 89 | + "#71", |
| 90 | + "gitlab_pipeline_status: respect branch option", |
| 91 | + Some("https://gitlab.com/g/splashboard/-/issues/71"), |
| 92 | + 1, |
| 93 | + 1_773_800_000, |
| 94 | + ), |
| 95 | + ]); |
| 96 | + forge_items::dispatch_sample(&rows, shape, "open issue", "open issues") |
| 97 | + } |
| 98 | + async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> { |
| 99 | + let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?; |
| 100 | + let host = resolve_host(opts.host.as_deref())?; |
| 101 | + let project = resolve_project(opts.project.as_deref(), &host)?; |
| 102 | + let limit = opts.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, 30); |
| 103 | + let path = format!( |
| 104 | + "/projects/{}/issues?state=opened&per_page={limit}&order_by=updated_at", |
| 105 | + project.encoded() |
| 106 | + ); |
| 107 | + let items: Vec<GitlabIssueLike> = rest_get(&host, &path).await?; |
| 108 | + let rows = to_forge_rows(&items, false); |
| 109 | + let shape = ctx.shape.unwrap_or(Shape::LinkedTextBlock); |
| 110 | + let body = forge_items::dispatch_rows_async(rows, shape, "open issue", "open issues").await; |
| 111 | + Ok(payload(body)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn cache_extra(ctx: &FetchContext) -> String { |
| 116 | + let host = ctx |
| 117 | + .options |
| 118 | + .as_ref() |
| 119 | + .and_then(|v| v.get("host")) |
| 120 | + .and_then(|v| v.as_str()) |
| 121 | + .unwrap_or(default_host()) |
| 122 | + .to_string(); |
| 123 | + let project = ctx |
| 124 | + .options |
| 125 | + .as_ref() |
| 126 | + .and_then(|v| v.get("project")) |
| 127 | + .and_then(|v| v.as_str()) |
| 128 | + .map(String::from) |
| 129 | + .or_else(|| { |
| 130 | + resolve_project(None, &host) |
| 131 | + .ok() |
| 132 | + .map(|p: ProjectPath| p.as_str().to_string()) |
| 133 | + }) |
| 134 | + .unwrap_or_default(); |
| 135 | + format!("{host}|{project}") |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +mod tests { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn options_deserialize_and_reject_unknown_keys() { |
| 144 | + let opts: Options = |
| 145 | + toml::from_str("host = \"git.example.org\"\nproject = \"g/p\"\nlimit = 3").unwrap(); |
| 146 | + assert_eq!(opts.host.as_deref(), Some("git.example.org")); |
| 147 | + assert_eq!(opts.project.as_deref(), Some("g/p")); |
| 148 | + assert_eq!(opts.limit, Some(3)); |
| 149 | + assert!(toml::from_str::<Options>("bogus = 1").is_err()); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn shapes_table_matches_shared_list() { |
| 154 | + let fetcher = GitlabRepoIssues; |
| 155 | + assert_eq!(fetcher.shapes(), LIST_SHAPES); |
| 156 | + assert_eq!(fetcher.safety(), Safety::Safe); |
| 157 | + assert_eq!(fetcher.default_shape(), Shape::LinkedTextBlock); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn sample_body_covers_every_list_shape() { |
| 162 | + let fetcher = GitlabRepoIssues; |
| 163 | + for shape in LIST_SHAPES { |
| 164 | + assert!(fetcher.sample_body(*shape).is_some(), "missing {shape:?}"); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments