Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
havontuur authored and dmgorsky committed Jun 14, 2024
1 parent b70c087 commit bf4ff4d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/models/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub struct RepoCommit {
#[non_exhaustive]
pub struct RepoCommitPage {
pub url: Url,
pub author: Option<GitUserTime>,
pub committer: Option<GitUserTime>,
pub author: Option<CommitAuthor>,
pub committer: Option<CommitAuthor>,
pub message: String,
pub comment_count: u64,
pub tree: CommitObject,
Expand Down
80 changes: 80 additions & 0 deletions tests/repos_commit_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
mod mock_error;

use mock_error::setup_error_handler;
use octocrab::{models::repos::RepoCommit, Octocrab};
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

async fn setup_repos_commits_api(template: ResponseTemplate) -> MockServer {
let owner: &str = "org";
let repo: &str = "some-repo";

let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path(format!("/repos/{owner}/{repo}/commits")))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("POST on /repos/{owner}/{repo}/commits was not received"),
)
.await;
mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}

const OWNER: &str = "org";
const REPO: &str = "some-repo";

#[tokio::test]
async fn should_return_list_of_commits() {
let repos_list_commits_json: Vec::<RepoCommit> =
serde_json::from_str(include_str!("resources/repos_list_commits.json")).unwrap();
let template = ResponseTemplate::new(201).set_body_json(&repos_list_commits_json);
let mock_server = setup_repos_commits_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client
.repos(OWNER.to_owned(), REPO.to_owned())
.list_commits()
.send()
.await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);

let result = result.unwrap();
let result = result.items;

assert!(
result.len() == 1,
"expected '1' for len(), got {:#?}",
result.len()
);

result.iter().for_each(|commit| {
println!("Commit = {:#?}", commit);
assert!(
commit.sha == "24606b5f326a1356f031dd06431cfb0beddd475f",
"expected '24606b5f326a1356f031dd06431cfb0beddd475f' value, got {:#?}",
commit.sha
);

assert!(
commit.commit.author.as_ref().unwrap().date.unwrap().to_rfc3339() == "2024-04-30T08:54:10+00:00",
"expected '2024-04-30T08:54:10+00:00' value, got {:#?}",
commit.commit.author.as_ref().unwrap().date.unwrap().to_rfc3339()
);
});
}
83 changes: 83 additions & 0 deletions tests/resources/repos_list_commits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[
{
"url": "https://api.github.com/repos/owner/my_repo/commits/24606b5f326a1356f031dd06431cfb0beddd475f",
"sha": "24606b5f326a1356f031dd06431cfb0beddd475f",
"node_id": "C_kwDOExNuVtoAKDIzNjA2YjVmMzI2YTEzNTZmMDMxZGQwNjQzMWNmYjBiZWRkZDQ3NWY",
"html_url": "https://github.com/owner/my_repo/commit/24606b5f326a1356f031dd06431cfb0beddd475f",
"comments_url": "https://api.github.com/repos/owner/my_repo/commits/24606b5f326a1356f031dd06431cfb0beddd475f/comments",
"commit": {
"url": "https://api.github.com/repos/owner/my_repo/git/commits/24606b5f326a1356f031dd06431cfb0beddd475f",
"author": {
"name": "First Lastname",
"email": "first.last@orgcom",
"date": "2024-04-30T08:54:10Z"
},
"committer": {
"name": "GitHub",
"email": "noreply@github.com",
"date": "2024-04-30T08:54:10Z"
},
"message": "Improved logs (#21717)",
"comment_count": 0,
"tree": {
"sha": "cadf65e5b5e1ba2b6293e5f95cb3012868b82082",
"url": "https://api.github.com/repos/owner/my_repo/git/trees/cadf65e5b5e1ba2b6293e5f95cb3012868b82082"
},
"verification": {
"verified": true,
"reason": "valid",
"payload": "tree cadf65e5b5e1ba2b6293e5f95cb3012868b82082\nparent 7cf97159fe31b0e011bbb24109b5e10d278f551f\nauthor First Samuel Leal Lastname first.Lastname@shell.com1714467250 +0200\ncommitter GitHubnoreply@github.com 1714467250 +0200\n\nImproved logs related to GetActiveVersionedContainer Error (#21717)\n\n",
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsFcBAABCAAQBQJnNLGyCRD1/dR8FrJdSOVEqr39d3HZaUPGvI/WyKJKZhFkjI/aMk\nOd2QBPCn/QE51VqkzxNy\n=Cjql\n-----END PGP SIGNATURE-----\n"
}
},
"author": {
"login": "flastname",
"id": 8143842,
"node_id": "MDQ6VXNlcjkxNDI4NDA=",
"avatar_url": "https://avatars.githubusercontent.com/u/8143842?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/flastname",
"html_url": "https://github.com/flastname",
"followers_url": "https://api.github.com/users/flastname/followers",
"following_url": "https://api.github.com/users/flastname/following%7B/other_user",
"gists_url": "https://api.github.com/users/flastname/gists%7B/gist_id",
"starred_url": "https://api.github.com/users/flastname/starred%7B/owner%7B/repo",
"subscriptions_url": "https://api.github.com/users/flastname/subscriptions",
"organizations_url": "https://api.github.com/users/flastname/orgs",
"repos_url": "https://api.github.com/users/flastname/repos",
"events_url": "https://api.github.com/users/flastname/events%7B/privacy",
"received_events_url": "https://api.github.com/users/flastname/received_events",
"type": "User",
"site_admin": false,
"patch_url": null
},
"committer": {
"login": "web-flow",
"id": 29874547,
"node_id": "MDQ6VXNlcjE5ODY0NDQ3",
"avatar_url": "https://avatars.githubusercontent.com/u/19864447?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/web-flow",
"html_url": "https://github.com/web-flow",
"followers_url": "https://api.github.com/users/web-flow/followers",
"following_url": "https://api.github.com/users/web-flow/following%7B/other_user",
"gists_url": "https://api.github.com/users/web-flow/gists%7B/gist_id",
"starred_url": "https://api.github.com/users/web-flow/starred%7B/owner%7B/repo",
"subscriptions_url": "https://api.github.com/users/web-flow/subscriptions",
"organizations_url": "https://api.github.com/users/web-flow/orgs",
"repos_url": "https://api.github.com/users/web-flow/repos",
"events_url": "https://api.github.com/users/web-flow/events%7B/privacy",
"received_events_url": "https://api.github.com/users/web-flow/received_events",
"type": "User",
"site_admin": false,
"patch_url": null
},
"parents": [
{
"url": "https://api.github.com/repos/owner/my_repo/commits/7cf97159fe31b0e011bbb24109b5e10d278f551f",
"sha": "7cf97159fe31b0e011bbb24109b5e10d278f551f",
"html_url": "https://github.com/owner/my_repo/commit/7cf97159fe31b0e011bbb24109b5e10d278f551f"
}
]
}
]

0 comments on commit bf4ff4d

Please sign in to comment.