Skip to main content

ironflow_ops_gitlab/endpoints/
repository.rs

1//! Repository routes missing from the `gitlab` crate:
2//! `GET /projects/:id/repository/merge_base`.
3
4use std::borrow::Cow;
5
6use gitlab::api::common::NameOrId;
7use gitlab::api::endpoint_prelude::{Endpoint, Method, QueryParams};
8
9/// Find the common ancestor of two or more refs (branch names, tags or SHAs).
10///
11/// Maps to `GET /projects/:id/repository/merge_base?refs[]=a&refs[]=b`.
12/// The answer is the merge base commit (`id`, `short_id`, ...). Comparing it
13/// with one of the refs tells whether that ref is an ancestor of the others.
14///
15/// # Examples
16///
17/// ```no_run
18/// use ironflow_ops_gitlab::GitLab;
19/// use ironflow_ops_gitlab::endpoints::repository::MergeBase;
20/// use gitlab::api::common::NameOrId;
21///
22/// # async fn example() -> Result<(), ironflow_core::error::OperationError> {
23/// let gitlab = GitLab::new("glpat-xxxx", "gitlab.com").await?;
24/// let endpoint = MergeBase {
25///     project: NameOrId::from("group/project"),
26///     refs: vec!["0a1b2c3".to_string(), "main".to_string()],
27/// };
28/// let op = gitlab.op(endpoint);
29/// # Ok(())
30/// # }
31/// ```
32///
33/// # Errors
34///
35/// Returns an error if the request fails, for instance when the refs share no
36/// history (GitLab answers 400) or a ref does not exist.
37pub struct MergeBase {
38    /// The project holding the refs.
39    pub project: NameOrId<'static>,
40    /// The refs to find the common ancestor of (at least two).
41    pub refs: Vec<String>,
42}
43
44impl Endpoint for MergeBase {
45    fn method(&self) -> Method {
46        Method::GET
47    }
48
49    fn endpoint(&self) -> Cow<'static, str> {
50        format!("projects/{}/repository/merge_base", self.project).into()
51    }
52
53    fn parameters(&self) -> QueryParams<'_> {
54        let mut params = QueryParams::default();
55        params.extend(self.refs.iter().map(|r| ("refs[]", r.as_str())));
56        params
57    }
58}