Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat: use an enum for event types
Lars Wirzenius committed 2 years ago
commit 68b589ce376cdb50730a94191e2179281083665f
parent fee8700dc8d1845c7c8c55e0fd2dd99b29d3efe6
1 file changed +17 -7
modified src/msg.rs
@@ -135,9 +135,9 @@ impl<'a> RequestBuilder<'a> {
        let author = extract_author(profile, event)?;
        let push_info: Option<PushEvent>;
        let patch_info: Option<PatchEvent>;
-
        let event_type: String;
+
        let event_type: EventType;
        if is_patch {
-
            event_type = "patch".to_string();
+
            event_type = EventType::Patch;
            let patch_id = event.patch_id().ok_or(MessageError::Trigger)?;
            let patch = patch::Patches::open(&repository)?
                .get(&patch_id.into())?
@@ -202,7 +202,7 @@ impl<'a> RequestBuilder<'a> {
                },
            });
        } else {
-
            event_type = "push".to_string();
+
            event_type = EventType::Push;
            let before_oid: Oid = old.unwrap_or(*oid);
            let push_commits: Vec<Oid> = repo
                .history(oid)?
@@ -342,11 +342,21 @@ impl fmt::Display for Request {
    }
}

+
/// Type of event.
+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
+
pub enum EventType {
+
    /// A push event to a branch.
+
    Push,
+

+
    /// A new or changed patch.
+
    Patch,
+
}
+

/// Common fields in all variations of a [`Request`] message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventCommonFields {
    /// The type of the event.
-
    pub event_type: String,
+
    pub event_type: EventType,

    /// The repository the event is related to.
    pub repository: Repository,
@@ -638,7 +648,7 @@ pub enum MessageError {
#[cfg(test)]
pub mod tests {
    use crate::event::BrokerEvent;
-
    use crate::msg::{Request, RequestBuilder};
+
    use crate::msg::{EventType, Request, RequestBuilder};
    use radicle::git::raw::Oid;
    use radicle::git::RefString;
    use radicle::patch::{MergeTarget, Patches};
@@ -682,7 +692,7 @@ pub mod tests {

        assert!(patch.is_none());
        assert!(push.is_some());
-
        assert_eq!(common.event_type, "push");
+
        assert_eq!(common.event_type, EventType::Push);
        assert_eq!(common.repository.id, project.id);
        assert_eq!(common.repository.name, project.repo.project()?.name());

@@ -751,7 +761,7 @@ pub mod tests {

        assert!(patch.is_some());
        assert!(push.is_none());
-
        assert_eq!(common.event_type, "patch");
+
        assert_eq!(common.event_type, EventType::Patch);
        assert_eq!(common.repository.id, project.id);
        assert_eq!(common.repository.name, project.repo.project()?.name());