소스 검색

Optionally thread configuration into input parsing

Mark Rousskov 5 년 전
부모
커밋
c591d2cd7c
7개의 변경된 파일48개의 추가작업 그리고 10개의 파일을 삭제
  1. 12 4
      src/handlers.rs
  2. 6 1
      src/handlers/assign.rs
  3. 6 1
      src/handlers/major_change.rs
  4. 6 1
      src/handlers/nominate.rs
  5. 6 1
      src/handlers/ping.rs
  6. 6 1
      src/handlers/prioritize.rs
  7. 6 1
      src/handlers/relabel.rs

+ 12 - 4
src/handlers.rs

@@ -27,10 +27,13 @@ macro_rules! handlers {
         mod notification;
 
         pub async fn handle(ctx: &Context, event: &Event) -> Result<(), HandlerError> {
+            let config = config::get(&ctx.github, event.repo_name()).await;
+
             $(
             if let Some(input) = Handler::parse_input(
-                    &$handler, ctx, event).map_err(HandlerError::Message)? {
-                let config = match config::get(&ctx.github, event.repo_name()).await {
+                &$handler, ctx, event, config.as_ref().ok().and_then(|c| c.$name.as_ref()),
+            ).map_err(HandlerError::Message)? {
+                let config = match &config {
                     Ok(config) => config,
                     Err(e @ ConfigurationError::Missing) => {
                         return Err(HandlerError::Message(e.to_string()));
@@ -39,7 +42,7 @@ macro_rules! handlers {
                         return Err(HandlerError::Message(e.to_string()));
                     }
                     Err(e @ ConfigurationError::Http(_)) => {
-                        return Err(HandlerError::Other(e.into()));
+                        return Err(HandlerError::Other(e.clone().into()));
                     }
                 };
                 if let Some(config) = &config.$name {
@@ -83,7 +86,12 @@ pub trait Handler: Sync + Send {
     type Input;
     type Config;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String>;
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        config: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String>;
 
     fn handle_input<'a>(
         &self,

+ 6 - 1
src/handlers/assign.rs

@@ -33,7 +33,12 @@ impl Handler for AssignmentHandler {
     type Input = AssignCommand;
     type Config = AssignConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&AssignConfig>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {

+ 6 - 1
src/handlers/major_change.rs

@@ -19,7 +19,12 @@ impl Handler for MajorChangeHandler {
     type Input = Invocation;
     type Config = MajorChangeConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {

+ 6 - 1
src/handlers/nominate.rs

@@ -16,7 +16,12 @@ impl Handler for NominateHandler {
     type Input = NominateCommand;
     type Config = NominateConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {

+ 6 - 1
src/handlers/ping.rs

@@ -20,7 +20,12 @@ impl Handler for PingHandler {
     type Input = PingCommand;
     type Config = PingConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {

+ 6 - 1
src/handlers/prioritize.rs

@@ -14,7 +14,12 @@ impl Handler for PrioritizeHandler {
     type Input = PrioritizeCommand;
     type Config = PrioritizeConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {

+ 6 - 1
src/handlers/relabel.rs

@@ -24,7 +24,12 @@ impl Handler for RelabelHandler {
     type Input = RelabelCommand;
     type Config = RelabelConfig;
 
-    fn parse_input(&self, ctx: &Context, event: &Event) -> Result<Option<Self::Input>, String> {
+    fn parse_input(
+        &self,
+        ctx: &Context,
+        event: &Event,
+        _: Option<&Self::Config>,
+    ) -> Result<Option<Self::Input>, String> {
         let body = if let Some(b) = event.comment_body() {
             b
         } else {