Browse Source

Fix Repository deserialization in webhooks

Mark Rousskov 3 years ago
parent
commit
c6dcf47dd4
3 changed files with 19 additions and 18 deletions
  1. 2 3
      src/actions.rs
  2. 13 10
      src/github.rs
  3. 4 5
      src/github/graphql.rs

+ 2 - 3
src/actions.rs

@@ -79,8 +79,7 @@ impl<'a> Action for Step<'a> {
         for Query { repos, queries } in &self.actions {
             for repo in repos {
                 let repository = Repository {
-                    owner: repo.0.to_string(),
-                    name: repo.1.to_string(),
+                    full_name: format!("{}/{}", repo.0, repo.1),
                 };
 
                 for QueryMap { name, kind, query } in queries {
@@ -97,7 +96,7 @@ impl<'a> Action for Step<'a> {
                                                 title: issue.title.clone(),
                                                 number: issue.number,
                                                 html_url: issue.html_url.clone(),
-                                                repo_name: repository.name.clone(),
+                                                repo_name: repository.name().to_owned(),
                                                 labels: issue
                                                     .labels
                                                     .iter()

+ 13 - 10
src/github.rs

@@ -759,16 +759,19 @@ pub struct IssueSearchResult {
 
 #[derive(Debug, serde::Deserialize)]
 pub struct Repository {
-    pub owner: String,
-    pub name: String,
+    pub full_name: String,
 }
 
 impl Repository {
     const GITHUB_API_URL: &'static str = "https://api.github.com";
     const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
 
-    pub fn full_name(&self) -> String {
-        return format!("{}/{}", self.owner, self.name);
+    pub fn owner(&self) -> &str {
+        self.full_name.split_once('/').unwrap().0
+    }
+
+    pub fn name(&self) -> &str {
+        self.full_name.split_once('/').unwrap().1
     }
 
     pub async fn get_issues<'a>(
@@ -881,7 +884,7 @@ impl Repository {
         format!(
             "{}/repos/{}/{}?{}",
             Repository::GITHUB_API_URL,
-            self.full_name(),
+            self.full_name,
             endpoint,
             filters
         )
@@ -908,7 +911,7 @@ impl Repository {
                     .iter()
                     .map(|label| format!("-label:{}", label)),
             )
-            .chain(std::iter::once(format!("repo:{}", self.full_name())))
+            .chain(std::iter::once(format!("repo:{}", self.full_name)))
             .collect::<Vec<_>>()
             .join("+");
         format!(
@@ -968,10 +971,10 @@ pub enum Event {
 impl Event {
     pub fn repo_name(&self) -> String {
         match self {
-            Event::Create(event) => event.repository.full_name(),
-            Event::IssueComment(event) => event.repository.full_name(),
-            Event::Issue(event) => event.repository.full_name(),
-            Event::Push(event) => event.repository.full_name(),
+            Event::Create(event) => event.repository.full_name.clone(),
+            Event::IssueComment(event) => event.repository.full_name.clone(),
+            Event::Issue(event) => event.repository.full_name.clone(),
+            Event::Push(event) => event.repository.full_name.clone(),
         }
     }
 

+ 4 - 5
src/github/graphql.rs

@@ -176,15 +176,14 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
     ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
         use cynic::QueryBuilder;
 
-        let repo_name = repo.name.clone();
-        let repository_owner = repo.owner.clone();
-        let repository_name = repo.name.clone();
+        let repository_owner = repo.owner().to_owned();
+        let repository_name = repo.name().to_owned();
 
         let mut prs = vec![];
 
         let mut args = queries::LeastRecentlyReviewedPullRequestsArguments {
             repository_owner,
-            repository_name,
+            repository_name: repository_name.clone(),
             after: None,
         };
         loop {
@@ -314,7 +313,7 @@ impl IssuesQuery for LeastRecentlyReviewedPullRequests {
                     pr.number as u64,
                     pr.title,
                     pr.url.0,
-                    repo_name.clone(),
+                    repository_name.clone(),
                     labels,
                     assignees,
                 ))