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

feat: add git remote task provider #4233

Merged
merged 31 commits into from
Feb 8, 2025
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
680afc7
feat: add git provider and detection
acesyde Jan 26, 2025
6324132
fix: unit tests
acesyde Jan 26, 2025
3558e32
fix: unit tests
acesyde Jan 26, 2025
eb8f27c
feat: refacto some code
acesyde Jan 26, 2025
9beb58b
feat: documentation
acesyde Jan 26, 2025
ca9f622
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Jan 29, 2025
79dd9a7
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Jan 31, 2025
6aa70d3
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 1, 2025
dfe8174
fix: code and add e2e
acesyde Feb 1, 2025
25e073b
fix: code and add e2e
acesyde Feb 1, 2025
663ad62
Merge branch 'main' into feature/git_remote_task
acesyde Feb 1, 2025
53f295d
fix: code and add e2e
acesyde Feb 1, 2025
205e138
fix: code and add e2e
acesyde Feb 1, 2025
2784291
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 1, 2025
2b10f5e
fix: code and add e2e
acesyde Feb 1, 2025
53c21e0
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 2, 2025
9fd120a
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 2, 2025
de47e62
Merge branch 'main' into feature/git_remote_task
acesyde Feb 3, 2025
bb32c5c
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 4, 2025
58a7e3d
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 5, 2025
802c6ac
Merge branch 'jdx:main' into feature/git_remote_task
acesyde Feb 7, 2025
d94aa37
fix: code
acesyde Feb 7, 2025
1d83cfa
fix: test pipeline
acesyde Feb 7, 2025
53c2a86
fix: code
acesyde Feb 7, 2025
eca61fb
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 7, 2025
f999329
fix: code
acesyde Feb 7, 2025
f0e2f23
fix: code
acesyde Feb 7, 2025
5af907a
fix: code
acesyde Feb 8, 2025
a867a64
fix: ci
acesyde Feb 8, 2025
f97d7c4
fix: ci
acesyde Feb 8, 2025
b4a264b
fix: ci
acesyde Feb 8, 2025
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
Prev Previous commit
Next Next commit
fix: unit tests
acesyde committed Jan 26, 2025
commit 3558e3236085ec25f94786b134658c89ba017356
155 changes: 119 additions & 36 deletions src/task/task_file_providers/remote_task_git.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::path::PathBuf;

use regex::Regex;
use xx::git;

use crate::{dirs, env};
use crate::{dirs, env, hash};

use super::TaskFileProvider;

@@ -42,38 +43,33 @@ pub struct RemoteTaskGit {
is_cached: bool,
}

#[derive(Debug, Clone)]
struct GitRepoStructure {
url: String,
user: Option<String>,
host: String,
repo: String,
query: Option<String>,
url_without_path: String,
path: String,
}

