octofer 0.1.0

A framework for building GitHub Apps in Rust
Documentation
//! Workflow and actions event handlers
//!
//! This module provides implementations for registering handlers for GitHub Actions
//! workflow-related webhook events.

use std::sync::Arc;

use octocrab::models::webhook_events::WebhookEventType;

use crate::{Context, Octofer, SerdeToString};

impl Octofer {
    /// Register a handler for workflow run events
    pub async fn on_workflow_run<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
    where
        F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
        E: Send + Sync + 'static,
    {
        self.server
            .on(WebhookEventType::WorkflowRun.to_string(), handler, extra)
            .await;
        self
    }

    /// Register a handler for workflow job events
    pub async fn on_workflow_job<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
    where
        F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
        E: Send + Sync + 'static,
    {
        self.server
            .on(WebhookEventType::WorkflowJob.to_string(), handler, extra)
            .await;
        self
    }

    /// Register a handler for workflow dispatch events
    pub async fn on_workflow_dispatch<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
    where
        F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
        E: Send + Sync + 'static,
    {
        self.server
            .on(
                WebhookEventType::WorkflowDispatch.to_string(),
                handler,
                extra,
            )
            .await;
        self
    }

    /// Register a handler for status events
    pub async fn on_status<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
    where
        F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
        E: Send + Sync + 'static,
    {
        self.server
            .on(WebhookEventType::Status.to_string(), handler, extra)
            .await;
        self
    }
}