aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 21d5a620c5e3c6cba9ec46ae16056d5c3ef374c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#[macro_use]
extern crate dotenv_codegen;
extern crate git2;

use std::error::Error;
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)]
struct Args {
    /// The path to save the repository to
    #[arg(short, long)]
    path: String,

    /// The URL of the repository to clone
    #[arg(short, long)]
    repo: String,
}

fn get_project_url(repo: &str) -> String {
    format!("https://{}/{}.git", dotenv!("GITLAB_URL"), repo)
}

fn get_remote_url(repo: &str) -> String {
    format!(
        "https://{}:{}@{}/{}",
        dotenv!("REMOTE_USERNAME"),
        dotenv!("REMOTE_PASSWORD"),
        dotenv!("REMOTE_URL"),
        repo
    )
}

/* Clone project from Gitlab */
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| {
        Cred::userpass_plaintext(dotenv!("GITLAB_USERNAME"), dotenv!("GITLAB_PASSWORD"))
    });

    // Prepare fetch options
    let mut fetch_options = git2::FetchOptions::new();
    fetch_options.remote_callbacks(cb);

    // Prepare builder
    let mut builder = git2::build::RepoBuilder::new();
    builder.remote_create(|repo, name, url| repo.remote_with_fetch(name, url, "+refs/*:refs/*"));

    builder.fetch_options(fetch_options);
    builder.bare(true);

    // Clone
    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 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);

    let api_url = format!(
        "https://{}/api/v4/projects/{}",
        dotenv!("GITLAB_URL"),
        encoded_name
    );

    let response = client
        .get(api_url)
        .bearer_auth(dotenv!("GITLAB_TOKEN"))
        .send()?;

    if response.status().is_success() {
        let project_info: serde_json::Value = response.json()?;

        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())
    }
}

/* Add mirror to Gitlab */
fn add_gitlab_mirror(repo_id: u64, mirror_url: &str) -> Result<(), Box<dyn Error>> {
    let client = reqwest::blocking::Client::new();

    let api_url = format!(
        "https://{}/api/v4/projects/{}/remote_mirrors",
        dotenv!("GITLAB_URL"),
        repo_id
    );

    let response = client
        .post(api_url)
        .bearer_auth(dotenv!("GITLAB_TOKEN"))
        .json(&serde_json::json!({
            "url": mirror_url,
            "enabled": true,
            "keep_divergent_refs": false,
            "only_protected_branches": false,
        }))
        .send()?;

    if response.status().is_success() {
        Ok(())
    } else {
        Err("Failed to add mirror".into())
    }
}

fn main() {
    dotenv::dotenv().ok();

    let args = Args::parse();

    let project = get_project_detail(&args.repo).unwrap();
    let mirror_url = get_remote_url(&args.path);
    add_gitlab_mirror(project.id, &mirror_url).unwrap();

    clone_remote_project(
        &get_project_url(&args.repo),
        &project.default_branch,
        &args.path,
    );
    println!("Done!")
}