瀏覽代碼

further simplify code; move FCPDecorator to actioins

chaz-kiker 3 年之前
父節點
當前提交
d5af74231b
共有 2 個文件被更改,包括 52 次插入46 次删除
  1. 35 16
      src/actions.rs
  2. 17 30
      src/rfcbot.rs

+ 35 - 16
src/actions.rs

@@ -2,8 +2,8 @@ use chrono::{DateTime, Utc};
 use std::collections::HashMap;
 
 use async_trait::async_trait;
-
 use reqwest::Client;
+use serde::{Deserialize, Serialize};
 use tera::{Context, Tera};
 
 use crate::github::{self, GithubClient, Repository};
@@ -46,6 +46,22 @@ pub struct IssueDecorator {
     pub updated_at: String,
 }
 
+#[derive(Serialize, Deserialize, Debug)]
+pub struct FCPDecorator {
+    pub number: u64,
+    pub title: String,
+    pub html_url: String,
+    pub repo_name: String,
+    pub labels: String,
+    pub assignees: String,
+    pub updated_at: String,
+
+    pub bot_tracking_comment_html_url: String,
+    pub bot_tracking_comment_content: String,
+    pub initiating_comment_html_url: String,
+    pub initiating_comment_content: String,
+}
+
 lazy_static! {
     pub static ref TEMPLATES: Tera = {
         match Tera::new("templates/*") {
@@ -119,21 +135,24 @@ impl<'a> Action for Step<'a> {
         for (name, issues) in &results {
             if name == &"proposed_fcp" {
                 let fcp_map = crate::rfcbot::get_all_fcps().await.unwrap();
-                let mut fcp_results = Vec::new();
-                for issue_decorator in issues.into_iter() {
-                    let key = format!(
-                        "rust-lang/{}:{}:{}",
-                        issue_decorator.repo_name.clone(),
-                        issue_decorator.number.clone(),
-                        issue_decorator.title.clone(),
-                    );
-                    if let Some(fcp) = fcp_map.get(&key) {
-                        let fcp_detail =
-                            crate::rfcbot::FCPDecorator::from_issue_fcp(&fcp, &issue_decorator);
-
-                        fcp_results.push(fcp_detail);
-                    }
-                }
+
+                let fcp_results = issues
+                    .into_iter()
+                    .filter_map(|issue_decorator| {
+                        let key = format!(
+                            "rust-lang/{}:{}:{}",
+                            issue_decorator.repo_name.clone(),
+                            issue_decorator.number.clone(),
+                            issue_decorator.title.clone(),
+                        );
+                        if let Some(fcp) = fcp_map.get(&key) {
+                            Some(FCPDecorator::from_issue_fcp(&fcp, &issue_decorator))
+                        } else {
+                            None
+                        }
+                    })
+                    .collect::<Vec<_>>();
+
                 context.insert(*name, &fcp_results);
             } else {
                 context.insert(*name, issues);

+ 17 - 30
src/rfcbot.rs

@@ -1,3 +1,4 @@
+use crate::actions::FCPDecorator;
 use reqwest::Url;
 use serde::{Deserialize, Serialize};
 use std::collections::HashMap;
@@ -61,24 +62,6 @@ pub struct FullFCP {
     pub status_comment: StatusComment,
 }
 
-#[derive(Serialize, Deserialize, Debug)]
-pub struct FCPDecorator {
-    pub number: u64,
-    pub title: String,
-    pub html_url: String,
-    pub repo_name: String,
-    pub labels: String,
-    pub assignees: String,
-    pub updated_at: String,
-
-    pub bot_tracking_comment: String,
-    pub bot_tracking_comment_html_url: String,
-    pub bot_tracking_comment_content: String,
-    pub initiating_comment: String,
-    pub initiating_comment_html_url: String,
-    pub initiating_comment_content: String,
-}
-
 fn quote_reply(markdown: &str) -> String {
     if markdown.is_empty() {
         String::from("*No content*")
@@ -92,6 +75,18 @@ impl FCPDecorator {
         full_fcp: &FullFCP,
         issue_decorator: &crate::actions::IssueDecorator,
     ) -> Self {
+        let bot_tracking_comment_html_url = format!(
+            "{}#issuecomment-{}",
+            issue_decorator.html_url, full_fcp.fcp.fk_bot_tracking_comment
+        );
+        let bot_tracking_comment_content = quote_reply(&full_fcp.status_comment.body);
+        let initiating_comment_html_url = format!(
+            "{}#issuecomment-{}",
+            issue_decorator.html_url, full_fcp.fcp.fk_initiating_comment
+        );
+        // TODO: get from GitHub
+        let initiating_comment_content = quote_reply(&String::new());
+
         Self {
             // shared properties with IssueDecorator
             number: issue_decorator.number.clone(),
@@ -103,18 +98,10 @@ impl FCPDecorator {
             updated_at: issue_decorator.updated_at.clone(),
 
             // additional properties from FullFCP (from rfcbot)
-            bot_tracking_comment: full_fcp.fcp.fk_bot_tracking_comment.to_string(),
-            bot_tracking_comment_html_url: format!(
-                "{}#issuecomment-{}",
-                issue_decorator.html_url, full_fcp.fcp.fk_bot_tracking_comment
-            ),
-            bot_tracking_comment_content: quote_reply(&full_fcp.status_comment.body),
-            initiating_comment: full_fcp.fcp.fk_initiating_comment.to_string(),
-            initiating_comment_html_url: format!(
-                "{}#issuecomment-{}",
-                issue_decorator.html_url, full_fcp.fcp.fk_initiating_comment
-            ),
-            initiating_comment_content: quote_reply(&String::new()), // TODO: get from GitHub
+            bot_tracking_comment_html_url,
+            bot_tracking_comment_content,
+            initiating_comment_html_url,
+            initiating_comment_content,
         }
     }
 }