Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #8

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[![Crates.io](https://img.shields.io/crates/d/codecov)](https://crates.io/crates/codecov)
[![codecov](https://codecov.io/gh/kitsuyui/rust-codecov/branch/main/graph/badge.svg?token=0OM9KWFZQC)](https://codecov.io/gh/kitsuyui/rust-codecov)
[![crates.io](https://img.shields.io/crates/v/codecov.svg)](https://crates.io/crates/codecov)
[![docs.rs](https://docs.rs/codecov/badge.svg)](https://docs.rs/codecov)
[![License: BSD-3-Clause](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)

## Description

Expand All @@ -12,16 +14,20 @@ https://docs.codecov.com/reference/overview
## Usage

```rust
use codecov::{Client, Owner};

// let client = Client::new("1234-5678-9012-3456");
let client = Client::new_from_env(); // Read from CODECOV_OWNER_TOKEN
let owner = Owner {
service: "github".to_string(),
username: "codecov".to_string(),
};
let repos = client.get_all_repos(&owner).unwrap();
println!("{:?}", repos)
use codecov::{Client, owner::Owner};

fn main() {
// let client = Client::new("1234-5678-9012-3456"); // Set token directly
let client = Client::new_from_env().unwrap(); // Read CODECOV_OWNER_TOKEN from environment variable
let owner = Owner::new("github", "kitsuyui");
let repos = client.get_all_repos(&owner).unwrap();
println!("{:?}", repos);

let author = owner.new_author("rust-codecov");
let repo_detail = client.get_branch_detail(&author, "main").unwrap();
println!("{:?}", repo_detail);
println!("{}", repo_detail.latest_coverage());
}
```

## LICENSE
Expand Down
21 changes: 21 additions & 0 deletions src/author.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::owner::Owner;

/**
* Author is a struct that represents the author of a repo.
*/
Expand All @@ -8,3 +11,21 @@ pub struct Author {
pub username: String,
pub name: String,
}

impl Author {
pub fn new(service: &str, username: &str, name: &str) -> Author {
Author {
service: service.to_string(),
username: username.to_string(),
name: name.to_string(),
}
}

pub fn from_owner(owner: &Owner, name: &str) -> Author {
Self::new(&owner.service, &owner.username, name)
}

pub fn to_owner(&self) -> Owner {
Owner::new(&self.service, &self.username)
}
}
9 changes: 9 additions & 0 deletions src/branch_detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ pub struct File {
pub name: String,
pub totals: Totals,
}

impl BranchDetailAPIResponse {
/**
* Returns the latest coverage for a branch.
*/
pub fn latest_coverage(&self) -> f64 {
self.head_commit.totals.coverage
}
}
59 changes: 26 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod branch_detail;
pub mod branches;
pub mod commits;
pub mod errors;
pub mod owner;
pub mod repos;
pub mod totals;
pub mod url;
Expand All @@ -11,18 +12,27 @@ use author::Author;
use crate::errors::Error;

/**
* Client is a struct that represents a client to the Codecov API.
```
use codecov::{Client, owner::Owner};

// let client = Client::new("1234-5678-9012-3456"); // Set token directly
let client = Client::new_from_env().unwrap(); // Read CODECOV_OWNER_TOKEN from environment variable
let owner = Owner::new("github", "kitsuyui");
let repos = client.get_all_repos(&owner).unwrap();
println!("{:?}", repos);

let author = owner.new_author("rust-codecov");
let repo_detail = client.get_branch_detail(&author, "main").unwrap();
println!("{:?}", repo_detail);
println!("{}", repo_detail.latest_coverage());
```
*/
pub struct Client {
token: String,
}

/**
* Owner is a struct that represents an owner of repos.
* Client is a struct that represents a client to the Codecov API.
*/
pub struct Owner {
service: String,
username: String,
pub struct Client {
token: String,
}

impl Client {
Expand All @@ -42,16 +52,13 @@ impl Client {
format!("bearer {}", self.token)
}

fn owner_endpoint(&self, owner: &Owner) -> String {
fn owner_endpoint(&self, owner: &owner::Owner) -> String {
let api_endpoint = "https://codecov.io/api/v2";
format!("{}/{}/{}", api_endpoint, owner.service, owner.username)
}

fn repos_endpoint(&self, author: &author::Author) -> String {
let owner_endpoint = self.owner_endpoint(&Owner {
service: author.service.clone(),
username: author.username.clone(),
});
let owner_endpoint = self.owner_endpoint(&author.to_owner());
format!("{}/repos/{}", owner_endpoint, author.name)
}

Expand All @@ -60,7 +67,7 @@ impl Client {
* /repos endpoint returns a list of repos for a given owner with pagination.
* This function will make multiple requests to get all repos.
*/
pub fn get_all_repos(&self, owner: &Owner) -> Result<Vec<repos::Repo>, Error> {
pub fn get_all_repos(&self, owner: &owner::Owner) -> Result<Vec<repos::Repo>, Error> {
let mut repos = Vec::new();
let mut url = format!("{}/repos?page_size=100", self.owner_endpoint(owner));
loop {
Expand Down Expand Up @@ -178,48 +185,34 @@ mod tests {
#[test]
fn test_get_all_repos() {
let client = Client::new_from_env().unwrap();
let owner = Owner {
service: "github".to_string(),
username: "codecov".to_string(),
};
let owner = owner::Owner::new("github", "codecov");
let repos = client.get_all_repos(&owner).unwrap();
assert!(!repos.is_empty());
}

#[test]
fn test_get_commits() {
let client = Client::new_from_env().unwrap();
let author = author::Author {
service: "github".to_string(),
username: "codecov".to_string(),
name: "codecov-demo".to_string(),
};
let author = author::Author::new("github", "codecov", "codecov-demo");
let commits = client.get_commits(&author).unwrap();
assert!(!commits.results.is_empty());
}

#[test]
fn test_get_branches() {
let client = Client::new_from_env().unwrap();
let author = author::Author {
service: "github".to_string(),
username: "codecov".to_string(),
name: "codecov-demo".to_string(),
};
let author = author::Author::new("github", "codecov", "codecov-demo");
let branches = client.get_branches(&author).unwrap();
assert!(!branches.results.is_empty());
}

#[test]
fn test_get_branch_detail() {
let client = Client::new_from_env().unwrap();
let author = author::Author {
service: "github".to_string(),
username: "codecov".to_string(),
name: "codecov-demo".to_string(),
};
let author = author::Author::new("github", "codecov", "codecov-demo");
let branch_name = "main";
let branch_detail = client.get_branch_detail(&author, branch_name).unwrap();
assert_eq!(branch_detail.name, branch_name);
assert!(branch_detail.latest_coverage() > 0.0)
}
}
24 changes: 24 additions & 0 deletions src/owner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::author::Author;
/**
* Owner is a struct that represents an owner of repos.
*/
pub struct Owner {
pub service: String,
pub username: String,
}

impl Owner {
pub fn new(service: &str, username: &str) -> Owner {
Owner {
service: service.to_string(),
username: username.to_string(),
}
}

/**
* Returns a new Author for a given repo name.
*/
pub fn new_author(&self, name: &str) -> Author {
Author::from_owner(self, name)
}
}