gitlab 0.1900.0

Gitlab API client.
Documentation
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use chrono::NaiveDate;
use derive_builder::Builder;

use crate::api::common::SortOrder;
use crate::api::endpoint_prelude::*;
use crate::api::ParamValue;

/// Actions events may represent.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventAction {
    /// Approval of a merge request.
    Approved,
    /// Closure of a merge request, issue, etc.
    Closed,
    /// A comment was made.
    Commented,
    /// The creation of an entity.
    Created,
    /// The destruction of an entity.
    Destroyed,
    /// The expiration of a membership.
    Expired,
    /// The addition of a member to a project or group.
    Joined,
    /// The departure of a member from a project or group.
    Left,
    /// The merge of a merge request.
    Merged,
    /// A push to a project's repository.
    Pushed,
    /// The reopening of an issue.
    Reopened,
    /// An update to an entity.
    Updated,
}

impl EventAction {
    /// The string representation of the action.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Approved => "approved",
            Self::Closed => "closed",
            Self::Commented => "commented",
            Self::Created => "created",
            Self::Destroyed => "destroyed",
            Self::Expired => "expired",
            Self::Joined => "joined",
            Self::Left => "left",
            Self::Merged => "merged",
            Self::Pushed => "pushed",
            Self::Reopened => "reopened",
            Self::Updated => "updated",
        }
    }
}

impl ParamValue<'static> for EventAction {
    fn as_value(&self) -> Cow<'static, str> {
        self.as_str().into()
    }
}

/// Targets that trigger events.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventTarget {
    /// An epic.
    Epic,
    /// An issue.
    Issue,
    /// A merge request.
    MergeRequest,
    /// A milestone.
    Milestone,
    /// A comment.
    Note,
    /// A project.
    Project,
    /// A code snippet.
    Snippet,
    /// A user.
    User,
    /// A wiki page.
    Wiki,
    /// A design document.
    Design,
}

impl EventTarget {
    /// The string representation of the target.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Epic => "epic",
            Self::Issue => "issue",
            Self::MergeRequest => "merge_request",
            Self::Milestone => "milestone",
            Self::Note => "note",
            Self::Project => "project",
            Self::Snippet => "snippet",
            Self::User => "user",
            Self::Wiki => "wiki",
            Self::Design => "design",
        }
    }
}

impl ParamValue<'static> for EventTarget {
    fn as_value(&self) -> Cow<'static, str> {
        self.as_str().into()
    }
}

/// Scopes for events.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventScope {
    /// Consider all visible events.
    All,
}

impl EventScope {
    /// The string representation of the scope.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::All => "all",
        }
    }
}

impl ParamValue<'static> for EventScope {
    fn as_value(&self) -> Cow<'static, str> {
        self.as_str().into()
    }
}

/// Query for events on the instance.
///
/// Fetches all events on the GitLab instance, sorted by most recent first.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct Events {
    /// Filter events to those with a specific action type.
    #[builder(default)]
    action: Option<EventAction>,
    /// Filter events to those acting on a given type of target.
    #[builder(default)]
    target_type: Option<EventTarget>,
    /// Filter events to those before the given date.
    #[builder(default)]
    before: Option<NaiveDate>,
    /// Filter events to those after the given date.
    #[builder(default)]
    after: Option<NaiveDate>,
    /// Return events for a given scope.
    ///
    /// By default, only the user's own events are returned.
    #[builder(default)]
    scope: Option<EventScope>,
    /// The sort order of the events.
    #[builder(default)]
    sort: Option<SortOrder>,
}

impl Events {
    /// Create a new endpoint for querying events.
    pub fn builder() -> EventsBuilder {
        EventsBuilder::default()
    }
}

impl Endpoint for Events {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "events".into()
    }

    fn parameters(&self) -> QueryParams<'_> {
        let mut params = QueryParams::default();

        params
            .push_opt("action", self.action)
            .push_opt("target_type", self.target_type)
            .push_opt("before", self.before)
            .push_opt("after", self.after)
            .push_opt("scope", self.scope)
            .push_opt("sort", self.sort);

        params
    }
}

impl Pageable for Events {}

#[cfg(test)]
mod tests {
    use chrono::NaiveDate;

    use crate::api::common::SortOrder;
    use crate::api::events::{EventAction, EventScope, EventTarget, Events};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    #[test]
    fn event_action_as_str() {
        let items = &[
            (EventAction::Approved, "approved"),
            (EventAction::Closed, "closed"),
            (EventAction::Commented, "commented"),
            (EventAction::Created, "created"),
            (EventAction::Destroyed, "destroyed"),
            (EventAction::Expired, "expired"),
            (EventAction::Joined, "joined"),
            (EventAction::Left, "left"),
            (EventAction::Merged, "merged"),
            (EventAction::Pushed, "pushed"),
            (EventAction::Reopened, "reopened"),
            (EventAction::Updated, "updated"),
        ];

        for (i, s) in items {
            assert_eq!(i.as_str(), *s);
        }
    }

    #[test]
    fn event_target_as_str() {
        let items = &[
            (EventTarget::Epic, "epic"),
            (EventTarget::Issue, "issue"),
            (EventTarget::MergeRequest, "merge_request"),
            (EventTarget::Milestone, "milestone"),
            (EventTarget::Note, "note"),
            (EventTarget::Project, "project"),
            (EventTarget::Snippet, "snippet"),
            (EventTarget::User, "user"),
            (EventTarget::Wiki, "wiki"),
            (EventTarget::Design, "design"),
        ];

        for (i, s) in items {
            assert_eq!(i.as_str(), *s);
        }
    }

    #[test]
    fn event_scope_as_str() {
        let items = &[(EventScope::All, "all")];

        for (i, s) in items {
            assert_eq!(i.as_str(), *s);
        }
    }

    #[test]
    fn defaults_are_sufficient() {
        Events::builder().build().unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder().endpoint("events").build().unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder().build().unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_action() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("action", "reopened")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder()
            .action(EventAction::Reopened)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_target_type() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("target_type", "issue")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder()
            .target_type(EventTarget::Issue)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_before() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("before", "2025-01-01")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder()
            .before(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap())
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_after() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("after", "2025-01-01")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder()
            .after(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap())
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_scope() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("scope", "all")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder().scope(EventScope::All).build().unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_sort() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("events")
            .add_query_params(&[("sort", "asc")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Events::builder()
            .sort(SortOrder::Ascending)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}