Skip to content

Commit

Permalink
Fix the model for parsing responses to a specific commit sha (#302)
Browse files Browse the repository at this point in the history
* 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 :')
  • Loading branch information
atilag authored Mar 29, 2023
1 parent 52c4521 commit a546dcf
Show file tree
Hide file tree
Showing 26 changed files with 82 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/api/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'octo> CurrentAuthHandler<'octo> {
}

/// Fetches information about the current user.
pub async fn user(&self) -> Result<models::User> {
pub async fn user(&self) -> Result<models::Author> {
self.crab.get("user", None::<&()>).await
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<'octo, 'r> ListAssigneesBuilder<'octo, 'r> {
}

/// Send the actual request.
pub async fn send(self) -> Result<crate::Page<models::User>> {
pub async fn send(self) -> Result<crate::Page<models::Author>> {
let route = format!(
"repos/{owner}/{repo}/assignees",
owner = self.handler.owner,
Expand Down
2 changes: 1 addition & 1 deletion src/api/orgs/list_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'octo, 'r> ListOrgMembersBuilder<'octo, 'r> {
self
}

pub async fn send(self) -> crate::Result<crate::Page<crate::models::User>> {
pub async fn send(self) -> crate::Result<crate::Page<crate::models::Author>> {
let url = format!("orgs/{org}/members", org = self.handler.owner);
self.handler.crab.get(url, Some(&self)).await
}
Expand Down
18 changes: 9 additions & 9 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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: "[email protected]".to_string(),
/// })
/// .author(GitUser {
/// .author(CommitAuthor {
/// name: "Ferris".to_string(),
/// email: "[email protected]".to_string(),
/// })
Expand Down Expand Up @@ -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()
Expand All @@ -265,11 +265,11 @@ impl<'octo> RepoHandler<'octo> {
/// blob_sha
/// )
/// .branch("master")
/// .commiter(GitUser {
/// .commiter(CommitAuthor {
/// name: "Octocat".to_string(),
/// email: "[email protected]".to_string(),
/// })
/// .author(GitUser {
/// .author(CommitAuthor {
/// name: "Ferris".to_string(),
/// email: "[email protected]".to_string(),
/// })
Expand Down Expand Up @@ -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()
Expand All @@ -310,11 +310,11 @@ impl<'octo> RepoHandler<'octo> {
/// blob_sha
/// )
/// .branch("master")
/// .commiter(GitUser {
/// .commiter(CommitAuthor {
/// name: "Octocat".to_string(),
/// email: "[email protected]".to_string(),
/// })
/// .author(GitUser {
/// .author(CommitAuthor {
/// name: "Ferris".to_string(),
/// email: "[email protected]".to_string(),
/// })
Expand Down
26 changes: 13 additions & 13 deletions src/api/repos/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub struct UpdateFileBuilder<'octo, 'r> {
#[serde(skip_serializing_if = "Option::is_none")]
branch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
commiter: Option<models::repos::GitUser>,
commiter: Option<models::repos::CommitAuthor>,
#[serde(skip_serializing_if = "Option::is_none")]
author: Option<models::repos::GitUser>,
author: Option<models::repos::CommitAuthor>,
}

impl<'octo, 'r> UpdateFileBuilder<'octo, 'r> {
Expand Down Expand Up @@ -90,13 +90,13 @@ impl<'octo, 'r> UpdateFileBuilder<'octo, 'r> {
}

/// The person that commited the file.
pub fn commiter(mut self, commiter: impl Into<models::repos::GitUser>) -> Self {
pub fn commiter(mut self, commiter: impl Into<models::repos::CommitAuthor>) -> Self {
self.commiter = Some(commiter.into());
self
}

/// The author of the file.
pub fn author(mut self, author: impl Into<models::repos::GitUser>) -> Self {
pub fn author(mut self, author: impl Into<models::repos::CommitAuthor>) -> Self {
self.author = Some(author.into());
self
}
Expand Down Expand Up @@ -124,9 +124,9 @@ pub struct DeleteFileBuilder<'octo, 'r> {
#[serde(skip_serializing_if = "Option::is_none")]
branch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
commiter: Option<models::repos::GitUser>,
commiter: Option<models::repos::CommitAuthor>,
#[serde(skip_serializing_if = "Option::is_none")]
author: Option<models::repos::GitUser>,
author: Option<models::repos::CommitAuthor>,
}

impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> {
Expand Down Expand Up @@ -154,13 +154,13 @@ impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> {
}

/// The person that commited the file.
pub fn commiter(mut self, commiter: impl Into<models::repos::GitUser>) -> Self {
pub fn commiter(mut self, commiter: impl Into<models::repos::CommitAuthor>) -> Self {
self.commiter = Some(commiter.into());
self
}

/// The author of the file.
pub fn author(mut self, author: impl Into<models::repos::GitUser>) -> Self {
pub fn author(mut self, author: impl Into<models::repos::CommitAuthor>) -> Self {
self.author = Some(author.into());
self
}
Expand All @@ -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() {
Expand All @@ -193,11 +193,11 @@ mod tests {
"testsha",
)
.branch("not-master")
.commiter(GitUser {
.commiter(CommitAuthor {
name: "Octocat".to_string(),
email: "[email protected]".to_string(),
})
.author(GitUser {
.author(CommitAuthor {
name: "Ferris".to_string(),
email: "[email protected]".to_string(),
});
Expand Down Expand Up @@ -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: "[email protected]".to_string(),
})
.author(GitUser {
.author(CommitAuthor {
name: "Ferris".to_string(),
email: "[email protected]".to_string(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/api/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'octo> SearchHandler<'octo> {
pub fn users<'query>(
self,
query: &'query (impl AsRef<str> + ?Sized),
) -> QueryHandler<'octo, 'query, models::User> {
) -> QueryHandler<'octo, 'query, models::Author> {
QueryHandler::new(self.crab, "users", query.as_ref())
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/teams/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'octo, 'r> ListTeamMembersBuilder<'octo, 'r> {
}

/// Sends the actual request.
pub async fn send(self) -> Result<Page<models::User>> {
pub async fn send(self) -> Result<Page<models::Author>> {
let url = format!(
"orgs/{org}/teams/{team}/members",
org = self.handler.owner,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
22 changes: 11 additions & 11 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ pub struct IssueEvent {
pub node_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
pub actor: User,
pub actor: Author,
#[serde(skip_serializing_if = "Option::is_none")]
pub assignee: Option<User>,
pub assignee: Option<Author>,
#[serde(skip_serializing_if = "Option::is_none")]
pub assignees: Option<Vec<User>>,
pub assignees: Option<Vec<Author>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub assigner: Option<User>,
pub assigner: Option<Author>,
#[serde(skip_serializing_if = "Option::is_none")]
pub labels: Option<Vec<Label>>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -260,7 +260,7 @@ pub struct Project {
pub number: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<String>,
pub creator: User,
pub creator: Author,
pub created_at: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_at: Option<DateTime<Utc>>,
Expand Down Expand Up @@ -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,
Expand All @@ -327,7 +327,7 @@ pub struct User {
#[non_exhaustive]
pub struct StarGazer {
pub starred_at: Option<DateTime<Utc>>,
pub user: Option<User>,
pub user: Option<Author>,
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -359,7 +359,7 @@ pub struct Milestone {
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub creator: Option<User>,
pub creator: Option<Author>,
#[serde(skip_serializing_if = "Option::is_none")]
pub open_issues: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -383,7 +383,7 @@ pub struct Repository {
#[serde(skip_serializing_if = "Option::is_none")]
pub full_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owner: Option<User>,
pub owner: Option<Author>,
#[serde(skip_serializing_if = "Option::is_none")]
pub private: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -682,7 +682,7 @@ pub struct Status {
pub updated_at: Option<DateTime<Utc>>,
pub state: StatusState,
#[serde(skip_serializing_if = "Option::is_none")]
pub creator: Option<User>,
pub creator: Option<Author>,
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<String>,
}
Expand All @@ -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<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion src/models/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct App {
#[serde(skip_serializing_if = "Option::is_none")]
pub slug: Option<String>,
pub node_id: String,
pub owner: User,
pub owner: Author,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/models/commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Comment {
#[serde(skip_serializing_if = "Option::is_none")]
pub line: Option<u64>,
pub commit_id: String,
pub user: User,
pub user: Author,
pub created_at: chrono::DateTime<chrono::Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
Expand Down
4 changes: 2 additions & 2 deletions src/models/events/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/models/events/payload/issues.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,7 +15,7 @@ pub struct IssuesEventPayload {
///
/// Set when they type is [`IssuesEventAction::Assigned`] or
/// [`IssuesEventAction::Unassigned`].
pub assignee: Option<User>,
pub assignee: Option<Author>,
/// The optional label added or removed from the issue.
///
/// Set when the type is [`IssuesEventAction::Labeled`] or
Expand Down
4 changes: 2 additions & 2 deletions src/models/events/payload/member.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::User;
use crate::models::Author;
use serde::{Deserialize, Serialize};

/// The payload in a [`super::EventPayload::MemberEvent`] type.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/models/events/payload/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct PushEventPayload {
mod test {
use crate::models::{
events::{payload::EventPayload, Event},
repos::GitUser,
repos::CommitAuthor,
};
use reqwest::Url;

Expand All @@ -43,7 +43,7 @@ mod test {
assert_eq!(commit.sha, "eb1a60c03544dcea290f2d57bb66ae188ce25778");
assert_eq!(
commit.author,
GitUser {
CommitAuthor {
name: "readme-bot".to_string(),
email: "[email protected]".to_string()
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/events/payload/workflow_run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::{
orgs::Organization,
workflows::{Run, WorkFlow},
Repository, User,
Repository, Author,
};
use serde::{Deserialize, Serialize};

Expand All @@ -14,7 +14,7 @@ pub struct WorkflowRunEventPayload {
pub workflow: WorkFlow,
pub organization: Option<Organization>,
pub repository: Repository,
pub sender: User,
pub sender: Author,
}

/// The action on a pull request this event corresponds to.
Expand Down
Loading

0 comments on commit a546dcf

Please sign in to comment.