diff --git a/Cargo.lock b/Cargo.lock index 0f001b0..436e2be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,7 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ + "jobserver", "libc", ] @@ -170,6 +171,7 @@ name = "forgejo-api" version = "0.1.0" dependencies = [ "eyre", + "git2", "reqwest", "serde", "soft_assert", @@ -233,6 +235,21 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "git2" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd" +dependencies = [ + "bitflags 2.4.1", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + [[package]] name = "h2" version = "0.3.21" @@ -367,6 +384,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "jobserver" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.65" @@ -388,6 +414,46 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "libgit2-sys" +version = "0.16.1+1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2a2bb3680b094add03bb3732ec520ece34da31a8cd2d633d1389d0f0fb60d0c" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + +[[package]] +name = "libssh2-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.4.11" diff --git a/Cargo.toml b/Cargo.toml index 56cea66..6989702 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ time = { version = "0.3.22", features = ["parsing", "serde", "formatting"] } [dev-dependencies] eyre = "0.6.9" -tokio = { version = "1.29.1", features = ["net", "rt", "macros"] } +tokio = { version = "1.29.1", features = ["net", "fs", "rt", "macros"] } +git2 = "0.18.1" diff --git a/tests/ci_test.rs b/tests/ci_test.rs index 622fcdb..a74793f 100644 --- a/tests/ci_test.rs +++ b/tests/ci_test.rs @@ -10,6 +10,7 @@ async fn ci() -> eyre::Result<()> { let mut results = Vec::new(); results.push(user(&api).await.map_err(|e| eyre!("user error: {e}"))); + results.push(repo(&api).await.map_err(|e| eyre!("repo error: {e}"))); let mut errors = 0; for res in results.into_iter().filter_map(Result::err) { @@ -41,3 +42,53 @@ async fn user(api: &forgejo_api::Forgejo) -> eyre::Result<()> { Ok(()) } + +async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { + tokio::fs::create_dir("/test_repo").await?; + let local_repo = git2::Repository::init("/test_repo")?; + tokio::fs::write("/test_repo/README.md", "# Test\nThis is a test repo").await?; + let mut index = local_repo.index()?; + index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?; + index.write()?; + let tree = local_repo + .find_tree(index.write_tree().unwrap()) + .unwrap(); + let author = git2::Signature::now("TestingAdmin", "admin@noreply.example.org").unwrap(); + let commit_oid = local_repo + .commit(None, &author, &author, "bibblybeebly", &tree, &[]) + .unwrap(); + let branch = local_repo + .branch("main", &local_repo.find_commit(commit_oid).unwrap(), true) + .unwrap(); + let branch_ref = branch.into_reference(); + let branch_ref_name = branch_ref.name().unwrap(); + local_repo.set_head(branch_ref_name).unwrap(); + + let repo_opt = forgejo_api::CreateRepoOption { + auto_init: false, + default_branch: "main".into(), + description: Some("Test Repo".into()), + gitignores: "".into(), + issue_labels: "".into(), + license: "".into(), + name: "test".into(), + private: false, + readme: "".into(), + template: false, + trust_model: forgejo_api::TrustModel::Default, + }; + let remote_repo = api.create_repo(repo_opt).await?; + ensure!(remote_repo.owner.login == "TestingAdmin", "repo owner is not \"TestingAdmin\""); + ensure!(remote_repo.name == "test", "repo owner is not \"test\""); + + let mut callbacks = git2::RemoteCallbacks::new(); + callbacks.credentials(|_url, _username_from_url, _allowed_types| { + git2::Cred::userpass_plaintext("TestingAdmin", "password") + }); + let mut push_options = git2::PushOptions::new(); + push_options.remote_callbacks(callbacks); + let mut origin = local_repo.remote("origin", remote_repo.clone_url.as_str()).unwrap(); + origin.push(&[branch_ref_name], Some(&mut push_options)).unwrap(); + + Ok(()) +}