use chrono::NaiveDate;
use derive_builder::Builder;
use crate::api::common::SortOrder;
use crate::api::endpoint_prelude::*;
use crate::api::ParamValue;
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventAction {
Approved,
Closed,
Commented,
Created,
Destroyed,
Expired,
Joined,
Left,
Merged,
Pushed,
Reopened,
Updated,
}
impl EventAction {
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()
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventTarget {
Epic,
Issue,
MergeRequest,
Milestone,
Note,
Project,
Snippet,
User,
Wiki,
Design,
}
impl EventTarget {
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()
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum EventScope {
All,
}
impl EventScope {
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()
}
}
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct Events {
#[builder(default)]
action: Option<EventAction>,
#[builder(default)]
target_type: Option<EventTarget>,
#[builder(default)]
before: Option<NaiveDate>,
#[builder(default)]
after: Option<NaiveDate>,
#[builder(default)]
scope: Option<EventScope>,
#[builder(default)]
sort: Option<SortOrder>,
}
impl 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();
}
}