Skip to content

Commit d08dbd1

Browse files
unhappychoiceclaude
andcommitted
feat(fetcher): add gitlab_repo_mrs
Open MRs against one specific GitLab project. Project resolves from the `project` option or (fallback) the git remote of cwd, gated on the configured `host` so a polyrepo cwd's GitHub remote doesn't get misrouted. Same 9-shape coverage as the user-scope siblings. Cache key mixes host + project path so two projects don't collide. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent d8fdc3e commit d08dbd1

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

‎src/fetcher/gitlab/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub(crate) mod client;
1313
pub(crate) mod common;
1414
pub(crate) mod items;
1515
mod my_mrs;
16+
mod repo_mrs;
1617
mod review_requests;
1718

1819
use super::Fetcher;
@@ -21,5 +22,6 @@ pub fn fetchers() -> Vec<Arc<dyn Fetcher>> {
2122
vec![
2223
Arc::new(my_mrs::GitlabMyMrs),
2324
Arc::new(review_requests::GitlabReviewRequests),
25+
Arc::new(repo_mrs::GitlabRepoMrs),
2426
]
2527
}

‎src/fetcher/gitlab/repo_mrs.rs‎

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
//! `gitlab_repo_mrs` — open merge requests against one specific GitLab project. Falls back to
2+
//! the git remote of the cwd when `project` is unset.
3+
4+
use async_trait::async_trait;
5+
use serde::Deserialize;
6+
7+
use crate::fetcher::forge_items::{self, LIST_SHAPES};
8+
use crate::options::OptionSchema;
9+
use crate::payload::{Body, Payload};
10+
use crate::render::Shape;
11+
12+
use super::super::{FetchContext, FetchError, Fetcher, Safety};
13+
use super::client::{default_host, rest_get};
14+
use super::common::{ProjectPath, cache_key, parse_options, payload, resolve_project};
15+
use super::items::{GitlabIssueLike, sample_rows, to_forge_rows};
16+
use super::my_mrs::resolve_host;
17+
18+
const DEFAULT_LIMIT: u32 = 10;
19+
20+
const OPTION_SCHEMAS: &[OptionSchema] = &[
21+
OptionSchema {
22+
name: "host",
23+
type_hint: "hostname",
24+
required: false,
25+
default: Some("gitlab.com"),
26+
description: "GitLab instance host. Use this for self-hosted GitLab.",
27+
},
28+
OptionSchema {
29+
name: "project",
30+
type_hint: "\"group/name\" or \"group/sub/name\"",
31+
required: false,
32+
default: Some("git remote of cwd"),
33+
description: "Project to query. Falls back to the current directory's GitLab remote.",
34+
},
35+
OptionSchema {
36+
name: "limit",
37+
type_hint: "integer (1..=30)",
38+
required: false,
39+
default: Some("10"),
40+
description: "Maximum number of MRs to show.",
41+
},
42+
];
43+
44+
pub struct GitlabRepoMrs;
45+
46+
#[derive(Debug, Default, Deserialize)]
47+
#[serde(deny_unknown_fields)]
48+
pub struct Options {
49+
#[serde(default)]
50+
pub host: Option<String>,
51+
#[serde(default)]
52+
pub project: Option<String>,
53+
#[serde(default)]
54+
pub limit: Option<u32>,
55+
}
56+
57+
#[async_trait]
58+
impl Fetcher for GitlabRepoMrs {
59+
fn name(&self) -> &str {
60+
"gitlab_repo_mrs"
61+
}
62+
fn safety(&self) -> Safety {
63+
Safety::Safe
64+
}
65+
fn description(&self) -> &'static str {
66+
"Open merge requests against one specific GitLab project. Use `gitlab_my_mrs` instead for MRs you authored across every project."
67+
}
68+
fn refresh_interval(&self) -> u64 {
69+
60 * 10
70+
}
71+
fn shapes(&self) -> &[Shape] {
72+
LIST_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+
let rows = sample_rows(&[
82+
(
83+
"!54",
84+
"feat(docs): generate widget catalogue",
85+
Some("https://gitlab.com/g/splashboard/-/merge_requests/54"),
86+
7,
87+
1_774_000_000,
88+
),
89+
(
90+
"!51",
91+
"feat(fetcher): split clock options",
92+
Some("https://gitlab.com/g/splashboard/-/merge_requests/51"),
93+
2,
94+
1_773_800_000,
95+
),
96+
]);
97+
forge_items::dispatch_sample(&rows, shape, "open MR", "open MRs")
98+
}
99+
async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> {
100+
let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?;
101+
let host = resolve_host(opts.host.as_deref())?;
102+
let project = resolve_project(opts.project.as_deref(), &host)?;
103+
let limit = opts.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, 30);
104+
let path = format!(
105+
"/projects/{}/merge_requests?state=opened&per_page={limit}&order_by=updated_at",
106+
project.encoded()
107+
);
108+
let items: Vec<GitlabIssueLike> = rest_get(&host, &path).await?;
109+
let rows = to_forge_rows(&items, false);
110+
let shape = ctx.shape.unwrap_or(Shape::LinkedTextBlock);
111+
let body = forge_items::dispatch_rows_async(rows, shape, "open MR", "open MRs").await;
112+
Ok(payload(body))
113+
}
114+
}
115+
116+
fn cache_extra(ctx: &FetchContext) -> String {
117+
let host = ctx
118+
.options
119+
.as_ref()
120+
.and_then(|v| v.get("host"))
121+
.and_then(|v| v.as_str())
122+
.unwrap_or(default_host())
123+
.to_string();
124+
let project = ctx
125+
.options
126+
.as_ref()
127+
.and_then(|v| v.get("project"))
128+
.and_then(|v| v.as_str())
129+
.map(String::from)
130+
.or_else(|| {
131+
resolve_project(None, &host)
132+
.ok()
133+
.map(|p: ProjectPath| p.as_str().to_string())
134+
})
135+
.unwrap_or_default();
136+
format!("{host}|{project}")
137+
}
138+
139+
#[cfg(test)]
140+
mod tests {
141+
use std::time::Duration;
142+
143+
use super::*;
144+
145+
#[test]
146+
fn options_deserialize_and_reject_unknown_keys() {
147+
let opts: Options =
148+
toml::from_str("host = \"git.example.org\"\nproject = \"g/p\"\nlimit = 3").unwrap();
149+
assert_eq!(opts.host.as_deref(), Some("git.example.org"));
150+
assert_eq!(opts.project.as_deref(), Some("g/p"));
151+
assert_eq!(opts.limit, Some(3));
152+
assert!(toml::from_str::<Options>("bogus = 1").is_err());
153+
}
154+
155+
#[test]
156+
fn shapes_table_matches_shared_list() {
157+
let fetcher = GitlabRepoMrs;
158+
assert_eq!(fetcher.shapes(), LIST_SHAPES);
159+
assert_eq!(fetcher.safety(), Safety::Safe);
160+
assert_eq!(fetcher.default_shape(), Shape::LinkedTextBlock);
161+
}
162+
163+
#[test]
164+
fn sample_body_covers_every_list_shape() {
165+
let fetcher = GitlabRepoMrs;
166+
for shape in LIST_SHAPES {
167+
assert!(fetcher.sample_body(*shape).is_some(), "missing {shape:?}");
168+
}
169+
assert!(fetcher.sample_body(Shape::Ratio).is_none());
170+
}
171+
172+
#[test]
173+
fn cache_key_changes_with_project_option() {
174+
let fetcher = GitlabRepoMrs;
175+
let a = fetcher.cache_key(&FetchContext {
176+
options: Some(toml::from_str("project = \"a/b\"").unwrap()),
177+
..Default::default()
178+
});
179+
let b = fetcher.cache_key(&FetchContext {
180+
options: Some(toml::from_str("project = \"a/c\"").unwrap()),
181+
..Default::default()
182+
});
183+
assert_ne!(a, b);
184+
}
185+
186+
#[tokio::test]
187+
async fn fetch_rejects_invalid_project_before_network_request() {
188+
let err = GitlabRepoMrs
189+
.fetch(&FetchContext {
190+
options: Some(toml::from_str("project = \"not a path\"").unwrap()),
191+
timeout: Duration::from_secs(1),
192+
..Default::default()
193+
})
194+
.await
195+
.unwrap_err();
196+
assert!(matches!(
197+
err,
198+
FetchError::Failed(m) if m.contains("invalid project option")
199+
));
200+
}
201+
}

0 commit comments

Comments
 (0)