Skip to content

Commit

Permalink
refactor: get_tree and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 20, 2024
1 parent d4f8b4b commit ab35ad9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "amp-common"
description = "Rust libraries shared across Amphitheatre components and libraries"
version = "0.7.4"
version = "0.7.5"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand Down
60 changes: 50 additions & 10 deletions src/scm/driver/github/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::utils::convert_list_options;
use super::GithubFile;
use crate::http::{Client, Endpoint};
use crate::scm::client::ListOptions;
use crate::scm::git::{Commit, GitService, Reference, Signature, Tree};
use crate::scm::git::{Commit, GitService, Reference, Signature, Tree, TreeEntry};
use crate::scm::utils;

pub struct GithubGitService {
Expand All @@ -35,7 +35,7 @@ impl GitService for GithubGitService {
fn list_branches(&self, repo: &str, opts: ListOptions) -> anyhow::Result<Vec<Reference>> {
let path = GITHUB_PATH_BRANCHES.replace("{repo}", repo);
let options = Some(convert_list_options(opts));
let res = self.client.get::<GithubBranchsEndpoint>(&path, options)?;
let res = self.client.get::<GithubBranchesEndpoint>(&path, options)?;

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

if let Some(tags) = res.data {
return Ok(tags.iter().map(|v| v.into()).collect());
Expand Down Expand Up @@ -84,8 +84,9 @@ impl GitService for GithubGitService {
let options = recursive
.map(|r| Some(HashMap::from([("recursive".to_string(), r.to_string())])))
.unwrap_or_default();
let res = self.client.get::<TreeResponseEndpoint>(&path, options)?;
Ok(res.data)
let res = self.client.get::<GithubTreeEndpoint>(&path, options)?;

Ok(res.data.map(|v| v.into()))
}
}

Expand Down Expand Up @@ -166,9 +167,9 @@ pub struct GithubAuthor {
pub login: String,
}

struct GithubBranchsEndpoint;
struct GithubBranchesEndpoint;

impl Endpoint for GithubBranchsEndpoint {
impl Endpoint for GithubBranchesEndpoint {
type Output = Vec<GithubBranch>;
}

Expand All @@ -178,8 +179,47 @@ impl Endpoint for GithubCommitEndpoint {
type Output = GithubCommit;
}

struct TreeResponseEndpoint;
#[derive(Debug, Deserialize, Serialize)]
pub struct GithubTree {
pub sha: String,
pub tree: Vec<GithubTreeEntry>,
pub truncated: bool,
}

impl From<GithubTree> for Tree {
fn from(val: GithubTree) -> Self {
Self {
sha: val.sha,
tree: val.tree.iter().map(|v| v.into()).collect(),
truncated: val.truncated,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GithubTreeEntry {
pub mode: String,
pub path: String,
pub sha: String,
#[serde(rename = "type")]
pub kind: String,
pub size: Option<u64>,
}

impl From<&GithubTreeEntry> for TreeEntry {
fn from(val: &GithubTreeEntry) -> Self {
Self {
mode: val.mode.clone(),
path: val.path.clone(),
sha: val.sha.clone(),
kind: val.kind.clone(),
size: val.size,
}
}
}

struct GithubTreeEndpoint;

impl Endpoint for TreeResponseEndpoint {
type Output = Tree;
impl Endpoint for GithubTreeEndpoint {
type Output = GithubTree;
}
9 changes: 5 additions & 4 deletions src/scm/driver/gitlab/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl GitService for GitlabGitService {
fn list_branches(&self, repo: &str, opts: ListOptions) -> anyhow::Result<Vec<Reference>> {
let path = GITLAB_PATH_BRANCHES.replace("{repo}", &encode(repo));
let options = Some(convert_list_options(opts));
let res = self.client.get::<GitlabBranchsEndpoint>(&path, options)?;
let res = self.client.get::<GitlabBranchesEndpoint>(&path, options)?;

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

if let Some(tags) = res.data {
return Ok(tags.iter().map(|v| v.into()).collect());
Expand All @@ -71,6 +71,7 @@ impl GitService for GitlabGitService {
Ok(res.data.map(|v| v.into()))
}

/// @TODO: Get a tree of a project.
fn get_tree(
&self,
_repo: &str,
Expand All @@ -97,9 +98,9 @@ impl From<&GitlabBranch> for Reference {
}
}

struct GitlabBranchsEndpoint;
struct GitlabBranchesEndpoint;

impl Endpoint for GitlabBranchsEndpoint {
impl Endpoint for GitlabBranchesEndpoint {
type Output = Vec<GitlabBranch>;
}

Expand Down
9 changes: 3 additions & 6 deletions src/scm/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,18 @@ pub struct Signature {
#[derive(Debug, Default, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct Tree {
pub sha: String,
pub url: String,
pub tree: Vec<TreeEntry>,
pub truncated: bool,
}

#[derive(Debug, Default, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct TreeEntry {
pub path: String,
pub mode: String,
pub path: String,
pub sha: String,
#[serde(rename = "type")]
pub entry_type: String,
pub kind: String,
pub size: Option<u64>,
// Some entries have a "size" field, but it might be absent
pub sha: String,
pub url: String,
}
/// Provides access to git resources.
pub trait GitService {
Expand Down

0 comments on commit ab35ad9

Please sign in to comment.