Browse Source

Fix glacier command (#949)

* Fix glacier command

It was fetching the HTML rather than the raw source;
see rust-lang/glacier#530 for an example.

* Refactor raw gist fetching into utility function

* Add docs
Camelid 4 years ago
parent
commit
fe16faf195
2 changed files with 19 additions and 2 deletions
  1. 15 0
      src/github.rs
  2. 4 2
      src/handlers/glacier.rs

+ 15 - 0
src/github.rs

@@ -1056,6 +1056,21 @@ impl GithubClient {
         }
     }
 
+    /// Get the raw gist content from the URL of the HTML version of the gist:
+    ///
+    /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
+    ///
+    /// `filename` is the name of the file you want the content of.
+    pub async fn raw_gist_from_url(
+        &self,
+        html_url: &str,
+        filename: &str,
+    ) -> anyhow::Result<String> {
+        let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
+        let response = self.raw().get(&url).send().await?;
+        response.text().await.context("raw gist from url")
+    }
+
     fn get(&self, url: &str) -> RequestBuilder {
         log::trace!("get {:?}", url);
         self.client.get(url).configure(self)

+ 4 - 2
src/handlers/glacier.rs

@@ -22,8 +22,10 @@ pub(super) async fn handle_command(
         return Ok(());
     };
 
-    let response = ctx.github.raw().get(&cmd.source).send().await?;
-    let body = response.text().await?;
+    let body = ctx
+        .github
+        .raw_gist_from_url(&cmd.source, "playground.rs")
+        .await?;
 
     let number = event.issue().unwrap().number;
     let user = event.user();