aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs46
1 files changed, 32 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 1d6f34d..21d5a62 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,6 +8,12 @@ use std::path::Path;
use clap::Parser;
use git2::{Cred, RemoteCallbacks};
+#[derive(Debug)]
+struct Project {
+ id: u64,
+ default_branch: String,
+}
+
/// Create a mirror of a repository
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
@@ -36,7 +42,7 @@ fn get_remote_url(repo: &str) -> String {
}
/* Clone project from Gitlab */
-fn clone_remote_project(repo: &str, path: &str) {
+fn clone_remote_project(repo: &str, branch: &str, path: &str) {
// Callbacks for authentication
let mut cb = RemoteCallbacks::new();
cb.credentials(|_url, _username_from_url, _allowed_types| {
@@ -55,11 +61,18 @@ fn clone_remote_project(repo: &str, path: &str) {
builder.bare(true);
// Clone
- builder.clone(repo, Path::new(&path)).unwrap();
+ let repository = builder.clone(repo, Path::new(&path)).unwrap();
+
+ // Set default branch and config
+ repository
+ .set_head(&format!("refs/heads/{}", branch))
+ .unwrap();
+ let mut config = repository.config().unwrap();
+ config.set_str("remote.origin.mirror", "true").unwrap();
}
-/* Get project ID from Gitlab */
-fn get_project_id(project_name: &str) -> Result<u64, Box<dyn Error>> {
+/* Get project detail from Gitlab */
+fn get_project_detail(project_name: &str) -> Result<Project, Box<dyn Error>> {
let client = reqwest::blocking::Client::new();
let encoded_name = urlencoding::encode(project_name);
@@ -70,13 +83,17 @@ fn get_project_id(project_name: &str) -> Result<u64, Box<dyn Error>> {
);
let response = client
- .get(&api_url)
+ .get(api_url)
.bearer_auth(dotenv!("GITLAB_TOKEN"))
.send()?;
if response.status().is_success() {
let project_info: serde_json::Value = response.json()?;
- Ok(project_info["id"].as_u64().unwrap())
+
+ Ok(Project {
+ id: project_info["id"].as_u64().unwrap(),
+ default_branch: project_info["default_branch"].as_str().unwrap().to_string(),
+ })
} else {
Err("Failed to get project ID".into())
}
@@ -93,7 +110,7 @@ fn add_gitlab_mirror(repo_id: u64, mirror_url: &str) -> Result<(), Box<dyn Error
);
let response = client
- .post(&api_url)
+ .post(api_url)
.bearer_auth(dotenv!("GITLAB_TOKEN"))
.json(&serde_json::json!({
"url": mirror_url,
@@ -103,7 +120,6 @@ fn add_gitlab_mirror(repo_id: u64, mirror_url: &str) -> Result<(), Box<dyn Error
}))
.send()?;
- dbg!(response.status());
if response.status().is_success() {
Ok(())
} else {
@@ -116,12 +132,14 @@ fn main() {
let args = Args::parse();
- let project_id = get_project_id(&args.repo).unwrap();
+ let project = get_project_detail(&args.repo).unwrap();
let mirror_url = get_remote_url(&args.path);
- add_gitlab_mirror(project_id, &mirror_url).unwrap();
+ add_gitlab_mirror(project.id, &mirror_url).unwrap();
- dbg!(&project_id);
- dbg!(&mirror_url);
-
- clone_remote_project(&get_project_url(&args.repo), &args.path);
+ clone_remote_project(
+ &get_project_url(&args.repo),
+ &project.default_branch,
+ &args.path,
+ );
+ println!("Done!")
}