Browse Source

Make Action::call be async and fix the hang problem

Santiago Pastorino 4 years ago
parent
commit
d5a27a60dd
4 changed files with 38 additions and 36 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 1 1
      src/bin/prioritization.rs
  4. 35 35
      src/prioritization/mod.rs

+ 1 - 0
Cargo.lock

@@ -1410,6 +1410,7 @@ name = "triagebot"
 version = "0.1.0"
 dependencies = [
  "anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
+ "async-trait 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
  "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "dotenv 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",

+ 1 - 0
Cargo.toml

@@ -24,6 +24,7 @@ toml = "0.5.1"
 hyper = "0.13"
 tokio = { version = "0.2", features = ["macros", "time"] }
 futures = { version = "0.3", default-features = false, features = ["std"] }
+async-trait = "0.1.31"
 uuid = { version = "0.8", features = ["v4"] }
 url = "2.1.0"
 once_cell = "1"

+ 1 - 1
src/bin/prioritization.rs

@@ -6,7 +6,7 @@ async fn main() {
     let meeting = prioritization::config::prepare_meeting();
 
     for step in &meeting.steps {
-        println!("{}", step.call());
+        println!("{}", step.call().await);
 
         //press_key_to_continue();
     }

+ 35 - 35
src/prioritization/mod.rs

@@ -1,4 +1,5 @@
-use futures::executor::block_on;
+use async_trait::async_trait;
+
 use reqwest::Client;
 use std::env;
 use std::fs::File;
@@ -12,8 +13,9 @@ pub struct Meeting<A: Action> {
     pub steps: Vec<A>,
 }
 
+#[async_trait]
 pub trait Action {
-    fn call(&self) -> String;
+    async fn call(&self) -> String;
 }
 
 pub struct Step<'a> {
@@ -46,45 +48,43 @@ pub struct FileTemplate<'a> {
     map: Vec<(&'a str, Vec<Issue>)>,
 }
 
+#[async_trait]
 impl<'a> Action for Step<'a> {
-    fn call(&self) -> String {
+    async fn call(&self) -> String {
         let gh = GithubClient::new(
             Client::new(),
             env::var("GITHUB_API_TOKEN").expect("Missing GITHUB_API_TOKEN"),
         );
 
-        let map = self
-            .actions
-            .iter()
-            .flat_map(|RepoQuery { repo, queries }| {
-                let repository = Repository {
-                    full_name: repo.to_string(),
-                };
-
-                queries
-                    .iter()
-                    .map(|NamedQuery { name, query }| {
-                        let issues_search_result = block_on(repository.get_issues(
-                            &gh,
-                            &query.filters,
-                            &query.include_labels,
-                            &query.exclude_labels,
-                        ));
-
-                        match issues_search_result {
-                            Ok(issues) => (*name, issues),
-                            Err(err) => {
-                                eprintln!("ERROR: {}", err);
-                                err.chain()
-                                    .skip(1)
-                                    .for_each(|cause| eprintln!("because: {}", cause));
-                                std::process::exit(1);
-                            }
-                        }
-                    })
-                    .collect::<Vec<_>>()
-            })
-            .collect();
+        let mut map = Vec::new();
+
+        for RepoQuery { repo, queries } in &self.actions {
+            let repository = Repository {
+                full_name: repo.to_string(),
+            };
+
+            for NamedQuery { name, query } in queries {
+                let issues_search_result = repository
+                    .get_issues(
+                        &gh,
+                        &query.filters,
+                        &query.include_labels,
+                        &query.exclude_labels,
+                    )
+                    .await;
+
+                match issues_search_result {
+                    Ok(issues) => map.push((*name, issues)),
+                    Err(err) => {
+                        eprintln!("ERROR: {}", err);
+                        err.chain()
+                            .skip(1)
+                            .for_each(|cause| eprintln!("because: {}", cause));
+                        std::process::exit(1);
+                    }
+                }
+            }
+        }
 
         let template = FileTemplate::new(self.name, map);
         template.render()