Skip to content

Commit

Permalink
Improve AtomGit unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao6635 committed Feb 27, 2024
1 parent 032f2a4 commit e109b54
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 89 deletions.
20 changes: 10 additions & 10 deletions src/scm/driver/atomgit/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use super::constants::ATOMGIT_PATH_CONTENTS;
use crate::http::{Client, Endpoint};
use crate::scm::content::{Content, ContentService, File};

pub struct AtomgitContentService {
pub struct AtomGitContentService {
pub client: Client,
}

impl ContentService for AtomgitContentService {
impl ContentService for AtomGitContentService {
/// Gets the contents of a file in a repository.
///
/// Docs: https://docs.atomgit.com/en/openAPI/api_versioned/get-repo-conent/
Expand Down Expand Up @@ -64,7 +64,7 @@ impl ContentService for AtomgitContentService {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitContent {
pub struct AtomGitContent {
pub name: String,
pub path: String,
pub sha: String,
Expand All @@ -73,10 +73,10 @@ pub struct AtomgitContent {
pub kind: String,
}

impl TryFrom<AtomgitContent> for Content {
impl TryFrom<AtomGitContent> for Content {
type Error = data_encoding::DecodeError;

fn try_from(val: AtomgitContent) -> Result<Self, Self::Error> {
fn try_from(val: AtomGitContent) -> Result<Self, Self::Error> {
Ok(Self {
path: val.path,
data: BASE64.decode(val.content.as_bytes())?,
Expand All @@ -90,21 +90,21 @@ impl TryFrom<AtomgitContent> for Content {
struct GithubContentEndpoint;

impl Endpoint for GithubContentEndpoint {
type Output = AtomgitContent;
type Output = AtomGitContent;
}

/// represents a file in a repository.
#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitFile {
pub struct AtomGitFile {
pub name: String,
pub path: String,
pub sha: String,
#[serde(rename = "type")]
pub kind: String,
}

impl From<&AtomgitFile> for File {
fn from(val: &AtomgitFile) -> Self {
impl From<&AtomGitFile> for File {
fn from(val: &AtomGitFile) -> Self {
Self {
name: val.name.clone(),
path: val.path.clone(),
Expand All @@ -118,5 +118,5 @@ impl From<&AtomgitFile> for File {
struct GithubFileEndpoint;

impl Endpoint for GithubFileEndpoint {
type Output = Vec<AtomgitFile>;
type Output = Vec<AtomGitFile>;
}
12 changes: 6 additions & 6 deletions src/scm/driver/atomgit/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::content::AtomgitContentService;
use super::content::AtomGitContentService;
use super::git::AtomGitService;
use super::repo::AtomgitRepoService;
use super::repo::AtomGitRepoService;
use crate::http::Client;
use crate::scm::content::ContentService;
use crate::scm::driver::DriverTrait;
use crate::scm::git::GitService;
use crate::scm::repo::RepositoryService;

pub struct AtomgitDriver {
pub struct AtomGitDriver {
pub client: Client,
}

impl DriverTrait for AtomgitDriver {
impl DriverTrait for AtomGitDriver {
fn contents(&self) -> Box<dyn ContentService> {
Box::new(AtomgitContentService {
Box::new(AtomGitContentService {
client: self.client.clone(),
})
}
Expand All @@ -39,7 +39,7 @@ impl DriverTrait for AtomgitDriver {
}

fn repositories(&self) -> Box<dyn RepositoryService> {
Box::new(AtomgitRepoService {
Box::new(AtomGitRepoService {
client: self.client.clone(),
})
}
Expand Down
84 changes: 42 additions & 42 deletions src/scm/driver/atomgit/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::constants::{
ATOMGIT_PATH_BRANCHES, ATOMGIT_PATH_COMMITS, ATOMGIT_PATH_GIT_TREES, ATOMGIT_PATH_TAGS,
};
use super::utils::convert_list_options;
use super::AtomgitFile;
use super::AtomGitFile;
use crate::http::{Client, Endpoint};
use crate::scm::client::ListOptions;
use crate::scm::git::{Commit, GitService, Reference, Signature, Tree, TreeEntry};
Expand All @@ -37,7 +37,7 @@ impl GitService for AtomGitService {
fn list_branches(&self, repo: &str, opts: ListOptions) -> anyhow::Result<Vec<Reference>> {
let path = ATOMGIT_PATH_BRANCHES.replace("{repo}", repo);
let options = Some(convert_list_options(opts));
let res = self.client.get::<AtomgitBranchesEndpoint>(&path, options)?;
let res = self.client.get::<AtomGitBranchesEndpoint>(&path, options)?;

if let Some(branches) = res.data {
return Ok(branches.iter().map(|v| v.into()).collect());
Expand All @@ -53,7 +53,7 @@ impl GitService for AtomGitService {
fn list_tags(&self, repo: &str, opts: ListOptions) -> anyhow::Result<Vec<Reference>> {
let path = ATOMGIT_PATH_TAGS.replace("{repo}", repo);
let options = Some(convert_list_options(opts));
let res = self.client.get::<AtomgitBranchesEndpoint>(&path, options)?;
let res = self.client.get::<AtomGitBranchesEndpoint>(&path, options)?;

if let Some(tags) = res.data {
return Ok(tags.iter().map(|v| v.into()).collect());
Expand All @@ -70,7 +70,7 @@ impl GitService for AtomGitService {
let path = ATOMGIT_PATH_COMMITS
.replace("{repo}", repo)
.replace("{reference}", reference);
let res = self.client.get::<AtomgitCommitEndpoint>(&path, None)?;
let res = self.client.get::<AtomGitCommitEndpoint>(&path, None)?;

Ok(res.data.map(|v| v.into()))
}
Expand All @@ -86,7 +86,7 @@ impl GitService for AtomGitService {
let options = recursive
.map(|r| Some(HashMap::from([("recursive".to_string(), r.to_string())])))
.unwrap_or_default();
let res = self.client.get::<AtomgitTreeEndpoint>(&path, options)?;
let res = self.client.get::<AtomGitTreeEndpoint>(&path, options)?;
let option = res.data.unwrap();
let tree = Tree {
tree: option,
Expand All @@ -97,14 +97,14 @@ impl GitService for AtomGitService {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitBranch {
pub struct AtomGitBranch {
pub name: String,
pub commit: AtomgitSimpleCommit,
pub commit: AtomGitSimpleCommit,
pub protected: bool,
}

impl From<&AtomgitBranch> for Reference {
fn from(val: &AtomgitBranch) -> Self {
impl From<&AtomGitBranch> for Reference {
fn from(val: &AtomGitBranch) -> Self {
Self {
name: utils::trim_ref(&val.name),
path: utils::expand_ref(&val.name, "refs/heads/"),
Expand All @@ -114,86 +114,86 @@ impl From<&AtomgitBranch> for Reference {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitSimpleCommit {
pub struct AtomGitSimpleCommit {
pub sha: String,
pub url: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitCommit {
pub struct AtomGitCommit {
pub sha: String,
pub html_url: Option<String>,
pub commit: AtomgitCommitObject,
pub author: Option<AtomgitAuthor>,
pub committer: Option<AtomgitAuthor>,
pub files: Vec<AtomgitFile>,
pub commit: AtomGitCommitObject,
pub author: Option<AtomGitAuthor>,
pub committer: Option<AtomGitAuthor>,
pub files: Vec<AtomGitFile>,
}

impl From<AtomgitCommit> for Commit {
fn from(val: AtomgitCommit) -> Self {
impl From<AtomGitCommit> for Commit {
fn from(val: AtomGitCommit) -> Self {
Self {
sha: val.sha,
message: val.commit.message.unwrap_or_default(),
author: Signature {
name: val.commit.author.name,
email: val.commit.author.email,
date: val.commit.author.date,
login: Some(val.author.unwrap_or_default().login),
avatar: Some(val.author.unwrap_or_default().avatar_url),
login: Some(val.author.clone().unwrap_or_default().login),
avatar: Some(val.author.clone().unwrap_or_default().avatar_url),
},
committer: Signature {
name: val.commit.committer.name,
email: val.commit.committer.email,
date: val.commit.committer.date,
login: Some(val.committer.unwrap_or_default().login),
avatar: Some(val.committer.unwrap_or_default().avatar_url),
login: Some(val.committer.clone().unwrap_or_default().login),
avatar: Some(val.committer.clone().unwrap_or_default().avatar_url),
},
link: val.html_url.unwrap_or_default(),
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitCommitObject {
pub author: AtomgitCommitObjectAuthor,
pub committer: AtomgitCommitObjectAuthor,
pub struct AtomGitCommitObject {
pub author: AtomGitCommitObjectAuthor,
pub committer: AtomGitCommitObjectAuthor,
pub message: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitCommitObjectAuthor {
pub struct AtomGitCommitObjectAuthor {
pub name: String,
pub email: String,
pub date: String,
}

#[derive(Debug, Deserialize, Serialize, Default)]
pub struct AtomgitAuthor {
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct AtomGitAuthor {
pub avatar_url: String,
pub login: String,
}

struct AtomgitBranchesEndpoint;
struct AtomGitBranchesEndpoint;

impl Endpoint for AtomgitBranchesEndpoint {
type Output = Vec<AtomgitBranch>;
impl Endpoint for AtomGitBranchesEndpoint {
type Output = Vec<AtomGitBranch>;
}

struct AtomgitCommitEndpoint;
struct AtomGitCommitEndpoint;

impl Endpoint for AtomgitCommitEndpoint {
type Output = AtomgitCommit;
impl Endpoint for AtomGitCommitEndpoint {
type Output = AtomGitCommit;
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitTree {
pub struct AtomGitTree {
pub sha: String,
pub tree: Vec<AtomgitTreeEntry>,
pub tree: Vec<AtomGitTreeEntry>,
pub truncated: bool,
}

impl From<AtomgitTree> for Tree {
fn from(val: AtomgitTree) -> Self {
impl From<AtomGitTree> for Tree {
fn from(val: AtomGitTree) -> Self {
Self {
sha: val.sha,
tree: val.tree.iter().map(|v| v.into()).collect(),
Expand All @@ -203,7 +203,7 @@ impl From<AtomgitTree> for Tree {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitTreeEntry {
pub struct AtomGitTreeEntry {
pub mode: String,
pub path: String,
pub sha: String,
Expand All @@ -212,8 +212,8 @@ pub struct AtomgitTreeEntry {
pub size: Option<u64>,
}

impl From<&AtomgitTreeEntry> for TreeEntry {
fn from(val: &AtomgitTreeEntry) -> Self {
impl From<&AtomGitTreeEntry> for TreeEntry {
fn from(val: &AtomGitTreeEntry) -> Self {
Self {
mode: val.mode.clone(),
path: val.path.clone(),
Expand All @@ -224,8 +224,8 @@ impl From<&AtomgitTreeEntry> for TreeEntry {
}
}

struct AtomgitTreeEndpoint;
struct AtomGitTreeEndpoint;

impl Endpoint for AtomgitTreeEndpoint {
impl Endpoint for AtomGitTreeEndpoint {
type Output = Vec<TreeEntry>;
}
6 changes: 3 additions & 3 deletions src/scm/driver/atomgit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub mod repo;
mod utils;

use self::constants::ATOMGIT_ENDPOINT;
use self::pr::AtomgitFile;
use self::pr::AtomGitFile;
use super::Driver;
use crate::http::Client;
use crate::scm::driver::atomgit::driver::AtomgitDriver;
use crate::scm::driver::atomgit::driver::AtomGitDriver;

/// Returns a new atomgit driver using the default api.atomgit.com address.
#[inline]
Expand All @@ -40,7 +40,7 @@ pub fn new(url: &str, token: Option<String>) -> Driver {

/// Returns a new atomgit driver using the given client.
pub fn from(client: Client) -> Driver {
Driver::Atomgit(AtomgitDriver { client })
Driver::AtomGit(AtomGitDriver { client })
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/scm/driver/atomgit/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct AtomgitFile {
pub struct AtomGitFile {
pub sha: String,
pub filename: String,
pub status: String,
Expand Down
Loading

0 comments on commit e109b54

Please sign in to comment.