diff --git a/lib/db.py b/lib/db.py index ecf507a..5d817c7 100644 --- a/lib/db.py +++ b/lib/db.py @@ -215,6 +215,12 @@ class DB: "CREATE INDEX ON linked_revs(considered)", "UPDATE scheme SET version=20", ) + schemes[21] = ( + "ALTER TABLE revisions ADD COLUMN api_url VARCHAR(40)", + "UPDATE revisions SET api_url='https://api.opensuse.org'", + "ALTER TABLE revisions ALTER COLUMN api_url SET NOT NULL", + "UPDATE scheme SET version=21", + ) schema_version = self.schema_version() if (schema_version + 1) not in schemes: return diff --git a/lib/db_revision.py b/lib/db_revision.py index bc3d467..1985066 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -26,6 +26,7 @@ class DBRevision: self.request_number, self.request_id, self.files_hash, + self.api_url, ) = row self.rev = float(self.rev) self._files = None @@ -67,6 +68,7 @@ class DBRevision: "comment": self.comment, "broken": self.broken, "expanded_srcmd5": self.expanded_srcmd5, + "api_url": self.api_url, "files_hash": self.files_hash, "files": self.files_list(), } @@ -85,8 +87,8 @@ class DBRevision: def import_obs_rev(db: DB, revision: OBSRevision): with db.cursor() as cur: cur.execute( - """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, request_number) - VALUES(%s, %s, %s, %s, %s, %s, %s, %s)""", + """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, request_number, api_url) + VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)""", ( revision.project, revision.package, @@ -96,6 +98,7 @@ class DBRevision: revision.userid, revision.comment, revision.request_number, + revision.obs.url, ), ) return DBRevision.fetch_revision( @@ -104,6 +107,8 @@ class DBRevision: @staticmethod def fetch_revision(db, project, package, rev): + """Technically we would need the api_url as well, but we assume projects are unique + (e.g. not importing SLE from obs)""" with db.cursor() as cur: cur.execute( "SELECT * FROM revisions where project=%s and package=%s and rev=%s", @@ -266,9 +271,9 @@ class DBRevision: """Used in test cases to read a revision from fixtures into the test database""" with db.cursor() as cur: cur.execute( - """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, expanded_srcmd5, - commit_time, userid, comment, broken, files_hash) - VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""", + """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, expanded_srcmd5, + commit_time, userid, comment, broken, files_hash, api_url) + VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""", ( rev_dict["project"], rev_dict["package"], @@ -280,6 +285,7 @@ class DBRevision: rev_dict["comment"], rev_dict["broken"], rev_dict["files_hash"], + rev_dict.get("api_url", "https://api.opensuse.org"), ), ) rev_id = cur.fetchone()[0] diff --git a/lib/git.py b/lib/git.py index f1c20c5..19561d9 100644 --- a/lib/git.py +++ b/lib/git.py @@ -23,7 +23,6 @@ class Git: def is_open(self): return self.repo is not None - # TODO: Extend it to packages and files def exists(self): """Check if the path is a valid git repository""" return (self.path / ".git").exists() diff --git a/lib/git_exporter.py b/lib/git_exporter.py index d9c1748..5540f37 100644 --- a/lib/git_exporter.py +++ b/lib/git_exporter.py @@ -14,11 +14,9 @@ from lib.user import User class GitExporter: def __init__(self, api_url, project, package, repodir, cachedir): - self.obs = OBS() + self.obs = OBS(api_url) self.project = project self.package = package - # TODO: Store the api url in the revision - self.obs.change_url(api_url) self.proxy_sha256 = ProxySHA256(self.obs, enabled=True) self.git = Git( repodir / package, @@ -99,6 +97,7 @@ class GitExporter: ) self.git.add_lfs(file.name, file_sha256["sha256"], size) else: + self.obs.change_url(flat.commit.api_url) self.obs.download( flat.commit.project, flat.commit.package, diff --git a/lib/importer.py b/lib/importer.py index 5cd3062..ac7a2f3 100644 --- a/lib/importer.py +++ b/lib/importer.py @@ -28,9 +28,8 @@ class Importer: self.project = project self.db = DB() - self.obs = OBS() + self.obs = OBS(api_url) assert project == "openSUSE:Factory" - self.obs.change_url(api_url) self.refreshed_packages = set() def import_request(self, number): diff --git a/lib/obs.py b/lib/obs.py index 8e5d38d..9291c20 100644 --- a/lib/obs.py +++ b/lib/obs.py @@ -59,13 +59,14 @@ osc.core.http_GET = retry(osc.core.http_GET) class OBS: - def __init__(self, url=None): - if url: - self.change_url(url) + def __init__(self, url): + self.url = None + self.change_url(url) def change_url(self, url): - self.url = url - osc.conf.get_config(override_apiurl=url) + if url != self.url: + self.url = url + osc.conf.get_config(override_apiurl=url) def _xml(self, url_path, **params): url = osc.core.makeurl(self.url, [url_path], params) diff --git a/tests/db_import_test.py b/tests/db_import_test.py index 6545e72..e34a032 100644 --- a/tests/db_import_test.py +++ b/tests/db_import_test.py @@ -6,11 +6,14 @@ from lib.db_revision import DBRevision from lib.obs import OBS from lib.obs_revision import OBSRevision +# needs to exist in local oscrc (little tricky) +API_URL = "https://api.opensuse.org" + class TestDBMethods(unittest.TestCase): def setUp(self): self.db = DB(section="test") - self.obs = OBS() + self.obs = OBS(API_URL) def test_import(self): test_rev = OBSRevision(self.obs, "openSUSE:Factory", "xz") @@ -30,6 +33,7 @@ class TestDBMethods(unittest.TestCase): db_rev = DBRevision.fetch_revision( self.db, project="openSUSE:Factory", package="xz", rev="70" ) + self.assertEqual(db_rev.api_url, API_URL) self.assertEqual(str(test_rev), str(db_rev))