Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: move PatchEvent creation to its own function
Lars Wirzenius committed 1 year ago
commit b4484b946d321688073930a76dd9a3b4bf7e0f4e
parent f38697e2d9af3f12dd2df41992a6b7ccffebb2e2
1 file changed +75 -62
modified src/msg.rs
@@ -160,69 +160,10 @@ impl<'a> RequestBuilder<'a> {
            match parsed_ref {
                ParsedRef::Patch(_oid) => {
                    event_type = EventType::Patch;
-
                    let patch_id = event.patch_id().ok_or(MessageError::Trigger)?;
-
                    let patch = patch::Patches::open(&repository)?
-
                        .get(&patch_id.into())?
-
                        .ok_or(MessageError::Trigger)?;
+
                    patch_info = Some(
+
                        self.build_trigger_from_patch(event, repository, &repo, profile, author)?,
+
                    );
                    push_info = None;
-

-
                    let revs: Vec<Revision> = patch
-
                        .revisions()
-
                        .map(|(rid, r)| {
-
                            Ok::<Revision, MessageError>(Revision {
-
                                id: rid.into(),
-
                                author: did_to_author(profile, r.author().id())?,
-
                                description: r.description().to_string(),
-
                                base: *r.base(),
-
                                oid: r.head(),
-
                                timestamp: r.timestamp().as_secs(),
-
                            })
-
                        })
-
                        .collect::<Result<Vec<Revision>, MessageError>>()?;
-
                    let patch_author_pk = radicle::crypto::PublicKey::from(author.id);
-
                    let patch_latest_revision = patch
-
                        .latest_by(&patch_author_pk)
-
                        .ok_or(MessageError::Trigger)?;
-
                    let patch_head = patch_latest_revision.1.head();
-
                    let patch_base = patch_latest_revision.1.base();
-
                    let patch_commits: Vec<Oid> = repo
-
                        .history(patch_head)?
-
                        .take_while(|c| {
-
                            if let Ok(c) = c {
-
                                c.id != *patch_base
-
                            } else {
-
                                false
-
                            }
-
                        })
-
                        .map(|r| r.map(|c| c.id))
-
                        .collect::<Result<Vec<Oid>, _>>()?;
-
                    let action = if patch.revisions().count() > 1 {
-
                        PatchAction::Updated
-
                    } else {
-
                        PatchAction::Created
-
                    };
-
                    patch_info = Some(PatchEvent {
-
                        action,
-
                        patch: Patch {
-
                            id: patch_id,
-
                            author,
-
                            title: patch.title().to_string(),
-
                            state: State {
-
                                status: patch.state().to_string(),
-
                                conflicts: match patch.state() {
-
                                    patch::State::Open { conflicts, .. } => conflicts.to_vec(),
-
                                    _ => vec![],
-
                                },
-
                            },
-
                            before: *patch_base,
-
                            after: patch_head,
-
                            commits: patch_commits,
-
                            target: patch.target().head(&repository)?,
-
                            labels: patch.labels().map(|l| l.name().to_string()).collect(),
-
                            assignees: patch.assignees().collect(),
-
                            revisions: revs,
-
                        },
-
                    });
                }
                ParsedRef::Push(_branch) => {
                    event_type = EventType::Push;
@@ -248,6 +189,78 @@ impl<'a> RequestBuilder<'a> {
        }
    }

+
    fn build_trigger_from_patch(
+
        &self,
+
        event: &BrokerEvent,
+
        repository: radicle::storage::git::Repository,
+
        repo: &radicle_surf::Repository,
+
        profile: &Profile,
+
        author: Author,
+
    ) -> Result<PatchEvent, MessageError> {
+
        let patch_id = event.patch_id().ok_or(MessageError::Trigger)?;
+
        let patch = patch::Patches::open(&repository)?
+
            .get(&patch_id.into())?
+
            .ok_or(MessageError::Trigger)?;
+

+
        let revs: Vec<Revision> = patch
+
            .revisions()
+
            .map(|(rid, r)| {
+
                Ok::<Revision, MessageError>(Revision {
+
                    id: rid.into(),
+
                    author: did_to_author(profile, r.author().id())?,
+
                    description: r.description().to_string(),
+
                    base: *r.base(),
+
                    oid: r.head(),
+
                    timestamp: r.timestamp().as_secs(),
+
                })
+
            })
+
            .collect::<Result<Vec<Revision>, MessageError>>()?;
+
        let patch_author_pk = radicle::crypto::PublicKey::from(author.id);
+
        let patch_latest_revision = patch
+
            .latest_by(&patch_author_pk)
+
            .ok_or(MessageError::Trigger)?;
+
        let patch_head = patch_latest_revision.1.head();
+
        let patch_base = patch_latest_revision.1.base();
+
        let patch_commits: Vec<Oid> = repo
+
            .history(patch_head)?
+
            .take_while(|c| {
+
                if let Ok(c) = c {
+
                    c.id != *patch_base
+
                } else {
+
                    false
+
                }
+
            })
+
            .map(|r| r.map(|c| c.id))
+
            .collect::<Result<Vec<Oid>, _>>()?;
+
        let action = if patch.revisions().count() > 1 {
+
            PatchAction::Updated
+
        } else {
+
            PatchAction::Created
+
        };
+
        Ok(PatchEvent {
+
            action,
+
            patch: Patch {
+
                id: patch_id,
+
                author,
+
                title: patch.title().to_string(),
+
                state: State {
+
                    status: patch.state().to_string(),
+
                    conflicts: match patch.state() {
+
                        patch::State::Open { conflicts, .. } => conflicts.to_vec(),
+
                        _ => vec![],
+
                    },
+
                },
+
                before: *patch_base,
+
                after: patch_head,
+
                commits: patch_commits,
+
                target: patch.target().head(&repository)?,
+
                labels: patch.labels().map(|l| l.name().to_string()).collect(),
+
                assignees: patch.assignees().collect(),
+
                revisions: revs,
+
            },
+
        })
+
    }
+

    fn build_trigger_from_push(
        &self,
        repo: radicle_surf::Repository,