Procházet zdrojové kódy

Post link to Zulip on major change issues

Mark Rousskov před 4 roky
rodič
revize
ca0e7cfd16
2 změnil soubory, kde provedl 43 přidání a 0 odebrání
  1. 17 0
      src/handlers/major_change.rs
  2. 26 0
      src/zulip.rs

+ 17 - 0
src/handlers/major_change.rs

@@ -4,6 +4,7 @@ use crate::{
     handlers::{Context, Handler},
     interactions::ErrorComment,
 };
+use anyhow::Context as _;
 use futures::future::{BoxFuture, FutureExt};
 use parser::command::second::SecondCommand;
 use parser::command::{Command, Input};
@@ -154,6 +155,22 @@ async fn handle_input(
         content: &zulip_msg,
     };
 
+    let topic_url = zulip_req.url();
+    let comment = format!(
+        "This issue is not meant to be used for technical discussion. \
+        There is a Zulip [stream] for that. Use this issue to leave \
+        procedural comments, such as volunteering to review, indicating that you \
+        second the proposal (or third, etc), or raising a concern that you would \
+        like to be addressed.
+
+        [stream]: {}",
+        topic_url
+    );
+    issue
+        .post_comment(&ctx.github, &comment)
+        .await
+        .context("post major change comment")?;
+
     let zulip_req = zulip_req.send(&ctx.github.raw());
 
     let (gh_res, zulip_res) = futures::join!(github_req, zulip_req);

+ 26 - 0
src/zulip.rs

@@ -5,6 +5,7 @@ use crate::handlers::Context;
 use anyhow::Context as _;
 use std::convert::TryInto;
 use std::env;
+use std::io::Write as _;
 
 #[derive(Debug, serde::Deserialize)]
 pub struct Request {
@@ -316,6 +317,31 @@ pub struct MessageApiRequest<'a> {
 }
 
 impl MessageApiRequest<'_> {
+    pub fn url(&self) -> String {
+        // FIXME: support private links too
+        assert_eq!(self.to, "stream");
+        // See
+        // https://github.com/zulip/zulip/blob/46247623fc279/zerver/lib/url_encoding.py#L9
+        // ALWAYS_SAFE from
+        // https://github.com/python/cpython/blob/113e2b0a07c/Lib/urllib/parse.py#L772-L775
+        let always_safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~";
+        let mut encoded_topic = Vec::new();
+        for ch in self.topic.expect("topic").bytes() {
+            if !(always_safe.contains(ch as char) || ch == b'.') {
+                write!(encoded_topic, "%{:02X}", ch).unwrap();
+            } else {
+                encoded_topic.push(ch);
+            }
+        }
+        let mut encoded_topic = String::from_utf8(encoded_topic).unwrap();
+        encoded_topic = encoded_topic.replace("%", ".");
+
+        format!(
+            "https://rust-lang.zulipchat.com/#narrow/stream/{}-xxx/topic/{}",
+            self.to, encoded_topic,
+        )
+    }
+
     pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
         if self.type_ == "stream" {
             assert!(self.topic.is_some());