|
| 1 | +//! `gitlab_review_requests` — open MRs where the authenticated GitLab user is a reviewer. |
| 2 | +//! Resolves `@me` to a username via `/api/v4/user` (cached per host) and then queries |
| 3 | +//! `/api/v4/merge_requests?reviewer_username=<me>&state=opened`. |
| 4 | +
|
| 5 | +use async_trait::async_trait; |
| 6 | +use serde::Deserialize; |
| 7 | + |
| 8 | +use crate::fetcher::forge_items::{self, LIST_SHAPES}; |
| 9 | +use crate::options::OptionSchema; |
| 10 | +use crate::payload::{Body, Payload}; |
| 11 | +use crate::render::Shape; |
| 12 | + |
| 13 | +use super::super::{FetchContext, FetchError, Fetcher, Safety}; |
| 14 | +use super::client::{resolve_authenticated_username, rest_get}; |
| 15 | +use super::common::{cache_key, parse_options, payload}; |
| 16 | +use super::items::{GitlabIssueLike, sample_rows, to_forge_rows}; |
| 17 | +use super::my_mrs::resolve_host; |
| 18 | + |
| 19 | +const DEFAULT_LIMIT: u32 = 10; |
| 20 | + |
| 21 | +const OPTION_SCHEMAS: &[OptionSchema] = &[ |
| 22 | + OptionSchema { |
| 23 | + name: "host", |
| 24 | + type_hint: "hostname", |
| 25 | + required: false, |
| 26 | + default: Some("gitlab.com"), |
| 27 | + description: "GitLab instance host. Use this for self-hosted GitLab.", |
| 28 | + }, |
| 29 | + OptionSchema { |
| 30 | + name: "limit", |
| 31 | + type_hint: "integer (1..=30)", |
| 32 | + required: false, |
| 33 | + default: Some("10"), |
| 34 | + description: "Maximum number of MRs to show.", |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +pub struct GitlabReviewRequests; |
| 39 | + |
| 40 | +#[derive(Debug, Default, Deserialize)] |
| 41 | +#[serde(deny_unknown_fields)] |
| 42 | +pub struct Options { |
| 43 | + #[serde(default)] |
| 44 | + pub host: Option<String>, |
| 45 | + #[serde(default)] |
| 46 | + pub limit: Option<u32>, |
| 47 | +} |
| 48 | + |
| 49 | +#[async_trait] |
| 50 | +impl Fetcher for GitlabReviewRequests { |
| 51 | + fn name(&self) -> &str { |
| 52 | + "gitlab_review_requests" |
| 53 | + } |
| 54 | + fn safety(&self) -> Safety { |
| 55 | + Safety::Safe |
| 56 | + } |
| 57 | + fn description(&self) -> &'static str { |
| 58 | + "Open merge requests where the authenticated GitLab user is a reviewer. Mirror of `github_review_requests` for GitLab." |
| 59 | + } |
| 60 | + fn refresh_interval(&self) -> u64 { |
| 61 | + 60 * 5 |
| 62 | + } |
| 63 | + fn shapes(&self) -> &[Shape] { |
| 64 | + LIST_SHAPES |
| 65 | + } |
| 66 | + fn option_schemas(&self) -> &[OptionSchema] { |
| 67 | + OPTION_SCHEMAS |
| 68 | + } |
| 69 | + fn cache_key(&self, ctx: &FetchContext) -> String { |
| 70 | + cache_key(self.name(), ctx, &host_for_key(ctx)) |
| 71 | + } |
| 72 | + fn sample_body(&self, shape: Shape) -> Option<Body> { |
| 73 | + let rows = sample_rows(&[ |
| 74 | + ( |
| 75 | + "g/splashboard!54", |
| 76 | + "feat(docs): generate widget catalogue", |
| 77 | + Some("https://gitlab.com/g/splashboard/-/merge_requests/54"), |
| 78 | + 7, |
| 79 | + 1_774_000_000, |
| 80 | + ), |
| 81 | + ( |
| 82 | + "other/proj!12", |
| 83 | + "fix(auth): rotate session token", |
| 84 | + Some("https://gitlab.com/other/proj/-/merge_requests/12"), |
| 85 | + 1, |
| 86 | + 1_773_800_000, |
| 87 | + ), |
| 88 | + ]); |
| 89 | + forge_items::dispatch_sample(&rows, shape, "to review", "to review") |
| 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 limit = opts.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, 30); |
| 95 | + let username = resolve_authenticated_username(&host).await?; |
| 96 | + let path = format!( |
| 97 | + "/merge_requests?reviewer_username={username}&state=opened&per_page={limit}&order_by=updated_at" |
| 98 | + ); |
| 99 | + let items: Vec<GitlabIssueLike> = rest_get(&host, &path).await?; |
| 100 | + let rows = to_forge_rows(&items, true); |
| 101 | + let shape = ctx.shape.unwrap_or(Shape::LinkedTextBlock); |
| 102 | + let body = forge_items::dispatch_rows_async(rows, shape, "to review", "to review").await; |
| 103 | + Ok(payload(body)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +fn host_for_key(ctx: &FetchContext) -> String { |
| 108 | + ctx.options |
| 109 | + .as_ref() |
| 110 | + .and_then(|v| v.get("host")) |
| 111 | + .and_then(|v| v.as_str()) |
| 112 | + .unwrap_or(super::client::default_host()) |
| 113 | + .to_string() |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod tests { |
| 118 | + use super::*; |
| 119 | + use crate::payload::{BadgeData, Status}; |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn options_deserialize_and_reject_unknown_keys() { |
| 123 | + let opts: Options = toml::from_str("host = \"git.example.org\"\nlimit = 3").unwrap(); |
| 124 | + assert_eq!(opts.host.as_deref(), Some("git.example.org")); |
| 125 | + assert_eq!(opts.limit, Some(3)); |
| 126 | + assert!(toml::from_str::<Options>("bogus = 1").is_err()); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn shapes_table_matches_shared_list() { |
| 131 | + let fetcher = GitlabReviewRequests; |
| 132 | + assert_eq!(fetcher.shapes(), LIST_SHAPES); |
| 133 | + assert_eq!(fetcher.default_shape(), Shape::LinkedTextBlock); |
| 134 | + assert_eq!(fetcher.safety(), Safety::Safe); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn sample_badge_uses_to_review_noun_without_pluralising() { |
| 139 | + let Some(Body::Badge(BadgeData { status, label })) = |
| 140 | + GitlabReviewRequests.sample_body(Shape::Badge) |
| 141 | + else { |
| 142 | + panic!("expected badge"); |
| 143 | + }; |
| 144 | + assert_eq!(status, Status::Warn); |
| 145 | + assert_eq!(label, "2 to review"); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn sample_body_covers_every_list_shape() { |
| 150 | + let fetcher = GitlabReviewRequests; |
| 151 | + for shape in LIST_SHAPES { |
| 152 | + assert!(fetcher.sample_body(*shape).is_some(), "missing {shape:?}"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn cache_key_changes_with_host() { |
| 158 | + let fetcher = GitlabReviewRequests; |
| 159 | + let a = fetcher.cache_key(&FetchContext::default()); |
| 160 | + let b = fetcher.cache_key(&FetchContext { |
| 161 | + options: Some(toml::from_str("host = \"git.example.org\"").unwrap()), |
| 162 | + ..Default::default() |
| 163 | + }); |
| 164 | + assert_ne!(a, b); |
| 165 | + } |
| 166 | +} |
0 commit comments