Răsfoiți Sursa

Verify that labels exist before passing them to replacement API

GitHub will just implicitly create labels otherwise which isn't what we
want.
Mark Rousskov 6 ani în urmă
părinte
comite
661ccbb58d
1 a modificat fișierele cu 24 adăugiri și 1 ștergeri
  1. 24 1
      src/github.rs

+ 24 - 1
src/github.rs

@@ -13,6 +13,19 @@ pub struct Label {
     pub name: String,
 }
 
+impl Label {
+    fn exists(&self, repo_api_prefix: &str, client: &GithubClient) -> bool {
+        match client
+            .get(&format!("{}/labels/{}", repo_api_prefix, self.name))
+            .send_req()
+        {
+            Ok(_) => true,
+            // XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
+            Err(_) => false,
+        }
+    }
+}
+
 #[derive(Debug, serde::Deserialize)]
 pub struct Issue {
     number: u64,
@@ -31,7 +44,11 @@ pub struct Comment {
 }
 
 impl Issue {
-    pub fn set_labels(&mut self, client: &GithubClient, labels: Vec<Label>) -> Result<(), Error> {
+    pub fn set_labels(
+        &mut self,
+        client: &GithubClient,
+        mut labels: Vec<Label>,
+    ) -> Result<(), Error> {
         // PUT /repos/:owner/:repo/issues/:number/labels
         // repo_url = https://api.github.com/repos/Codertocat/Hello-World
         // Might need `Accept: application/vnd.github.symmetra-preview+json` for emoji and descriptions
@@ -41,6 +58,8 @@ impl Issue {
             number = self.number
         );
 
+        labels.retain(|label| label.exists(&self.repository_url, &client));
+
         #[derive(serde::Serialize)]
         struct LabelsReq {
             labels: Vec<String>,
@@ -96,6 +115,10 @@ impl GithubClient {
         GithubClient { client: c, token }
     }
 
+    fn get(&self, url: &str) -> RequestBuilder {
+        self.client.get(url).configure(self)
+    }
+
     fn put(&self, url: &str) -> RequestBuilder {
         self.client.put(url).configure(self)
     }