impl GitRepoStructure {
pub fn new(
url: &str,
user: Option<String>,
host: &str,
repo: &str,
query: Option<String>,
path: &str,
) -> Self {
pub fn new(url: &str, url_without_path: &str, path: &str) -> Self {
Self {
url: url.to_string(),
user,
host: host.to_string(),
repo: repo.to_string(),
query,
url_without_path: url_without_path.to_string(),
path: path.to_string(),
}
}
}

impl RemoteTaskGit {
fn get_cache_key(&self, file: &str) -> String {
"".to_string()
fn get_cache_key(&self, repo_structure: &GitRepoStructure) -> String {
hash::hash_sha256_to_str(&repo_structure.url_without_path)
}

fn get_repo_structure(&self, file: &str) -> GitRepoStructure {
if self.detect_ssh(file).is_ok() {
return self.detect_ssh(file).unwrap();
}
self.detect_https(file).unwrap()
}

fn detect_ssh(&self, file: &str) -> Result<GitRepoStructure, Box<dyn std::error::Error>> {
@@ -85,14 +81,9 @@ impl RemoteTaskGit {

let captures = re.captures(file).unwrap();

Ok(GitRepoStructure::new(
file,
Some(captures.name("user").unwrap().as_str().to_string()),
captures.name("host").unwrap().as_str(),
captures.name("repo").unwrap().as_str(),
captures.name("query").map(|m| m.as_str().to_string()),
captures.name("path").unwrap().as_str(),
))
let path = captures.name("path").unwrap().as_str();

Ok(GitRepoStructure::new(file, &file.replace(path, ""), path))
}

fn detect_https(&self, file: &str) -> Result<GitRepoStructure, Box<dyn std::error::Error>> {
@@ -104,14 +95,9 @@ impl RemoteTaskGit {

let captures = re.captures(file).unwrap();

Ok(GitRepoStructure::new(
file,
None,
captures.name("host").unwrap().as_str(),
captures.name("repo").unwrap().as_str(),
captures.name("query").map(|m| m.as_str().to_string()),
captures.name("path").unwrap().as_str(),
))
let path = captures.name("path").unwrap().as_str();

Ok(GitRepoStructure::new(file, &file.replace(path, ""), path))
}
}

@@ -129,7 +115,32 @@ impl TaskFileProvider for RemoteTaskGit {
}

fn get_local_path(&self, file: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(PathBuf::new())
let repo_structure = self.get_repo_structure(file);
let cache_key = self.get_cache_key(&repo_structure);
let destination = self.storage_path.join(&cache_key);
let repo_file_path = repo_structure.path.clone();
let full_path = destination.join(&repo_file_path);

match self.is_cached {
true => {
trace!("Cache mode enabled");

if full_path.exists() {
return Ok(full_path);
}
}
false => {
trace!("Cache mode disabled");

if full_path.exists() {
crate::file::remove_dir(full_path)?;
}
}
}

let git_cloned = git::clone(repo_structure.url.as_str(), destination)?;

Ok(git_cloned.dir.join(&repo_file_path))
}
}

@@ -206,4 +217,76 @@ mod tests {
assert!(result.is_err());
}
}

#[test]
fn test_compare_ssh_get_cache_key() {
let remote_task_git = RemoteTaskGitBuilder::new().build();

let test_cases = vec![
(
"git::ssh://[email protected]:myorg/example.git//myfile?ref=v1.0.0",
"git::ssh://[email protected]:myorg/example.git//myfile?ref=v2.0.0",
false,
),
(
"git::ssh://[email protected]:myorg/example.git//myfile?ref=v1.0.0",
"git::ssh://[email protected]/example.git//myfile?ref=master",
false,
),
(
"git::ssh://[email protected]/example.git//myfile?ref=v1.0.0",
"git::ssh://[email protected]/example.git//subfolder/mysecondfile?ref=v1.0.0",
true,
),
(
"git::ssh://[email protected]:myorg/example.git//myfile?ref=v1.0.0",
"git::ssh://[email protected]:myorg/example.git//subfolder/mysecondfile?ref=v1.0.0",
true,
),
];

for (first_url, second_url, expected) in test_cases {
let first_repo = remote_task_git.detect_ssh(first_url).unwrap();
let second_repo = remote_task_git.detect_ssh(second_url).unwrap();
let first_cache_key = remote_task_git.get_cache_key(&first_repo);
let second_cache_key = remote_task_git.get_cache_key(&second_repo);
assert_eq!(expected, first_cache_key == second_cache_key);
}
}

#[test]
fn test_compare_https_get_cache_key() {
let remote_task_git = RemoteTaskGitBuilder::new().build();

let test_cases = vec![
(
"git::https://github.com/myorg/example.git//myfile?ref=v1.0.0",
"git::https://github.com/myorg/example.git//myfile?ref=v2.0.0",
false,
),
(
"git::https://github.com/myorg/example.git//myfile?ref=v1.0.0",
"git::https://bitbucket.com/myorg/example.git//myfile?ref=v1.0.0",
false,
),
(
"git::https://github.com/myorg/example.git//myfile?ref=v1.0.0",
"git::https://github.com/myorg/example.git//subfolder/myfile?ref=v1.0.0",
true,
),
(
"git::https://github.com/example.git//myfile?ref=v1.0.0",
"git::https://github.com/example.git//subfolder/myfile?ref=v1.0.0",
true,
),
];

for (first_url, second_url, expected) in test_cases {
let first_repo = remote_task_git.detect_https(first_url).unwrap();
let second_repo = remote_task_git.detect_https(second_url).unwrap();
let first_cache_key = remote_task_git.get_cache_key(&first_repo);
let second_cache_key = remote_task_git.get_cache_key(&second_repo);
assert_eq!(expected, first_cache_key == second_cache_key);
}
}
}