From cb22fbca98b8988310d9fb6b27a67a5121484165 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 26 Feb 2017 22:36:51 +0000 Subject: Add service methods to update repositories --- gentoobrowse-api/api/maintenance.ice | 1 + gentoobrowse-api/db/schema.sql | 4 +- .../service/maintenanceGitOperations.cpp | 12 +++++ gentoobrowse-api/service/maintenanceimpl.h | 1 + .../service/sql/maintenance/reposToUpdate.sql | 4 ++ gentoobrowse-api/unittests/Jamfile.jam | 2 + gentoobrowse-api/unittests/testMaintenance.cpp | 57 ++++++++++++++++++++++ 7 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql diff --git a/gentoobrowse-api/api/maintenance.ice b/gentoobrowse-api/api/maintenance.ice index 335cd7e..3a4c038 100644 --- a/gentoobrowse-api/api/maintenance.ice +++ b/gentoobrowse-api/api/maintenance.ice @@ -13,6 +13,7 @@ module Gentoo { idempotent void refreshPackageTree(); idempotent void refreshBugs(); idempotent void refreshChangeLogs() throws GitError; + idempotent void updateRepositories() throws GitError; UserNews getUserNews(); void sendNotifications(); }; diff --git a/gentoobrowse-api/db/schema.sql b/gentoobrowse-api/db/schema.sql index cb74add..f1bee54 100644 --- a/gentoobrowse-api/db/schema.sql +++ b/gentoobrowse-api/db/schema.sql @@ -473,7 +473,9 @@ COMMENT ON TABLE package_urls IS 'URLs associated with a given package'; CREATE TABLE repos ( repoid integer NOT NULL, name text NOT NULL, - path text NOT NULL + path text NOT NULL, + upstream text, + branch text ); ALTER TABLE repos OWNER TO gentoo; -- Name: repos_repoid_seq; Type: SEQUENCE; Schema: gentoobrowse; Owner: gentoo diff --git a/gentoobrowse-api/service/maintenanceGitOperations.cpp b/gentoobrowse-api/service/maintenanceGitOperations.cpp index 84fe352..5aa3ae4 100644 --- a/gentoobrowse-api/service/maintenanceGitOperations.cpp +++ b/gentoobrowse-api/service/maintenanceGitOperations.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "utils/git.h" #include "converters.h" @@ -114,6 +115,17 @@ namespace Gentoo { } }); } + + void + Maintenance::updateRepositories(const Ice::Current &) + { + git_libgit2_init(); + AdHoc::ScopeExit shutdownlibgit2(&git_libgit2_shutdown); + + auto dbc = db->get(); + sql::maintenance::reposToUpdate.select(dbc.get())->forEachRow(&updateRepository); + } + } } diff --git a/gentoobrowse-api/service/maintenanceimpl.h b/gentoobrowse-api/service/maintenanceimpl.h index 57ea94a..c55d74b 100644 --- a/gentoobrowse-api/service/maintenanceimpl.h +++ b/gentoobrowse-api/service/maintenanceimpl.h @@ -42,6 +42,7 @@ namespace Gentoo { void refreshPackageTree(const Ice::Current &) override; void refreshBugs(const Ice::Current &) override; void refreshChangeLogs(const Ice::Current &) override; + void updateRepositories(const Ice::Current &) override; Gentoo::UserNews getUserNews(const Ice::Current &) override; void sendNotifications(const Ice::Current &) override; diff --git a/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql b/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql new file mode 100644 index 0000000..d373418 --- /dev/null +++ b/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql @@ -0,0 +1,4 @@ +SELECT path, upstream, branch +FROM gentoobrowse.repos +WHERE upstream IS NOT NULL +AND branch IS NOT NULL diff --git a/gentoobrowse-api/unittests/Jamfile.jam b/gentoobrowse-api/unittests/Jamfile.jam index 7ee7ab1..558d7d9 100644 --- a/gentoobrowse-api/unittests/Jamfile.jam +++ b/gentoobrowse-api/unittests/Jamfile.jam @@ -3,6 +3,7 @@ import testing ; lib boost_utf : : boost_unit_test_framework ; lib dbpp-postgresql : : : : /usr/include/dbpp-postgresql ; lib dl ; +lib git2 ; lib tidy ; lib dryice : : : : /usr/include/icetray ; @@ -83,6 +84,7 @@ run ../db/schema.sql BOOST_TEST_DYN_LINK testCommon + git2 testCommon : testMaintenance ; diff --git a/gentoobrowse-api/unittests/testMaintenance.cpp b/gentoobrowse-api/unittests/testMaintenance.cpp index 015a596..1ee94bc 100644 --- a/gentoobrowse-api/unittests/testMaintenance.cpp +++ b/gentoobrowse-api/unittests/testMaintenance.cpp @@ -5,6 +5,7 @@ #include #include #include +#include class MaintenanceClientCombined : public Maintenance, public TestClient { }; @@ -114,5 +115,61 @@ BOOST_AUTO_TEST_CASE( refreshPackageTree ) m, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } +static +git_oid +create_initial_commit(git_repository *repo) +{ + git_signature * sig; + git_index * index; + git_oid tree_id, commit_id; + git_tree * tree; + + BOOST_REQUIRE_EQUAL(0, git_signature_default(&sig, repo)); + BOOST_REQUIRE_EQUAL(0, git_repository_index(&index, repo)); + BOOST_REQUIRE_EQUAL(0, git_index_write_tree(&tree_id, index)); + BOOST_REQUIRE_EQUAL(0, git_tree_lookup(&tree, repo, &tree_id)); + BOOST_REQUIRE_EQUAL(0, git_commit_create_v( &commit_id, repo, "HEAD", sig, sig, NULL, "Initial commit", tree, 0)); + + git_index_free(index); + git_tree_free(tree); + git_signature_free(sig); + return commit_id; +} + +BOOST_AUTO_TEST_CASE( testUpdateGitRepository ) +{ + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("GentooBrowseAPI")); + auto testRepo = binDir / "testrepo"; + boost::filesystem::remove_all(testRepo); + boost::filesystem::create_directories(testRepo); + // Create a stub repo to update + git_libgit2_init(); + git_repository * repo; + BOOST_REQUIRE_EQUAL(0, git_repository_init(&repo, testRepo.c_str(), false)); + git_remote * origin; + BOOST_REQUIRE_EQUAL(0, git_remote_create(&origin, repo, "origin", "http://git.randomdan.homeip.net/git/portage")); + git_remote_free(origin); + create_initial_commit(repo); + git_repository_free(repo); + git_libgit2_shutdown(); + // Reference it + auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path, upstream, branch) VALUES(?, ?, ?, ?)"); + insRepo->bindParamS(0, "testrepo"); + insRepo->bindParamS(1, testRepo.string()); + insRepo->bindParamS(2, "origin"); + insRepo->bindParamS(3, "master"); + insRepo->execute(); + + // Update + m->updateRepositories(); + + // Verify checkout + BOOST_REQUIRE(boost::filesystem::is_directory(testRepo)); + BOOST_REQUIRE(boost::filesystem::is_directory(testRepo / "virtual")); + BOOST_REQUIRE(boost::filesystem::is_directory(testRepo / "net-misc")); + BOOST_REQUIRE(boost::filesystem::is_directory(testRepo / "net-misc" / "gentoobrowse-api")); + BOOST_REQUIRE(boost::filesystem::is_regular_file(testRepo / "net-misc" / "gentoobrowse-api" / "Manifest")); +} + BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3