From a546dcfd3603aa4d3af4324a9083dce905448289 Mon Sep 17 00:00:00 2001 From: Juan Gomez Date: Wed, 29 Mar 2023 08:53:37 +0200 Subject: [PATCH] Fix the model for parsing responses to a specific commit sha (#302) * Fix the model for parsing responses to a specific commit sha. The response from Github to something like: https://api.github.com/repos/[owner]/[repo]/commits/[sha], has two type of Authors. Prior to these changes the "author" key in the root level has different fields than the "author" key from the praent "commit" key. This commit fixes the problem by properly naming the fields so the parser don't mix the two "author" keys. * Rebasing with master, and renaming User to Author * Fixing tests. Ok, this time I run the tests before commmiting :') --- src/api/current.rs | 2 +- src/api/issues.rs | 2 +- src/api/orgs/list_members.rs | 2 +- src/api/repos.rs | 18 ++++++++-------- src/api/repos/file.rs | 26 +++++++++++------------ src/api/search.rs | 2 +- src/api/teams/members.rs | 2 +- src/lib.rs | 2 +- src/models.rs | 22 +++++++++---------- src/models/apps.rs | 2 +- src/models/commits.rs | 2 +- src/models/events/payload.rs | 4 ++-- src/models/events/payload/issues.rs | 4 ++-- src/models/events/payload/member.rs | 4 ++-- src/models/events/payload/push.rs | 4 ++-- src/models/events/payload/workflow_run.rs | 4 ++-- src/models/gists.rs | 2 +- src/models/issues.rs | 8 +++---- src/models/orgs.rs | 2 +- src/models/pulls.rs | 16 +++++++------- src/models/reactions.rs | 2 +- src/models/repos.rs | 16 +++++++------- src/models/teams.rs | 4 ++-- src/models/workflows.rs | 4 ++-- tests/org_members_tests.rs | 4 ++-- tests/team_members_tests.rs | 4 ++-- 26 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/api/current.rs b/src/api/current.rs index 877d4765..0c3b634e 100644 --- a/src/api/current.rs +++ b/src/api/current.rs @@ -21,7 +21,7 @@ impl<'octo> CurrentAuthHandler<'octo> { } /// Fetches information about the current user. - pub async fn user(&self) -> Result { + pub async fn user(&self) -> Result { self.crab.get("user", None::<&()>).await } diff --git a/src/api/issues.rs b/src/api/issues.rs index 172c7ed2..c0a757e0 100644 --- a/src/api/issues.rs +++ b/src/api/issues.rs @@ -304,7 +304,7 @@ impl<'octo, 'r> ListAssigneesBuilder<'octo, 'r> { } /// Send the actual request. - pub async fn send(self) -> Result> { + pub async fn send(self) -> Result> { let route = format!( "repos/{owner}/{repo}/assignees", owner = self.handler.owner, diff --git a/src/api/orgs/list_members.rs b/src/api/orgs/list_members.rs index 20f149d2..993ea367 100644 --- a/src/api/orgs/list_members.rs +++ b/src/api/orgs/list_members.rs @@ -31,7 +31,7 @@ impl<'octo, 'r> ListOrgMembersBuilder<'octo, 'r> { self } - pub async fn send(self) -> crate::Result> { + pub async fn send(self) -> crate::Result> { let url = format!("orgs/{org}/members", org = self.handler.owner); self.handler.crab.get(url, Some(&self)).await } diff --git a/src/api/repos.rs b/src/api/repos.rs index 74b44437..2cb3f7b5 100644 --- a/src/api/repos.rs +++ b/src/api/repos.rs @@ -210,7 +210,7 @@ impl<'octo> RepoHandler<'octo> { /// Creates a new file in the repository. /// ```no_run /// # async fn run() -> octocrab::Result<()> { - /// use octocrab::models::repos::GitUser; + /// use octocrab::models::repos::CommitAuthor; /// /// // Commit to add "crabs/ferris.txt" /// octocrab::instance() @@ -221,11 +221,11 @@ impl<'octo> RepoHandler<'octo> { /// "Thought thereā€™d never be a Rust Rap?\n" /// ) /// .branch("master") - /// .commiter(GitUser { + /// .commiter(CommitAuthor { /// name: "Octocat".to_string(), /// email: "octocat@github.com".to_string(), /// }) - /// .author(GitUser { + /// .author(CommitAuthor { /// name: "Ferris".to_string(), /// email: "ferris@rust-lang.org".to_string(), /// }) @@ -253,7 +253,7 @@ impl<'octo> RepoHandler<'octo> { /// ```no_run /// # async fn run() -> octocrab::Result<()> { /// # let blob_sha = ""; - /// use octocrab::models::repos::GitUser; + /// use octocrab::models::repos::CommitAuthor; /// /// // Given the file blob for "crabs/ferris.txt", commit to update the file. /// octocrab::instance() @@ -265,11 +265,11 @@ impl<'octo> RepoHandler<'octo> { /// blob_sha /// ) /// .branch("master") - /// .commiter(GitUser { + /// .commiter(CommitAuthor { /// name: "Octocat".to_string(), /// email: "octocat@github.com".to_string(), /// }) - /// .author(GitUser { + /// .author(CommitAuthor { /// name: "Ferris".to_string(), /// email: "ferris@rust-lang.org".to_string(), /// }) @@ -299,7 +299,7 @@ impl<'octo> RepoHandler<'octo> { /// ```no_run /// # async fn run() -> octocrab::Result<()> { /// # let blob_sha = ""; - /// use octocrab::models::repos::GitUser; + /// use octocrab::models::repos::CommitAuthor; /// /// // Commit to delete "crabs/ferris.txt" /// octocrab::instance() @@ -310,11 +310,11 @@ impl<'octo> RepoHandler<'octo> { /// blob_sha /// ) /// .branch("master") - /// .commiter(GitUser { + /// .commiter(CommitAuthor { /// name: "Octocat".to_string(), /// email: "octocat@github.com".to_string(), /// }) - /// .author(GitUser { + /// .author(CommitAuthor { /// name: "Ferris".to_string(), /// email: "ferris@rust-lang.org".to_string(), /// }) diff --git a/src/api/repos/file.rs b/src/api/repos/file.rs index 4ad8eed4..eeda0e7c 100644 --- a/src/api/repos/file.rs +++ b/src/api/repos/file.rs @@ -58,9 +58,9 @@ pub struct UpdateFileBuilder<'octo, 'r> { #[serde(skip_serializing_if = "Option::is_none")] branch: Option, #[serde(skip_serializing_if = "Option::is_none")] - commiter: Option, + commiter: Option, #[serde(skip_serializing_if = "Option::is_none")] - author: Option, + author: Option, } impl<'octo, 'r> UpdateFileBuilder<'octo, 'r> { @@ -90,13 +90,13 @@ impl<'octo, 'r> UpdateFileBuilder<'octo, 'r> { } /// The person that commited the file. - pub fn commiter(mut self, commiter: impl Into) -> Self { + pub fn commiter(mut self, commiter: impl Into) -> Self { self.commiter = Some(commiter.into()); self } /// The author of the file. - pub fn author(mut self, author: impl Into) -> Self { + pub fn author(mut self, author: impl Into) -> Self { self.author = Some(author.into()); self } @@ -124,9 +124,9 @@ pub struct DeleteFileBuilder<'octo, 'r> { #[serde(skip_serializing_if = "Option::is_none")] branch: Option, #[serde(skip_serializing_if = "Option::is_none")] - commiter: Option, + commiter: Option, #[serde(skip_serializing_if = "Option::is_none")] - author: Option, + author: Option, } impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> { @@ -154,13 +154,13 @@ impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> { } /// The person that commited the file. - pub fn commiter(mut self, commiter: impl Into) -> Self { + pub fn commiter(mut self, commiter: impl Into) -> Self { self.commiter = Some(commiter.into()); self } /// The author of the file. - pub fn author(mut self, author: impl Into) -> Self { + pub fn author(mut self, author: impl Into) -> Self { self.author = Some(author.into()); self } @@ -179,7 +179,7 @@ impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> { #[cfg(test)] mod tests { - use crate::models::repos::GitUser; + use crate::models::repos::CommitAuthor; #[test] fn serialize() { @@ -193,11 +193,11 @@ mod tests { "testsha", ) .branch("not-master") - .commiter(GitUser { + .commiter(CommitAuthor { name: "Octocat".to_string(), email: "octocat@github.com".to_string(), }) - .author(GitUser { + .author(CommitAuthor { name: "Ferris".to_string(), email: "ferris@rust-lang.org".to_string(), }); @@ -228,11 +228,11 @@ mod tests { let builder = repo .delete_file("tests/test.txt", "Update test.txt", "testsha") .branch("not-master") - .commiter(GitUser { + .commiter(CommitAuthor { name: "Octocat".to_string(), email: "octocat@github.com".to_string(), }) - .author(GitUser { + .author(CommitAuthor { name: "Ferris".to_string(), email: "ferris@rust-lang.org".to_string(), }); diff --git a/src/api/search.rs b/src/api/search.rs index cfb4de36..4f5625fb 100644 --- a/src/api/search.rs +++ b/src/api/search.rs @@ -70,7 +70,7 @@ impl<'octo> SearchHandler<'octo> { pub fn users<'query>( self, query: &'query (impl AsRef + ?Sized), - ) -> QueryHandler<'octo, 'query, models::User> { + ) -> QueryHandler<'octo, 'query, models::Author> { QueryHandler::new(self.crab, "users", query.as_ref()) } diff --git a/src/api/teams/members.rs b/src/api/teams/members.rs index 3d94a7fc..ce61b60e 100644 --- a/src/api/teams/members.rs +++ b/src/api/teams/members.rs @@ -36,7 +36,7 @@ impl<'octo, 'r> ListTeamMembersBuilder<'octo, 'r> { } /// Sends the actual request. - pub async fn send(self) -> Result> { + pub async fn send(self) -> Result> { let url = format!( "orgs/{org}/teams/{team}/members", org = self.handler.owner, diff --git a/src/lib.rs b/src/lib.rs index d01e38ac..fc698700 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ //! //! ```no_run //! # async fn run() -> octocrab::Result<()> { -//! let user: octocrab::models::User = octocrab::instance() +//! let user: octocrab::models::Author = octocrab::instance() //! .get("user", None::<&()>) //! .await?; //! # Ok(()) diff --git a/src/models.rs b/src/models.rs index 54d86d10..63237612 100644 --- a/src/models.rs +++ b/src/models.rs @@ -209,13 +209,13 @@ pub struct IssueEvent { pub node_id: Option, #[serde(skip_serializing_if = "Option::is_none")] pub url: Option, - pub actor: User, + pub actor: Author, #[serde(skip_serializing_if = "Option::is_none")] - pub assignee: Option, + pub assignee: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub assignees: Option>, + pub assignees: Option>, #[serde(skip_serializing_if = "Option::is_none")] - pub assigner: Option, + pub assigner: Option, #[serde(skip_serializing_if = "Option::is_none")] pub labels: Option>, #[serde(skip_serializing_if = "Option::is_none")] @@ -260,7 +260,7 @@ pub struct Project { pub number: u32, #[serde(skip_serializing_if = "Option::is_none")] pub state: Option, - pub creator: User, + pub creator: Author, pub created_at: DateTime, #[serde(skip_serializing_if = "Option::is_none")] pub updated_at: Option>, @@ -302,7 +302,7 @@ pub struct IssuePullRequest { #[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)] #[non_exhaustive] -pub struct User { +pub struct Author { pub login: String, pub id: UserId, pub node_id: String, @@ -327,7 +327,7 @@ pub struct User { #[non_exhaustive] pub struct StarGazer { pub starred_at: Option>, - pub user: Option, + pub user: Option, } #[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)] @@ -359,7 +359,7 @@ pub struct Milestone { #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub creator: Option, + pub creator: Option, #[serde(skip_serializing_if = "Option::is_none")] pub open_issues: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -383,7 +383,7 @@ pub struct Repository { #[serde(skip_serializing_if = "Option::is_none")] pub full_name: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub owner: Option, + pub owner: Option, #[serde(skip_serializing_if = "Option::is_none")] pub private: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -682,7 +682,7 @@ pub struct Status { pub updated_at: Option>, pub state: StatusState, #[serde(skip_serializing_if = "Option::is_none")] - pub creator: Option, + pub creator: Option, #[serde(skip_serializing_if = "Option::is_none")] pub context: Option, } @@ -709,7 +709,7 @@ pub struct InstallationRepositories { #[non_exhaustive] pub struct Installation { pub id: InstallationId, - pub account: User, + pub account: Author, #[serde(skip_serializing_if = "Option::is_none")] pub access_tokens_url: Option, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/models/apps.rs b/src/models/apps.rs index 21b109d0..ea07c557 100644 --- a/src/models/apps.rs +++ b/src/models/apps.rs @@ -8,7 +8,7 @@ pub struct App { #[serde(skip_serializing_if = "Option::is_none")] pub slug: Option, pub node_id: String, - pub owner: User, + pub owner: Author, pub name: String, #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, diff --git a/src/models/commits.rs b/src/models/commits.rs index 2c3b72ca..32202215 100644 --- a/src/models/commits.rs +++ b/src/models/commits.rs @@ -16,7 +16,7 @@ pub struct Comment { #[serde(skip_serializing_if = "Option::is_none")] pub line: Option, pub commit_id: String, - pub user: User, + pub user: Author, pub created_at: chrono::DateTime, #[serde(skip_serializing_if = "Option::is_none")] pub updated_at: Option>, diff --git a/src/models/events/payload.rs b/src/models/events/payload.rs index be322b4a..461366f9 100644 --- a/src/models/events/payload.rs +++ b/src/models/events/payload.rs @@ -12,7 +12,7 @@ mod pull_request_review_comment; mod push; mod workflow_run; -use crate::models::repos::GitUser; +use crate::models::repos::CommitAuthor; pub use commit_comment::*; pub use create::*; pub use delete::*; @@ -60,7 +60,7 @@ pub enum EventPayload { #[non_exhaustive] pub struct Commit { pub sha: String, - pub author: GitUser, + pub author: CommitAuthor, pub message: String, pub distinct: bool, pub url: Url, diff --git a/src/models/events/payload/issues.rs b/src/models/events/payload/issues.rs index 86fd07aa..6ce08929 100644 --- a/src/models/events/payload/issues.rs +++ b/src/models/events/payload/issues.rs @@ -1,4 +1,4 @@ -use crate::models::{issues::Issue, Label, User}; +use crate::models::{issues::Issue, Label, Author}; use serde::{Deserialize, Serialize}; /// The payload in a [`super::EventPayload::IssuesEvent`] type. @@ -15,7 +15,7 @@ pub struct IssuesEventPayload { /// /// Set when they type is [`IssuesEventAction::Assigned`] or /// [`IssuesEventAction::Unassigned`]. - pub assignee: Option, + pub assignee: Option, /// The optional label added or removed from the issue. /// /// Set when the type is [`IssuesEventAction::Labeled`] or diff --git a/src/models/events/payload/member.rs b/src/models/events/payload/member.rs index cc5a2c85..eec764bf 100644 --- a/src/models/events/payload/member.rs +++ b/src/models/events/payload/member.rs @@ -1,4 +1,4 @@ -use crate::models::User; +use crate::models::Author; use serde::{Deserialize, Serialize}; /// The payload in a [`super::EventPayload::MemberEvent`] type. @@ -8,7 +8,7 @@ pub struct MemberEventPayload { /// The action this event represents. pub action: MemberEventAction, /// The user this event corresponds to. - pub member: User, + pub member: Author, /// Only available on webhooks. /// /// Changes to the collaborator permissions. diff --git a/src/models/events/payload/push.rs b/src/models/events/payload/push.rs index 1179547f..3812b346 100644 --- a/src/models/events/payload/push.rs +++ b/src/models/events/payload/push.rs @@ -20,7 +20,7 @@ pub struct PushEventPayload { mod test { use crate::models::{ events::{payload::EventPayload, Event}, - repos::GitUser, + repos::CommitAuthor, }; use reqwest::Url; @@ -43,7 +43,7 @@ mod test { assert_eq!(commit.sha, "eb1a60c03544dcea290f2d57bb66ae188ce25778"); assert_eq!( commit.author, - GitUser { + CommitAuthor { name: "readme-bot".to_string(), email: "readme-bot@example.com".to_string() } diff --git a/src/models/events/payload/workflow_run.rs b/src/models/events/payload/workflow_run.rs index 970b42d3..34fc570a 100644 --- a/src/models/events/payload/workflow_run.rs +++ b/src/models/events/payload/workflow_run.rs @@ -1,7 +1,7 @@ use crate::models::{ orgs::Organization, workflows::{Run, WorkFlow}, - Repository, User, + Repository, Author, }; use serde::{Deserialize, Serialize}; @@ -14,7 +14,7 @@ pub struct WorkflowRunEventPayload { pub workflow: WorkFlow, pub organization: Option, pub repository: Repository, - pub sender: User, + pub sender: Author, } /// The action on a pull request this event corresponds to. diff --git a/src/models/gists.rs b/src/models/gists.rs index 3c5073f1..76262171 100644 --- a/src/models/gists.rs +++ b/src/models/gists.rs @@ -35,7 +35,7 @@ pub struct GistFile { #[non_exhaustive] #[derive(Debug, Deserialize)] pub struct GistCommit { - pub user: Option, + pub user: Option, pub version: String, pub committed_at: DateTime, pub change_status: GistChangeStatus, diff --git a/src/models/issues.rs b/src/models/issues.rs index d4371bbd..3197030d 100644 --- a/src/models/issues.rs +++ b/src/models/issues.rs @@ -20,11 +20,11 @@ pub struct Issue { pub body_text: Option, #[serde(skip_serializing_if = "Option::is_none")] pub body_html: Option, - pub user: User, + pub user: Author, pub labels: Vec