Parcourir la source

Add rendered link when RFCs are opened

Mark Rousskov il y a 2 ans
Parent
commit
4d7126fce9
3 fichiers modifiés avec 72 ajouts et 1 suppressions
  1. 21 1
      src/github.rs
  2. 9 0
      src/handlers.rs
  3. 42 0
      src/handlers/rfc_helper.rs

+ 21 - 1
src/github.rs

@@ -745,6 +745,26 @@ impl Issue {
         let diff = client.send_req(req).await?;
         Ok(Some(String::from(String::from_utf8_lossy(&diff))))
     }
+
+    pub async fn files(&self, client: &GithubClient) -> anyhow::Result<Vec<PullRequestFile>> {
+        if !self.is_pr() {
+            return Ok(vec![]);
+        }
+
+        let req = client.get(&format!(
+            "{}/pulls/{}/files",
+            self.repository().url(),
+            self.number
+        ));
+        Ok(client.json(req).await?)
+    }
+}
+
+#[derive(Debug, serde::Deserialize)]
+pub struct PullRequestFile {
+    pub sha: String,
+    pub filename: String,
+    pub blob_url: String,
 }
 
 #[derive(serde::Serialize)]
@@ -853,7 +873,7 @@ pub struct IssuesEvent {
 #[derive(Debug, serde::Deserialize)]
 struct PullRequestEventFields {}
 
-#[derive(Default, Clone, Debug, serde::Deserialize)]
+#[derive(Clone, Debug, serde::Deserialize)]
 pub struct CommitBase {
     sha: String,
 }

+ 9 - 0
src/handlers.rs

@@ -38,6 +38,7 @@ mod ping;
 mod prioritize;
 mod relabel;
 mod review_submitted;
+mod rfc_helper;
 mod rustc_commits;
 mod shortcut;
 
@@ -77,6 +78,14 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
         );
     }
 
+    if let Err(e) = rfc_helper::handle(ctx, event).await {
+        log::error!(
+            "failed to process event {:?} with rfc_helper handler: {:?}",
+            event,
+            e
+        );
+    }
+
     if let Some(config) = config
         .as_ref()
         .ok()

+ 42 - 0
src/handlers/rfc_helper.rs

@@ -0,0 +1,42 @@
+use crate::{
+    github::{Event, IssuesAction, IssuesEvent},
+    handlers::Context,
+};
+
+pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
+    let e = if let Event::Issue(e) = event {
+        e
+    } else {
+        return Ok(());
+    };
+
+    let repo = e.issue.repository();
+    if !(repo.organization == "rust-lang" && repo.repository == "rfcs") {
+        return Ok(());
+    }
+
+    if let Err(e) = add_rendered_link(&ctx, &e).await {
+        tracing::error!("Error adding rendered link: {:?}", e);
+    }
+
+    Ok(())
+}
+
+async fn add_rendered_link(ctx: &Context, e: &IssuesEvent) -> anyhow::Result<()> {
+    if e.action == IssuesAction::Opened || e.action == IssuesAction::Synchronize {
+        let files = e.issue.files(&ctx.github).await?;
+
+        if let Some(file) = files.iter().find(|f| f.filename.starts_with("text/")) {
+            if !e.issue.body.contains("[Rendered]") {
+                e.issue
+                    .edit_body(
+                        &ctx.github,
+                        &format!("{}\n\n[Rendered]({})", e.issue.body, file.blob_url),
+                    )
+                    .await?;
+            }
+        }
+    }
+
+    Ok(())
+}