handlers.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use crate::github::{Event, GithubClient};
  2. use failure::Error;
  3. use futures::future::BoxFuture;
  4. macro_rules! handlers {
  5. ($($name:ident = $handler:expr,)*) => {
  6. $(mod $name;)*
  7. pub async fn handle(ctx: &Context, event: &Event) -> Result<(), Error> {
  8. let config = crate::config::get(&ctx.github, event.repo_name()).await?;
  9. $(if let Some(input) = Handler::parse_input(&$handler, ctx, event)? {
  10. if let Some(config) = &config.$name {
  11. Handler::handle_input(&$handler, ctx, config, event, input).await?;
  12. } else {
  13. failure::bail!(
  14. "The feature `{}` is not enabled in this repository.\n\
  15. To enable it add its section in the `triagebot.toml` \
  16. in the root of the repository.",
  17. stringify!($name)
  18. );
  19. }
  20. })*
  21. Ok(())
  22. }
  23. }
  24. }
  25. handlers! {
  26. assign = assign::AssignmentHandler,
  27. relabel = relabel::RelabelHandler,
  28. //tracking_issue = tracking_issue::TrackingIssueHandler,
  29. }
  30. pub struct Context {
  31. pub github: GithubClient,
  32. pub username: String,
  33. }
  34. pub trait Handler: Sync + Send {
  35. type Input;
  36. type Config;
  37. fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, Error>;
  38. fn handle_input<'a>(
  39. &self,
  40. ctx: &'a Context,
  41. config: &'a Self::Config,
  42. event: &'a Event,
  43. input: Self::Input,
  44. ) -> BoxFuture<'a, Result<(), Error>>;
  45. }